TimerReceiver.java revision 3982c7f7458da18e3b08c23bf026828d0db77539
1/* 2 * Copyright (C) 2012 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 17package com.android.deskclock.timer; 18 19import android.app.AlarmManager; 20import android.app.Notification; 21import android.app.PendingIntent; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.SharedPreferences; 26import android.preference.PreferenceManager; 27import android.util.Log; 28 29import com.android.deskclock.Alarm; 30import com.android.deskclock.AlarmKlaxon; 31import com.android.deskclock.Alarms; 32import com.android.deskclock.DeskClock; 33 34import java.util.ArrayList; 35import java.util.Iterator; 36 37public class TimerReceiver extends BroadcastReceiver { 38 private static final String TAG = "TimerReceiver"; 39 40 ArrayList<TimerObj> mTimers; 41 42 @Override 43 public void onReceive(final Context context, final Intent intent) { 44 45 int timer; 46 Log.d(TAG, " got intent with action " + intent.getAction()); 47 48 if (intent.hasExtra(Timers.TIMER_INTENT_EXTRA)) { 49 // Get the alarm out of the Intent 50 timer = intent.getIntExtra(Timers.TIMER_INTENT_EXTRA, -1); 51 if (timer == -1) { 52 Log.d(TAG, " got intent without Timer data"); 53 } 54 } else { 55 // No data to work with, do nothing 56 Log.d(TAG, " got intent without Timer data"); 57 return; 58 } 59 60 61// NotificationManager nm = 62 // (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 63 64 // Get the updated timers data. 65 if (mTimers == null) { 66 mTimers = new ArrayList<TimerObj> (); 67 } 68 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 69 TimerObj.getTimersFromSharedPrefs(prefs, mTimers); 70 71 // TODO: Update notifications 72 73 if (Timers.TIMES_UP.equals(intent.getAction())) { 74 // Find the timer (if it doesn't exists, it was probably deleted). 75 TimerObj t = Timers.findTimer(mTimers, timer); 76 if (t == null) { 77 Log.d(TAG, " timer not found in list - do nothing"); 78 return; 79 } 80 81 t.mState = TimerObj.STATE_TIMESUP; 82 t.writeToSharedPref(prefs); 83 // Play ringtone by using AlarmKlaxon service with a default alarm. 84 Log.d(TAG, "playing ringtone"); 85 Alarm a = new Alarm(); 86 Intent si = new Intent(); 87 si.setClass(context, AlarmKlaxon.class); 88 si.putExtra(Alarms.ALARM_INTENT_EXTRA, a); 89 context.startService(si); 90 91 // Start the DeskClock Activity 92 Intent i = new Intent(); 93 i.setClass(context, DeskClock.class); 94 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 95 i.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX); 96 context.startActivity(i); 97 98 } else if (Timers.TIMER_RESET.equals(intent.getAction()) 99 || Timers.DELETE_TIMER.equals(intent.getAction()) 100 || Timers.TIMER_DONE.equals(intent.getAction())) { 101 // Stop Ringtone if all tiemrs are not in timesup status 102 if (Timers.findExpiredTimer(mTimers) == null) { 103 // Stop ringtone 104 Log.d(TAG, "stopping ringtone"); 105 Intent si = new Intent(); 106 si.setClass(context, AlarmKlaxon.class); 107 context.startService(si); 108 context.stopService(si); 109 } 110 } 111 // Update the next "Times up" alarm 112 updateNextTimesup(context) ; 113 } 114 private static PendingIntent createTimerActivityIntent(Context context, TimerObj t, String action) { 115 Intent i = new Intent(); 116 i.setClass(context, TimerReceiver.class); 117 i.setAction(action); 118 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 119 i.putExtra(Timers.TIMER_INTENT_EXTRA, t); 120 return PendingIntent.getActivity(context, 0, i, 121 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); 122 } 123 124 private static Notification buildTimerNotification( 125 Context context, TimerObj t, Boolean onGoing, PendingIntent contentIntent) { 126 Notification.Builder builder = new Notification.Builder(context); 127 builder.setContentIntent(contentIntent); 128 builder.setContentTitle("Timer"); 129 builder.setContentText("Now or Never"); 130// builder.setDeleteIntent(null); // what to do when the notification is cleared 131 builder.setOngoing(onGoing); 132// builder.setSound(null); 133 builder.setWhen(System.currentTimeMillis()); 134 builder.setTicker("Timer is here"); 135 // builder.setSmallIcon(R.drawable.ic_clock_alarm_on); 136 // builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_clock_alarm_on)); 137 138 return builder.build(); 139 140 } 141 142 // Scan all timers and find the one that will expire next. 143 // Tell AlarmManager to send a "Time's up" message to this receiver when this timer expires. 144 // If no timer exists, clear "time's up" message. 145 private void updateNextTimesup(Context context) { 146 long nextTimesup = Long.MAX_VALUE; 147 boolean nextTimerfound = false; 148 Iterator<TimerObj> i = mTimers.iterator(); 149 TimerObj t = null; 150 while(i.hasNext()) { 151 t = i.next(); 152 if (t.mState == TimerObj.STATE_RUNNING) { 153 long timesupTime = t.getTimesupTime(); 154 if (timesupTime < nextTimesup) { 155 nextTimesup = timesupTime; 156 nextTimerfound = true; 157 } 158 } 159 } 160 161 Intent intent = new Intent(); 162 intent.setAction(Timers.TIMES_UP); 163 intent.setClass(context, TimerReceiver.class); 164 if (t != null) { 165 intent.putExtra(Timers.TIMER_INTENT_EXTRA, t.mTimerId); 166 } 167 AlarmManager mngr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 168 PendingIntent p = PendingIntent.getBroadcast(context, 169 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); 170 if (nextTimerfound) { 171 mngr.set(AlarmManager.RTC_WAKEUP, nextTimesup, p); 172 Log.d(TAG,"Setting times up to " + nextTimesup); 173 } else { 174 Log.d(TAG,"canceling times up"); 175 mngr.cancel(p); 176 } 177 } 178 179}