Home | History | Annotate | Download | only in timer
      1 /*
      2  * Copyright (C) 2014 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.example.android.wearable.timer;
     18 
     19 import android.app.AlarmManager;
     20 import android.app.IntentService;
     21 import android.app.Notification;
     22 import android.app.NotificationManager;
     23 import android.app.PendingIntent;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.util.Log;
     27 
     28 import com.example.android.wearable.timer.util.Constants;
     29 
     30 /**
     31  * Service class that manages notifications of the timer.
     32  */
     33 public class TimerNotificationService extends IntentService {
     34 
     35     public static final String TAG = "TimerNotificationSvc";
     36 
     37     public TimerNotificationService() {
     38         super(TAG);
     39     }
     40 
     41     @Override
     42     public void onCreate() {
     43         super.onCreate();
     44     }
     45 
     46     @Override
     47     protected void onHandleIntent(Intent intent) {
     48         if (Log.isLoggable(TAG, Log.DEBUG)) {
     49             Log.d(TAG, "onHandleIntent called with intent: " + intent);
     50         }
     51         String action = intent.getAction();
     52         if (Constants.ACTION_SHOW_ALARM.equals(action)) {
     53             showTimerDoneNotification();
     54         } else if (Constants.ACTION_DELETE_ALARM.equals(action)) {
     55             deleteTimer();
     56         } else if (Constants.ACTION_RESTART_ALARM.equals(action)) {
     57             restartAlarm();
     58         } else {
     59             throw new IllegalStateException("Undefined constant used: " + action);
     60         }
     61     }
     62 
     63     private void restartAlarm() {
     64         Intent dialogIntent = new Intent(this, SetTimerActivity.class);
     65         dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     66         startActivity(dialogIntent);
     67         if (Log.isLoggable(TAG, Log.DEBUG)) {
     68             Log.d(TAG, "Timer restarted.");
     69         }
     70     }
     71 
     72     private void deleteTimer() {
     73         cancelCountdownNotification();
     74 
     75         AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
     76         Intent intent = new Intent(Constants.ACTION_SHOW_ALARM, null, this,
     77                 TimerNotificationService.class);
     78         PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
     79                 PendingIntent.FLAG_UPDATE_CURRENT);
     80         alarm.cancel(pendingIntent);
     81 
     82         if (Log.isLoggable(TAG, Log.DEBUG)) {
     83             Log.d(TAG, "Timer deleted.");
     84         }
     85     }
     86 
     87     private void cancelCountdownNotification() {
     88         NotificationManager notifyMgr =
     89                 ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
     90         notifyMgr.cancel(Constants.NOTIFICATION_TIMER_COUNTDOWN);
     91     }
     92 
     93     private void showTimerDoneNotification() {
     94         // Cancel the countdown notification to show the "timer done" notification.
     95         cancelCountdownNotification();
     96 
     97         // Create an intent to restart a timer.
     98         Intent restartIntent = new Intent(Constants.ACTION_RESTART_ALARM, null, this,
     99                 TimerNotificationService.class);
    100         PendingIntent pendingIntentRestart = PendingIntent
    101                 .getService(this, 0, restartIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    102 
    103         // Create notification that timer has expired.
    104         NotificationManager notifyMgr =
    105                 ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
    106         Notification notif = new Notification.Builder(this)
    107                 .setSmallIcon(R.drawable.ic_cc_alarm)
    108                 .setContentTitle(getString(R.string.timer_done))
    109                 .setContentText(getString(R.string.timer_done))
    110                 .setUsesChronometer(true)
    111                 .setWhen(System.currentTimeMillis())
    112                 .addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_restart),
    113                         pendingIntentRestart)
    114                 .setLocalOnly(true)
    115                 .build();
    116         notifyMgr.notify(Constants.NOTIFICATION_TIMER_EXPIRED, notif);
    117     }
    118 
    119 }
    120