OLD | NEW |
| (Empty) |
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.sync.notifier; | |
6 | |
7 import android.accounts.Account; | |
8 import android.content.Intent; | |
9 | |
10 import com.google.ipc.invalidation.external.client.types.ObjectId; | |
11 import com.google.protos.ipc.invalidation.Types; | |
12 | |
13 import org.chromium.base.CollectionUtil; | |
14 import org.chromium.sync.ModelTypeHelper; | |
15 | |
16 import java.util.ArrayList; | |
17 import java.util.HashSet; | |
18 import java.util.Set; | |
19 | |
20 /** | |
21 * Constants and utility methods to create the intents used to communicate betwe
en the | |
22 * controller and the invalidation client library. | |
23 */ | |
24 public class InvalidationIntentProtocol { | |
25 /** | |
26 * Action set on register intents. | |
27 */ | |
28 public static final String ACTION_REGISTER = | |
29 "org.chromium.sync.notifier.ACTION_REGISTER_TYPES"; | |
30 | |
31 /** | |
32 * Parcelable-valued intent extra containing the account of the user. | |
33 */ | |
34 public static final String EXTRA_ACCOUNT = "account"; | |
35 | |
36 /** | |
37 * String-list-valued intent extra of the syncable types to sync. | |
38 */ | |
39 public static final String EXTRA_REGISTERED_TYPES = "registered_types"; | |
40 | |
41 /** | |
42 * Int-array-valued intent extra containing sources of objects to register f
or. | |
43 * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES. | |
44 */ | |
45 public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_obj
ect_sources"; | |
46 | |
47 /** | |
48 * String-array-valued intent extra containing names of objects to register
for. | |
49 * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES. | |
50 */ | |
51 public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_objec
t_names"; | |
52 | |
53 /** | |
54 * Boolean-valued intent extra indicating that the service should be stopped
. | |
55 */ | |
56 public static final String EXTRA_STOP = "stop"; | |
57 | |
58 /** | |
59 * Create an Intent that will start the invalidation listener service and | |
60 * register for the specified types. | |
61 */ | |
62 public static Intent createRegisterIntent(Account account, Set<Integer> type
s) { | |
63 Intent registerIntent = new Intent(ACTION_REGISTER); | |
64 String[] selectedTypesArray = new String[types.size()]; | |
65 int pos = 0; | |
66 for (Integer type : types) { | |
67 selectedTypesArray[pos++] = ModelTypeHelper.toNotificationType(type)
; | |
68 } | |
69 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, | |
70 CollectionUtil.newArrayList(selectedTypesArray)); | |
71 registerIntent.putExtra(EXTRA_ACCOUNT, account); | |
72 return registerIntent; | |
73 } | |
74 | |
75 /** | |
76 * Create an Intent that will start the invalidation listener service and | |
77 * register for the object ids with the specified sources and names. | |
78 * Sync-specific objects are filtered out of the request since Sync types | |
79 * are registered using the other version of createRegisterIntent. | |
80 */ | |
81 public static Intent createRegisterIntent(Account account, int[] objectSourc
es, | |
82 String[] objectNames) { | |
83 if (objectSources.length != objectNames.length) { | |
84 throw new IllegalArgumentException( | |
85 "objectSources and objectNames must have the same length"); | |
86 } | |
87 | |
88 // Add all non-Sync objects to new lists. | |
89 ArrayList<Integer> sources = new ArrayList<Integer>(); | |
90 ArrayList<String> names = new ArrayList<String>(); | |
91 for (int i = 0; i < objectSources.length; i++) { | |
92 if (objectSources[i] != Types.ObjectSource.CHROME_SYNC) { | |
93 sources.add(objectSources[i]); | |
94 names.add(objectNames[i]); | |
95 } | |
96 } | |
97 | |
98 Intent registerIntent = new Intent(ACTION_REGISTER); | |
99 registerIntent.putIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES,
sources); | |
100 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES, na
mes); | |
101 registerIntent.putExtra(EXTRA_ACCOUNT, account); | |
102 return registerIntent; | |
103 } | |
104 | |
105 /** Returns whether {@code intent} is a stop intent. */ | |
106 public static boolean isStop(Intent intent) { | |
107 return intent.getBooleanExtra(EXTRA_STOP, false); | |
108 } | |
109 | |
110 /** Returns whether {@code intent} is a registered types change intent. */ | |
111 public static boolean isRegisteredTypesChange(Intent intent) { | |
112 return intent.hasExtra(EXTRA_REGISTERED_TYPES) | |
113 || intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES); | |
114 } | |
115 | |
116 /** Returns the object ids for which to register contained in the intent. */ | |
117 public static Set<ObjectId> getRegisteredObjectIds(Intent intent) { | |
118 ArrayList<Integer> objectSources = | |
119 intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES)
; | |
120 ArrayList<String> objectNames = | |
121 intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES); | |
122 if (objectSources == null || objectNames == null | |
123 || objectSources.size() != objectNames.size()) { | |
124 return null; | |
125 } | |
126 Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size()); | |
127 for (int i = 0; i < objectSources.size(); i++) { | |
128 objectIds.add(ObjectId.newInstance( | |
129 objectSources.get(i), objectNames.get(i).getBytes())); | |
130 } | |
131 return objectIds; | |
132 } | |
133 | |
134 private InvalidationIntentProtocol() { | |
135 // Disallow instantiation. | |
136 } | |
137 } | |
OLD | NEW |