OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.services; |
| 6 |
| 7 import android.accounts.AccountManager; |
| 8 import android.content.BroadcastReceiver; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.os.AsyncTask; |
| 12 import android.util.Log; |
| 13 |
| 14 import org.chromium.base.ApplicationStatus; |
| 15 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.annotations.SuppressFBWarnings; |
| 17 import org.chromium.base.library_loader.ProcessInitException; |
| 18 import org.chromium.chrome.browser.ChromeMobileApplication; |
| 19 import org.chromium.chrome.browser.ChromiumApplication; |
| 20 import org.chromium.chrome.browser.signin.SigninHelper; |
| 21 import org.chromium.content.browser.BrowserStartupController.StartupCallback; |
| 22 |
| 23 /** |
| 24 * This receiver is notified when accounts are added, accounts are removed, or |
| 25 * an account's credentials (saved password, etc) are changed. |
| 26 */ |
| 27 public class AccountsChangedReceiver extends BroadcastReceiver { |
| 28 private static final String TAG = "AccountsChangeReceiver"; |
| 29 |
| 30 @SuppressFBWarnings("DM_EXIT") |
| 31 @Override |
| 32 public void onReceive(final Context context, final Intent intent) { |
| 33 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 34 @Override |
| 35 protected Void doInBackground(Void... params) { |
| 36 SigninHelper.updateAccountRenameData(context); |
| 37 return null; |
| 38 } |
| 39 |
| 40 @Override |
| 41 protected void onPostExecute(Void result) { |
| 42 continueHandleAccountChangeIfNeeded(context, intent); |
| 43 } |
| 44 }; |
| 45 task.execute(); |
| 46 } |
| 47 |
| 48 private void continueHandleAccountChangeIfNeeded(final Context context, Inte
nt intent) { |
| 49 if (AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(intent.getAction
())) { |
| 50 boolean isChromeVisible = ApplicationStatus.hasVisibleActivities(); |
| 51 if (isChromeVisible) { |
| 52 StartupCallback startupCallback = new StartupCallback() { |
| 53 @Override |
| 54 public void onSuccess(boolean alreadyStarted) { |
| 55 ThreadUtils.runOnUiThread(new Runnable() { |
| 56 @Override |
| 57 public void run() { |
| 58 SigninHelper.get(context.getApplicationContext()
) |
| 59 .validateAccountSettings(true); |
| 60 } |
| 61 }); |
| 62 } |
| 63 @Override |
| 64 public void onFailure() { |
| 65 // Startup failed. So notify SigninHelper of changed acc
ounts via |
| 66 // shared prefs. |
| 67 SigninHelper.markAccountsChangedPref(context); |
| 68 } |
| 69 }; |
| 70 |
| 71 try { |
| 72 ((ChromeMobileApplication) context.getApplicationContext()) |
| 73 .startChromeBrowserProcessesAsync(startupCallback); |
| 74 } catch (ProcessInitException e) { |
| 75 Log.e(TAG, "Unable to load native library.", e); |
| 76 ChromiumApplication.reportStartupErrorAndExit(e); |
| 77 } |
| 78 } else { |
| 79 // Notify SigninHelper of changed accounts (via shared prefs). |
| 80 SigninHelper.markAccountsChangedPref(context); |
| 81 } |
| 82 } |
| 83 |
| 84 } |
| 85 } |
OLD | NEW |