Home | History | Annotate | Download | only in cellbroadcastreceiver
      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 
     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
     56     public IBinder onBind(Intent intent) {
     57         return null;
     58     }
     59 
     60     @Override
     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      */
     83     private void playAlertReminderSound() {
     84         Uri notificationUri = RingtoneManager.getDefaultUri(
     85                 RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_ALARM);
     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         if (r != null) {
     92             log("playing alert reminder sound");
     93             r.play();
     94         } else {
     95             loge("can't get Ringtone for alert reminder sound");
     96         }
     97     }
     98 
     99     /**
    100      * Helper method to start the alert reminder service to queue the alert reminder.
    101      * @return true if a pending reminder was set; false if there are no more reminders
    102      */
    103     static boolean queueAlertReminder(Context context, boolean firstTime) {
    104         // Stop any alert reminder sound and cancel any previously queued reminders.
    105         cancelAlertReminder();
    106 
    107         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    108         String prefStr = prefs.getString(CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
    109 
    110         if (prefStr == null) {
    111             if (DBG) log("no preference value for alert reminder");
    112             return false;
    113         }
    114 
    115         int interval;
    116         try {
    117             interval = Integer.valueOf(prefStr);
    118         } catch (NumberFormatException ignored) {
    119             loge("invalid alert reminder interval preference: " + prefStr);
    120             return false;
    121         }
    122 
    123         if (interval == 0 || (interval == 1 && !firstTime)) {
    124             return false;
    125         }
    126         if (interval == 1) {
    127             interval = 2;   // "1" = one reminder after 2 minutes
    128         }
    129 
    130         if (DBG) log("queueAlertReminder() in " + interval + " minutes");
    131 
    132         Intent playIntent = new Intent(context, CellBroadcastAlertReminder.class);
    133         playIntent.setAction(ACTION_PLAY_ALERT_REMINDER);
    134         sPlayReminderIntent = PendingIntent.getService(context, 0, playIntent,
    135                 PendingIntent.FLAG_UPDATE_CURRENT);
    136 
    137         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    138         if (alarmManager == null) {
    139             loge("can't get Alarm Service");
    140             return false;
    141         }
    142 
    143         // remind user after 2 minutes or 15 minutes
    144         long triggerTime = SystemClock.elapsedRealtime() + (interval * 60000);
    145         alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, sPlayReminderIntent);
    146         return true;
    147     }
    148 
    149     /**
    150      * Stops alert reminder and cancels any queued reminders.
    151      */
    152     static void cancelAlertReminder() {
    153         if (DBG) log("cancelAlertReminder()");
    154         if (sPlayReminderRingtone != null) {
    155             if (DBG) log("stopping play reminder ringtone");
    156             sPlayReminderRingtone.stop();
    157             sPlayReminderRingtone = null;
    158         }
    159         if (sPlayReminderIntent != null) {
    160             if (DBG) log("canceling pending play reminder intent");
    161             sPlayReminderIntent.cancel();
    162             sPlayReminderIntent = null;
    163         }
    164     }
    165 
    166     private static void log(String msg) {
    167         Log.d(TAG, msg);
    168     }
    169 
    170     private static void loge(String msg) {
    171         Log.e(TAG, msg);
    172     }
    173 }
    174