1 /* 2 * Copyright (C) 2022 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.android.devicelockcontroller.receivers; 18 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; 20 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 21 import static android.content.pm.PackageManager.DONT_KILL_APP; 22 23 import android.content.BroadcastReceiver; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.pm.PackageManager; 28 import android.os.UserManager; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.devicelockcontroller.policy.DeviceStateController; 33 import com.android.devicelockcontroller.policy.PolicyObjectsInterface; 34 import com.android.devicelockcontroller.util.LogUtil; 35 36 /** 37 * Handle {@link Intent#ACTION_LOCKED_BOOT_COMPLETED}. This receiver runs for any user 38 * (singleUser="false"). 39 * 40 * This receiver starts lock task mode if applicable. 41 */ 42 public final class LockedBootCompletedReceiver extends BroadcastReceiver { 43 private static final String TAG = "LockedBootCompletedReceiver"; 44 getDeviceStateController(Context context)45 private static DeviceStateController getDeviceStateController(Context context) { 46 return ((PolicyObjectsInterface) context.getApplicationContext()) 47 .getStateController(); 48 } 49 50 @VisibleForTesting startLockTaskModeIfApplicable(Context context)51 static void startLockTaskModeIfApplicable(Context context) { 52 final PackageManager pm = context.getPackageManager(); 53 final ComponentName lockTaskBootCompletedReceiver = new ComponentName(context, 54 LockTaskBootCompletedReceiver.class); 55 if (getDeviceStateController(context).isInSetupState()) { 56 // b/172281939: WorkManager is not available at this moment, and we may not launch 57 // lock task mode successfully. Therefore, defer it to LockTaskBootCompletedReceiver. 58 LogUtil.i(TAG, 59 "Setup has not completed yet when ACTION_LOCKED_BOOT_COMPLETED is received. " 60 + "We can not start lock task mode here."); 61 pm.setComponentEnabledSetting(lockTaskBootCompletedReceiver, 62 COMPONENT_ENABLED_STATE_DEFAULT, DONT_KILL_APP); 63 return; 64 } 65 66 BootUtils.startLockTaskModeAtBoot(context); 67 pm.setComponentEnabledSetting( 68 new ComponentName(context, LockTaskBootCompletedReceiver.class), 69 COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); 70 } 71 72 @Override onReceive(Context context, Intent intent)73 public void onReceive(Context context, Intent intent) { 74 LogUtil.d(TAG, "Locked Boot completed"); 75 if (!intent.getAction().equals(Intent.ACTION_LOCKED_BOOT_COMPLETED)) { 76 return; 77 } 78 79 final boolean isUserProfile = 80 context.getSystemService(UserManager.class).isProfile(); 81 82 if (isUserProfile) { 83 return; 84 } 85 86 startLockTaskModeIfApplicable(context); 87 } 88 } 89