1 /*
2   * Copyright (C) 2021 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.fakesystemapp.launcher;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.ResolveInfo;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.Message;
28 import android.os.PowerManager;
29 import android.os.SystemClock;
30 import android.os.UserHandle;
31 import android.os.UserManager;
32 import android.util.Log;
33 
34 import java.util.Objects;
35 
36 /**
37  * Home Activity that gets launched before user has unlocked device.
38  *
39  * Effectively replaces and mirrors logic in  com.android.settings.FallbackHome, so device
40  * boots when Settings app is removed from image.
41  */
42 public final class FallbackHome extends Activity {
43 
44     private static final String TAG = "FallbackHome";
45 
46     private Handler mHandler;
47     private BroadcastReceiver mReceiver;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         mReceiver = new BroadcastReceiver() {
54             @Override
55             public void onReceive(Context context, Intent intent) {
56                 maybeFinish();
57             }
58         };
59 
60         mHandler = new Handler(getMainLooper()) {
61             @Override
62             public void handleMessage(Message msg) {
63                 maybeFinish();
64             }
65         };
66 
67         // TODO: consider adding logic to make screen black if not provisioned
68 
69         registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
70         maybeFinish();
71     }
72 
73     @Override
onDestroy()74     public void onDestroy() {
75         super.onDestroy();
76         unregisterReceiver(mReceiver);
77     }
78 
maybeFinish()79     private void maybeFinish() {
80         if (getSystemService(UserManager.class).isUserUnlocked()) {
81             final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
82                     .addCategory(Intent.CATEGORY_HOME);
83             final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
84             if (Objects.equals(this.getClass().getName(), homeInfo.activityInfo.name)) {
85                 Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
86                 mHandler.sendEmptyMessageDelayed(0, 500);
87             } else {
88                 Log.d(TAG, String.format("User unlocked and real home %s found; let's go!", homeInfo.activityInfo.name));
89                 getSystemService(PowerManager.class).userActivity(
90                         SystemClock.uptimeMillis(), false);
91                 finish();
92             }
93         }
94     }
95 }
96