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;
18 
19 import com.example.android.directboot.alarms.Alarm;
20 import com.example.android.directboot.alarms.AlarmStorage;
21 import com.example.android.directboot.alarms.AlarmUtil;
22 
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.support.v4.os.BuildCompat;
27 import android.support.v4.os.UserManagerCompat;
28 import android.util.Log;
29 
30 /**
31  * BroadcastReceiver that receives the following implicit broadcasts:
32  * <ul>
33  *     <li>Intent.ACTION_BOOT_COMPLETED</li>
34  *     <li>Intent.ACTION_LOCKED_BOOT_COMPLETED</li>
35  * </ul>
36  *
37  * To receive the Intent.ACTION_LOCKED_BOOT_COMPLETED broadcast, the receiver needs to have
38  * <code>directBootAware="true"</code> property in the manifest.
39  */
40 public class BootBroadcastReceiver extends BroadcastReceiver {
41 
42     private static final String TAG = "BootBroadcastReceiver";
43 
44     @Override
onReceive(Context context, Intent intent)45     public void onReceive(Context context, Intent intent) {
46         boolean bootCompleted;
47         String action = intent.getAction();
48         Log.i(TAG, "Received action: " + action + ", user unlocked: " + UserManagerCompat
49                 .isUserUnlocked(context));
50         if (BuildCompat.isAtLeastN()) {
51             bootCompleted = Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(action);
52         } else {
53             bootCompleted = Intent.ACTION_BOOT_COMPLETED.equals(action);
54         }
55         if (!bootCompleted) {
56             return;
57         }
58         AlarmUtil util = new AlarmUtil(context);
59         AlarmStorage alarmStorage = new AlarmStorage(context);
60         for (Alarm alarm : alarmStorage.getAlarms()) {
61             util.scheduleAlarm(alarm);
62         }
63     }
64 }
65