• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings;
18 
19 import static android.car.settings.CarSettings.Global.ENABLE_USER_SWITCH_DEVELOPER_MESSAGE;
20 
21 import android.app.Activity;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.pm.ResolveInfo;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.os.PowerManager;
31 import android.os.SystemClock;
32 import android.os.UserManager;
33 import android.provider.Settings;
34 import android.view.Gravity;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.view.WindowManager.LayoutParams;
38 import android.view.animation.AnimationUtils;
39 import android.widget.LinearLayout;
40 import android.widget.TextView;
41 
42 import com.android.car.internal.common.UserHelperLite;
43 import com.android.car.settings.common.Logger;
44 
45 import java.util.Objects;
46 
47 /**
48  * Copied over from phone settings. This covers the fallback case where no launcher is available.
49  */
50 public class FallbackHome extends Activity {
51     private static final Logger LOG = new Logger(FallbackHome.class);
52     private static final int PROGRESS_TIMEOUT = 2000;
53 
54     private boolean mProvisioned;
55 
56     private boolean mFinished;
57 
58     private final Runnable mProgressTimeoutRunnable = () -> {
59         View v = getLayoutInflater().inflate(
60                 R.layout.fallback_home_finishing_boot, /* root= */ null);
61         setContentView(v);
62         v.setAlpha(0f);
63         v.animate()
64                 .alpha(1f)
65                 .setDuration(500)
66                 .setInterpolator(AnimationUtils.loadInterpolator(
67                         this, android.R.interpolator.fast_out_slow_in))
68                 .start();
69         getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
70     };
71 
72     @Override
onCreate(Bundle savedInstanceState)73     protected void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75 
76         // Set ourselves totally black before the device is provisioned
77         mProvisioned = Settings.Global.getInt(getContentResolver(),
78                 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
79         int flags;
80         boolean showInfo = false;
81         if (!mProvisioned) {
82             setTheme(R.style.FallbackHome_SetupWizard);
83             flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
84                     | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
85         } else {
86             flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
87                     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
88             showInfo = "true".equals(Settings.Global.getString(getContentResolver(),
89                     ENABLE_USER_SWITCH_DEVELOPER_MESSAGE));
90         }
91 
92         if (showInfo) {
93             // Display some info about the current user, which is useful to debug / track user
94             // switching delays.
95             // NOTE: we're manually creating the view (instead of inflating it from XML) to
96             // minimize the performance impact.
97             TextView view = new TextView(this);
98             view.setText("FallbackHome for user " + getUserId() + ".\n\n"
99                     + "This activity is displayed while the user is starting, \n"
100                     + "and it will be replaced by the proper Home \n"
101                     + "(once the user is unlocked).\n\n"
102                     + "NOTE: this message is only shown on debuggable builds");
103             view.setGravity(Gravity.CENTER);
104             view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
105                     ViewGroup.LayoutParams.WRAP_CONTENT));
106 
107             LinearLayout parent = new LinearLayout(this);
108             parent.setOrientation(LinearLayout.VERTICAL);
109             parent.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
110                     ViewGroup.LayoutParams.MATCH_PARENT));
111             parent.addView(view);
112 
113             setContentView(parent);
114         }
115 
116         getWindow().getDecorView().setSystemUiVisibility(flags);
117 
118         registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
119         maybeFinish();
120     }
121 
122     @Override
onResume()123     protected void onResume() {
124         super.onResume();
125         LOG.d("onResume() for user " + getUserId() + ". Provisioned: " + mProvisioned);
126         if (mProvisioned) {
127             mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
128         }
129     }
130 
131     @Override
onPause()132     protected void onPause() {
133         super.onPause();
134         mHandler.removeCallbacks(mProgressTimeoutRunnable);
135     }
136 
onDestroy()137     protected void onDestroy() {
138         super.onDestroy();
139         unregisterReceiver(mReceiver);
140         if (!mFinished) {
141             LOG.d("User " + getUserId() + " FallbackHome is finished");
142             finishFallbackHome();
143         }
144     }
145 
146     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
147         @Override
148         public void onReceive(Context context, Intent intent) {
149             maybeFinish();
150         }
151     };
152 
maybeFinish()153     private void maybeFinish() {
154         if (getSystemService(UserManager.class).isUserUnlocked()) {
155             final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
156                     .addCategory(Intent.CATEGORY_HOME);
157             final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
158             if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
159                 LOG.d("User " + getUserId() + " unlocked but no home; let's hope someone enables "
160                         + "one soon?");
161                 mHandler.sendEmptyMessageDelayed(0, 500);
162             } else {
163                 String homePackageName = homeInfo.activityInfo.packageName;
164                 if (UserHelperLite.isHeadlessSystemUser(getUserId())) {
165                     // This is the transient state in HeadlessSystemMode to boot for user 10+.
166                     LOG.d("User 0 unlocked, but will not launch real home: " + homePackageName);
167                     return;
168                 }
169                 LOG.d("User " + getUserId() + " unlocked and real home (" + homePackageName
170                         + ") found; let's go!");
171                 finishFallbackHome();
172             }
173         }
174     }
175 
finishFallbackHome()176     private void finishFallbackHome() {
177         getSystemService(PowerManager.class).userActivity(SystemClock.uptimeMillis(), false);
178         finishAndRemoveTask();
179         mFinished = true;
180     }
181 
182     private Handler mHandler = new Handler() {
183         @Override
184         public void handleMessage(Message msg) {
185             maybeFinish();
186         }
187     };
188 }
189