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

Commit 6324c31b authored by Guojing Yuan's avatar Guojing Yuan
Browse files

[CDM][Refactoring 8/N] Make maxId global

Bug: 26987375

Test: CTS
Change-Id: I7dd390ad043da9961fc1d010023e36b8b5e383a9
parent ee1dbf70
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ class BackupRestoreProcessor {

            // Create a new association reassigned to this user and a valid association ID
            final String packageName = restored.getPackageName();
            final int newId = mAssociationStore.getNextId(userId);
            final int newId = mAssociationStore.getNextId();
            AssociationInfo newAssociation = new AssociationInfo.Builder(newId, userId, packageName,
                    restored).build();

+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ class CompanionDeviceShellCommand extends ShellCommand {
                    final int userId = getNextIntArgRequired();
                    final List<AssociationInfo> associationsForUser =
                            mAssociationStore.getActiveAssociationsByUser(userId);
                    final int maxId = mAssociationStore.getMaxId(userId);
                    final int maxId = mAssociationStore.getMaxId();
                    out.println("Max ID: " + maxId);
                    out.println("Association ID | Package Name | Mac Address");
                    for (AssociationInfo association : associationsForUser) {
+1 −1
Original line number Diff line number Diff line
@@ -284,7 +284,7 @@ public class AssociationRequestsProcessor {
            @Nullable String deviceProfile, @Nullable AssociatedDevice associatedDevice,
            boolean selfManaged, @Nullable IAssociationRequestCallback callback,
            @Nullable ResultReceiver resultReceiver) {
        final int id = mAssociationStore.getNextId(userId);
        final int id = mAssociationStore.getNextId();
        final long timestamp = System.currentTimeMillis();

        final AssociationInfo association = new AssociationInfo(id, userId, packageName,
+9 −9
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ public class AssociationStore {
    @GuardedBy("mLock")
    private final Map<Integer, AssociationInfo> mIdToAssociationMap = new HashMap<>();
    @GuardedBy("mLock")
    private final Map<Integer, Integer> mUserToMaxId = new HashMap<>();
    private int mMaxId = 0;

    @GuardedBy("mLocalListeners")
    private final Set<OnChangeListener> mLocalListeners = new LinkedHashSet<>();
@@ -162,7 +162,7 @@ public class AssociationStore {
                mPersisted = false;

                mIdToAssociationMap.clear();
                mUserToMaxId.clear();
                mMaxId = 0;

                // 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).
@@ -172,7 +172,7 @@ public class AssociationStore {
                    for (AssociationInfo association : entry.getValue().getAssociations()) {
                        mIdToAssociationMap.put(association.getId(), association);
                    }
                    mUserToMaxId.put(entry.getKey(), entry.getValue().getMaxId());
                    mMaxId = Math.max(mMaxId, entry.getValue().getMaxId());
                }

                mPersisted = true;
@@ -183,18 +183,18 @@ public class AssociationStore {
    /**
     * Get the current max association id.
     */
    public int getMaxId(int userId) {
    public int getMaxId() {
        synchronized (mLock) {
            return mUserToMaxId.getOrDefault(userId, 0);
            return mMaxId;
        }
    }

    /**
     * Get the next available association id.
     */
    public int getNextId(int userId) {
    public int getNextId() {
        synchronized (mLock) {
            return getMaxId(userId) + 1;
            return getMaxId() + 1;
        }
    }

@@ -214,7 +214,7 @@ public class AssociationStore {
            }

            mIdToAssociationMap.put(id, association);
            mUserToMaxId.put(userId, Math.max(mUserToMaxId.getOrDefault(userId, 0), id));
            mMaxId = Math.max(mMaxId, id);

            writeCacheToDisk(userId);

@@ -305,7 +305,7 @@ public class AssociationStore {
        mExecutor.execute(() -> {
            Associations associations = new Associations();
            synchronized (mLock) {
                associations.setMaxId(mUserToMaxId.getOrDefault(userId, 0));
                associations.setMaxId(mMaxId);
                associations.setAssociations(
                        CollectionUtils.filter(mIdToAssociationMap.values().stream().toList(),
                                a -> a.getUserId() == userId));