blob: e3944a65c649edb403d976174273da7816a59fa6 [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;
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +080028import android.os.AsyncTask;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070029import android.os.Bundle;
Jeff Sharkeybc16a072015-12-18 17:19:27 -070030import android.os.Handler;
31import android.os.Message;
Jorim Jaggi98407942016-08-02 11:53:12 +020032import android.os.PowerManager;
33import android.os.SystemClock;
Fan Zhangc7162cd2018-06-18 15:21:41 -070034import android.os.UserHandle;
Jeff Sharkeybc16a072015-12-18 17:19:27 -070035import android.os.UserManager;
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060036import android.provider.Settings;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070037import android.util.Log;
Jorim Jaggia616a0d2016-06-29 16:33:39 -070038import android.view.View;
Jorim Jaggi98407942016-08-02 11:53:12 +020039import android.view.WindowManager.LayoutParams;
40import android.view.animation.AnimationUtils;
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070041
Jeff Sharkeybc16a072015-12-18 17:19:27 -070042import java.util.Objects;
43
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070044public class FallbackHome extends Activity {
45 private static final String TAG = "FallbackHome";
Jorim Jaggi98407942016-08-02 11:53:12 +020046 private static final int PROGRESS_TIMEOUT = 2000;
47
48 private boolean mProvisioned;
Matthew Ng932b2022018-07-19 13:30:54 -070049 private WallpaperManager mWallManager;
Jorim Jaggi98407942016-08-02 11:53:12 +020050
51 private final Runnable mProgressTimeoutRunnable = () -> {
52 View v = getLayoutInflater().inflate(
53 R.layout.fallback_home_finishing_boot, null /* root */);
54 setContentView(v);
55 v.setAlpha(0f);
56 v.animate()
57 .alpha(1f)
58 .setDuration(500)
59 .setInterpolator(AnimationUtils.loadInterpolator(
60 this, android.R.interpolator.fast_out_slow_in))
61 .start();
62 getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
63 };
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070064
Matthew Ng932b2022018-07-19 13:30:54 -070065 private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() {
66 @Override
67 public void onColorsChanged(WallpaperColors colors, int which) {
68 if (colors != null) {
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +080069 final View decorView = getWindow().getDecorView();
Matthew Ng932b2022018-07-19 13:30:54 -070070 decorView.setSystemUiVisibility(
71 updateVisibilityFlagsFromColors(colors, decorView.getSystemUiVisibility()));
72 mWallManager.removeOnColorsChangedListener(this);
73 }
74 }
75 };
76
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -070077 @Override
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060080
81 // Set ourselves totally black before the device is provisioned so that
82 // we don't flash the wallpaper before SUW
Jorim Jaggi98407942016-08-02 11:53:12 +020083 mProvisioned = Settings.Global.getInt(getContentResolver(),
84 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +080085 final int flags;
Jorim Jaggi98407942016-08-02 11:53:12 +020086 if (!mProvisioned) {
Jorim Jaggia616a0d2016-06-29 16:33:39 -070087 setTheme(R.style.FallbackHome_SetupWizard);
Matthew Ngdd396492018-05-31 13:30:27 -070088 flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
89 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Jorim Jaggi98407942016-08-02 11:53:12 +020090 } else {
Matthew Ngdd396492018-05-31 13:30:27 -070091 flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
92 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
Jeff Sharkeyc80dc5e2016-05-03 17:25:37 -060093 }
94
Matthew Ng932b2022018-07-19 13:30:54 -070095 mWallManager = getSystemService(WallpaperManager.class);
96 if (mWallManager == null) {
97 Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!");
98 } else {
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +080099 loadWallpaperColors(flags);
Matthew Ngdd396492018-05-31 13:30:27 -0700100 }
101 getWindow().getDecorView().setSystemUiVisibility(flags);
102
Jorim Jaggic0c583c2016-11-02 20:33:32 +0000103 registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700104 maybeFinish();
105 }
106
107 @Override
Jorim Jaggi98407942016-08-02 11:53:12 +0200108 protected void onResume() {
109 super.onResume();
110 if (mProvisioned) {
111 mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
112 }
113 }
114
115 @Override
116 protected void onPause() {
117 super.onPause();
118 mHandler.removeCallbacks(mProgressTimeoutRunnable);
119 }
120
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700121 protected void onDestroy() {
122 super.onDestroy();
123 unregisterReceiver(mReceiver);
Matthew Ng932b2022018-07-19 13:30:54 -0700124 if (mWallManager != null) {
125 mWallManager.removeOnColorsChangedListener(mColorsChangedListener);
126 }
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -0700127 }
128
129 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
130 @Override
131 public void onReceive(Context context, Intent intent) {
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700132 maybeFinish();
133 }
134 };
135
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +0800136 private void loadWallpaperColors(int flags) {
137 final AsyncTask loadWallpaperColorsTask = new AsyncTask<Object, Void, Integer>() {
138 @Override
139 protected Integer doInBackground(Object... params) {
140 final WallpaperColors colors =
141 mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
142
143 // Use a listener to wait for colors if not ready yet.
144 if (colors == null) {
145 mWallManager.addOnColorsChangedListener(mColorsChangedListener,
146 null /* handler */);
147 return null;
148 }
149 return updateVisibilityFlagsFromColors(colors, flags);
150 }
151
152 @Override
153 protected void onPostExecute(Integer flagsToUpdate) {
154 if (flagsToUpdate == null) {
155 return;
156 }
157 getWindow().getDecorView().setSystemUiVisibility(flagsToUpdate);
158 }
159 };
160 loadWallpaperColorsTask.execute();
161 }
162
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700163 private void maybeFinish() {
164 if (getSystemService(UserManager.class).isUserUnlocked()) {
165 final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
166 .addCategory(Intent.CATEGORY_HOME);
167 final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
168 if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
Muyuan Li4811ab42016-05-20 14:27:14 -0700169 if (UserManager.isSplitSystemUser()
170 && UserHandle.myUserId() == UserHandle.USER_SYSTEM) {
171 // This avoids the situation where the system user has no home activity after
172 // SUW and this activity continues to throw out warnings. See b/28870689.
173 return;
174 }
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700175 Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
176 mHandler.sendEmptyMessageDelayed(0, 500);
177 } else {
178 Log.d(TAG, "User unlocked and real home found; let's go!");
Jorim Jaggi98407942016-08-02 11:53:12 +0200179 getSystemService(PowerManager.class).userActivity(
180 SystemClock.uptimeMillis(), false);
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700181 finish();
182 }
183 }
184 }
185
Yi-Ling Chuangbef25b52019-05-28 16:14:08 +0800186 // Set the system ui flags to light status bar if the wallpaper supports dark text to match
187 // current system ui color tints.
Matthew Ng932b2022018-07-19 13:30:54 -0700188 private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) {
189 if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
190 return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
191 | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
192 }
193 return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
194 & ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
195 }
196
Jeff Sharkeybc16a072015-12-18 17:19:27 -0700197 private Handler mHandler = new Handler() {
198 @Override
199 public void handleMessage(Message msg) {
200 maybeFinish();
Jeff Sharkey9e9f7d12015-11-30 13:28:19 -0700201 }
202 };
203}