1 | // Copyright (c) 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.sync.notifier; |
6 | |
7 | import android.accounts.Account; |
8 | import android.content.Context; |
9 | import android.content.Intent; |
10 | |
11 | import com.google.common.annotations.VisibleForTesting; |
12 | import com.google.common.base.Preconditions; |
13 | |
14 | import org.chromium.base.ActivityStatus; |
15 | import org.chromium.base.CollectionUtil; |
16 | import org.chromium.sync.internal_api.pub.base.ModelType; |
17 | |
18 | import java.util.Set; |
19 | |
20 | /** |
21 | * Controller used to send start, stop, and registration-change commands to the invalidation |
22 | * client library used by Sync. |
23 | */ |
24 | public class InvalidationController implements ActivityStatus.StateListener { |
25 | /** |
26 | * Constants and utility methods to create the intents used to communicate between the |
27 | * controller and the invalidation client library. |
28 | */ |
29 | public static class IntentProtocol { |
30 | /** |
31 | * Action set on register intents. |
32 | */ |
33 | public static final String ACTION_REGISTER = |
34 | "org.chromium.sync.notifier.ACTION_REGISTER_TYPES"; |
35 | |
36 | /** |
37 | * Parcelable-valued intent extra containing the account of the user. |
38 | */ |
39 | public static final String EXTRA_ACCOUNT = "account"; |
40 | |
41 | /** |
42 | * String-list-valued intent extra of the syncable types to sync. |
43 | */ |
44 | public static final String EXTRA_REGISTERED_TYPES = "registered_types"; |
45 | |
46 | /** |
47 | * Boolean-valued intent extra indicating that the service should be stopped. |
48 | */ |
49 | public static final String EXTRA_STOP = "stop"; |
50 | |
51 | /** |
52 | * Create an Intent that will start the invalidation listener service and |
53 | * register for the specified types. |
54 | */ |
55 | public static Intent createRegisterIntent(Account account, |
56 | boolean allTypes, Set<ModelType> types) { |
57 | Intent registerIntent = new Intent(ACTION_REGISTER); |
58 | String[] selectedTypesArray; |
59 | if (allTypes) { |
60 | selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE}; |
61 | } else { |
62 | selectedTypesArray = new String[types.size()]; |
63 | int pos = 0; |
64 | for (ModelType type : types) { |
65 | selectedTypesArray[pos++] = type.name(); |
66 | } |
67 | } |
68 | registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, |
69 | CollectionUtil.newArrayList(selectedTypesArray)); |
70 | registerIntent.putExtra(EXTRA_ACCOUNT, account); |
71 | return registerIntent; |
72 | } |
73 | |
74 | /** Returns whether {@code intent} is a stop intent. */ |
75 | public static boolean isStop(Intent intent) { |
76 | return intent.getBooleanExtra(EXTRA_STOP, false); |
77 | } |
78 | |
79 | /** Returns whether {@code intent} is a registered types change intent. */ |
80 | public static boolean isRegisteredTypesChange(Intent intent) { |
81 | return intent.hasExtra(EXTRA_REGISTERED_TYPES); |
82 | } |
83 | |
84 | private IntentProtocol() { |
85 | // Disallow instantiation. |
86 | } |
87 | } |
88 | |
89 | private static final Object LOCK = new Object(); |
90 | |
91 | private static InvalidationController sInstance; |
92 | |
93 | private final Context mContext; |
94 | |
95 | /** |
96 | * Sets the types for which the client should register for notifications. |
97 | * |
98 | * @param account Account of the user. |
99 | * @param allTypes If {@code true}, registers for all types, and {@code types} is ignored |
100 | * @param types Set of types for which to register. Ignored if {@code allTypes == true}. |
101 | */ |
102 | public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) { |
103 | Intent registerIntent = IntentProtocol.createRegisterIntent(account, allTypes, types); |
104 | registerIntent.setClass(mContext, InvalidationService.class); |
105 | mContext.startService(registerIntent); |
106 | } |
107 | |
108 | /** |
109 | * Reads all stored preferences and calls |
110 | * {@link #setRegisteredTypes(android.accounts.Account, boolean, java.util.Set)} with the stored |
111 | * values, refreshing the set of types with {@code types}. It can be used on startup of Chrome |
112 | * to ensure we always have a set of registrations consistent with the native code. |
113 | * @param types Set of types for which to register. |
114 | */ |
115 | public void refreshRegisteredTypes(Set<ModelType> types) { |
116 | InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext); |
117 | Set<String> savedSyncedTypes = invalidationPreferences.getSavedSyncedTypes(); |
118 | Account account = invalidationPreferences.getSavedSyncedAccount(); |
119 | boolean allTypes = savedSyncedTypes != null && |
120 | savedSyncedTypes.contains(ModelType.ALL_TYPES_TYPE); |
121 | setRegisteredTypes(account, allTypes, types); |
122 | } |
123 | |
124 | /** |
125 | * Starts the invalidation client. |
126 | */ |
127 | public void start() { |
128 | Intent intent = new Intent(mContext, InvalidationService.class); |
129 | mContext.startService(intent); |
130 | } |
131 | |
132 | /** |
133 | * Stops the invalidation client. |
134 | */ |
135 | public void stop() { |
136 | Intent intent = new Intent(mContext, InvalidationService.class); |
137 | intent.putExtra(IntentProtocol.EXTRA_STOP, true); |
138 | mContext.startService(intent); |
139 | } |
140 | |
141 | /** |
142 | * Returns the instance that will use {@code context} to issue intents. |
143 | * |
144 | * Calling this method will create the instance if it does not yet exist. |
145 | */ |
146 | public static InvalidationController get(Context context) { |
147 | synchronized (LOCK) { |
148 | if (sInstance == null) { |
149 | sInstance = new InvalidationController(context); |
150 | } |
151 | return sInstance; |
152 | } |
153 | } |
154 | |
155 | /** |
156 | * Creates an instance using {@code context} to send intents. |
157 | */ |
158 | @VisibleForTesting |
159 | InvalidationController(Context context) { |
160 | mContext = Preconditions.checkNotNull(context.getApplicationContext()); |
161 | ActivityStatus.registerStateListener(this); |
162 | } |
163 | |
164 | @Override |
165 | public void onActivityStateChange(int newState) { |
166 | if (SyncStatusHelper.get(mContext).isSyncEnabled()) { |
167 | if (newState == ActivityStatus.PAUSED) { |
168 | stop(); |
169 | } else if (newState == ActivityStatus.RESUMED) { |
170 | start(); |
171 | } |
172 | } |
173 | } |
174 | } |