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 int mProgressTimeout; 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 mProgressTimeout = getResources().getInteger( 80 com.android.internal.R.integer.config_progressTimeoutFallbackHome); 81 82 if (mProgressTimeout <= 0) { 83 mProgressTimeout = 0; 84 } 85 86 // Set ourselves totally black before the device is provisioned so that 87 // we don't flash the wallpaper before SUW 88 mProvisioned = Settings.Global.getInt(getContentResolver(), 89 Settings.Global.DEVICE_PROVISIONED, 0) != 0; 90 final int flags; 91 if (!mProvisioned) { 92 setTheme(R.style.FallbackHome_SetupWizard); 93 flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 94 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 95 } else { 96 flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 97 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; 98 } 99 100 mWallManager = getSystemService(WallpaperManager.class); 101 if (mWallManager == null) { 102 Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!"); 103 } else { 104 loadWallpaperColors(flags); 105 } 106 getWindow().getDecorView().setSystemUiVisibility(flags); 107 108 registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED)); 109 maybeFinish(); 110 } 111 112 @Override onResume()113 protected void onResume() { 114 super.onResume(); 115 if (mProvisioned) { 116 mHandler.postDelayed(mProgressTimeoutRunnable, mProgressTimeout); 117 } 118 } 119 120 @Override onPause()121 protected void onPause() { 122 super.onPause(); 123 mHandler.removeCallbacks(mProgressTimeoutRunnable); 124 } 125 onDestroy()126 protected void onDestroy() { 127 super.onDestroy(); 128 unregisterReceiver(mReceiver); 129 if (mWallManager != null) { 130 mWallManager.removeOnColorsChangedListener(mColorsChangedListener); 131 } 132 } 133 134 private BroadcastReceiver mReceiver = new BroadcastReceiver() { 135 @Override 136 public void onReceive(Context context, Intent intent) { 137 maybeFinish(); 138 } 139 }; 140 loadWallpaperColors(int flags)141 private void loadWallpaperColors(int flags) { 142 final AsyncTask loadWallpaperColorsTask = new AsyncTask<Object, Void, Integer>() { 143 @Override 144 protected Integer doInBackground(Object... params) { 145 final WallpaperColors colors = 146 mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); 147 148 // Use a listener to wait for colors if not ready yet. 149 if (colors == null) { 150 mWallManager.addOnColorsChangedListener(mColorsChangedListener, 151 null /* handler */); 152 return null; 153 } 154 return updateVisibilityFlagsFromColors(colors, flags); 155 } 156 157 @Override 158 protected void onPostExecute(Integer flagsToUpdate) { 159 if (flagsToUpdate == null) { 160 return; 161 } 162 getWindow().getDecorView().setSystemUiVisibility(flagsToUpdate); 163 } 164 }; 165 loadWallpaperColorsTask.execute(); 166 } 167 maybeFinish()168 private void maybeFinish() { 169 if (getSystemService(UserManager.class).isUserUnlocked()) { 170 final Intent homeIntent = new Intent(Intent.ACTION_MAIN) 171 .addCategory(Intent.CATEGORY_HOME); 172 final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0); 173 if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) { 174 Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?"); 175 mHandler.sendEmptyMessageDelayed(0, 500); 176 } else { 177 Log.d(TAG, "User unlocked and real home found; let's go!"); 178 getSystemService(PowerManager.class).userActivity( 179 SystemClock.uptimeMillis(), false); 180 finish(); 181 } 182 } 183 } 184 185 // Set the system ui flags to light status bar if the wallpaper supports dark text to match 186 // current system ui color tints. updateVisibilityFlagsFromColors(WallpaperColors colors, int flags)187 private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) { 188 if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) { 189 return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 190 | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; 191 } 192 return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) 193 & ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); 194 } 195 196 private Handler mHandler = new Handler() { 197 @Override 198 public void handleMessage(Message msg) { 199 maybeFinish(); 200 } 201 }; 202 } 203