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.sync.notifier;
7 import android.accounts.Account;
8 import android.content.Intent;
10 import com.google.ipc.invalidation.external.client.types.ObjectId;
11 import com.google.protos.ipc.invalidation.Types;
13 import org.chromium.base.CollectionUtil;
14 import org.chromium.sync.internal_api.pub.base.ModelType;
16 import java.util.ArrayList;
17 import java.util.HashSet;
21 * Constants and utility methods to create the intents used to communicate between the
22 * controller and the invalidation client library.
24 public class InvalidationIntentProtocol {
26 * Action set on register intents.
28 public static final String ACTION_REGISTER =
29 "org.chromium.sync.notifier.ACTION_REGISTER_TYPES";
32 * Parcelable-valued intent extra containing the account of the user.
34 public static final String EXTRA_ACCOUNT = "account";
37 * String-list-valued intent extra of the syncable types to sync.
39 public static final String EXTRA_REGISTERED_TYPES = "registered_types";
42 * Int-array-valued intent extra containing sources of objects to register for.
43 * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES.
45 public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_object_sources";
48 * String-array-valued intent extra containing names of objects to register for.
49 * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES.
51 public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_object_names";
54 * Boolean-valued intent extra indicating that the service should be stopped.
56 public static final String EXTRA_STOP = "stop";
59 * Create an Intent that will start the invalidation listener service and
60 * register for the specified types.
62 public static Intent createRegisterIntent(Account account,
63 boolean allTypes, Set<ModelType> types) {
64 Intent registerIntent = new Intent(ACTION_REGISTER);
65 String[] selectedTypesArray;
67 selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE};
69 selectedTypesArray = new String[types.size()];
71 for (ModelType type : types) {
72 selectedTypesArray[pos++] = type.name();
75 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES,
76 CollectionUtil.newArrayList(selectedTypesArray));
77 registerIntent.putExtra(EXTRA_ACCOUNT, account);
78 return registerIntent;
82 * Create an Intent that will start the invalidation listener service and
83 * register for the object ids with the specified sources and names.
84 * Sync-specific objects are filtered out of the request since Sync types
85 * are registered using the other version of createRegisterIntent.
87 public static Intent createRegisterIntent(Account account, int[] objectSources,
88 String[] objectNames) {
89 if (objectSources.length != objectNames.length) {
90 throw new IllegalArgumentException(
91 "objectSources and objectNames must have the same length");
94 // Add all non-Sync objects to new lists.
95 ArrayList<Integer> sources = new ArrayList<Integer>();
96 ArrayList<String> names = new ArrayList<String>();
97 for (int i = 0; i < objectSources.length; i++) {
98 if (objectSources[i] != Types.ObjectSource.CHROME_SYNC) {
99 sources.add(objectSources[i]);
100 names.add(objectNames[i]);
104 Intent registerIntent = new Intent(ACTION_REGISTER);
105 registerIntent.putIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES, sources);
106 registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES, names);
107 registerIntent.putExtra(EXTRA_ACCOUNT, account);
108 return registerIntent;
111 /** Returns whether {@code intent} is a stop intent. */
112 public static boolean isStop(Intent intent) {
113 return intent.getBooleanExtra(EXTRA_STOP, false);
116 /** Returns whether {@code intent} is a registered types change intent. */
117 public static boolean isRegisteredTypesChange(Intent intent) {
118 return intent.hasExtra(EXTRA_REGISTERED_TYPES) ||
119 intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
122 /** Returns the object ids for which to register contained in the intent. */
123 public static Set<ObjectId> getRegisteredObjectIds(Intent intent) {
124 ArrayList<Integer> objectSources =
125 intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
126 ArrayList<String> objectNames =
127 intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES);
128 if (objectSources == null || objectNames == null ||
129 objectSources.size() != objectNames.size()) {
132 Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size());
133 for (int i = 0; i < objectSources.size(); i++) {
134 objectIds.add(ObjectId.newInstance(
135 objectSources.get(i), objectNames.get(i).getBytes()));
140 private InvalidationIntentProtocol() {
141 // Disallow instantiation.