blob: 59347addeab847ae81719ce03ba4254c8b3fdc24 [file] [log] [blame]
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -07001/*
2 * Copyright (C) 2015 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
17package com.android.settings;
18
19import android.app.Activity;
Matthew Ngdd396492018-05-31 13:30:27 -070020import android.app.WallpaperColors;
21import android.app.WallpaperManager;
Matthew Ng932b2022018-07-19 13:30:54 -070022import android.app.WallpaperManager.OnColorsChangedListener;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070023import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Jeff Sharkeybc16a072015-12-18 17:19:27 -070027import android.content.pm.ResolveInfo;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070028import android.os.Bundle;
Jeff Sharkeybc16a072015-12-18 17:19:27 -070029import android.os.Handler;
30import android.os.Message;
Jorim Jaggi98407942016-08-02 11:53:12 +020031import android.os.PowerManager;
32import android.os.SystemClock;
Fan Zhangc7162cd2018-06-18 15:21:41 -070033import android.os.UserHandle;
Jeff Sharkeybc16a072015-12-18 17:19:27 -070034import android.os.UserManager;
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060035import android.provider.Settings;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070036import android.util.Log;
Jorim Jaggia616a0d2016-06-29 16:33:39 -070037import android.view.View;
Jorim Jaggi98407942016-08-02 11:53:12 +020038import android.view.WindowManager.LayoutParams;
39import android.view.animation.AnimationUtils;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070040
Jeff Sharkeybc16a072015-12-18 17:19:27 -070041import java.util.Objects;
42
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070043public class FallbackHome extends Activity {
44 private static final String TAG = "FallbackHome";
Jorim Jaggi98407942016-08-02 11:53:12 +020045 private static final int PROGRESS_TIMEOUT = 2000;
46
47 private boolean mProvisioned;
Matthew Ng932b2022018-07-19 13:30:54 -070048 private WallpaperManager mWallManager;
Jorim Jaggi98407942016-08-02 11:53:12 +020049
50 private final Runnable mProgressTimeoutRunnable = () -> {
51 View v = getLayoutInflater().inflate(
52 R.layout.fallback_home_finishing_boot, null /* root */);
53 setContentView(v);
54 v.setAlpha(0f);
55 v.animate()
56 .alpha(1f)
57 .setDuration(500)
58 .setInterpolator(AnimationUtils.loadInterpolator(
59 this, android.R.interpolator.fast_out_slow_in))
60 .start();
61 getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
62 };
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070063
Matthew Ng932b2022018-07-19 13:30:54 -070064 private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() {
65 @Override
66 public void onColorsChanged(WallpaperColors colors, int which) {
67 if (colors != null) {
68 View decorView = getWindow().getDecorView();
69 decorView.setSystemUiVisibility(
70 updateVisibilityFlagsFromColors(colors, decorView.getSystemUiVisibility()));
71 mWallManager.removeOnColorsChangedListener(this);
72 }
73 }
74 };
75
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070076 @Override
77 protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060079
80 // Set ourselves totally black before the device is provisioned so that
81 // we don't flash the wallpaper before SUW
Jorim Jaggi98407942016-08-02 11:53:12 +020082 mProvisioned = Settings.Global.getInt(getContentResolver(),
83 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
Matthew Ngdd396492018-05-31 13:30:27 -070084 int flags;
Jorim Jaggi98407942016-08-02 11:53:12 +020085 if (!mProvisioned) {
Jorim Jaggia616a0d2016-06-29 16:33:39 -070086 setTheme(R.style.FallbackHome_SetupWizard);
Matthew Ngdd396492018-05-31 13:30:27 -070087 flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
88 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Jorim Jaggi98407942016-08-02 11:53:12 +020089 } else {
Matthew Ngdd396492018-05-31 13:30:27 -070090 flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
91 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060092 }
93
Matthew Ngdd396492018-05-31 13:30:27 -070094 // Set the system ui flags to light status bar if the wallpaper supports dark text to match
Matthew Ng932b2022018-07-19 13:30:54 -070095 // current system ui color tints. Use a listener to wait for colors if not ready yet.
96 mWallManager = getSystemService(WallpaperManager.class);
97 if (mWallManager == null) {
98 Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!");
99 } else {
100 WallpaperColors colors = mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
101 if (colors == null) {
102 mWallManager.addOnColorsChangedListener(mColorsChangedListener, null /* handler */);
103 } else {
104 flags = updateVisibilityFlagsFromColors(colors, flags);
105 }
Matthew Ngdd396492018-05-31 13:30:27 -0700106 }
107 getWindow().getDecorView().setSystemUiVisibility(flags);
108
Jorim Jaggic0c583c2016-11-02 20:33:32 +0000109 registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700110 maybeFinish();
111 }
112
113 @Override
Jorim Jaggi98407942016-08-02 11:53:12 +0200114 protected void onResume() {
115 super.onResume();
116 if (mProvisioned) {
117 mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
118 }
119 }
120
121 @Override
122 protected void onPause() {
123 super.onPause();
124 mHandler.removeCallbacks(mProgressTimeoutRunnable);
125 }
126
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700127 protected void onDestroy() {
128 super.onDestroy();
129 unregisterReceiver(mReceiver);
Matthew Ng932b2022018-07-19 13:30:54 -0700130 if (mWallManager != null) {
131 mWallManager.removeOnColorsChangedListener(mColorsChangedListener);
132 }
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -0700133 }
134
135 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
136 @Override
137 public void onReceive(Context context, Intent intent) {
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700138 maybeFinish();
139 }
140 };
141
142 private void maybeFinish() {
143 if (getSystemService(UserManager.class).isUserUnlocked()) {
144 final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
145 .addCategory(Intent.CATEGORY_HOME);
146 final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
147 if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
Muyuan Li4811ab42016-05-20 14:27:14 -0700148 if (UserManager.isSplitSystemUser()
149 && UserHandle.myUserId() == UserHandle.USER_SYSTEM) {
150 // This avoids the situation where the system user has no home activity after
151 // SUW and this activity continues to throw out warnings. See b/28870689.
152 return;
153 }
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700154 Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
155 mHandler.sendEmptyMessageDelayed(0, 500);
156 } else {
157 Log.d(TAG, "User unlocked and real home found; let's go!");
Jorim Jaggi98407942016-08-02 11:53:12 +0200158 getSystemService(PowerManager.class).userActivity(
159 SystemClock.uptimeMillis(), false);
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700160 finish();
161 }
162 }
163 }
164
Matthew Ng932b2022018-07-19 13:30:54 -0700165 private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) {
166 if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
167 return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
168 | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
169 }
170 return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
171 & ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
172 }
173
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700174 private Handler mHandler = new Handler() {
175 @Override
176 public void handleMessage(Message msg) {
177 maybeFinish();
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -0700178 }
179 };
180}