Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0309ed5d authored by Nikolas Havrikov's avatar Nikolas Havrikov Committed by Android (Google) Code Review
Browse files

Merge "Add clear-association-memory-cache shell command"

parents 86608e36 d7f3452d
Loading
Loading
Loading
Loading
+37 −20
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;

/**
 * Implementation of the {@link AssociationStore}, with addition of the methods for modification.
@@ -58,33 +59,15 @@ class AssociationStoreImpl implements AssociationStore {
    private final Object mLock = new Object();

    @GuardedBy("mLock")
    private final Map<Integer, AssociationInfo> mIdMap;
    private final Map<Integer, AssociationInfo> mIdMap = new HashMap<>();
    @GuardedBy("mLock")
    private final Map<MacAddress, Set<Integer>> mAddressMap;
    private final Map<MacAddress, Set<Integer>> mAddressMap = new HashMap<>();
    @GuardedBy("mLock")
    private final SparseArray<List<AssociationInfo>> mCachedPerUser = new SparseArray<>();

    @GuardedBy("mListeners")
    private final Set<OnChangeListener> mListeners = new LinkedHashSet<>();

    AssociationStoreImpl(Collection<AssociationInfo> associations) {
        synchronized (mLock) {
            final int size = associations.size();
            mIdMap = new HashMap<>(size);
            mAddressMap = new HashMap<>(size);

            for (AssociationInfo association : associations) {
                final int id = association.getId();
                mIdMap.put(id, association);

                final MacAddress address = association.getDeviceMacAddress();
                if (address != null) {
                    mAddressMap.computeIfAbsent(address, it -> new HashSet<>()).add(id);
                }
            }
        }
    }

    void addAssociation(@NonNull AssociationInfo association) {
        final int id = association.getId();

@@ -301,4 +284,38 @@ class AssociationStoreImpl implements AssociationStore {
            }
        }
    }

    void setAssociations(Collection<AssociationInfo> allAssociations) {
        if (DEBUG) {
            Log.i(TAG, "setAssociations() n=" + allAssociations.size());
            final StringJoiner stringJoiner = new StringJoiner(", ");
            allAssociations.forEach(assoc -> stringJoiner.add(assoc.toShortString()));
            Log.v(TAG, "  associations=" + stringJoiner);
        }
        synchronized (mLock) {
            setAssociationsLocked(allAssociations);
        }
    }

    @GuardedBy("mLock")
    private void setAssociationsLocked(Collection<AssociationInfo> associations) {
        clearLocked();

        for (AssociationInfo association : associations) {
            final int id = association.getId();
            mIdMap.put(id, association);

            final MacAddress address = association.getDeviceMacAddress();
            if (address != null) {
                mAddressMap.computeIfAbsent(address, it -> new HashSet<>()).add(id);
            }
        }
    }

    @GuardedBy("mLock")
    private void clearLocked() {
        mIdMap.clear();
        mAddressMap.clear();
        mCachedPerUser.clear();
    }
}
+14 −10
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ public class CompanionDeviceManagerService extends SystemService

    // Persistent data store for all Associations.
    private PersistentDataStore mPersistentStore;
    private AssociationStoreImpl mAssociationStore;
    private final AssociationStoreImpl mAssociationStore = new AssociationStoreImpl();
    private AssociationRequestsProcessor mAssociationRequestsProcessor;

    private PowerWhitelistManager mPowerWhitelistManager;
@@ -219,16 +219,8 @@ public class CompanionDeviceManagerService extends SystemService
    @Override
    public void onStart() {
        mPersistentStore = new PersistentDataStore();
        final Set<AssociationInfo> allAssociations = new ArraySet<>();

        synchronized (mPreviouslyUsedIds) {
            // The data is stored in DE directories, so we can read the data for all users now
            // (which would not be possible if the data was stored to CE directories).
            mPersistentStore.readStateForUsers(
                    mUserManager.getAliveUsers(), allAssociations, mPreviouslyUsedIds);
        }

        mAssociationStore = new AssociationStoreImpl(allAssociations);
        loadAssociationsFromDisk();
        mAssociationStore.registerListener(this);

        mCompanionDevicePresenceController = new CompanionDevicePresenceController(this);
@@ -239,6 +231,18 @@ public class CompanionDeviceManagerService extends SystemService
        publishBinderService(Context.COMPANION_DEVICE_SERVICE, impl);
    }

    void loadAssociationsFromDisk() {
        final Set<AssociationInfo> allAssociations = new ArraySet<>();
        synchronized (mPreviouslyUsedIds) {
            // The data is stored in DE directories, so we can read the data for all users now
            // (which would not be possible if the data was stored to CE directories).
            mPersistentStore.readStateForUsers(
                    mUserManager.getAliveUsers(), allAssociations, mPreviouslyUsedIds);
        }

        mAssociationStore.setAssociations(allAssociations);
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
+7 −1
Original line number Diff line number Diff line
@@ -82,7 +82,10 @@ class CompanionDeviceShellCommand extends android.os.ShellCommand {
                    mService.onDeviceDisconnected(getNextArgRequired());
                }
                break;

                case "clear-association-memory-cache": {
                    mService.loadAssociationsFromDisk();
                }
                break;
                default:
                    return handleDefaultCommands(cmd);
            }
@@ -110,5 +113,8 @@ class CompanionDeviceShellCommand extends android.os.ShellCommand {
        pw.println("      Create a new Association.");
        pw.println("  disassociate USER_ID PACKAGE MAC_ADDRESS");
        pw.println("      Remove an existing Association.");
        pw.println("  clear-association-memory-cache");
        pw.println("      Clear the in-memory association cache and reload all association "
                + "information from persistent storage. USE FOR DEBUGGING PURPOSES ONLY.");
    }
}