2 * Copyright 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.ipc.invalidation.ticl.android.c2dm;
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
25 * Utilities for device registration.
27 * Will keep track of the registration token in a private preference.
29 * This is based on the open source chrometophone project.
31 public class C2DMessaging {
32 static final String ACTION_MESSAGE = "com.google.android.c2dm.manager.intent.MESSAGE";
34 static final String ACTION_REGISTER = "com.google.android.c2dm.manager.intent.REGISTER";
36 static final String ACTION_UNREGISTER = "com.google.android.c2dm.manager.intent.UNREGISTER";
38 static final String ACTION_REGISTERED = "com.google.android.c2dm.manager.intent.REGISTERED";
40 static final String ACTION_UNREGISTERED = "com.google.android.c2dm.manager.intent.UNREGISTERED";
42 static final String ACTION_REGISTRATION_ERROR =
43 "com.google.android.c2dm.manager.intent.REGISTRATION_ERROR";
45 static final String EXTRA_REGISTRATION_ID =
46 "com.google.android.c2dm.manager.extra.REGISTRATION_ID";
48 static final String EXTRA_REGISTRATION_ERROR = "com.google.android.c2dm.manager.extra.ERROR";
50 static final String EXTRA_CANONICAL_CLASS =
51 "com.google.android.c2dm.manager.extra.CANONICAL_CLASS";
53 static final String EXTRA_FILTER_KEY = "com.google.android.c2dm.manager.extra.FILTER_KEY";
55 static final String EXTRA_FILTER_VALUE = "com.google.android.c2dm.manager.extra.FILTER_VALUE";
57 static final String EXTRA_HANDLE_WAKELOCK =
58 "com.google.android.c2dm.manager.extra.HANDLE_WAKELOCK";
60 static final String EXTRA_RELEASE_WAKELOCK =
61 "com.google.android.c2dm.manager.extra.RELEASE_WAKELOCK";
64 * The device can't read the response, or there was a 500/503 from the server that can be retried
65 * later. The C2DMManager will automatically use exponential back off and retry.
67 public static final String ERR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAILABLE";
70 * There is no Google account on the phone. The application should ask the user to open the
71 * account manager and add a Google account. Fix on the device side.
73 public static final String ERR_ACCOUNT_MISSING = "ACCOUNT_MISSING";
76 * Bad password. The application should ask the user to enter his/her password, and let user retry
77 * manually later. Fix on the device side.
79 public static final String ERR_AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED";
82 * The user has too many applications registered. The application should tell the user to
83 * uninstall some other applications, let user retry manually. Fix on the device side.
85 public static final String ERR_TOO_MANY_REGISTRATIONS = "TOO_MANY_REGISTRATIONS";
88 * Invalid parameters found in C2DM registration or message.
90 public static final String ERR_INVALID_PARAMETERS = "INVALID_PARAMETERS";
93 * The sender account is not recognized.
95 public static final String ERR_INVALID_SENDER = "INVALID_SENDER";
97 /** Incorrect phone registration with Google. This phone doesn't currently support C2DM. */
98 public static final String ERR_PHONE_REGISTRATION_ERROR = "PHONE_REGISTRATION_ERROR";
100 public static String getSenderId(Context context) {
101 return C2DMManager.readSenderIdFromMetaData(context);
105 * Returns the current C2DM registration ID for the application or {@code null} if not yet full
108 public static String getRegistrationId(Context context) {
109 return C2DMSettings.getC2DMRegistrationId(context);
113 * Registers a new C2DM observer service that will receive registration notifications and
114 * delivered messages. Receipt of messages can be made conditional based upon the presence of a
115 * particular extra in the c2dm message and optionally the value of that extra.
117 * @param context the current application context
118 * @param clazz the service that will receive c2dm activity intents
119 * @param selectKey the name of an extra that will be present in messages selected for this
120 * observer. If {@code null}, all messages are delivered.
121 * @param selectValue defines a specific value that must match for the messages selected for this
122 * observer. If {@code null}, any value will match.
123 * @param handleWakeLock if {@code true} indicates that a wake lock should be acquired from the
124 * {@link WakeLockManager} before messages are delivered to the observer and that the
125 * observer will be responsible for releasing the lock.
127 public static void register(Context context, Class<? extends Service> clazz,
128 String selectKey, String selectValue, boolean handleWakeLock) {
130 Intent intent = new Intent();
131 intent.setAction(ACTION_REGISTER);
132 intent.putExtra(EXTRA_CANONICAL_CLASS, clazz.getCanonicalName());
133 intent.putExtra(EXTRA_FILTER_KEY, selectKey);
134 intent.putExtra(EXTRA_FILTER_VALUE, selectValue);
135 intent.putExtra(EXTRA_HANDLE_WAKELOCK, handleWakeLock);
136 C2DMManager.runIntentInService(context, intent);
140 * Unregisters an existing C2DM observer service so it will no longer receive notifications or
141 * messages (or than a final unregister notification indicating that the observer has been
144 * @param context the current application context
145 * @param clazz the service that will receive c2dm activity intents
146 * @param selectKey the name of an extra that will be present in messages selected for this
147 * observer. If {@code null}, all messages are delivered.
148 * @param selectValue defines a specific value that must match for the messages selected for this
149 * observer. If {@code null}, any value will match.
150 * @param handleWakeLock if {@code true} indicates that a wake lock should be acquired from the
151 * {@link WakeLockManager} before messages are delivered to the observer and that the
152 * observer will be responsible for releasing the lock.
154 public static void unregister(Context context, Class<?> clazz, String selectKey,
155 String selectValue, boolean handleWakeLock) {
156 Intent intent = new Intent();
157 intent.setAction(ACTION_UNREGISTER);
158 intent.putExtra(EXTRA_CANONICAL_CLASS, clazz.getCanonicalName());
159 intent.putExtra(EXTRA_FILTER_KEY, selectKey);
160 intent.putExtra(EXTRA_FILTER_VALUE, selectValue);
161 intent.putExtra(EXTRA_HANDLE_WAKELOCK, handleWakeLock);
162 C2DMManager.runIntentInService(context, intent);