1 /*
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 
17 package com.android.settings;
18 
19 import android.app.Activity;
20 import android.app.WallpaperColors;
21 import android.app.WallpaperManager;
22 import android.app.WallpaperManager.OnColorsChangedListener;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.pm.ResolveInfo;
28 import android.os.AsyncTask;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Message;
32 import android.os.PowerManager;
33 import android.os.SystemClock;
34 import android.os.UserManager;
35 import android.provider.Settings;
36 import android.util.Log;
37 import android.view.View;
38 import android.view.WindowManager.LayoutParams;
39 import android.view.animation.AnimationUtils;
40 
41 import java.util.Objects;
42 
43 public class FallbackHome extends Activity {
44     private static final String TAG = "FallbackHome";
45     private static final int PROGRESS_TIMEOUT = 2000;
46 
47     private boolean mProvisioned;
48     private WallpaperManager mWallManager;
49 
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     };
63 
64     private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() {
65         @Override
66         public void onColorsChanged(WallpaperColors colors, int which) {
67             if (colors != null) {
68                 final View decorView = getWindow().getDecorView();
69                 decorView.setSystemUiVisibility(
70                         updateVisibilityFlagsFromColors(colors, decorView.getSystemUiVisibility()));
71                 mWallManager.removeOnColorsChangedListener(this);
72             }
73         }
74     };
75 
76     @Override
onCreate(Bundle savedInstanceState)77     protected void onCreate(Bundle savedInstanceState) {
78         super.onCreate(savedInstanceState);
79 
80         // Set ourselves totally black before the device is provisioned so that
81         // we don't flash the wallpaper before SUW
82         mProvisioned = Settings.Global.getInt(getContentResolver(),
83                 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
84         final int flags;
85         if (!mProvisioned) {
86             setTheme(R.style.FallbackHome_SetupWizard);
87             flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
88                     | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
89         } else {
90             flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
91                     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
92         }
93 
94         mWallManager = getSystemService(WallpaperManager.class);
95         if (mWallManager == null) {
96             Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!");
97         } else {
98             loadWallpaperColors(flags);
99         }
100         getWindow().getDecorView().setSystemUiVisibility(flags);
101 
102         registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
103         maybeFinish();
104     }
105 
106     @Override
onResume()107     protected void onResume() {
108         super.onResume();
109         if (mProvisioned) {
110             mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
111         }
112     }
113 
114     @Override
onPause()115     protected void onPause() {
116         super.onPause();
117         mHandler.removeCallbacks(mProgressTimeoutRunnable);
118     }
119 
onDestroy()120     protected void onDestroy() {
121         super.onDestroy();
122         unregisterReceiver(mReceiver);
123         if (mWallManager != null) {
124             mWallManager.removeOnColorsChangedListener(mColorsChangedListener);
125         }
126     }
127 
128     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
129         @Override
130         public void onReceive(Context context, Intent intent) {
131             maybeFinish();
132         }
133     };
134 
loadWallpaperColors(int flags)135     private void loadWallpaperColors(int flags) {
136         final AsyncTask loadWallpaperColorsTask = new AsyncTask<Object, Void, Integer>() {
137             @Override
138             protected Integer doInBackground(Object... params) {
139                 final WallpaperColors colors =
140                         mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
141 
142                 // Use a listener to wait for colors if not ready yet.
143                 if (colors == null) {
144                     mWallManager.addOnColorsChangedListener(mColorsChangedListener,
145                             null /* handler */);
146                     return null;
147                 }
148                 return updateVisibilityFlagsFromColors(colors, flags);
149             }
150 
151             @Override
152             protected void onPostExecute(Integer flagsToUpdate) {
153                 if (flagsToUpdate == null) {
154                     return;
155                 }
156                 getWindow().getDecorView().setSystemUiVisibility(flagsToUpdate);
157             }
158         };
159         loadWallpaperColorsTask.execute();
160     }
161 
maybeFinish()162     private void maybeFinish() {
163         if (getSystemService(UserManager.class).isUserUnlocked()) {
164             final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
165                     .addCategory(Intent.CATEGORY_HOME);
166             final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
167             if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
168                 Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
169                 mHandler.sendEmptyMessageDelayed(0, 500);
170             } else {
171                 Log.d(TAG, "User unlocked and real home found; let's go!");
172                 getSystemService(PowerManager.class).userActivity(
173                         SystemClock.uptimeMillis(), false);
174                 finish();
175             }
176         }
177     }
178 
179     // Set the system ui flags to light status bar if the wallpaper supports dark text to match
180     // current system ui color tints.
updateVisibilityFlagsFromColors(WallpaperColors colors, int flags)181     private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) {
182         if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
183             return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
184                     | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
185         }
186         return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
187                 & ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
188     }
189 
190     private Handler mHandler = new Handler() {
191         @Override
192         public void handleMessage(Message msg) {
193             maybeFinish();
194         }
195     };
196 }
197