Update the toolbar and system bars for preview in Picker

In general preview case, it has black background, white icon and dark
system bars. In screenshot case, the toolbar overlays the item.
We will update it for screenshot case

Test: manual. video on bug (b/191746858)
Bug: 191746858
Bug: 169737802
Change-Id: I2d7d80ef30e95ba2cfaa8a691ca80e6a9fed18bc
Merged-In: I2d7d80ef30e95ba2cfaa8a691ca80e6a9fed18bc
(cherry picked from commit c5c8362ac68d1f43abc3f94b03d773c369beb5dc)
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 6316d8a..59886bd 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -59,8 +59,6 @@
         <item name="android:statusBarColor">?android:colorBackground</item>
         <item name="android:navigationBarColor">?android:colorBackground</item>
         <item name="android:windowBackground">?android:colorBackground</item>
-        <item name="android:windowLightStatusBar">true</item>
-        <item name="android:windowLightNavigationBar">true</item>
     </style>
 
 </resources>
diff --git a/src/com/android/providers/media/photopicker/PhotoPickerActivity.java b/src/com/android/providers/media/photopicker/PhotoPickerActivity.java
index f66195a..260ab70 100644
--- a/src/com/android/providers/media/photopicker/PhotoPickerActivity.java
+++ b/src/com/android/providers/media/photopicker/PhotoPickerActivity.java
@@ -22,6 +22,10 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.content.res.Configuration;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.SystemProperties;
 import android.provider.MediaStore;
@@ -30,6 +34,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowInsetsController;
 import android.widget.CompoundButton;
 import android.widget.Switch;
 import android.widget.Toast;
@@ -40,7 +45,6 @@
 import androidx.lifecycle.ViewModelProvider;
 
 import com.android.providers.media.R;
-
 import com.android.providers.media.photopicker.data.UserIdManager;
 import com.android.providers.media.photopicker.data.model.Category;
 import com.android.providers.media.photopicker.data.model.Item;
@@ -123,7 +127,7 @@
     public void setTitle(CharSequence title) {
         super.setTitle(title);
         getSupportActionBar().setTitle(title);
-        updateToolbar(TextUtils.isEmpty(title));
+        updateToolbar(TextUtils.isEmpty(title), /* isLightBackgroundMode= */ true);
     }
 
     /**
@@ -292,18 +296,62 @@
     }
 
     /**
-     * Update the icons and show/hide the tab chips with {@code shouldShowTabChips}
+     * Update the icons and show/hide the tab chips with {@code shouldShowTabChips}.
+     *
+     * When the tab chips are shown, picker is always in light background mode.
+     * When the tab chips are not shown, whether picker is in light background mode or dark
+     * background mode depends on {@code isLightBackgroundMode}.
      *
      * @param shouldShowTabChips {@code true}, show the tab chips and show close icon. Otherwise,
      *                           hide the tab chips and show back icon
+     * @param isLightBackgroundMode {@code true}, show light background and dark icon.
+     *                              Otherwise, show dark background and light icon.
+     *
      */
-    public void updateToolbar(boolean shouldShowTabChips) {
+    public void updateToolbar(boolean shouldShowTabChips, boolean isLightBackgroundMode) {
         if (shouldShowTabChips) {
             getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
+            getSupportActionBar().setBackgroundDrawable(
+                    new ColorDrawable(getColor(R.color.picker_background_color)));
             mTabChipContainer.setVisibility(View.VISIBLE);
+            // In show tab chips case, picker is always in lightBackground mode in light theme.
+            updateStatusBarAndNavBar(/* isLightBackgroundMode= */ true);
         } else {
-            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back);
+            final Drawable icon = getDrawable(R.drawable.ic_arrow_back);
+            if (isLightBackgroundMode) {
+                icon.setTint(getColor(R.color.picker_toolbar_icon_color));
+                getSupportActionBar().setBackgroundDrawable(
+                        new ColorDrawable(getColor(R.color.picker_background_color)));
+            } else {
+                icon.setTint(Color.WHITE);
+                getSupportActionBar().setBackgroundDrawable(
+                        new ColorDrawable(getColor(R.color.preview_default_black)));
+            }
+            updateStatusBarAndNavBar(isLightBackgroundMode);
+            getSupportActionBar().setHomeAsUpIndicator(icon);
             mTabChipContainer.setVisibility(View.GONE);
         }
     }
+
+    private void updateStatusBarAndNavBar(boolean isLightBackgroundMode) {
+        final int mask = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
+                | WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
+        final int backgroundColor;
+        if (isLightBackgroundMode) {
+            backgroundColor = getColor(R.color.picker_background_color);
+
+            final int uiModeNight =
+                    getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
+            // if the system is not in Dark theme, set the system bars to light mode.
+            if (uiModeNight == Configuration.UI_MODE_NIGHT_NO) {
+                getWindow().getInsetsController().setSystemBarsAppearance(mask, mask);
+            }
+        } else {
+            backgroundColor = getColor(R.color.preview_default_black);
+            getWindow().getInsetsController().setSystemBarsAppearance(/* appearance= */ 0, mask);
+        }
+        getWindow().setStatusBarColor(backgroundColor);
+        getWindow().setNavigationBarColor(backgroundColor);
+    }
 }
\ No newline at end of file
diff --git a/src/com/android/providers/media/photopicker/ui/PreviewFragment.java b/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
index 31f1e42..661004d 100644
--- a/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
@@ -129,7 +129,8 @@
         // TODO(185801129): Change the layout of the toolbar or add new toolbar that can overlap
         // with image/video preview if necessary
         getActivity().setTitle("");
-        ((PhotoPickerActivity) getActivity()).updateToolbar(/* showTabChips= */ false);
+        ((PhotoPickerActivity) getActivity()).updateToolbar(/* showTabChips= */ false,
+                /* isLightBackgroundMode= */ false);
 
         // This is necessary to ensure we call ViewHolder#bind() onResume()
         if (mAdapter != null) {