Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / android / c2dm / C2DMessaging.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.ipc.invalidation.ticl.android.c2dm;
18
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22
23
24 /**
25  * Utilities for device registration.
26  *
27  *  Will keep track of the registration token in a private preference.
28  *
29  * This is based on the open source chrometophone project.
30  */
31 public class C2DMessaging {
32   static final String ACTION_MESSAGE = "com.google.android.c2dm.manager.intent.MESSAGE";
33
34   static final String ACTION_REGISTER = "com.google.android.c2dm.manager.intent.REGISTER";
35
36   static final String ACTION_UNREGISTER = "com.google.android.c2dm.manager.intent.UNREGISTER";
37
38   static final String ACTION_REGISTERED = "com.google.android.c2dm.manager.intent.REGISTERED";
39
40   static final String ACTION_UNREGISTERED = "com.google.android.c2dm.manager.intent.UNREGISTERED";
41
42   static final String ACTION_REGISTRATION_ERROR =
43       "com.google.android.c2dm.manager.intent.REGISTRATION_ERROR";
44
45   static final String EXTRA_REGISTRATION_ID =
46       "com.google.android.c2dm.manager.extra.REGISTRATION_ID";
47
48   static final String EXTRA_REGISTRATION_ERROR = "com.google.android.c2dm.manager.extra.ERROR";
49
50   static final String EXTRA_CANONICAL_CLASS =
51       "com.google.android.c2dm.manager.extra.CANONICAL_CLASS";
52
53   static final String EXTRA_FILTER_KEY = "com.google.android.c2dm.manager.extra.FILTER_KEY";
54
55   static final String EXTRA_FILTER_VALUE = "com.google.android.c2dm.manager.extra.FILTER_VALUE";
56
57   static final String EXTRA_HANDLE_WAKELOCK =
58       "com.google.android.c2dm.manager.extra.HANDLE_WAKELOCK";
59
60   static final String EXTRA_RELEASE_WAKELOCK =
61       "com.google.android.c2dm.manager.extra.RELEASE_WAKELOCK";
62
63   /**
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.
66    */
67   public static final String ERR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAILABLE";
68
69   /**
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.
72    */
73   public static final String ERR_ACCOUNT_MISSING = "ACCOUNT_MISSING";
74
75   /**
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.
78    */
79   public static final String ERR_AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED";
80
81   /**
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.
84    */
85   public static final String ERR_TOO_MANY_REGISTRATIONS = "TOO_MANY_REGISTRATIONS";
86
87   /**
88    * Invalid parameters found in C2DM registration or message.
89    */
90   public static final String ERR_INVALID_PARAMETERS = "INVALID_PARAMETERS";
91
92   /**
93    * The sender account is not recognized.
94    */
95   public static final String ERR_INVALID_SENDER = "INVALID_SENDER";
96
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";
99
100   public static String getSenderId(Context context) {
101     return C2DMManager.readSenderIdFromMetaData(context);
102   }
103
104   /**
105    * Returns the current C2DM registration ID for the application or {@code null} if not yet full
106    * registered.
107    */
108   public static String getRegistrationId(Context context) {
109     return C2DMSettings.getC2DMRegistrationId(context);
110   }
111
112   /**
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.
116    *
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.
126    */
127   public static void register(Context context, Class<? extends Service> clazz,
128       String selectKey, String selectValue, boolean handleWakeLock) {
129
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);
137   }
138
139   /**
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
142    * unregistered.
143    *
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.
153    */
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);
163   }
164 }