Commit 96d865d9 authored by Tiger Oakes's avatar Tiger Oakes Committed by Commit Bot

Remove getApplicationContext calls from sync

Continuation of work in bug 646094.
Preparing to activate an errorprone check to flag
context#getApplicationContext calls. The goal is to prevent storing the
resulting context as a class property, and instead use ContextUtils.

Bug: 846456
Change-Id: I0739a803ff80c38df73632f62298f83bb90d28f8
Reviewed-on: https://chromium-review.googlesource.com/1095974Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Commit-Queue: Tiger Oakes <tigero@google.com>
Cr-Commit-Position: refs/heads/master@{#577890}
parent c18ebe7a
...@@ -66,13 +66,11 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen ...@@ -66,13 +66,11 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen
private static SyncController sInstance; private static SyncController sInstance;
private static boolean sInitialized; private static boolean sInitialized;
private final Context mContext;
private final ChromeSigninController mChromeSigninController; private final ChromeSigninController mChromeSigninController;
private final ProfileSyncService mProfileSyncService; private final ProfileSyncService mProfileSyncService;
private final SyncNotificationController mSyncNotificationController; private final SyncNotificationController mSyncNotificationController;
private SyncController(Context context) { private SyncController() {
mContext = context;
mChromeSigninController = ChromeSigninController.get(); mChromeSigninController = ChromeSigninController.get();
AndroidSyncSettings.registerObserver(this); AndroidSyncSettings.registerObserver(this);
mProfileSyncService = ProfileSyncService.get(); mProfileSyncService = ProfileSyncService.get();
...@@ -89,7 +87,7 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen ...@@ -89,7 +87,7 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen
// Create the SyncNotificationController. // Create the SyncNotificationController.
mSyncNotificationController = new SyncNotificationController( mSyncNotificationController = new SyncNotificationController(
mContext, PassphraseActivity.class, AccountManagementFragment.class); PassphraseActivity.class, AccountManagementFragment.class);
mProfileSyncService.addSyncStateChangedListener(mSyncNotificationController); mProfileSyncService.addSyncStateChangedListener(mSyncNotificationController);
updateSyncStateFromAndroid(); updateSyncStateFromAndroid();
...@@ -123,21 +121,30 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen ...@@ -123,21 +121,30 @@ public class SyncController implements ProfileSyncService.SyncStateChangedListen
/** /**
* Retrieve the singleton instance of this class. * Retrieve the singleton instance of this class.
* *
* @param context the current context.
* @return the singleton instance. * @return the singleton instance.
*/ */
@Nullable @Nullable
public static SyncController get(Context context) { public static SyncController get() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
if (!sInitialized) { if (!sInitialized) {
if (ProfileSyncService.get() != null) { if (ProfileSyncService.get() != null) {
sInstance = new SyncController(context.getApplicationContext()); sInstance = new SyncController();
} }
sInitialized = true; sInitialized = true;
} }
return sInstance; return sInstance;
} }
/**
* Retrieve the singleton instance of this class.
* @deprecated Use get with no arguments instead.
* @return the singleton instance.
*/
@Nullable
public static SyncController get(Context context) {
return get();
}
/** /**
* Updates sync to reflect the state of the Android sync settings. * Updates sync to reflect the state of the Android sync settings.
*/ */
......
...@@ -14,6 +14,7 @@ import android.content.Context; ...@@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder; import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
...@@ -32,18 +33,15 @@ import org.chromium.components.sync.AndroidSyncSettings; ...@@ -32,18 +33,15 @@ import org.chromium.components.sync.AndroidSyncSettings;
*/ */
public class SyncNotificationController implements ProfileSyncService.SyncStateChangedListener { public class SyncNotificationController implements ProfileSyncService.SyncStateChangedListener {
private static final String TAG = "SyncNotificationController"; private static final String TAG = "SyncNotificationController";
private final Context mApplicationContext;
private final NotificationManagerProxy mNotificationManager; private final NotificationManagerProxy mNotificationManager;
private final Class<? extends Activity> mPassphraseRequestActivity; private final Class<? extends Activity> mPassphraseRequestActivity;
private final Class<? extends Fragment> mAccountManagementFragment; private final Class<? extends Fragment> mAccountManagementFragment;
private final ProfileSyncService mProfileSyncService; private final ProfileSyncService mProfileSyncService;
public SyncNotificationController(Context context, public SyncNotificationController(Class<? extends Activity> passphraseRequestActivity,
Class<? extends Activity> passphraseRequestActivity,
Class<? extends Fragment> accountManagementFragment) { Class<? extends Fragment> accountManagementFragment) {
mApplicationContext = context.getApplicationContext();
mNotificationManager = new NotificationManagerProxyImpl( mNotificationManager = new NotificationManagerProxyImpl(
(NotificationManager) mApplicationContext.getSystemService( (NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE)); Context.NOTIFICATION_SERVICE));
mProfileSyncService = ProfileSyncService.get(); mProfileSyncService = ProfileSyncService.get();
assert mProfileSyncService != null; assert mProfileSyncService != null;
...@@ -95,11 +93,12 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -95,11 +93,12 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
* @param intent Intent to send when the user activates the notification. * @param intent Intent to send when the user activates the notification.
*/ */
private void showSyncNotification(int message, Intent intent) { private void showSyncNotification(int message, Intent intent) {
String title = mApplicationContext.getString(R.string.app_name); Context applicationContext = ContextUtils.getApplicationContext();
String text = mApplicationContext.getString(R.string.sign_in_sync) + ": " String title = applicationContext.getString(R.string.app_name);
+ mApplicationContext.getString(message); String text = applicationContext.getString(R.string.sign_in_sync) + ": "
+ applicationContext.getString(message);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, intent, 0); PendingIntent contentIntent = PendingIntent.getActivity(applicationContext, 0, intent, 0);
// There is no need to provide a group summary notification because the NOTIFICATION_ID_SYNC // There is no need to provide a group summary notification because the NOTIFICATION_ID_SYNC
// notification id ensures there's only one sync notification at a time. // notification id ensures there's only one sync notification at a time.
...@@ -151,8 +150,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -151,8 +150,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
* @return the intent for opening the settings * @return the intent for opening the settings
*/ */
private Intent createSettingsIntent() { private Intent createSettingsIntent() {
return PreferencesLauncher.createIntentForSettingsPage( return PreferencesLauncher.createIntentForSettingsPage(ContextUtils.getApplicationContext(),
mApplicationContext, mAccountManagementFragment.getCanonicalName()); mAccountManagementFragment.getCanonicalName());
} }
/** /**
...@@ -165,7 +164,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -165,7 +164,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
mProfileSyncService.setPassphrasePrompted(true); mProfileSyncService.setPassphrasePrompted(true);
Intent intent = new Intent(Intent.ACTION_MAIN); Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(mApplicationContext, mPassphraseRequestActivity)); intent.setComponent(new ComponentName(
ContextUtils.getApplicationContext(), mPassphraseRequestActivity));
intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addCategory(Intent.CATEGORY_LAUNCHER);
// This activity will become the start of a new task on this history stack. // This activity will become the start of a new task on this history stack.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
......
...@@ -138,7 +138,7 @@ public class PassphraseActivity extends FragmentActivity implements ...@@ -138,7 +138,7 @@ public class PassphraseActivity extends FragmentActivity implements
@Override @Override
public void onPassphraseCanceled() { public void onPassphraseCanceled() {
// Re add the notification. // Re add the notification.
SyncController.get(this).getSyncNotificationController().syncStateChanged(); SyncController.get().getSyncNotificationController().syncStateChanged();
finish(); finish();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment