summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Sergey Nikolaienkov <sergeynv@google.com> 2022-01-21 07:26:48 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2022-01-21 07:26:48 +0000
commit10e7ab0b92a1e5a12752071f3d66dab263a8b2a4 (patch)
tree957d989582acc7b60bea17e7b6eb9ba08c1a815b
parent541ffd698bdd1cddb4e5287812f34b01dd4bae80 (diff)
parent1c620df4b83294fdcb6cb0b9f42bf20c7b0e3834 (diff)
Merge changes from topic "cdm-proper-persistence"
* changes: Ensure cdm clear cache command persists state Avoid accumulation of user state persist requests
-rw-r--r--services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java58
-rw-r--r--services/companion/java/com/android/server/companion/CompanionDeviceShellCommand.java1
2 files changed, 56 insertions, 3 deletions
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
index 1e50c80f0e71..b2b55765f178 100644
--- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
+++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java
@@ -77,11 +77,13 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
+import android.content.pm.UserInfo;
import android.net.MacAddress;
import android.net.NetworkPolicyManager;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
+import android.os.Message;
import android.os.Parcel;
import android.os.PowerWhitelistManager;
import android.os.RemoteCallbackList;
@@ -185,6 +187,7 @@ public class CompanionDeviceManagerService extends SystemService
private final CompanionDeviceManagerServiceInternal mLocalService = new LocalService(this);
final Handler mMainHandler = Handler.getMain();
+ private final PersistUserStateHandler mUserPersistenceHandler = new PersistUserStateHandler();
private CompanionDevicePresenceController mCompanionDevicePresenceController;
/**
@@ -366,9 +369,8 @@ public class CompanionDeviceManagerService extends SystemService
final List<AssociationInfo> updatedAssociations =
mAssociationStore.getAssociationsForUser(userId);
- final Map<String, Set<Integer>> usedIdsForUser = getPreviouslyUsedIdsForUser(userId);
- BackgroundThread.getHandler().post(() ->
- mPersistentStore.persistStateForUser(userId, updatedAssociations, usedIdsForUser));
+
+ mUserPersistenceHandler.postPersistUserState(userId);
// Notify listeners if ADDED, REMOVED or UPDATED_ADDRESS_CHANGED.
// Do NOT notify when UPDATED_ADDRESS_UNCHANGED, which means a minor tweak in association's
@@ -381,6 +383,13 @@ public class CompanionDeviceManagerService extends SystemService
restartBleScan();
}
+ private void persistStateForUser(@UserIdInt int userId) {
+ final List<AssociationInfo> updatedAssociations =
+ mAssociationStore.getAssociationsForUser(userId);
+ final Map<String, Set<Integer>> usedIdsForUser = getPreviouslyUsedIdsForUser(userId);
+ mPersistentStore.persistStateForUser(userId, updatedAssociations, usedIdsForUser);
+ }
+
private void notifyListeners(
@UserIdInt int userId, @NonNull List<AssociationInfo> associations) {
mListeners.broadcast((listener, callbackUserId) -> {
@@ -1349,4 +1358,47 @@ public class CompanionDeviceManagerService extends SystemService
mService.associationCleanUp(profile);
}
}
+
+ /**
+ * This method must only be called from {@link CompanionDeviceShellCommand} for testing
+ * purposes only!
+ */
+ void persistState() {
+ mUserPersistenceHandler.clearMessages();
+ for (UserInfo user : mUserManager.getAliveUsers()) {
+ persistStateForUser(user.id);
+ }
+ }
+
+ /**
+ * This class is dedicated to handling requests to persist user state.
+ */
+ @SuppressLint("HandlerLeak")
+ private class PersistUserStateHandler extends Handler {
+ PersistUserStateHandler() {
+ super(BackgroundThread.get().getLooper());
+ }
+
+ /**
+ * Persists user state unless there is already an outstanding request for the given user.
+ */
+ synchronized void postPersistUserState(@UserIdInt int userId) {
+ if (!hasMessages(userId)) {
+ sendMessage(obtainMessage(userId));
+ }
+ }
+
+ /**
+ * Clears *ALL* outstanding persist requests for *ALL* users.
+ */
+ synchronized void clearMessages() {
+ removeCallbacksAndMessages(null);
+ }
+
+ @Override
+ public void handleMessage(@NonNull Message msg) {
+ final int userId = msg.what;
+ persistStateForUser(userId);
+ }
+ }
}
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceShellCommand.java b/services/companion/java/com/android/server/companion/CompanionDeviceShellCommand.java
index 5f46d5c4c4bf..9b2bd82fcfed 100644
--- a/services/companion/java/com/android/server/companion/CompanionDeviceShellCommand.java
+++ b/services/companion/java/com/android/server/companion/CompanionDeviceShellCommand.java
@@ -83,6 +83,7 @@ class CompanionDeviceShellCommand extends android.os.ShellCommand {
}
break;
case "clear-association-memory-cache": {
+ mService.persistState();
mService.loadAssociationsFromDisk();
}
break;