1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.example.android.pingme; 16 17 import android.app.IntentService; 18 import android.app.Notification; 19 import android.app.NotificationManager; 20 import android.app.PendingIntent; 21 import android.content.Intent; 22 import android.support.v4.app.NotificationCompat; 23 import android.util.Log; 24 25 /** 26 * PingService creates a notification that includes 2 buttons: one to snooze the 27 * notification, and one to dismiss it. 28 */ 29 public class PingService extends IntentService { 30 31 private NotificationManager mNotificationManager; 32 private String mMessage; 33 private int mMillis; 34 NotificationCompat.Builder builder; 35 36 public PingService() { 37 38 // The super call is required. The background thread that IntentService 39 // starts is labeled with the string argument you pass. 40 super("com.example.android.pingme"); 41 } 42 43 @Override 44 protected void onHandleIntent(Intent intent) { 45 // The reminder message the user set. 46 mMessage = intent.getStringExtra(CommonConstants.EXTRA_MESSAGE); 47 // The timer duration the user set. The default is 10 seconds. 48 mMillis = intent.getIntExtra(CommonConstants.EXTRA_TIMER, 49 CommonConstants.DEFAULT_TIMER_DURATION); 50 NotificationManager nm = (NotificationManager) 51 getSystemService(NOTIFICATION_SERVICE); 52 53 String action = intent.getAction(); 54 // This section handles the 3 possible actions: 55 // ping, snooze, and dismiss. 56 if(action.equals(CommonConstants.ACTION_PING)) { 57 issueNotification(intent, mMessage); 58 } else if (action.equals(CommonConstants.ACTION_SNOOZE)) { 59 nm.cancel(CommonConstants.NOTIFICATION_ID); 60 Log.d(CommonConstants.DEBUG_TAG, getString(R.string.snoozing)); 61 // Sets a snooze-specific "done snoozing" message. 62 issueNotification(intent, getString(R.string.done_snoozing)); 63 64 } else if (action.equals(CommonConstants.ACTION_DISMISS)) { 65 nm.cancel(CommonConstants.NOTIFICATION_ID); 66 } 67 } 68 69 private void issueNotification(Intent intent, String msg) { 70 mNotificationManager = (NotificationManager) 71 getSystemService(NOTIFICATION_SERVICE); 72 73 // Sets up the Snooze and Dismiss action buttons that will appear in the 74 // expanded view of the notification. 75 Intent dismissIntent = new Intent(this, PingService.class); 76 dismissIntent.setAction(CommonConstants.ACTION_DISMISS); 77 PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0); 78 79 Intent snoozeIntent = new Intent(this, PingService.class); 80 snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE); 81 PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0); 82 83 // Constructs the Builder object. 84 builder = 85 new NotificationCompat.Builder(this) 86 .setSmallIcon(R.drawable.ic_stat_notification) 87 .setContentTitle(getString(R.string.notification)) 88 .setContentText(getString(R.string.ping)) 89 .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission 90 /* 91 * Sets the big view "big text" style and supplies the 92 * text (the user's reminder message) that will be displayed 93 * in the detail area of the expanded notification. 94 * These calls are ignored by the support library for 95 * pre-4.1 devices. 96 */ 97 .setStyle(new NotificationCompat.BigTextStyle() 98 .bigText(msg)) 99 .addAction (R.drawable.ic_stat_dismiss, 100 getString(R.string.dismiss), piDismiss) 101 .addAction (R.drawable.ic_stat_snooze, 102 getString(R.string.snooze), piSnooze); 103 104 /* 105 * Clicking the notification itself displays ResultActivity, which provides 106 * UI for snoozing or dismissing the notification. 107 * This is available through either the normal view or big view. 108 */ 109 Intent resultIntent = new Intent(this, ResultActivity.class); 110 resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg); 111 resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 112 113 // Because clicking the notification opens a new ("special") activity, there's 114 // no need to create an artificial back stack. 115 PendingIntent resultPendingIntent = 116 PendingIntent.getActivity( 117 this, 118 0, 119 resultIntent, 120 PendingIntent.FLAG_UPDATE_CURRENT 121 ); 122 123 builder.setContentIntent(resultPendingIntent); 124 startTimer(mMillis); 125 } 126 127 private void issueNotification(NotificationCompat.Builder builder) { 128 mNotificationManager = (NotificationManager) 129 getSystemService(NOTIFICATION_SERVICE); 130 // Including the notification ID allows you to update the notification later on. 131 mNotificationManager.notify(CommonConstants.NOTIFICATION_ID, builder.build()); 132 } 133 134 // Starts the timer according to the number of seconds the user specified. 135 private void startTimer(int millis) { 136 Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_start)); 137 try { 138 Thread.sleep(millis); 139 140 } catch (InterruptedException e) { 141 Log.d(CommonConstants.DEBUG_TAG, getString(R.string.sleep_error)); 142 } 143 Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_finished)); 144 issueNotification(builder); 145 } 146 } 147