am a848d344: Merge "Fix bug in permission grant system." into gingerbread

* commit 'a848d3445090e114cbfe61f1c7449122a665fba6':
  Fix bug in permission grant system.
diff --git a/core/java/android/accounts/AccountManagerService.java b/core/java/android/accounts/AccountManagerService.java
index 1d9e0f1..9a8cc15 100644
--- a/core/java/android/accounts/AccountManagerService.java
+++ b/core/java/android/accounts/AccountManagerService.java
@@ -802,6 +802,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) {
@@ -912,36 +950,36 @@
                 .notify(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,
-                        mContext.getPackageManager().getPackagesForUid(uid));
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid);
+
         return intent;
     }
 
diff --git a/core/java/android/accounts/GrantCredentialsPermissionActivity.java b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
index fd340cbe..83e4fd9 100644
--- a/core/java/android/accounts/GrantCredentialsPermissionActivity.java
+++ b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
@@ -17,6 +17,7 @@
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.os.RemoteException;
 import android.widget.TextView;
 import android.widget.LinearLayout;
 import android.widget.ImageView;
@@ -26,6 +27,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.pm.RegisteredServicesCache;
 import android.text.TextUtils;
 import android.graphics.drawable.Drawable;
 import com.android.internal.R;
@@ -46,6 +48,7 @@
     private int mUid;
     private Bundle mResultBundle = null;
     protected LayoutInflater mInflater;
+    private final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
 
     protected void onCreate(Bundle savedInstanceState) {
         requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -55,27 +58,56 @@
         mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
         final Bundle extras = getIntent().getExtras();
+
+        // Grant 'account'/'type' to mUID
         mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
         mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
+        mUid = extras.getInt(EXTRAS_REQUESTING_UID);
+        final PackageManager pm = getPackageManager();
+        final String[] packages = pm.getPackagesForUid(mUid);
 
-        if (mAccount == null || mAuthTokenType == null) {
+        if (mAccount == null || mAuthTokenType == null || packages == null) {
             // we were somehow started with bad parameters. abort the activity.
             setResult(Activity.RESULT_CANCELED);
             finish();
             return;
         }
 
-        mUid = extras.getInt(EXTRAS_REQUESTING_UID);
-        final String accountTypeLabel = extras.getString(EXTRAS_ACCOUNT_TYPE_LABEL);
-        final String[] packages = extras.getStringArray(EXTRAS_PACKAGES);
-        final String authTokenLabel = extras.getString(EXTRAS_AUTH_TOKEN_LABEL);
+        final String accountTypeLabel = accountManagerService.getAccountLabel(mAccount.type);
+
+
+        final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
+        authTokenTypeView.setVisibility(View.GONE);
+
+        /** Handles the responses from the AccountManager */
+        IAccountManagerResponse response = new IAccountManagerResponse.Stub() {
+            public void onResult(Bundle bundle) {
+                final String authTokenLabel =
+                    bundle.getString(AccountManager.KEY_AUTH_TOKEN_LABEL);
+                if (!TextUtils.isEmpty(authTokenLabel)) {
+                    runOnUiThread(new Runnable() {
+                        public void run() {
+                            if (!isFinishing()) {
+                                authTokenTypeView.setText(authTokenLabel);
+                                authTokenTypeView.setVisibility(View.VISIBLE);
+                            }
+                        }
+                    });
+                }
+            }
+
+            public void onError(int code, String message) {
+            }
+        };
+
+        accountManagerService.getAuthTokenLabel(
+                response, mAccount, mAuthTokenType);
 
         findViewById(R.id.allow_button).setOnClickListener(this);
         findViewById(R.id.deny_button).setOnClickListener(this);
 
         LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list);
 
-        final PackageManager pm = getPackageManager();
         for (String pkg : packages) {
             String packageLabel;
             try {
@@ -88,12 +120,6 @@
 
         ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
         ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
-        TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
-        if (TextUtils.isEmpty(authTokenLabel)) {
-            authTokenTypeView.setVisibility(View.GONE);
-        } else {
-            authTokenTypeView.setText(authTokenLabel);
-        }
     }
 
     private View newPackageView(String packageLabel) {
@@ -103,7 +129,6 @@
     }
 
     public void onClick(View v) {
-        final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
         switch (v.getId()) {
             case R.id.allow_button:
                 accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid);