11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file. 41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)package org.chromium.sync.notifier; 61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import android.accounts.Account; 81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import android.content.Intent; 91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import com.google.ipc.invalidation.external.client.types.ObjectId; 111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import com.google.protos.ipc.invalidation.Types; 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import org.chromium.base.CollectionUtil; 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import org.chromium.sync.internal_api.pub.base.ModelType; 151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import java.util.ArrayList; 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import java.util.HashSet; 181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)import java.util.Set; 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)/** 211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Constants and utility methods to create the intents used to communicate between the 221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * controller and the invalidation client library. 231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)public class InvalidationIntentProtocol { 251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Action set on register intents. 271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String ACTION_REGISTER = 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) "org.chromium.sync.notifier.ACTION_REGISTER_TYPES"; 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Parcelable-valued intent extra containing the account of the user. 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String EXTRA_ACCOUNT = "account"; 351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * String-list-valued intent extra of the syncable types to sync. 381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String EXTRA_REGISTERED_TYPES = "registered_types"; 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Int-array-valued intent extra containing sources of objects to register for. 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES. 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_object_sources"; 461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * String-array-valued intent extra containing names of objects to register for. 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES. 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_object_names"; 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Boolean-valued intent extra indicating that the service should be stopped. 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static final String EXTRA_STOP = "stop"; 571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Create an Intent that will start the invalidation listener service and 601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * register for the specified types. 611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static Intent createRegisterIntent(Account account, 631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) boolean allTypes, Set<ModelType> types) { 641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Intent registerIntent = new Intent(ACTION_REGISTER); 651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) String[] selectedTypesArray; 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (allTypes) { 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE}; 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } else { 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) selectedTypesArray = new String[types.size()]; 701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) int pos = 0; 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) for (ModelType type : types) { 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) selectedTypesArray[pos++] = type.name(); 731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES, 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) CollectionUtil.newArrayList(selectedTypesArray)); 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) registerIntent.putExtra(EXTRA_ACCOUNT, account); 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return registerIntent; 791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Create an Intent that will start the invalidation listener service and 831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * register for the object ids with the specified sources and names. 841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Sync-specific objects are filtered out of the request since Sync types 851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * are registered using the other version of createRegisterIntent. 861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static Intent createRegisterIntent(Account account, int[] objectSources, 881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) String[] objectNames) { 891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (objectSources.length != objectNames.length) { 901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci throw new IllegalArgumentException( 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci "objectSources and objectNames must have the same length"); 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci } 931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Add all non-Sync objects to new lists. 951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ArrayList<Integer> sources = new ArrayList<Integer>(); 961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ArrayList<String> names = new ArrayList<String>(); 971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) for (int i = 0; i < objectSources.length; i++) { 981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (objectSources[i] != Types.ObjectSource.CHROME_SYNC) { 991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) sources.add(objectSources[i]); 1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) names.add(objectNames[i]); 1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Intent registerIntent = new Intent(ACTION_REGISTER); 1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) registerIntent.putIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES, sources); 1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES, names); 1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) registerIntent.putExtra(EXTRA_ACCOUNT, account); 1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return registerIntent; 1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Returns whether {@code intent} is a stop intent. */ 1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static boolean isStop(Intent intent) { 1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return intent.getBooleanExtra(EXTRA_STOP, false); 1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Returns whether {@code intent} is a registered types change intent. */ 1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static boolean isRegisteredTypesChange(Intent intent) { 1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return intent.hasExtra(EXTRA_REGISTERED_TYPES) || 1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES); 1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Returns the object ids for which to register contained in the intent. */ 1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public static Set<ObjectId> getRegisteredObjectIds(Intent intent) { 1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ArrayList<Integer> objectSources = 1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES); 1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ArrayList<String> objectNames = 1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES); 1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (objectSources == null || objectNames == null || 1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) objectSources.size() != objectNames.size()) { 1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return null; 1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size()); 1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) for (int i = 0; i < objectSources.size(); i++) { 1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) objectIds.add(ObjectId.newInstance( 1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) objectSources.get(i), objectNames.get(i).getBytes())); 1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return objectIds; 1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private InvalidationIntentProtocol() { 1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Disallow instantiation. 1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 144