From 717a8815c2298dcecdcf674122f0afecfae01918 Mon Sep 17 00:00:00 2001 From: Chris Wren Date: Fri, 31 Mar 2017 15:34:39 -0400 Subject: [PATCH] standardize account manager notification IDs Bug: 36807942 Test: frameworks/base/services/tests/runtests/py Change-Id: Iad5383ebbf9cf1765da5ad7405da1f79a3761c63 --- proto/src/system_messages.proto | 12 ++- .../server/accounts/AccountManagerService.java | 86 ++++++++++++---------- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/proto/src/system_messages.proto b/proto/src/system_messages.proto index f6d91f44b2cf..c4a28313ed0a 100644 --- a/proto/src/system_messages.proto +++ b/proto/src/system_messages.proto @@ -165,6 +165,14 @@ message SystemMessage { // Package: android NOTE_NET_LIMIT_SNOOZED = 36; + // Inform the user they need to sign in to an account + // Package: android, and others + NOTE_ACCOUNT_REQUIRE_SIGNIN = 37; + + // Inform the user that there has been a permission request for an account + // Package: android + NOTE_ACCOUNT_CREDENTIAL_PERMISSION = 38; + // ADD_NEW_IDS_ABOVE_THIS_LINE // Legacy IDs with arbitrary values appear below // Legacy IDs existed as stable non-conflicting constants prior to the O release @@ -216,9 +224,5 @@ message SystemMessage { // Notify the user that data or apps are being moved to external storage. // Package: com.android.systemui NOTE_STORAGE_MOVE = 0x534d4f56; - - // Account Manager allocates IDs sequentially, starting here. - // Package: android - ACCOUNT_MANAGER_BASE = 0x70000000; } } diff --git a/services/core/java/com/android/server/accounts/AccountManagerService.java b/services/core/java/com/android/server/accounts/AccountManagerService.java index d996ee282ef9..e560d325e6dd 100644 --- a/services/core/java/com/android/server/accounts/AccountManagerService.java +++ b/services/core/java/com/android/server/accounts/AccountManagerService.java @@ -123,7 +123,6 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; /** @@ -191,17 +190,14 @@ public class AccountManagerService } private final LinkedHashMap mSessions = new LinkedHashMap(); - private final AtomicInteger mNotificationIds = - new AtomicInteger(SystemMessage.ACCOUNT_MANAGER_BASE); static class UserAccounts { private final int userId; final AccountsDb accountsDb; - private final HashMap, Integer>, Integer> - credentialsPermissionNotificationIds = - new HashMap, Integer>, Integer>(); - private final HashMap signinRequiredNotificationIds = - new HashMap(); + private final HashMap, Integer>, NotificationId> + credentialsPermissionNotificationIds = new HashMap<>(); + private final HashMap signinRequiredNotificationIds + = new HashMap<>(); final Object cacheLock = new Object(); final Object dbLock = new Object(); // if needed, dbLock must be obtained before cacheLock /** protected by the {@link #cacheLock} */ @@ -1863,12 +1859,12 @@ public class AccountManagerService */ cancelNotification( getSigninRequiredNotificationId(accounts, accountToRename), - new UserHandle(accounts.userId)); + new UserHandle(accounts.userId)); synchronized(accounts.credentialsPermissionNotificationIds) { for (Pair, Integer> pair: accounts.credentialsPermissionNotificationIds.keySet()) { if (accountToRename.equals(pair.first.first)) { - int id = accounts.credentialsPermissionNotificationIds.get(pair); + NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); cancelNotification(id, new UserHandle(accounts.userId)); } } @@ -2021,7 +2017,7 @@ public class AccountManagerService for (Pair, Integer> pair: accounts.credentialsPermissionNotificationIds.keySet()) { if (account.equals(pair.first.first)) { - int id = accounts.credentialsPermissionNotificationIds.get(pair); + NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); cancelNotification(id, user); } } @@ -2912,8 +2908,8 @@ public class AccountManagerService // the intent from a non-Activity context. This is the default behavior. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } - intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, - authTokenType, uid) + (packageName != null ? packageName : ""))); + intent.addCategory(getCredentialPermissionNotificationId(account, + authTokenType, uid).mTag + (packageName != null ? packageName : "")); intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); @@ -2922,33 +2918,39 @@ public class AccountManagerService return intent; } - private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, - int uid) { - Integer id; + private NotificationId getCredentialPermissionNotificationId(Account account, + String authTokenType, int uid) { + NotificationId nId; UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); synchronized (accounts.credentialsPermissionNotificationIds) { final Pair, Integer> key = new Pair, Integer>( new Pair(account, authTokenType), uid); - id = accounts.credentialsPermissionNotificationIds.get(key); - if (id == null) { - id = mNotificationIds.incrementAndGet(); - accounts.credentialsPermissionNotificationIds.put(key, id); + nId = accounts.credentialsPermissionNotificationIds.get(key); + if (nId == null) { + String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION + + ":" + account.hashCode() + ":" + authTokenType.hashCode(); + int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; + nId = new NotificationId(tag, id); + accounts.credentialsPermissionNotificationIds.put(key, nId); } } - return id; + return nId; } - private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { - Integer id; + private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { + NotificationId nId; synchronized (accounts.signinRequiredNotificationIds) { - id = accounts.signinRequiredNotificationIds.get(account); - if (id == null) { - id = mNotificationIds.incrementAndGet(); - accounts.signinRequiredNotificationIds.put(account, id); + nId = accounts.signinRequiredNotificationIds.get(account); + if (nId == null) { + String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN + + ":" + account.hashCode(); + int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; + nId = new NotificationId(tag, id); + accounts.signinRequiredNotificationIds.put(account, nId); } } - return id; + return nId; } @Override @@ -4931,8 +4933,8 @@ public class AccountManagerService createNoCredentialsPermissionNotification(account, intent, packageName, userId); } else { Context contextForUser = getContextForUser(new UserHandle(userId)); - final Integer notificationId = getSigninRequiredNotificationId(accounts, account); - intent.addCategory(String.valueOf(notificationId)); + final NotificationId id = getSigninRequiredNotificationId(accounts, account); + intent.addCategory(id.mTag); final String notificationTitleFormat = contextForUser.getText(R.string.notification_title).toString(); @@ -4948,21 +4950,21 @@ public class AccountManagerService mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, new UserHandle(userId))) .build(); - installNotification(notificationId, n, packageName, userId); + installNotification(id, n, packageName, userId); } } finally { restoreCallingIdentity(identityToken); } } - private void installNotification(int notificationId, final Notification notification, + private void installNotification(NotificationId id, final Notification notification, String packageName, int userId) { final long token = clearCallingIdentity(); try { INotificationManager notificationManager = mInjector.getNotificationManager(); try { - notificationManager.enqueueNotificationWithTag(packageName, packageName, null, - notificationId, notification, new int[1], userId); + notificationManager.enqueueNotificationWithTag(packageName, packageName, + id.mTag, id.mId, notification, new int[1], userId); } catch (RemoteException e) { /* ignore - local call */ } @@ -4971,15 +4973,15 @@ public class AccountManagerService } } - private void cancelNotification(int id, UserHandle user) { + private void cancelNotification(NotificationId id, UserHandle user) { cancelNotification(id, mContext.getPackageName(), user); } - private void cancelNotification(int id, String packageName, UserHandle user) { + private void cancelNotification(NotificationId id, String packageName, UserHandle user) { long identityToken = clearCallingIdentity(); try { INotificationManager service = mInjector.getNotificationManager(); - service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); + service.cancelNotificationWithTag(packageName, id.mTag, id.mId, user.getIdentifier()); } catch (RemoteException e) { /* ignore - local call */ } finally { @@ -5893,4 +5895,14 @@ public class AccountManagerService return NotificationManager.getService(); } } + + private class NotificationId { + final String mTag; + private final int mId; + + NotificationId(String tag, int type) { + mTag = tag; + mId = type; + } + } } -- 2.11.0