3f953b768a228c5be82ed0c2f86e600680896933
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / invalidation / InvalidationController.java
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.
4
5 package org.chromium.chrome.browser.invalidation;
6
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.content.Intent;
10
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;
19
20 import java.util.Set;
21
22 /**
23  * Controller used to send start, stop, and registration-change commands to the invalidation
24  * client library used by Sync.
25  */
26 public class InvalidationController implements ApplicationStatus.ApplicationStateListener {
27     private static final Object LOCK = new Object();
28
29     private static InvalidationController sInstance;
30
31     private final Context mContext;
32
33     /**
34      * Sets the types for which the client should register for notifications.
35      *
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}.
39      */
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);
45     }
46
47     /**
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.
53      */
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);
61     }
62
63     /**
64      * Starts the invalidation client.
65      */
66     public void start() {
67         Intent intent = new Intent(mContext, InvalidationClientService.class);
68         mContext.startService(intent);
69     }
70
71     /**
72      * Stops the invalidation client.
73      */
74     public void stop() {
75         Intent intent = new Intent(mContext, InvalidationClientService.class);
76         intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
77         mContext.startService(intent);
78     }
79
80     /**
81      * Returns the instance that will use {@code context} to issue intents.
82      *
83      * Calling this method will create the instance if it does not yet exist.
84      */
85     public static InvalidationController get(Context context) {
86         synchronized (LOCK) {
87             if (sInstance == null) {
88                 sInstance = new InvalidationController(context);
89             }
90             return sInstance;
91         }
92     }
93
94     /**
95      * Creates an instance using {@code context} to send intents.
96      */
97     @VisibleForTesting
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);
103     }
104
105     @Override
106     public void onApplicationStateChange(int newState) {
107         if (SyncStatusHelper.get(mContext).isSyncEnabled()) {
108             if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
109                 start();
110             } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
111                 stop();
112             }
113         }
114     }
115 }