Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.accounts;
     18 
     19 import android.accounts.AccountManager;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.preference.PreferenceManager;
     25 import android.text.TextUtils;
     26 import android.util.Log;
     27 
     28 import com.android.inputmethod.annotations.UsedForTesting;
     29 import com.android.inputmethod.latin.settings.LocalSettingsConstants;
     30 
     31 /**
     32  * {@link BroadcastReceiver} for {@link AccountManager#LOGIN_ACCOUNTS_CHANGED_ACTION}.
     33  */
     34 public class AccountsChangedReceiver extends BroadcastReceiver {
     35     static final String TAG = "AccountsChangedReceiver";
     36 
     37     @Override
     38     public void onReceive(Context context, Intent intent) {
     39         if (!AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(intent.getAction())) {
     40             Log.w(TAG, "Received unknown broadcast: " + intent);
     41             return;
     42         }
     43 
     44         // Ideally the account preference could live in a different preferences file
     45         // that wasn't being backed up and restored, however the preference fragments
     46         // currently only deal with the default shared preferences which is why
     47         // separating this out into a different file is not trivial currently.
     48         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     49         final String currentAccount = prefs.getString(
     50                 LocalSettingsConstants.PREF_ACCOUNT_NAME, null);
     51         removeUnknownAccountFromPreference(prefs, getAccountsForLogin(context), currentAccount);
     52     }
     53 
     54     /**
     55      * Helper method to help test this receiver.
     56      */
     57     @UsedForTesting
     58     protected String[] getAccountsForLogin(Context context) {
     59         return LoginAccountUtils.getAccountsForLogin(context);
     60     }
     61 
     62     /**
     63      * Removes the currentAccount from preferences if it's not found
     64      * in the list of current accounts.
     65      */
     66     private static void removeUnknownAccountFromPreference(final SharedPreferences prefs,
     67             final String[] accounts, final String currentAccount) {
     68         if (currentAccount == null) {
     69             return;
     70         }
     71         for (final String account : accounts) {
     72             if (TextUtils.equals(currentAccount, account)) {
     73                 return;
     74             }
     75         }
     76         Log.i(TAG, "The current account was removed from the system: " + currentAccount);
     77         prefs.edit()
     78                 .remove(LocalSettingsConstants.PREF_ACCOUNT_NAME)
     79                 .apply();
     80     }
     81 }
     82