check for nulls

Change-Id: Ie02749db1dceb4288a70e5b28040459d669904c1
http://b/issue?id=2527346
diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java
index 8356029..15c8856 100644
--- a/core/java/android/accounts/AccountManager.java
+++ b/core/java/android/accounts/AccountManager.java
@@ -227,11 +227,13 @@
      * @hide for internal use only
      */
     public static Bundle sanitizeResult(Bundle result) {
-        if (result.containsKey(KEY_AUTHTOKEN)
-                && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
-            final Bundle newResult = new Bundle(result);
-            newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
-            return newResult;
+        if (result != null) {
+            if (result.containsKey(KEY_AUTHTOKEN)
+                    && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
+                final Bundle newResult = new Bundle(result);
+                newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
+                return newResult;
+            }
         }
         return result;
     }
@@ -251,6 +253,7 @@
      * @return An {@link AccountManager} instance
      */
     public static AccountManager get(Context context) {
+        if (context == null) throw new IllegalArgumentException("context is null");
         return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
     }
 
@@ -269,6 +272,7 @@
      * @return The account's password, null if none or if the account doesn't exist
      */
     public String getPassword(final Account account) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         try {
             return mService.getPassword(account);
         } catch (RemoteException e) {
@@ -293,6 +297,8 @@
      * @return The user data, null if the account or key doesn't exist
      */
     public String getUserData(final Account account, final String key) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (key == null) throw new IllegalArgumentException("key is null");
         try {
             return mService.getUserData(account, key);
         } catch (RemoteException e) {
@@ -393,6 +399,8 @@
     public AccountManagerFuture<Boolean> hasFeatures(final Account account,
             final String[] features,
             AccountManagerCallback<Boolean> callback, Handler handler) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (features == null) throw new IllegalArgumentException("features is null");
         return new Future2Task<Boolean>(handler, callback) {
             public void doWork() throws RemoteException {
                 mService.hasFeatures(mResponse, account, features);
@@ -436,13 +444,9 @@
     public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
             final String type, final String[] features,
             AccountManagerCallback<Account[]> callback, Handler handler) {
+        if (type == null) throw new IllegalArgumentException("type is null");
         return new Future2Task<Account[]>(handler, callback) {
             public void doWork() throws RemoteException {
-                if (type == null) {
-                    Log.e(TAG, "Type is null");
-                    set(new Account[0]);
-                    return;
-                }
                 mService.getAccountsByFeatures(mResponse, type, features);
             }
             public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
@@ -476,6 +480,7 @@
      *     already exists, the account is null, or another error occurs.
      */
     public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         try {
             return mService.addAccount(account, password, userdata);
         } catch (RemoteException e) {
@@ -507,6 +512,7 @@
      */
     public AccountManagerFuture<Boolean> removeAccount(final Account account,
             AccountManagerCallback<Boolean> callback, Handler handler) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         return new Future2Task<Boolean>(handler, callback) {
             public void doWork() throws RemoteException {
                 mService.removeAccount(mResponse, account);
@@ -537,6 +543,8 @@
      * @param authToken The auth token to invalidate
      */
     public void invalidateAuthToken(final String accountType, final String authToken) {
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
+        if (authToken == null) throw new IllegalArgumentException("authToken is null");
         try {
             mService.invalidateAuthToken(accountType, authToken);
         } catch (RemoteException e) {
@@ -563,13 +571,8 @@
      *     no auth token is cached or the account does not exist.
      */
     public String peekAuthToken(final Account account, final String authTokenType) {
-        if (account == null) {
-            Log.e(TAG, "peekAuthToken: the account must not be null");
-            return null;
-        }
-        if (authTokenType == null) {
-            return null;
-        }
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         try {
             return mService.peekAuthToken(account, authTokenType);
         } catch (RemoteException e) {
@@ -594,10 +597,7 @@
      * @param password The password to set, null to clear the password
      */
     public void setPassword(final Account account, final String password) {
-        if (account == null) {
-            Log.e(TAG, "the account must not be null");
-            return;
-        }
+        if (account == null) throw new IllegalArgumentException("account is null");
         try {
             mService.setPassword(account, password);
         } catch (RemoteException e) {
@@ -621,10 +621,7 @@
      * @param account The account whose password to clear
      */
     public void clearPassword(final Account account) {
-        if (account == null) {
-            Log.e(TAG, "the account must not be null");
-            return;
-        }
+        if (account == null) throw new IllegalArgumentException("account is null");
         try {
             mService.clearPassword(account);
         } catch (RemoteException e) {
@@ -649,14 +646,8 @@
      * @param value The value to set, null to clear this userdata key
      */
     public void setUserData(final Account account, final String key, final String value) {
-        if (account == null) {
-            Log.e(TAG, "the account must not be null");
-            return;
-        }
-        if (key == null) {
-            Log.e(TAG, "the key must not be null");
-            return;
-        }
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (key == null) throw new IllegalArgumentException("key is null");
         try {
             mService.setUserData(account, key, value);
         } catch (RemoteException e) {
@@ -682,6 +673,8 @@
      * @param authToken The auth token to add to the cache
      */
     public void setAuthToken(Account account, final String authTokenType, final String authToken) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         try {
             mService.setAuthToken(account, authTokenType, authToken);
         } catch (RemoteException e) {
@@ -716,6 +709,8 @@
     public String blockingGetAuthToken(Account account, String authTokenType,
             boolean notifyAuthFailure)
             throws OperationCanceledException, IOException, AuthenticatorException {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
                 null /* handler */).getResult();
         if (bundle == null) {
@@ -786,7 +781,7 @@
     public AccountManagerFuture<Bundle> getAuthToken(
             final Account account, final String authTokenType, final Bundle options,
             final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
-        if (activity == null) throw new IllegalArgumentException("activity is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
@@ -930,14 +925,9 @@
             final String authTokenType, final String[] requiredFeatures,
             final Bundle addAccountOptions,
             final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
-                if (accountType == null) {
-                    Log.e(TAG, "the account must not be null");
-                    // to unblock caller waiting on Future.get()
-                    set(new Bundle());
-                    return;
-                }
                 mService.addAcount(mResponse, accountType, authTokenType,
                         requiredFeatures, activity != null, addAccountOptions);
             }
@@ -1004,6 +994,7 @@
             final Activity activity,
             final AccountManagerCallback<Bundle> callback,
             final Handler handler) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
                 mService.confirmCredentials(mResponse, account, options, activity != null);
@@ -1064,6 +1055,7 @@
             final Bundle options, final Activity activity,
             final AccountManagerCallback<Bundle> callback,
             final Handler handler) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
                 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
@@ -1112,6 +1104,7 @@
     public AccountManagerFuture<Bundle> editProperties(final String accountType,
             final Activity activity, final AccountManagerCallback<Bundle> callback,
             final Handler handler) {
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
                 mService.editProperties(mResponse, accountType, activity != null);
@@ -1734,10 +1727,7 @@
      * @throws IllegalStateException if listener was not already added
      */
     public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
-        if (listener == null) {
-            Log.e(TAG, "Missing listener");
-            return;
-        }
+        if (listener == null) throw new IllegalArgumentException("listener is null");
         synchronized (mAccountsUpdatedListeners) {
             if (!mAccountsUpdatedListeners.containsKey(listener)) {
                 Log.e(TAG, "Listener was not previously added");
diff --git a/core/java/android/accounts/AccountManagerService.java b/core/java/android/accounts/AccountManagerService.java
index 16b603c..1d9e0f1 100644
--- a/core/java/android/accounts/AccountManagerService.java
+++ b/core/java/android/accounts/AccountManagerService.java
@@ -282,6 +282,7 @@
     }
 
     public String getPassword(Account account) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
 
         long identityToken = clearCallingIdentity();
@@ -312,6 +313,8 @@
     }
 
     public String getUserData(Account account, String key) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (key == null) throw new IllegalArgumentException("key is null");
         checkAuthenticateAccountsPermission(account);
         long identityToken = clearCallingIdentity();
         try {
@@ -382,6 +385,7 @@
     }
 
     public boolean addAccount(Account account, String password, Bundle extras) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
 
         // fails if the account already exists
@@ -451,6 +455,9 @@
 
     public void hasFeatures(IAccountManagerResponse response,
             Account account, String[] features) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (features == null) throw new IllegalArgumentException("features is null");
         checkReadAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -509,6 +516,8 @@
     }
 
     public void removeAccount(IAccountManagerResponse response, Account account) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -565,6 +574,8 @@
     }
 
     public void invalidateAuthToken(String accountType, String authToken) {
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
+        if (authToken == null) throw new IllegalArgumentException("authToken is null");
         checkManageAccountsOrUseCredentialsPermissions();
         long identityToken = clearCallingIdentity();
         try {
@@ -658,6 +669,8 @@
     }
 
     public String peekAuthToken(Account account, String authTokenType) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkAuthenticateAccountsPermission(account);
         long identityToken = clearCallingIdentity();
         try {
@@ -668,6 +681,8 @@
     }
 
     public void setAuthToken(Account account, String authTokenType, String authToken) {
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkAuthenticateAccountsPermission(account);
         long identityToken = clearCallingIdentity();
         try {
@@ -678,6 +693,7 @@
     }
 
     public void setPassword(Account account, String password) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
         long identityToken = clearCallingIdentity();
         try {
@@ -714,6 +730,7 @@
     }
 
     public void clearPassword(Account account) {
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -724,6 +741,8 @@
     }
 
     public void setUserData(Account account, String key, String value) {
+        if (key == null) throw new IllegalArgumentException("key is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkAuthenticateAccountsPermission(account);
         long identityToken = clearCallingIdentity();
         if (account == null) {
@@ -786,6 +805,9 @@
     public void getAuthToken(IAccountManagerResponse response, final Account account,
             final String authTokenType, final boolean notifyOnAuthFailure,
             final boolean expectActivityLaunch, final Bundle loginOptions) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkBinderPermission(Manifest.permission.USE_CREDENTIALS);
         final int callerUid = Binder.getCallingUid();
         final boolean permissionGranted = permissionIsGranted(account, authTokenType, callerUid);
@@ -955,6 +977,8 @@
     public void addAcount(final IAccountManagerResponse response, final String accountType,
             final String authTokenType, final String[] requiredFeatures,
             final boolean expectActivityLaunch, final Bundle options) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -981,6 +1005,8 @@
 
     public void confirmCredentials(IAccountManagerResponse response,
             final Account account, final Bundle options, final boolean expectActivityLaunch) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -1002,6 +1028,9 @@
     public void updateCredentials(IAccountManagerResponse response, final Account account,
             final String authTokenType, final boolean expectActivityLaunch,
             final Bundle loginOptions) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (account == null) throw new IllegalArgumentException("account is null");
+        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -1025,6 +1054,8 @@
 
     public void editProperties(IAccountManagerResponse response, final String accountType,
             final boolean expectActivityLaunch) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (accountType == null) throw new IllegalArgumentException("accountType is null");
         checkManageAccountsPermission();
         long identityToken = clearCallingIdentity();
         try {
@@ -1142,6 +1173,8 @@
 
     public void getAccountsByFeatures(IAccountManagerResponse response,
             String type, String[] features) {
+        if (response == null) throw new IllegalArgumentException("response is null");
+        if (type == null) throw new IllegalArgumentException("accountType is null");
         checkReadAccountsPermission();
         if (features != null && type == null) {
             if (response != null) {
@@ -1878,7 +1911,8 @@
      * @hide
      */
     public void grantAppPermission(Account account, String authTokenType, int uid) {
-        if (account == null  || authTokenType == null) {
+        if (account == null || authTokenType == null) {
+            Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception());
             return;
         }
         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
@@ -1908,7 +1942,8 @@
      * @hide
      */
     public void revokeAppPermission(Account account, String authTokenType, int uid) {
-        if (account == null  || authTokenType == null) {
+        if (account == null || authTokenType == null) {
+            Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception());
             return;
         }
         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
diff --git a/core/java/android/accounts/GrantCredentialsPermissionActivity.java b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
index f4b7258..fd340cbe 100644
--- a/core/java/android/accounts/GrantCredentialsPermissionActivity.java
+++ b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
@@ -57,6 +57,14 @@
         final Bundle extras = getIntent().getExtras();
         mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
         mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
+
+        if (mAccount == null || mAuthTokenType == 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);