X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2Fcom%2Fgoogle%2Fandroid%2Fgcm%2FGCMRegistrar.java;fp=lib%2Fcom%2Fgoogle%2Fandroid%2Fgcm%2FGCMRegistrar.java;h=0000000000000000000000000000000000000000;hb=ca40a5cc0f95f37d2ac3008d5fa3f0adaf47d736;hp=0bd5c9f18ed74dead168487d70117912697dafe9;hpb=72de2947b5234e3b8495065e358530d9fddf3fb8;p=fonbot.git diff --git a/lib/com/google/android/gcm/GCMRegistrar.java b/lib/com/google/android/gcm/GCMRegistrar.java deleted file mode 100644 index 0bd5c9f..0000000 --- a/lib/com/google/android/gcm/GCMRegistrar.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.gcm; - -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Build; -import android.util.Log; - -/** - * Utilities for device registration. - *

- * Note: this class uses a private {@link SharedPreferences} - * object to keep track of the registration token. - */ -public final class GCMRegistrar { - - /** - * Default lifespan (7 days) of the {@link #isRegisteredOnServer(Context)} - * flag until it is considered expired. - */ - // NOTE: cannot use TimeUnit.DAYS because it's not available on API Level 8 - public static final long DEFAULT_ON_SERVER_LIFESPAN_MS = - 1000 * 3600 * 24 * 7; - - private static final String TAG = "GCMRegistrar"; - private static final String BACKOFF_MS = "backoff_ms"; - private static final String GSF_PACKAGE = "com.google.android.gsf"; - private static final String PREFERENCES = "com.google.android.gcm"; - private static final int DEFAULT_BACKOFF_MS = 3000; - private static final String PROPERTY_REG_ID = "regId"; - private static final String PROPERTY_APP_VERSION = "appVersion"; - /** - * {@link GCMBroadcastReceiver} instance used to handle the retry intent. - * - *

- * This instance cannot be the same as the one defined in the manifest - * because it needs a different permission. - */ - private static GCMBroadcastReceiver sRetryReceiver; - - private static String sRetryReceiverClassName; - - /** - * Checks if the device has the proper dependencies installed. - *

- * This method should be called when the application starts to verify that - * the device supports GCM. - * - * @param context application context. - * @throws UnsupportedOperationException if the device does not support GCM. - */ - public static void checkDevice(final Context context) { - final int version = Build.VERSION.SDK_INT; - if (version < 8) { - throw new UnsupportedOperationException("Device must be at least " + - "API Level 8 (instead of " + version + ")"); - } - final PackageManager packageManager = context.getPackageManager(); - try { - packageManager.getPackageInfo(GSF_PACKAGE, 0); - } catch (NameNotFoundException e) { - throw new UnsupportedOperationException( - "Device does not have package " + GSF_PACKAGE); - } - } - - /** - * Initiate messaging registration for the current application. - *

- * The result will be returned as an - * {@link GCMConstants#INTENT_FROM_GCM_REGISTRATION_CALLBACK} intent with - * either a {@link GCMConstants#EXTRA_REGISTRATION_ID} or - * {@link GCMConstants#EXTRA_ERROR}. - * - * @param context application context. - * @param senderIds Google Project ID of the accounts authorized to send - * messages to this application. - * @throws IllegalStateException if device does not have all GCM - * dependencies installed. - */ - public static void register(Context context, String... senderIds) { - GCMRegistrar.resetBackoff(context); - internalRegister(context, senderIds); - } - - static void internalRegister(Context context, String... senderIds) { - String flatSenderIds = getFlatSenderIds(senderIds); - Log.v(TAG, "Registering app " + context.getPackageName() + - " of senders " + flatSenderIds); - Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_REGISTRATION); - intent.setPackage(GSF_PACKAGE); - intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT, - PendingIntent.getBroadcast(context, 0, new Intent(), 0)); - intent.putExtra(GCMConstants.EXTRA_SENDER, flatSenderIds); - context.startService(intent); - } - - static String getFlatSenderIds(String... senderIds) { - if (senderIds == null || senderIds.length == 0) { - throw new IllegalArgumentException("No senderIds"); - } - StringBuilder builder = new StringBuilder(senderIds[0]); - for (int i = 1; i < senderIds.length; i++) { - builder.append(',').append(senderIds[i]); - } - return builder.toString(); - } - - /** - * Clear internal resources. - * - *

- * This method should be called by the main activity's {@code onDestroy()} - * method. - */ - public static synchronized void onDestroy(Context context) { - if (sRetryReceiver != null) { - Log.v(TAG, "Unregistering receiver"); - context.unregisterReceiver(sRetryReceiver); - sRetryReceiver = null; - } - } - - static void internalUnregister(Context context) { - Log.v(TAG, "Unregistering app " + context.getPackageName()); - Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_UNREGISTRATION); - intent.setPackage(GSF_PACKAGE); - intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT, - PendingIntent.getBroadcast(context, 0, new Intent(), 0)); - context.startService(intent); - } - - /** - * Lazy initializes the {@link GCMBroadcastReceiver} instance. - */ - static synchronized void setRetryBroadcastReceiver(Context context) { - if (sRetryReceiver == null) { - if (sRetryReceiverClassName == null) { - // should never happen - Log.e(TAG, "internal error: retry receiver class not set yet"); - sRetryReceiver = new GCMBroadcastReceiver(); - } else { - Class clazz; - try { - clazz = Class.forName(sRetryReceiverClassName); - sRetryReceiver = (GCMBroadcastReceiver) clazz.newInstance(); - } catch (Exception e) { - Log.e(TAG, "Could not create instance of " + - sRetryReceiverClassName + ". Using " + - GCMBroadcastReceiver.class.getName() + - " directly."); - sRetryReceiver = new GCMBroadcastReceiver(); - } - } - String category = context.getPackageName(); - IntentFilter filter = new IntentFilter( - GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY); - filter.addCategory(category); - // must use a permission that is defined on manifest for sure - String permission = category + ".permission.C2D_MESSAGE"; - Log.v(TAG, "Registering receiver"); - context.registerReceiver(sRetryReceiver, filter, permission, null); - } - } - - /** - * Sets the name of the retry receiver class. - */ - static void setRetryReceiverClassName(String className) { - Log.v(TAG, "Setting the name of retry receiver class to " + className); - sRetryReceiverClassName = className; - } - - /** - * Gets the current registration id for application on GCM service. - *

- * If result is empty, the registration has failed. - * - * @return registration id, or empty string if the registration is not - * complete. - */ - public static String getRegistrationId(Context context) { - final SharedPreferences prefs = getGCMPreferences(context); - String registrationId = prefs.getString(PROPERTY_REG_ID, ""); - // check if app was updated; if so, it must clear registration id to - // avoid a race condition if GCM sends a message - int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); - int newVersion = getAppVersion(context); - if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) { - Log.v(TAG, "App version changed from " + oldVersion + " to " + - newVersion + "; resetting registration id"); - clearRegistrationId(context); - registrationId = ""; - } - return registrationId; - } - - /** - * Checks whether the application was successfully registered on GCM - * service. - */ - static boolean isRegistered(Context context) { - return getRegistrationId(context).length() > 0; - } - - /** - * Clears the registration id in the persistence store. - * - * @param context application's context. - * @return old registration id. - */ - static String clearRegistrationId(Context context) { - return setRegistrationId(context, ""); - } - - /** - * Sets the registration id in the persistence store. - * - * @param context application's context. - * @param regId registration id - */ - static String setRegistrationId(Context context, String regId) { - final SharedPreferences prefs = getGCMPreferences(context); - String oldRegistrationId = prefs.getString(PROPERTY_REG_ID, ""); - int appVersion = getAppVersion(context); - Log.v(TAG, "Saving regId on app version " + appVersion); - Editor editor = prefs.edit(); - editor.putString(PROPERTY_REG_ID, regId); - editor.putInt(PROPERTY_APP_VERSION, appVersion); - editor.commit(); - return oldRegistrationId; - } - - /** - * Gets the application version. - */ - private static int getAppVersion(Context context) { - try { - PackageInfo packageInfo = context.getPackageManager() - .getPackageInfo(context.getPackageName(), 0); - return packageInfo.versionCode; - } catch (NameNotFoundException e) { - // should never happen - throw new RuntimeException("Coult not get package name: " + e); - } - } - - /** - * Resets the backoff counter. - *

- * This method should be called after a GCM call succeeds. - * - * @param context application's context. - */ - static void resetBackoff(Context context) { - Log.d(TAG, "resetting backoff for " + context.getPackageName()); - setBackoff(context, DEFAULT_BACKOFF_MS); - } - - /** - * Gets the current backoff counter. - * - * @param context application's context. - * @return current backoff counter, in milliseconds. - */ - static int getBackoff(Context context) { - final SharedPreferences prefs = getGCMPreferences(context); - return prefs.getInt(BACKOFF_MS, DEFAULT_BACKOFF_MS); - } - - /** - * Sets the backoff counter. - *

- * This method should be called after a GCM call fails, passing an - * exponential value. - * - * @param context application's context. - * @param backoff new backoff counter, in milliseconds. - */ - static void setBackoff(Context context, int backoff) { - final SharedPreferences prefs = getGCMPreferences(context); - Editor editor = prefs.edit(); - editor.putInt(BACKOFF_MS, backoff); - editor.commit(); - } - - private static SharedPreferences getGCMPreferences(Context context) { - return context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE); - } - - private GCMRegistrar() { - throw new UnsupportedOperationException(); - } -}