1 /* 2 * Copyright (C) 2013 The Android Open Source Project 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.android.cellbroadcastreceiver; 18 19 import android.app.AlarmManager; 20 import android.app.PendingIntent; 21 import android.app.Service; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.SharedPreferences; 25 import android.media.Ringtone; 26 import android.media.RingtoneManager; 27 import android.net.Uri; 28 import android.os.IBinder; 29 import android.os.SystemClock; 30 import android.preference.PreferenceManager; 31 import android.util.Log; 32 import android.media.AudioManager; 33 import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.DBG; 34 35 /** 36 * Manages alert reminder notification. 37 */ 38 public class CellBroadcastAlertReminder extends Service { 39 private static final String TAG = "CellBroadcastAlertReminder"; 40 41 /** Action to wake up and play alert reminder sound. */ 42 static final String ACTION_PLAY_ALERT_REMINDER = "ACTION_PLAY_ALERT_REMINDER"; 43 44 /** 45 * Pending intent for alert reminder. This is static so that we don't have to start the 46 * service in order to cancel any pending reminders when user dismisses the alert dialog. 47 */ 48 private static PendingIntent sPlayReminderIntent; 49 50 /** 51 * Alert reminder for current ringtone being played. 52 */ 53 private static Ringtone sPlayReminderRingtone; 54 55 @Override onBind(Intent intent)56 public IBinder onBind(Intent intent) { 57 return null; 58 } 59 60 @Override onStartCommand(Intent intent, int flags, int startId)61 public int onStartCommand(Intent intent, int flags, int startId) { 62 // No intent or unrecognized action; tell the system not to restart us. 63 if (intent == null || !ACTION_PLAY_ALERT_REMINDER.equals(intent.getAction())) { 64 stopSelf(); 65 return START_NOT_STICKY; 66 } 67 68 log("playing alert reminder"); 69 playAlertReminderSound(); 70 71 if (queueAlertReminder(this, false)) { 72 return START_STICKY; 73 } else { 74 log("no reminders queued"); 75 stopSelf(); 76 return START_NOT_STICKY; 77 } 78 } 79 80 /** 81 * Use the RingtoneManager to play the alert reminder sound. 82 */ playAlertReminderSound()83 private void playAlertReminderSound() { 84 Uri notificationUri = RingtoneManager.getDefaultUri( 85 RingtoneManager.TYPE_NOTIFICATION); 86 if (notificationUri == null) { 87 loge("Can't get URI for alert reminder sound"); 88 return; 89 } 90 Ringtone r = RingtoneManager.getRingtone(this, notificationUri); 91 r.setStreamType(AudioManager.STREAM_NOTIFICATION); 92 93 if (r != null) { 94 log("playing alert reminder sound"); 95 r.play(); 96 } else { 97 loge("can't get Ringtone for alert reminder sound"); 98 } 99 } 100 101 /** 102 * Helper method to start the alert reminder service to queue the alert reminder. 103 * @return true if a pending reminder was set; false if there are no more reminders 104 */ queueAlertReminder(Context context, boolean firstTime)105 static boolean queueAlertReminder(Context context, boolean firstTime) { 106 // Stop any alert reminder sound and cancel any previously queued reminders. 107 cancelAlertReminder(); 108 109 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 110 String prefStr = prefs.getString(CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null); 111 112 if (prefStr == null) { 113 if (DBG) log("no preference value for alert reminder"); 114 return false; 115 } 116 117 int interval; 118 try { 119 interval = Integer.valueOf(prefStr); 120 } catch (NumberFormatException ignored) { 121 loge("invalid alert reminder interval preference: " + prefStr); 122 return false; 123 } 124 125 if (interval == 0 || (interval == 1 && !firstTime)) { 126 return false; 127 } 128 if (interval == 1) { 129 interval = 2; // "1" = one reminder after 2 minutes 130 } 131 132 if (DBG) log("queueAlertReminder() in " + interval + " minutes"); 133 134 Intent playIntent = new Intent(context, CellBroadcastAlertReminder.class); 135 playIntent.setAction(ACTION_PLAY_ALERT_REMINDER); 136 sPlayReminderIntent = PendingIntent.getService(context, 0, playIntent, 137 PendingIntent.FLAG_UPDATE_CURRENT); 138 139 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 140 if (alarmManager == null) { 141 loge("can't get Alarm Service"); 142 return false; 143 } 144 145 // remind user after 2 minutes or 15 minutes 146 long triggerTime = SystemClock.elapsedRealtime() + (interval * 60000); 147 // We use setExact instead of set because this is for emergency reminder. 148 alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, 149 triggerTime, sPlayReminderIntent); 150 return true; 151 } 152 153 /** 154 * Stops alert reminder and cancels any queued reminders. 155 */ cancelAlertReminder()156 static void cancelAlertReminder() { 157 if (DBG) log("cancelAlertReminder()"); 158 if (sPlayReminderRingtone != null) { 159 if (DBG) log("stopping play reminder ringtone"); 160 sPlayReminderRingtone.stop(); 161 sPlayReminderRingtone = null; 162 } 163 if (sPlayReminderIntent != null) { 164 if (DBG) log("canceling pending play reminder intent"); 165 sPlayReminderIntent.cancel(); 166 sPlayReminderIntent = null; 167 } 168 } 169 log(String msg)170 private static void log(String msg) { 171 Log.d(TAG, msg); 172 } 173 loge(String msg)174 private static void loge(String msg) { 175 Log.e(TAG, msg); 176 } 177 } 178