-
Notifications
You must be signed in to change notification settings - Fork 1
/
GcmIntentService.java
192 lines (171 loc) · 7.74 KB
/
GcmIntentService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 2013 The Android Open Source Project
*
* 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 org.android.gcm.client;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
/**
* This {@code IntentService} does the actual handling of the GCM message.
* {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a
* partial wake lock for this service while the service does its work. When the
* service is finished, it calls {@code completeWakefulIntent()} to release the
* wake lock.
*/
public class GcmIntentService extends IntentService {
private static int REGISTER_DURATION = 10000;
private static int RING_DURATION = 20000;
static final String TAG = "GCM Client";
private static String pushpak;
private static String pushact;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final SharedPreferences prefs = getSharedPreferences("gcmclient", Context.MODE_PRIVATE);
pushpak = prefs.getString("push_pak", "");
pushact = prefs.getString("push_act", "");
final boolean pushon = prefs.getBoolean("push_on", true);
final boolean pushnotif = prefs.getBoolean("push_notification", true);
if (pushnotif) {
final String notifact = prefs.getString("notification_act", "");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
// Send a notification.
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types you're
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(getString(R.string.send_error) + ": " + extras.toString(), notifact);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(getString(R.string.deleted) + ": " + extras.toString(),
notifact);
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
sendNotification(
getString(R.string.received, extras.getString("name"), extras.getString("num")),
notifact);
Log.i(TAG, "Received: " + extras.toString());
}
}
}
// End if push is not enabled.
if (!pushon) {
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
return;
}
final boolean fullwake = prefs.getBoolean("full_wake", false);
final boolean endoff = prefs.getBoolean("end_off", true);
// Manage the screen.
PowerManager mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock mWakeLock = mPowerManager.newWakeLock(
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
boolean misScreenOn = mPowerManager.isScreenOn();
int mScreenTimeout = 0;
if (!misScreenOn) {
if (endoff) {
// Change the screen timeout setting.
mScreenTimeout = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 0);
if (mScreenTimeout != 0) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 3000);
}
}
// Full wake lock
if (fullwake) {
mWakeLock.acquire();
}
}
// Start the activity.
try {
startActivity(getPushactIntent(
Intent.FLAG_ACTIVITY_NO_USER_ACTION |
Intent.FLAG_ACTIVITY_NO_ANIMATION |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
// Wait to register.
Thread.sleep(REGISTER_DURATION);
} catch (android.content.ActivityNotFoundException e) {
RING_DURATION = 0;
Log.i(TAG, "Activity not started");
} catch (InterruptedException e) {
}
// Release the wake lock.
if (!misScreenOn && fullwake) {
mWakeLock.release();
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
// Restore the screen timeout setting.
if (endoff && mScreenTimeout != 0) {
try {
Thread.sleep(RING_DURATION);
} catch (InterruptedException e) {
}
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, mScreenTimeout);
}
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg, String action) {
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = getPushactIntent(0);
if (!action.isEmpty()) {
intent.setAction(action);
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this).
setSmallIcon(R.drawable.ic_stat_gcm).
setContentTitle(getString(R.string.notification)).
setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).
setContentText(msg).
setAutoCancel(true).
setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
}
private Intent getPushactIntent(int flags) {
Intent intent = new Intent();
intent.setClassName(pushpak, pushact);
intent.setFlags(flags |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}