Merge "Implement handling of My Photos wallpapers" into ub-launcher3-master
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 48a3941..129f5b9 100755
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2,7 +2,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.android.wallpaper">
 
-    <uses-sdk android:targetSdkVersion="Q" android:minSdkVersion="Q"/>
+    <uses-sdk android:targetSdkVersion="Q" android:minSdkVersion="28"/>
 
     <uses-permission android:name="android.permission.CHANGE_OVERLAY_PACKAGES"/>
     <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
diff --git a/res/layout/fragment_clock_picker.xml b/res/layout/fragment_clock_picker.xml
index f24a354..d97a339 100644
--- a/res/layout/fragment_clock_picker.xml
+++ b/res/layout/fragment_clock_picker.xml
@@ -20,7 +20,7 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
-    android:background="@color/category_picker_background_color">
+    android:background="?android:colorPrimary">
     <include layout="@layout/section_header"/>
 
     <com.android.customization.widget.PreviewPager
diff --git a/res/layout/fragment_grid_picker.xml b/res/layout/fragment_grid_picker.xml
index 61cdfcf..5c21cd9 100644
--- a/res/layout/fragment_grid_picker.xml
+++ b/res/layout/fragment_grid_picker.xml
@@ -19,7 +19,7 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
     android:orientation="vertical"
-    android:background="@color/category_picker_background_color">
+    android:background="?android:colorPrimary">
     <include layout="@layout/section_header"/>
 
     <com.android.customization.widget.PreviewPager
diff --git a/res/values-night/colors.xml b/res/values-night/colors.xml
new file mode 100644
index 0000000..f9509e6
--- /dev/null
+++ b/res/values-night/colors.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+     Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <color name="theme_preview_icon_color">@color/white_70_alpha</color>
+
+    <color name="icon_thumbnail_color">@color/white_70_alpha</color>
+</resources>
\ No newline at end of file
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 66ef501..19177e4 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -31,4 +31,7 @@
     <color name="theme_preview_icon_color">@color/google_grey700</color>
 
     <color name="preview_pager_arrow_disabled">@android:color/darker_gray</color>
+
+    <color name="text_color_dark">#2d2d2d</color>
+    <color name="text_color_light">@color/material_white_text</color>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 12269f7..6773d65 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -19,11 +19,9 @@
         <item name="colorPrimary">?android:colorPrimary</item>
 
         <item name="android:statusBarColor">?android:colorPrimary</item>
-        <item name="android:windowLightStatusBar">true</item>
 
-        <item name="android:navigationBarColor">@color/system_navigation_bar_background</item>
+        <item name="android:navigationBarColor">?android:colorPrimaryDark</item>
         <item name="android:navigationBarDividerColor">@color/system_navigation_bar_divider</item>
-        <item name="android:windowLightNavigationBar">true</item>
 
         <item name="selectableItemBackground">?android:attr/selectableItemBackground</item>
     </style>
@@ -44,7 +42,6 @@
         <item name="android:textStyle">bold</item>
     </style>
     <style name="RegularToolbarStyle" parent="@android:style/Widget.DeviceDefault.Light.ActionBar.Solid">
-        <item name="android:background">?android:colorPrimary</item>
         <item name="android:elevation">0dp</item>
     </style>
 
@@ -57,17 +54,17 @@
     </style>
 
 
-    <style name="PreviewCard" parent="CardView.Light">
+    <style name="PreviewCard" parent="CardView">
         <item name="cardCornerRadius">?android:dialogCornerRadius</item>
         <item name="android:clipChildren">true</item>
         <item name="contentPadding">@dimen/preview_card_padding</item>
-        <item name="android:background">@color/primary_color</item>
+        <item name="cardBackgroundColor">?android:colorPrimary</item>
     </style>
 
     <style name="FullContentPreviewCard" parent="PreviewCard">
         <item name="android:clipChildren">true</item>
         <item name="contentPadding">0dp</item>
-        <item name="android:background">@color/primary_color</item>
+        <item name="cardBackgroundColor">?android:colorPrimary</item>
     </style>
 
     <style name="CardTitleTextAppearance" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
