TimerReceiver.java revision 815e2f7431c590086d5bd4eee5d7bf08108c77eb
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.Notification; 20import android.app.NotificationManager; 21import android.app.PendingIntent; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.graphics.BitmapFactory; 26import android.util.Log; 27 28public class TimerReceiver extends BroadcastReceiver { 29 private static final String TAG = "TimerReceiver"; 30 31 @Override 32 public void onReceive(final Context context, final Intent intent) { 33 34 Timer timer; 35 Log.d(TAG, " got intent"); 36 37 if (intent.hasExtra(Timer.TIMER_INTENT_EXTRA)) { 38 // Get the alarm out of the Intent 39 timer = intent.getParcelableExtra(Timer.TIMER_INTENT_EXTRA); 40 } else { 41 // No data to work with, do nothing 42 Log.d(TAG, " got intent without Timer data"); 43 return; 44 } 45 46 NotificationManager nm = 47 (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 48 49 if (Timer.START_TIMER.equals(intent.getAction())) { 50 // Start intent is send by the timer activity, show timer notification and set an alarm 51 Log.d(TAG," got start intent"); 52 PendingIntent i = createTimerActivityIntent(context, timer); 53 nm.notify(timer.mTimerId, buildTimerNotification(context, timer, false, i)); 54 55 } else if (Timer.CANCEL_TIMER.equals(intent.getAction())) { 56 // Cancel intent can be sent by either the app or from the notification 57 // Remove notification, cancel alarm and tell timer app the timer was canceled, 58 // 59 Log.d(TAG ," got cancel intent"); 60 nm.cancel(timer.mTimerId); 61 62 } else if (Timer.TIMES_UP.equals(intent.getAction())) { 63 // Times up comes as an alarm notification, update the notification, timer activity and 64 // play the alarm ring tone. 65 Log.d(TAG," got timesup intent"); 66 67 68 } else if (Timer.TIMER_RESET.equals(intent.getAction())) { 69 // Reset can come with the times up notification is swiped or from the activity 70 // Remove the notification, stop playing the alarm, tell timer activity to reset 71 } 72 } 73 74 75 private static PendingIntent createTimerActivityIntent(Context context, Timer t) { 76 Intent clickIntent = new Intent(); 77 // clickIntent.setClass(context, TimerActivity.class); 78 clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 79 clickIntent.putExtra(Timer.TIMER_INTENT_EXTRA, t); 80 return PendingIntent.getActivity(context, 0, clickIntent, 81 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); 82 } 83 84 private static Notification buildTimerNotification( 85 Context context, Timer t, Boolean onGoing, PendingIntent contentIntent) { 86 Notification.Builder builder = new Notification.Builder(context); 87 builder.setContentIntent(contentIntent); 88 builder.setContentTitle("Timer"); 89 builder.setContentText("Now or Never"); 90// builder.setDeleteIntent(null); // what to do when the notification is cleared 91 builder.setOngoing(onGoing); 92// builder.setSound(null); 93 builder.setWhen(System.currentTimeMillis()); 94 builder.setTicker("Timer is here"); 95 // builder.setSmallIcon(R.drawable.ic_clock_alarm_on); 96 // builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_clock_alarm_on)); 97 98 return builder.build(); 99 100 } 101}