Fix bug in permission grant system.

Change-Id: Ieae1a576fdcba30b24e0430f322843369de2a697
diff --git a/core/java/android/accounts/AccountManagerService.java b/core/java/android/accounts/AccountManagerService.java
index 61ef8f2..a815b3a 100644
--- a/core/java/android/accounts/AccountManagerService.java
+++ b/core/java/android/accounts/AccountManagerService.java
@@ -838,6 +838,44 @@
         }
     }
 
+    void getAuthTokenLabel(final IAccountManagerResponse response,
+            final Account account, final String authTokenType) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
+
+        checkBinderPermission(Manifest.permission.USE_CREDENTIALS);
+
+        long identityToken = clearCallingIdentity();
+        try {
+            new Session(response, account.type, false,
+                    false /* stripAuthTokenFromResult */) {
+                protected String toDebugString(long now) {
+                    return super.toDebugString(now) + ", getAuthTokenLabel"
+                            + ", " + account
+                            + ", authTokenType " + authTokenType;
+                }
+
+                public void run() throws RemoteException {
+                    mAuthenticator.getAuthTokenLabel(this, authTokenType);
+                }
+
+                public void onResult(Bundle result) {
+                    if (result != null) {
+                        String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL);
+                        Bundle bundle = new Bundle();
+                        bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label);
+                        super.onResult(bundle);
+                        return;
+                    } else {
+                        super.onResult(result);
+                    }
+                }
+            }.bind();
+        } finally {
+            restoreCallingIdentity(identityToken);
+        }
+    }
+
     public void getAuthToken(IAccountManagerResponse response, final Account account,
             final String authTokenType, final boolean notifyOnAuthFailure,
             final boolean expectActivityLaunch, final Bundle loginOptions) {
@@ -956,36 +994,36 @@
         installNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), n);
     }
 
-    private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
-            AccountAuthenticatorResponse response, String authTokenType, String authTokenLabel) {
+    String getAccountLabel(String accountType) {
         RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo =
-                mAuthenticatorCache.getServiceInfo(
-                        AuthenticatorDescription.newKey(account.type));
+            mAuthenticatorCache.getServiceInfo(
+                    AuthenticatorDescription.newKey(accountType));
         if (serviceInfo == null) {
-            throw new IllegalArgumentException("unknown account type: " + account.type);
+            throw new IllegalArgumentException("unknown account type: " + accountType);
         }
 
         final Context authContext;
         try {
             authContext = mContext.createPackageContext(
-                serviceInfo.type.packageName, 0);
+                    serviceInfo.type.packageName, 0);
         } catch (PackageManager.NameNotFoundException e) {
-            throw new IllegalArgumentException("unknown account type: " + account.type);
+            throw new IllegalArgumentException("unknown account type: " + accountType);
         }
+        return authContext.getString(serviceInfo.type.labelId);
+    }
+
+    private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
+            AccountAuthenticatorResponse response, String authTokenType, String authTokenLabel) {
 
         Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class);
-        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.addCategory(
                 String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid)));
+
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account);
-        intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_LABEL, authTokenLabel);
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType);
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response);
-        intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT_TYPE_LABEL,
-                        authContext.getString(serviceInfo.type.labelId));
-        intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_PACKAGES,
-                        mPackageManager.getPackagesForUid(uid));
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid);
+
         return intent;
     }