When wallpaper colors not ready set sys ui flags when ready

Uses a listener on wallpaper manager to get the wallpaper colors for
fallback home. When the colors are ready it can correctly set the system
ui flags.

Change-Id: I7119c0f035245539cb69bec1ccccecef64d3aff5
Fixes: 79776583
Test: reboot phone, unlock with pin/pattern with light them wallpaper
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FallbackHomeActivityTest
diff --git a/src/com/android/settings/FallbackHome.java b/src/com/android/settings/FallbackHome.java
index 7d5948e..59347ad 100644
--- a/src/com/android/settings/FallbackHome.java
+++ b/src/com/android/settings/FallbackHome.java
@@ -19,6 +19,7 @@
 import android.app.Activity;
 import android.app.WallpaperColors;
 import android.app.WallpaperManager;
+import android.app.WallpaperManager.OnColorsChangedListener;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -44,6 +45,7 @@
     private static final int PROGRESS_TIMEOUT = 2000;
 
     private boolean mProvisioned;
+    private WallpaperManager mWallManager;
 
     private final Runnable mProgressTimeoutRunnable = () -> {
         View v = getLayoutInflater().inflate(
@@ -59,6 +61,18 @@
         getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
     };
 
+    private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() {
+        @Override
+        public void onColorsChanged(WallpaperColors colors, int which) {
+            if (colors != null) {
+                View decorView = getWindow().getDecorView();
+                decorView.setSystemUiVisibility(
+                        updateVisibilityFlagsFromColors(colors, decorView.getSystemUiVisibility()));
+                mWallManager.removeOnColorsChangedListener(this);
+            }
+        }
+    };
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -78,13 +92,17 @@
         }
 
         // Set the system ui flags to light status bar if the wallpaper supports dark text to match
-        // current system ui color tints.
-        final WallpaperColors colors = getSystemService(WallpaperManager.class)
-                .getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
-        if (colors != null
-                && (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
-            flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
-                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+        // current system ui color tints. Use a listener to wait for colors if not ready yet.
+        mWallManager = getSystemService(WallpaperManager.class);
+        if (mWallManager == null) {
+            Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!");
+        } else {
+            WallpaperColors colors = mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
+            if (colors == null) {
+                mWallManager.addOnColorsChangedListener(mColorsChangedListener, null /* handler */);
+            } else {
+                flags = updateVisibilityFlagsFromColors(colors, flags);
+            }
         }
         getWindow().getDecorView().setSystemUiVisibility(flags);
 
@@ -109,6 +127,9 @@
     protected void onDestroy() {
         super.onDestroy();
         unregisterReceiver(mReceiver);
+        if (mWallManager != null) {
+            mWallManager.removeOnColorsChangedListener(mColorsChangedListener);
+        }
     }
 
     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -141,6 +162,15 @@
         }
     }
 
+    private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) {
+        if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
+            return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
+                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+        }
+        return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
+                & ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
+    }
+
     private Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {