Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | /* |
2 | * Copyright 2012 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.android.gcm; | |
18 | ||
19 | import static com.google.android.gcm.GCMConstants.DEFAULT_INTENT_SERVICE_CLASS_NAME; | |
20 | ||
21 | import android.app.Activity; | |
22 | import android.content.BroadcastReceiver; | |
23 | import android.content.Context; | |
24 | import android.content.Intent; | |
25 | import android.util.Log; | |
26 | ||
27 | /** | |
28 | * {@link BroadcastReceiver} that receives GCM messages and delivers them to | |
29 | * an application-specific {@link GCMBaseIntentService} subclass. | |
30 | * <p> | |
31 | * By default, the {@link GCMBaseIntentService} class belongs to the application | |
32 | * main package and is named | |
33 | * {@link GCMConstants#DEFAULT_INTENT_SERVICE_CLASS_NAME}. To use a new class, | |
34 | * the {@link #getGCMIntentServiceClassName(Context)} must be overridden. | |
35 | */ | |
36 | public final class GCMBroadcastReceiver extends BroadcastReceiver { | |
37 | ||
38 | private static final String TAG = "GCMBroadcastReceiver"; | |
39 | private static boolean mReceiverSet = false; | |
40 | ||
41 | @Override | |
42 | public final void onReceive(Context context, Intent intent) { | |
43 | Log.v(TAG, "onReceive: " + intent.getAction()); | |
44 | // do a one-time check if app is using a custom GCMBroadcastReceiver | |
45 | if (!mReceiverSet) { | |
46 | mReceiverSet = true; | |
47 | String myClass = getClass().getName(); | |
48 | if (!myClass.equals(GCMBroadcastReceiver.class.getName())) { | |
49 | GCMRegistrar.setRetryReceiverClassName(myClass); | |
50 | } | |
51 | } | |
52 | String className = getGCMIntentServiceClassName(context); | |
53 | Log.v(TAG, "GCM IntentService class: " + className); | |
54 | // Delegates to the application-specific intent service. | |
55 | GCMBaseIntentService.runIntentInService(context, intent, className); | |
56 | setResult(Activity.RESULT_OK, null /* data */, null /* extra */); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Gets the class name of the intent service that will handle GCM messages. | |
61 | */ | |
62 | protected static String getGCMIntentServiceClassName(final Context context) { | |
63 | return getDefaultIntentServiceClassName(context); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Gets the default class name of the intent service that will handle GCM | |
68 | * messages. | |
69 | */ | |
70 | private static final String getDefaultIntentServiceClassName(final Context context) { | |
71 | String className = context.getPackageName() + | |
72 | DEFAULT_INTENT_SERVICE_CLASS_NAME; | |
73 | return className; | |
74 | } | |
75 | } |