@@ -91,6 +91,8 @@ public class AccountManagerService
9191
9292 private final Context mContext ;
9393
94+ private final PackageManager mPackageManager ;
95+
9496 private HandlerThread mMessageThread ;
9597 private final MessageHandler mMessageHandler ;
9698
@@ -214,6 +216,7 @@ public int hashCode() {
214216
215217 public AccountManagerService (Context context ) {
216218 mContext = context ;
219+ mPackageManager = context .getPackageManager ();
217220
218221 mOpenHelper = new DatabaseHelper (mContext );
219222
@@ -520,6 +523,18 @@ public void removeAccount(IAccountManagerResponse response, Account account) {
520523 if (account == null ) throw new IllegalArgumentException ("account is null" );
521524 checkManageAccountsPermission ();
522525 long identityToken = clearCallingIdentity ();
526+
527+ cancelNotification (getSigninRequiredNotificationId (account ));
528+ synchronized (mCredentialsPermissionNotificationIds ) {
529+ for (Pair <Pair <Account , String >, Integer > pair :
530+ mCredentialsPermissionNotificationIds .keySet ()) {
531+ if (account .equals (pair .first .first )) {
532+ int id = mCredentialsPermissionNotificationIds .get (pair );
533+ cancelNotification (id );
534+ }
535+ }
536+ }
537+
523538 try {
524539 new RemoveAccountSession (response , account ).bind ();
525540 } finally {
@@ -842,19 +857,49 @@ public void onResult(Bundle result) {
842857
843858 public void getAuthToken (IAccountManagerResponse response , final Account account ,
844859 final String authTokenType , final boolean notifyOnAuthFailure ,
845- final boolean expectActivityLaunch , final Bundle loginOptions ) {
860+ final boolean expectActivityLaunch , Bundle loginOptionsIn ) {
861+ if (Log .isLoggable (TAG , Log .VERBOSE )) {
862+ Log .v (TAG , "getAuthToken: " + account
863+ + ", response " + response
864+ + ", authTokenType " + authTokenType
865+ + ", notifyOnAuthFailure " + notifyOnAuthFailure
866+ + ", expectActivityLaunch " + expectActivityLaunch
867+ + ", caller's uid " + Binder .getCallingUid ()
868+ + ", pid " + Binder .getCallingPid ());
869+ }
846870 if (response == null ) throw new IllegalArgumentException ("response is null" );
847871 if (account == null ) throw new IllegalArgumentException ("account is null" );
848872 if (authTokenType == null ) throw new IllegalArgumentException ("authTokenType is null" );
849873 checkBinderPermission (Manifest .permission .USE_CREDENTIALS );
850874 final int callerUid = Binder .getCallingUid ();
851- final boolean permissionGranted = permissionIsGranted (account , authTokenType , callerUid );
875+ final int callerPid = Binder .getCallingPid ();
876+
877+ AccountAuthenticatorCache .ServiceInfo <AuthenticatorDescription > authenticatorInfo =
878+ mAuthenticatorCache .getServiceInfo (
879+ AuthenticatorDescription .newKey (account .type ));
880+ final boolean customTokens =
881+ authenticatorInfo != null && authenticatorInfo .type .customTokens ;
882+
883+ // skip the check if customTokens
884+ final boolean permissionGranted = customTokens ||
885+ permissionIsGranted (account , authTokenType , callerUid );
886+
887+ final Bundle loginOptions = (loginOptionsIn == null ) ? new Bundle () :
888+ loginOptionsIn ;
889+ if (customTokens ) {
890+ // let authenticator know the identity of the caller
891+ loginOptions .putInt (AccountManager .KEY_CALLER_UID , callerUid );
892+ loginOptions .putInt (AccountManager .KEY_CALLER_PID , callerPid );
893+ if (notifyOnAuthFailure ) {
894+ loginOptions .putBoolean (AccountManager .KEY_NOTIFY_ON_FAILURE , true );
895+ }
896+ }
852897
853898 long identityToken = clearCallingIdentity ();
854899 try {
855900 // if the caller has permission, do the peek. otherwise go the more expensive
856901 // route of starting a Session
857- if (permissionGranted ) {
902+ if (! customTokens && permissionGranted ) {
858903 String authToken = readAuthTokenFromDatabase (account , authTokenType );
859904 if (authToken != null ) {
860905 Bundle result = new Bundle ();
@@ -908,12 +953,14 @@ public void onResult(Bundle result) {
908953 "the type and name should not be empty" );
909954 return ;
910955 }
911- saveAuthTokenToDatabase (new Account (name , type ),
912- authTokenType , authToken );
956+ if (!customTokens ) {
957+ saveAuthTokenToDatabase (new Account (name , type ),
958+ authTokenType , authToken );
959+ }
913960 }
914961
915962 Intent intent = result .getParcelable (AccountManager .KEY_INTENT );
916- if (intent != null && notifyOnAuthFailure ) {
963+ if (intent != null && notifyOnAuthFailure && ! customTokens ) {
917964 doNotification (
918965 account , result .getString (AccountManager .KEY_AUTH_FAILED_MESSAGE ),
919966 intent );
@@ -972,6 +1019,10 @@ private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
9721019 AccountAuthenticatorResponse response , String authTokenType , String authTokenLabel ) {
9731020
9741021 Intent intent = new Intent (mContext , GrantCredentialsPermissionActivity .class );
1022+ // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag.
1023+ // Since it was set in Eclair+ we can't change it without breaking apps using
1024+ // the intent from a non-Activity context.
1025+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
9751026 intent .addCategory (
9761027 String .valueOf (getCredentialPermissionNotificationId (account , authTokenType , uid )));
9771028
@@ -1849,12 +1900,12 @@ private void checkBinderPermission(String... permissions) {
18491900 }
18501901
18511902 private boolean inSystemImage (int callerUid ) {
1852- String [] packages = mContext . getPackageManager () .getPackagesForUid (callerUid );
1903+ String [] packages = mPackageManager .getPackagesForUid (callerUid );
18531904 for (String name : packages ) {
18541905 try {
1855- PackageInfo packageInfo =
1856- mContext . getPackageManager (). getPackageInfo ( name , 0 /* flags */ );
1857- if ( (packageInfo .applicationInfo .flags & ApplicationInfo .FLAG_SYSTEM ) != 0 ) {
1906+ PackageInfo packageInfo = mPackageManager . getPackageInfo ( name , 0 /* flags */ );
1907+ if ( packageInfo != null
1908+ && (packageInfo .applicationInfo .flags & ApplicationInfo .FLAG_SYSTEM ) != 0 ) {
18581909 return true ;
18591910 }
18601911 } catch (PackageManager .NameNotFoundException e ) {
@@ -1872,7 +1923,7 @@ private boolean permissionIsGranted(Account account, String authTokenType, int c
18721923 && hasExplicitlyGrantedPermission (account , authTokenType );
18731924 if (Log .isLoggable (TAG , Log .VERBOSE )) {
18741925 Log .v (TAG , "checkGrantsOrCallingUidAgainstAuthenticator: caller uid "
1875- + callerUid + ", account " + account
1926+ + callerUid + ", " + account
18761927 + ": is authenticator? " + fromAuthenticator
18771928 + ", has explicit permission? " + hasExplicitGrants );
18781929 }
@@ -1884,7 +1935,7 @@ private boolean hasAuthenticatorUid(String accountType, int callingUid) {
18841935 mAuthenticatorCache .getAllServices ()) {
18851936 if (serviceInfo .type .type .equals (accountType )) {
18861937 return (serviceInfo .uid == callingUid ) ||
1887- (mContext . getPackageManager () .checkSignatures (serviceInfo .uid , callingUid )
1938+ (mPackageManager .checkSignatures (serviceInfo .uid , callingUid )
18881939 == PackageManager .SIGNATURE_MATCH );
18891940 }
18901941 }
0 commit comments