1 // Copyright 2013 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.
5 package org.chromium.chrome.browser.invalidation;
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.content.Intent;
11 import org.chromium.base.ApplicationState;
12 import org.chromium.base.ApplicationStatus;
13 import org.chromium.base.VisibleForTesting;
14 import org.chromium.components.invalidation.InvalidationClientService;
15 import org.chromium.sync.internal_api.pub.base.ModelType;
16 import org.chromium.sync.notifier.InvalidationIntentProtocol;
17 import org.chromium.sync.notifier.InvalidationPreferences;
18 import org.chromium.sync.notifier.SyncStatusHelper;
23 * Controller used to send start, stop, and registration-change commands to the invalidation
24 * client library used by Sync.
26 public class InvalidationController implements ApplicationStatus.ApplicationStateListener {
27 private static final Object LOCK = new Object();
29 private static InvalidationController sInstance;
31 private final Context mContext;
34 * Sets the types for which the client should register for notifications.
36 * @param account Account of the user.
37 * @param allTypes If {@code true}, registers for all types, and {@code types} is ignored
38 * @param types Set of types for which to register. Ignored if {@code allTypes == true}.
40 public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) {
41 Intent registerIntent =
42 InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
43 registerIntent.setClass(mContext, InvalidationClientService.class);
44 mContext.startService(registerIntent);
48 * Reads all stored preferences and calls
49 * {@link #setRegisteredTypes(android.accounts.Account, boolean, java.util.Set)} with the stored
50 * values, refreshing the set of types with {@code types}. It can be used on startup of Chrome
51 * to ensure we always have a set of registrations consistent with the native code.
52 * @param types Set of types for which to register.
54 public void refreshRegisteredTypes(Set<ModelType> types) {
55 InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
56 Set<String> savedSyncedTypes = invalidationPreferences.getSavedSyncedTypes();
57 Account account = invalidationPreferences.getSavedSyncedAccount();
58 boolean allTypes = savedSyncedTypes != null &&
59 savedSyncedTypes.contains(ModelType.ALL_TYPES_TYPE);
60 setRegisteredTypes(account, allTypes, types);
64 * Starts the invalidation client.
67 Intent intent = new Intent(mContext, InvalidationClientService.class);
68 mContext.startService(intent);
72 * Stops the invalidation client.
75 Intent intent = new Intent(mContext, InvalidationClientService.class);
76 intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
77 mContext.startService(intent);
81 * Returns the instance that will use {@code context} to issue intents.
83 * Calling this method will create the instance if it does not yet exist.
85 public static InvalidationController get(Context context) {
87 if (sInstance == null) {
88 sInstance = new InvalidationController(context);
95 * Creates an instance using {@code context} to send intents.
98 InvalidationController(Context context) {
99 Context appContext = context.getApplicationContext();
100 if (appContext == null) throw new NullPointerException("Unable to get application context");
101 mContext = appContext;
102 ApplicationStatus.registerApplicationStateListener(this);
106 public void onApplicationStateChange(int newState) {
107 if (SyncStatusHelper.get(mContext).isSyncEnabled()) {
108 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
110 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {