Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / sync / android / java / src / org / chromium / sync / notifier / InvalidationIntentProtocol.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.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.internal_api.pub.base.ModelType;
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 between 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 for.
43      * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES.
44      */
45     public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_object_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_object_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,
63                                               boolean allTypes, Set<ModelType> types) {
64         Intent registerIntent = new Intent(ACTION_REGISTER);
65         String[] selectedTypesArray;
66         if (allTypes) {
67             selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE};
68         } else {
69             selectedTypesArray = new String[types.size()];
70             int pos = 0;
71             for (ModelType type : types) {
72                 selectedTypesArray[pos++] = type.name();
73             }
74         }
75         registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES,
76                 CollectionUtil.newArrayList(selectedTypesArray));
77         registerIntent.putExtra(EXTRA_ACCOUNT, account);
78         return registerIntent;
79     }
80
81     /**
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.
86      */
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");
92         }
93
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]);
101             }
102         }
103
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;
109     }
110
111     /** Returns whether {@code intent} is a stop intent. */
112     public static boolean isStop(Intent intent) {
113         return intent.getBooleanExtra(EXTRA_STOP, false);
114     }
115
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);
120     }
121
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()) {
130             return null;
131         }
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()));
136         }
137         return objectIds;
138     }
139
140     private InvalidationIntentProtocol() {
141         // Disallow instantiation.
142     }
143 }