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     public static final String ALARM_KEY = "alarm_instance";
38 
AlarmIntentService()39     public AlarmIntentService() {
40         super(AlarmIntentService.class.getName());
41     }
42 
43     @Override
onHandleIntent(Intent intent)44     protected void onHandleIntent(Intent intent) {
45         Context context = getApplicationContext();
46         Alarm alarm = intent.getParcelableExtra(ALARM_KEY);
47 
48         NotificationManager notificationManager = context
49                 .getSystemService(NotificationManager.class);
50         NotificationCompat.Builder builder =
51                 new NotificationCompat.Builder(context)
52                         .setSmallIcon(R.drawable.ic_fbe_notification)
53                         .setCategory(Notification.CATEGORY_ALARM)
54                         .setSound(Settings.System.DEFAULT_ALARM_ALERT_URI)
55                         .setContentTitle(context.getString(R.string.alarm_went_off, alarm.hour,
56                                 alarm.minute));
57         notificationManager.notify(alarm.id, builder.build());
58 
59         AlarmStorage alarmStorage = new AlarmStorage(context);
60         alarmStorage.deleteAlarm(alarm);
61         Intent wentOffIntent = new Intent(ALARM_WENT_OFF_ACTION);
62         wentOffIntent.putExtra(ALARM_KEY, alarm);
63         LocalBroadcastManager.getInstance(context).sendBroadcast(wentOffIntent);
64     }
65 }
66