diff --git a/src/com/android/customization/model/theme/ThemeBundle.java b/src/com/android/customization/model/theme/ThemeBundle.java
index aa28ad8..3af25d4 100644
--- a/src/com/android/customization/model/theme/ThemeBundle.java
+++ b/src/com/android/customization/model/theme/ThemeBundle.java
@@ -17,6 +17,7 @@
 
 import android.content.Context;
 import android.content.res.ColorStateList;
+import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
@@ -183,6 +184,17 @@
             this.colorPreviewAsset = colorPreviewAsset;
             this.shapePreviewAsset = shapePreviewAsset;
         }
+
+        /**
+         * Returns the accent color to be applied corresponding with the current configuration's
+         * UI mode.
+         * @return one of {@link #colorAccentDark} or {@link #colorAccentLight}
+         */
+        @ColorInt
+        public int resolveAccentColor(Resources res) {
+            return (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
+                    == Configuration.UI_MODE_NIGHT_YES ? colorAccentDark : colorAccentLight;
+        }
     }
 
     public static class Builder {
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index a7d8af2..a1d81c3 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -15,10 +15,14 @@
  */
 package com.android.customization.picker.theme;
 
+import static android.app.WallpaperColors.HINT_SUPPORTS_DARK_THEME;
+
 import android.app.Activity;
+import android.app.WallpaperColors;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.content.res.Resources;
+import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.os.Bundle;
 import android.util.Log;
@@ -42,6 +46,7 @@
 
 import com.android.customization.model.CustomizationManager.Callback;
 import com.android.customization.model.theme.ThemeBundle;
+import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
 import com.android.customization.model.theme.ThemeManager;
 import com.android.customization.picker.BasePreviewAdapter;
 import com.android.customization.picker.BasePreviewAdapter.PreviewPage;
@@ -252,37 +257,39 @@
         ThemePreviewAdapter(Activity activity, ThemeBundle theme) {
             super(activity, R.layout.theme_preview_card);
             final Resources res = activity.getResources();
+            final PreviewInfo previewInfo = theme.getPreviewInfo();
             addPage(new ThemePreviewPage(activity, R.string.preview_name_font, R.drawable.ic_font,
-                    R.layout.preview_card_font_content, theme.getPreviewInfo().colorAccentLight) {
+                    R.layout.preview_card_font_content,
+                    previewInfo.resolveAccentColor(res)) {
                 @Override
                 protected void bindBody(boolean forceRebind) {
                     TextView title = card.findViewById(R.id.font_card_title);
-                    title.setTypeface(theme.getPreviewInfo().headlineFontFamily);
+                    title.setTypeface(previewInfo.headlineFontFamily);
                     TextView body = card.findViewById(R.id.font_card_body);
-                    body.setTypeface(theme.getPreviewInfo().bodyFontFamily);
+                    body.setTypeface(previewInfo.bodyFontFamily);
                 }
             });
-            if (theme.getPreviewInfo().icons.size() >= mIconIds.length) {
+            if (previewInfo.icons.size() >= mIconIds.length) {
                 addPage(new ThemePreviewPage(activity, R.string.preview_name_icon,
                         R.drawable.ic_wifi_24px, R.layout.preview_card_icon_content,
-                        theme.getPreviewInfo().colorAccentLight) {
+                        previewInfo.colorAccentLight) {
                     @Override
                     protected void bindBody(boolean forceRebind) {
                         for (int i = 0; i < mIconIds.length; i++) {
                             ((ImageView) card.findViewById(mIconIds[i])).setImageDrawable(
-                                    theme.getPreviewInfo().icons.get(i));
+                                    previewInfo.icons.get(i));
                         }
                     }
                 });
             }
-            if (theme.getPreviewInfo().colorPreviewAsset != null) {
+            if (previewInfo.colorPreviewAsset != null) {
                 addPage(new ThemePreviewPage(activity, R.string.preview_name_color,
                         R.drawable.ic_colorize_24px, R.layout.preview_card_static_content,
-                        theme.getPreviewInfo().colorAccentLight) {
+                        previewInfo.resolveAccentColor(res)) {
                     @Override
                     protected void bindBody(boolean forceRebind) {
                         ImageView staticImage = card.findViewById(R.id.preview_static_image);
-                        theme.getPreviewInfo().colorPreviewAsset.loadDrawable(activity,
+                        previewInfo.colorPreviewAsset.loadDrawable(activity,
                                 staticImage, card.getCardBackgroundColor().getDefaultColor());
                         staticImage.getLayoutParams().width = res.getDimensionPixelSize(
                                 R.dimen.color_preview_image_width);
@@ -291,14 +298,14 @@
                     }
                 });
             }
-            if (theme.getPreviewInfo().shapePreviewAsset != null) {
+            if (previewInfo.shapePreviewAsset != null) {
                 addPage(new ThemePreviewPage(activity, R.string.preview_name_shape,
                         R.drawable.ic_shapes_24px, R.layout.preview_card_static_content,
-                        theme.getPreviewInfo().colorAccentLight) {
+                        previewInfo.resolveAccentColor(res)) {
                     @Override
                     protected void bindBody(boolean forceRebind) {
                         ImageView staticImage = card.findViewById(R.id.preview_static_image);
-                        theme.getPreviewInfo().shapePreviewAsset.loadDrawable(activity,
+                        previewInfo.shapePreviewAsset.loadDrawable(activity,
                                 staticImage, card.getCardBackgroundColor().getDefaultColor());
 
                         staticImage.getLayoutParams().width = res.getDimensionPixelSize(
@@ -308,13 +315,13 @@
                     }
                 });
             }
-            if (theme.getPreviewInfo().wallpaperAsset != null) {
+            if (previewInfo.wallpaperAsset != null) {
                 addPage(new ThemePreviewPage(activity, R.string.preview_name_wallpaper,
                         R.drawable.ic_wallpaper_24px, R.layout.preview_card_wallpaper_content,
-                        theme.getPreviewInfo().colorAccentLight) {
+                        previewInfo.resolveAccentColor(res)) {
 
                     private final WallpaperPreviewLayoutListener  mListener =
-                            new WallpaperPreviewLayoutListener(theme);
+                            new WallpaperPreviewLayoutListener(theme, previewInfo);
 
                     @Override
                     protected boolean containsWallpaper() {
@@ -345,9 +352,11 @@
 
         private static class WallpaperPreviewLayoutListener implements OnLayoutChangeListener {
             private final ThemeBundle mTheme;
+            private final PreviewInfo mPreviewInfo;
 
-            public WallpaperPreviewLayoutListener(ThemeBundle theme) {
+            public WallpaperPreviewLayoutListener(ThemeBundle theme, PreviewInfo previewInfo) {
                 mTheme = theme;
+                mPreviewInfo = previewInfo;
             }
 
             @Override
@@ -357,14 +366,29 @@
                 int targetHeight = bottom - top;
                 if (targetWidth > 0 && targetHeight > 0) {
                     mTheme.getWallpaperPreviewAsset(view.getContext()).decodeBitmap(
-                            targetWidth, targetHeight, bitmap -> {
-                                Resources res = view.getContext().getResources();
-                                view.findViewById(R.id.theme_preview_card_background)
-                                        .setBackground(new BitmapDrawable(res, bitmap));
-                            });
+                            targetWidth, targetHeight, bitmap -> setWallpaperBitmap(view, bitmap));
                     view.removeOnLayoutChangeListener(this);
                 }
             }
+
+            private void setWallpaperBitmap(View view, Bitmap bitmap) {
+                Resources res = view.getContext().getResources();
+                view.findViewById(
+                        R.id.theme_preview_card_background)
+                        .setBackground(
+                                new BitmapDrawable(res, bitmap));
+                int colorsHint = WallpaperColors.fromBitmap(bitmap).getColorHints();
+                TextView header = view.findViewById(R.id.theme_preview_card_header);
+                if ((colorsHint & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0) {
+                    header.setTextColor(res.getColor(R.color.text_color_light, null));
+                    header.setCompoundDrawableTintList(ColorStateList.valueOf(
+                            mPreviewInfo.colorAccentDark));
+                } else {
+                    header.setTextColor(res.getColor(R.color.text_color_dark, null));
+                    header.setCompoundDrawableTintList(ColorStateList.valueOf(
+                            mPreviewInfo.colorAccentLight));
+                }
+            }
         }
     }
 }