OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.sync.notifier; | 5 package org.chromium.sync.notifier; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 | 10 |
11 import com.google.common.annotations.VisibleForTesting; | 11 import com.google.common.annotations.VisibleForTesting; |
12 import com.google.common.base.Preconditions; | 12 import com.google.common.base.Preconditions; |
| 13 import com.google.ipc.invalidation.external.client.types.ObjectId; |
13 | 14 |
14 import org.chromium.base.ActivityStatus; | 15 import org.chromium.base.ActivityStatus; |
| 16 import org.chromium.base.CalledByNative; |
15 import org.chromium.base.CollectionUtil; | 17 import org.chromium.base.CollectionUtil; |
16 import org.chromium.sync.internal_api.pub.base.ModelType; | 18 import org.chromium.sync.internal_api.pub.base.ModelType; |
17 | 19 |
| 20 import java.util.HashSet; |
18 import java.util.Set; | 21 import java.util.Set; |
19 | 22 |
20 /** | 23 /** |
21 * Controller used to send start, stop, and registration-change commands to the
invalidation | 24 * Controller used to send start, stop, and registration-change commands to the
invalidation |
22 * client library used by Sync. | 25 * client library used by Sync. |
23 */ | 26 */ |
24 public class InvalidationController implements ActivityStatus.StateListener { | 27 public class InvalidationController implements ActivityStatus.StateListener { |
25 /** | 28 /** |
26 * Constants and utility methods to create the intents used to communicate b
etween the | 29 * Constants and utility methods to create the intents used to communicate b
etween the |
27 * controller and the invalidation client library. | 30 * controller and the invalidation client library. |
28 */ | 31 */ |
29 public static class IntentProtocol { | 32 public static class IntentProtocol { |
30 /** | 33 /** |
31 * Action set on register intents. | 34 * Action set on register intents. |
32 */ | 35 */ |
33 public static final String ACTION_REGISTER = | 36 public static final String ACTION_REGISTER = |
34 "org.chromium.sync.notifier.ACTION_REGISTER_TYPES"; | 37 "org.chromium.sync.notifier.ACTION_REGISTER_TYPES"; |
35 | 38 |
36 /** | 39 /** |
37 * Parcelable-valued intent extra containing the account of the user. | 40 * Parcelable-valued intent extra containing the account of the user. |
38 */ | 41 */ |
39 public static final String EXTRA_ACCOUNT = "account"; | 42 public static final String EXTRA_ACCOUNT = "account"; |
40 | 43 |
41 /** | 44 /** |
42 * String-list-valued intent extra of the syncable types to sync. | 45 * String-list-valued intent extra of the syncable types to sync. |
43 */ | 46 */ |
44 public static final String EXTRA_REGISTERED_TYPES = "registered_types"; | 47 public static final String EXTRA_REGISTERED_TYPES = "registered_types"; |
45 | 48 |
46 /** | 49 /** |
| 50 * Int-array-valued intent extra containing sources of objects to regist
er for. |
| 51 * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES. |
| 52 */ |
| 53 public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered
_object_sources"; |
| 54 |
| 55 /** |
| 56 * String-array-valued intent extra containing names of objects to regis
ter for. |
| 57 * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES. |
| 58 */ |
| 59 public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_o
bject_names"; |
| 60 |
| 61 /** |
47 * Boolean-valued intent extra indicating that the service should be sto
pped. | 62 * Boolean-valued intent extra indicating that the service should be sto
pped. |
48 */ | 63 */ |
49 public static final String EXTRA_STOP = "stop"; | 64 public static final String EXTRA_STOP = "stop"; |
50 | 65 |
51 /** | 66 /** |
52 * Create an Intent that will start the invalidation listener service an
d | 67 * Create an Intent that will start the invalidation listener service an
d |
53 * register for the specified types. | 68 * register for the specified types. |
54 */ | 69 */ |
55 public static Intent createRegisterIntent(Account account, | 70 public static Intent createRegisterIntent(Account account, |
56 boolean allTypes, Set<ModelTyp
e> types) { | 71 boolean allTypes, Set<ModelTyp
e> types) { |
57 Intent registerIntent = new Intent(ACTION_REGISTER); | 72 Intent registerIntent = new Intent(ACTION_REGISTER); |
58 String[] selectedTypesArray; | 73 String[] selectedTypesArray; |
59 if (allTypes) { | 74 if (allTypes) { |
60 selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE}; | 75 selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE}; |
61 } else { | 76 } else { |
62 selectedTypesArray = new String[types.size()]; | 77 selectedTypesArray = new String[types.size()]; |
63 int pos = 0; | 78 int pos = 0; |
64 for (ModelType type : types) { | 79 for (ModelType type : types) { |
65 selectedTypesArray[pos++] = type.name(); | 80 selectedTypesArray[pos++] = type.name(); |
66 } | 81 } |
67 } | 82 } |
68 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, | 83 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, |
69 CollectionUtil.newArrayList(selectedTypesArray)); | 84 CollectionUtil.newArrayList(selectedTypesArray)); |
70 registerIntent.putExtra(EXTRA_ACCOUNT, account); | 85 registerIntent.putExtra(EXTRA_ACCOUNT, account); |
71 return registerIntent; | 86 return registerIntent; |
72 } | 87 } |
73 | 88 |
| 89 /** |
| 90 * Create an Intent that will start the invalidation listener service an
d |
| 91 * register for the object ids with the specified sources and names. |
| 92 */ |
| 93 public static Intent createRegisterIntent(Account account, int[] objectS
ources, |
| 94 String[] objectNames) { |
| 95 Intent registerIntent = new Intent(ACTION_REGISTER); |
| 96 registerIntent.putExtra(EXTRA_REGISTERED_OBJECT_SOURCES, objectSourc
es); |
| 97 registerIntent.putExtra(EXTRA_REGISTERED_OBJECT_NAMES, objectNames); |
| 98 registerIntent.putExtra(EXTRA_ACCOUNT, account); |
| 99 return registerIntent; |
| 100 } |
| 101 |
74 /** Returns whether {@code intent} is a stop intent. */ | 102 /** Returns whether {@code intent} is a stop intent. */ |
75 public static boolean isStop(Intent intent) { | 103 public static boolean isStop(Intent intent) { |
76 return intent.getBooleanExtra(EXTRA_STOP, false); | 104 return intent.getBooleanExtra(EXTRA_STOP, false); |
77 } | 105 } |
78 | 106 |
79 /** Returns whether {@code intent} is a registered types change intent.
*/ | 107 /** Returns whether {@code intent} is a registered types change intent.
*/ |
80 public static boolean isRegisteredTypesChange(Intent intent) { | 108 public static boolean isRegisteredTypesChange(Intent intent) { |
81 return intent.hasExtra(EXTRA_REGISTERED_TYPES); | 109 return intent.hasExtra(EXTRA_REGISTERED_TYPES) || |
| 110 intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES); |
| 111 } |
| 112 |
| 113 /** Returns the object ids for which to register contained in the intent
. */ |
| 114 public static Set<ObjectId> getRegisteredObjectIds(Intent intent) { |
| 115 int[] objectSources = intent.getIntArrayExtra(EXTRA_REGISTERED_OBJEC
T_SOURCES); |
| 116 String[] objectNames = intent.getStringArrayExtra(EXTRA_REGISTERED_O
BJECT_NAMES); |
| 117 if (objectSources == null || objectNames == null || |
| 118 objectSources.length != objectNames.length) { |
| 119 return null; |
| 120 } |
| 121 Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.length
); |
| 122 for (int i = 0; i < objectSources.length; i++) { |
| 123 objectIds.add(ObjectId.newInstance(objectSources[i], objectNames
[i].getBytes())); |
| 124 } |
| 125 return objectIds; |
82 } | 126 } |
83 | 127 |
84 private IntentProtocol() { | 128 private IntentProtocol() { |
85 // Disallow instantiation. | 129 // Disallow instantiation. |
86 } | 130 } |
87 } | 131 } |
88 | 132 |
89 private static final Object LOCK = new Object(); | 133 private static final Object LOCK = new Object(); |
90 | 134 |
91 private static InvalidationController sInstance; | 135 private static InvalidationController sInstance; |
(...skipping 23 matching lines...) Expand all Loading... |
115 public void refreshRegisteredTypes(Set<ModelType> types) { | 159 public void refreshRegisteredTypes(Set<ModelType> types) { |
116 InvalidationPreferences invalidationPreferences = new InvalidationPrefer
ences(mContext); | 160 InvalidationPreferences invalidationPreferences = new InvalidationPrefer
ences(mContext); |
117 Set<String> savedSyncedTypes = invalidationPreferences.getSavedSyncedTyp
es(); | 161 Set<String> savedSyncedTypes = invalidationPreferences.getSavedSyncedTyp
es(); |
118 Account account = invalidationPreferences.getSavedSyncedAccount(); | 162 Account account = invalidationPreferences.getSavedSyncedAccount(); |
119 boolean allTypes = savedSyncedTypes != null && | 163 boolean allTypes = savedSyncedTypes != null && |
120 savedSyncedTypes.contains(ModelType.ALL_TYPES_TYPE); | 164 savedSyncedTypes.contains(ModelType.ALL_TYPES_TYPE); |
121 setRegisteredTypes(account, allTypes, types); | 165 setRegisteredTypes(account, allTypes, types); |
122 } | 166 } |
123 | 167 |
124 /** | 168 /** |
| 169 * Sets object ids for which the client should register for notification. Th
is is intended for |
| 170 * registering non-Sync types; Sync types are registered with {@code setRegi
steredTypes}. |
| 171 * |
| 172 * @param objectSources The sources of the objects. |
| 173 * @param objectNames The names of the objects. |
| 174 */ |
| 175 @CalledByNative |
| 176 public void setRegisteredObjectIds(int[] objectSources, String[] objectNames
) { |
| 177 InvalidationPreferences invalidationPreferences = new InvalidationPrefer
ences(mContext); |
| 178 Account account = invalidationPreferences.getSavedSyncedAccount(); |
| 179 Intent registerIntent = IntentProtocol.createRegisterIntent(account, obj
ectSources, |
| 180 objectNames); |
| 181 registerIntent.setClass(mContext, InvalidationService.class); |
| 182 mContext.startService(registerIntent); |
| 183 } |
| 184 |
| 185 /** |
125 * Starts the invalidation client. | 186 * Starts the invalidation client. |
126 */ | 187 */ |
127 public void start() { | 188 public void start() { |
128 Intent intent = new Intent(mContext, InvalidationService.class); | 189 Intent intent = new Intent(mContext, InvalidationService.class); |
129 mContext.startService(intent); | 190 mContext.startService(intent); |
130 } | 191 } |
131 | 192 |
132 /** | 193 /** |
133 * Stops the invalidation client. | 194 * Stops the invalidation client. |
134 */ | 195 */ |
135 public void stop() { | 196 public void stop() { |
136 Intent intent = new Intent(mContext, InvalidationService.class); | 197 Intent intent = new Intent(mContext, InvalidationService.class); |
137 intent.putExtra(IntentProtocol.EXTRA_STOP, true); | 198 intent.putExtra(IntentProtocol.EXTRA_STOP, true); |
138 mContext.startService(intent); | 199 mContext.startService(intent); |
139 } | 200 } |
140 | 201 |
141 /** | 202 /** |
142 * Returns the instance that will use {@code context} to issue intents. | 203 * Returns the instance that will use {@code context} to issue intents. |
143 * | 204 * |
144 * Calling this method will create the instance if it does not yet exist. | 205 * Calling this method will create the instance if it does not yet exist. |
145 */ | 206 */ |
| 207 @CalledByNative |
146 public static InvalidationController get(Context context) { | 208 public static InvalidationController get(Context context) { |
147 synchronized (LOCK) { | 209 synchronized (LOCK) { |
148 if (sInstance == null) { | 210 if (sInstance == null) { |
149 sInstance = new InvalidationController(context); | 211 sInstance = new InvalidationController(context); |
150 } | 212 } |
151 return sInstance; | 213 return sInstance; |
152 } | 214 } |
153 } | 215 } |
154 | 216 |
155 /** | 217 /** |
156 * Creates an instance using {@code context} to send intents. | 218 * Creates an instance using {@code context} to send intents. |
157 */ | 219 */ |
158 @VisibleForTesting | 220 @VisibleForTesting |
159 InvalidationController(Context context) { | 221 InvalidationController(Context context) { |
160 mContext = Preconditions.checkNotNull(context.getApplicationContext()); | 222 mContext = Preconditions.checkNotNull(context.getApplicationContext()); |
161 ActivityStatus.registerStateListener(this); | 223 ActivityStatus.registerStateListener(this); |
162 } | 224 } |
163 | 225 |
164 @Override | 226 @Override |
165 public void onActivityStateChange(int newState) { | 227 public void onActivityStateChange(int newState) { |
166 if (SyncStatusHelper.get(mContext).isSyncEnabled()) { | 228 if (SyncStatusHelper.get(mContext).isSyncEnabled()) { |
167 if (newState == ActivityStatus.PAUSED) { | 229 if (newState == ActivityStatus.PAUSED) { |
168 stop(); | 230 stop(); |
169 } else if (newState == ActivityStatus.RESUMED) { | 231 } else if (newState == ActivityStatus.RESUMED) { |
170 start(); | 232 start(); |
171 } | 233 } |
172 } | 234 } |
173 } | 235 } |
174 } | 236 } |
OLD | NEW |