TimerReceiver.java revision 815e2f7431c590086d5bd4eee5d7bf08108c77eb
1815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson/*
2815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * Copyright (C) 2012 The Android Open Source Project
3815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson *
4815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * Licensed under the Apache License, Version 2.0 (the "License");
5815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * you may not use this file except in compliance with the License.
6815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * You may obtain a copy of the License at
7815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson *
8815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson *      http://www.apache.org/licenses/LICENSE-2.0
9815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson *
10815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * Unless required by applicable law or agreed to in writing, software
11815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * distributed under the License is distributed on an "AS IS" BASIS,
12815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * See the License for the specific language governing permissions and
14815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson * limitations under the License.
15815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson */
16815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
17815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonpackage com.android.deskclock.timer;
18815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
19815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.app.Notification;
20815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.app.NotificationManager;
21815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.app.PendingIntent;
22815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.content.BroadcastReceiver;
23815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.content.Context;
24815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.content.Intent;
25815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.graphics.BitmapFactory;
26815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonimport android.util.Log;
27815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
28815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelsonpublic class TimerReceiver extends BroadcastReceiver {
29815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson    private static final String TAG = "TimerReceiver";
30815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
31815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson     @Override
32815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson    public void onReceive(final Context context, final Intent intent) {
33815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
34815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         Timer timer;
35815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         Log.d(TAG, " got intent");
36815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
37815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         if (intent.hasExtra(Timer.TIMER_INTENT_EXTRA)) {
38815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Get the alarm out of the Intent
39815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             timer = intent.getParcelableExtra(Timer.TIMER_INTENT_EXTRA);
40815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         } else {
41815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // No data to work with, do nothing
42815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             Log.d(TAG, " got intent without Timer data");
43815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             return;
44815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         }
45815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
46815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson        NotificationManager nm =
47815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson                (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
48815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
49815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         if (Timer.START_TIMER.equals(intent.getAction())) {
50815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Start intent is send by the timer activity, show timer notification and set an alarm
51815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             Log.d(TAG," got start intent");
52815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             PendingIntent i = createTimerActivityIntent(context, timer);
53815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             nm.notify(timer.mTimerId, buildTimerNotification(context, timer, false, i));
54815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
55815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         } else if (Timer.CANCEL_TIMER.equals(intent.getAction())) {
56815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Cancel intent can be sent by either the app or from the notification
57815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Remove notification, cancel alarm and tell timer app the timer was canceled,
58815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             //
59815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             Log.d(TAG ," got cancel intent");
60815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             nm.cancel(timer.mTimerId);
61815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
62815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         } else if (Timer.TIMES_UP.equals(intent.getAction())) {
63815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Times up comes as an alarm notification, update the notification, timer activity and
64815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // play the alarm ring tone.
65815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             Log.d(TAG," got timesup intent");
66815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
67815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
68815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         } else if (Timer.TIMER_RESET.equals(intent.getAction())) {
69815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Reset can come with the times up notification is swiped or from the activity
70815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson             // Remove the notification, stop playing the alarm, tell timer activity to reset
71815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         }
72815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson    }
73815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
74815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
75815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson     private static PendingIntent createTimerActivityIntent(Context context, Timer t) {
76815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         Intent clickIntent = new Intent();
77815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson     //    clickIntent.setClass(context, TimerActivity.class);
78815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         clickIntent.putExtra(Timer.TIMER_INTENT_EXTRA, t);
80815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         return PendingIntent.getActivity(context, 0, clickIntent,
81815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson                     PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
82815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson     }
83815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
84815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson    private static Notification buildTimerNotification(
85815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson            Context context, Timer t, Boolean onGoing, PendingIntent contentIntent) {
86815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         Notification.Builder builder = new Notification.Builder(context);
87815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setContentIntent(contentIntent);
88815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setContentTitle("Timer");
89815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setContentText("Now or Never");
90815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson//         builder.setDeleteIntent(null);  // what to do when the notification is cleared
91815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setOngoing(onGoing);
92815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson//         builder.setSound(null);
93815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setWhen(System.currentTimeMillis());
94815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         builder.setTicker("Timer is here");
95815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson    //    builder.setSmallIcon(R.drawable.ic_clock_alarm_on);
96815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson  //       builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_clock_alarm_on));
97815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
98815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson         return builder.build();
99815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson
100815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson     }
101815e2f7431c590086d5bd4eee5d7bf08108c77ebIsaac Katzenelson}