• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2016 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.directboot.alarms;
18 
19 import com.example.android.directboot.R;
20 
21 import android.app.IntentService;
22 import android.app.Notification;
23 import android.app.NotificationManager;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.provider.Settings;
27 import android.support.v4.app.NotificationCompat;
28 import android.support.v4.content.LocalBroadcastManager;
29 
30 /**
31  * IntentService to set off an alarm.
32  */
33 public class AlarmIntentService extends IntentService {
34 
35     public static final String ALARM_WENT_OFF_ACTION = AlarmIntentService.class.getName()
36             + ".ALARM_WENT_OFF";
37 
38 
39     public static final String KEY_ALARM_ID = "alarm_id";
40 
41     public static final String KEY_ALARM_MONTH = "alarm_month";
42 
43     public static final String KEY_ALARM_DATE = "alarm_date";
44 
45     public static final String KEY_ALARM_HOUR = "alarm_hour";
46 
47     public static final String KEY_ALARM_MINUTE = "alarm_minute";
48 
AlarmIntentService()49     public AlarmIntentService() {
50         super(AlarmIntentService.class.getName());
51     }
52 
53     @Override
onHandleIntent(Intent intent)54     protected void onHandleIntent(Intent intent) {
55         Context context = getApplicationContext();
56         Alarm alarm = AlarmUtil.readAlarm(intent.getExtras());
57 
58         NotificationManager notificationManager = context
59                 .getSystemService(NotificationManager.class);
60         NotificationCompat.Builder builder =
61                 new NotificationCompat.Builder(context)
62                         .setSmallIcon(R.drawable.ic_fbe_notification)
63                         .setCategory(Notification.CATEGORY_ALARM)
64                         .setSound(Settings.System.DEFAULT_ALARM_ALERT_URI)
65                         .setContentTitle(context.getString(R.string.alarm_went_off, alarm.hour,
66                                 alarm.minute));
67         notificationManager.notify(alarm.id, builder.build());
68 
69         AlarmStorage alarmStorage = new AlarmStorage(context);
70         alarmStorage.deleteAlarm(alarm);
71         Intent wentOffIntent = new Intent(ALARM_WENT_OFF_ACTION);
72         wentOffIntent.putExtras(AlarmUtil.writeAlarm(alarm));
73         LocalBroadcastManager.getInstance(context).sendBroadcast(wentOffIntent);
74     }
75 }
76