Merge "Refactor SetWallpaperDialogFragment for reuse" into ub-launcher3-master
diff --git a/res/values-notnight-v27/styles.xml b/res/values-notnight-v27/styles.xml
index 581ef68..7c104f7 100755
--- a/res/values-notnight-v27/styles.xml
+++ b/res/values-notnight-v27/styles.xml
@@ -26,5 +26,8 @@
         <item name="android:windowLightNavigationBar">true</item>
     </style>
 
-    <style name="LightDialogTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar"/>
+    <style name="LightDialogTheme" parent="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar">
+        <item name="android:layout">@layout/abc_alert_dialog_material</item>
+        <item name="windowActionBar">false</item>
+    </style>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 20f87b7..50548c4 100755
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -43,7 +43,10 @@
         <item name="android:theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
     </style>
 
-    <style name="LightDialogTheme" parent="@android:style/Theme.DeviceDefault.Dialog.NoActionBar"/>
+    <style name="LightDialogTheme" parent="@android:style/Theme.DeviceDefault.Dialog.NoActionBar">
+        <item name="android:layout">@layout/abc_alert_dialog_material</item>
+        <item name="windowActionBar">false</item>
+    </style>
 
     <style name="ProgressDialogThemePreL" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
         <item name="android:windowBackground">@android:color/transparent</item>
diff --git a/src/com/android/wallpaper/module/WallpaperSetter.java b/src/com/android/wallpaper/module/WallpaperSetter.java
index dc62729..bb71aa6 100644
--- a/src/com/android/wallpaper/module/WallpaperSetter.java
+++ b/src/com/android/wallpaper/module/WallpaperSetter.java
@@ -2,13 +2,15 @@
 
 import android.app.Activity;
 import android.app.ProgressDialog;
+import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.graphics.Rect;
 import android.os.Build.VERSION;
 import android.os.Build.VERSION_CODES;
 
 import androidx.annotation.Nullable;
-import androidx.fragment.app.Fragment;
+import androidx.annotation.StringRes;
+import androidx.fragment.app.FragmentManager;
 
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
@@ -18,6 +20,7 @@
 import com.android.wallpaper.module.WallpaperPersister.Destination;
 import com.android.wallpaper.module.WallpaperPersister.SetWallpaperCallback;
 import com.android.wallpaper.picker.SetWallpaperDialogFragment;
+import com.android.wallpaper.picker.SetWallpaperDialogFragment.Listener;
 import com.android.wallpaper.util.ThrowableAnalyzer;
 
 import com.bumptech.glide.Glide;
@@ -155,24 +158,36 @@
     /**
      * Show a dialog asking the user for the Wallpaper's destination
      * (eg, "Home screen", "Lock Screen")
-     * @param targetFragment fragment that will receive the response. It needs to implement
-     * {@link SetWallpaperDialogFragment.Listener} to receive the result.
+     * @param listener {@link SetWallpaperDialogFragment.Listener} that will receive the response.
      * @see Destination
-     * TODO(santie): refactor to not require the target fragment to implement "Listener" interface.
      */
-    public void requestDestination(Fragment targetFragment) {
+    public void requestDestination(Context context, FragmentManager fragmentManager,
+            Listener listener) {
+        requestDestination(context, fragmentManager, R.string.set_wallpaper_dialog_message,
+                listener);
+    }
+
+    /**
+     * Show a dialog asking the user for the Wallpaper's destination
+     * (eg, "Home screen", "Lock Screen")
+     * @param listener {@link SetWallpaperDialogFragment.Listener} that will receive the response.
+     * @param titleResId title for the dialog
+     * @see Destination
+     */
+    public void requestDestination(Context context, FragmentManager fragmentManager,
+            @StringRes int titleResId, Listener listener) {
         CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector()
-                .getCurrentWallpaperFactory(targetFragment.getContext());
+                .getCurrentWallpaperFactory(context);
 
         factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) -> {
             SetWallpaperDialogFragment setWallpaperDialog = new SetWallpaperDialogFragment();
-            setWallpaperDialog.setTargetFragment(targetFragment, UNUSED_REQUEST_CODE);
+            setWallpaperDialog.setTitleResId(titleResId);
+            setWallpaperDialog.setListener(listener);
             if (homeWallpaper instanceof LiveWallpaperInfo && lockWallpaper == null) {
                 // if the lock wallpaper is a live wallpaper, we cannot set a home-only static one
                 setWallpaperDialog.setHomeOptionAvailable(false);
             }
-            setWallpaperDialog.show(
-                    targetFragment.getFragmentManager(), TAG_SET_WALLPAPER_DIALOG_FRAGMENT);
+            setWallpaperDialog.show(fragmentManager, TAG_SET_WALLPAPER_DIALOG_FRAGMENT);
         }, true); // Force refresh as the wallpaper may have been set while this fragment was paused
     }
 
diff --git a/src/com/android/wallpaper/picker/PreviewFragment.java b/src/com/android/wallpaper/picker/PreviewFragment.java
index 00fdf2f..82fd195 100755
--- a/src/com/android/wallpaper/picker/PreviewFragment.java
+++ b/src/com/android/wallpaper/picker/PreviewFragment.java
@@ -55,6 +55,14 @@
 import android.widget.TextView;
 import android.widget.Toast;
 
+import androidx.annotation.IntDef;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.Toolbar;
+import androidx.core.view.ViewCompat;
+import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentActivity;
+
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
 import com.android.wallpaper.asset.Asset.BitmapReceiver;
@@ -86,14 +94,6 @@
 import java.util.Date;
 import java.util.List;
 
-import androidx.annotation.IntDef;
-import androidx.annotation.Nullable;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.Toolbar;
-import androidx.core.view.ViewCompat;
-import androidx.fragment.app.Fragment;
-import androidx.fragment.app.FragmentActivity;
-
 /**
  * Fragment which displays the UI for previewing an individual wallpaper and its attribution
  * information.
@@ -408,7 +408,7 @@
         int id = item.getItemId();
         if (id == R.id.set_wallpaper) {
             if (BuildCompat.isAtLeastN()) {
-                mWallpaperSetter.requestDestination(this);
+                mWallpaperSetter.requestDestination(getContext(), getFragmentManager(), this);
             } else {
                 setCurrentWallpaper(WallpaperPersister.DEST_HOME_SCREEN);
             }
diff --git a/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java b/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
index 2f6dfa9..cf69d9a 100755
--- a/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
+++ b/src/com/android/wallpaper/picker/SetWallpaperDialogFragment.java
@@ -21,13 +21,14 @@
 import android.view.View;
 import android.widget.Button;
 
-import com.android.wallpaper.R;
-import com.android.wallpaper.compat.ButtonDrawableSetterCompat;
-
+import androidx.annotation.StringRes;
 import androidx.appcompat.app.AlertDialog;
 import androidx.appcompat.view.ContextThemeWrapper;
 import androidx.fragment.app.DialogFragment;
 
+import com.android.wallpaper.R;
+import com.android.wallpaper.compat.ButtonDrawableSetterCompat;
+
 /**
  * Dialog fragment which shows the "Set wallpaper" destination dialog for N+ devices. Lets user
  * choose whether to set the wallpaper on the home screen, lock screen, or both.
@@ -39,6 +40,8 @@
     private Button mSetBothWallpaperButton;
 
     private boolean mHomeAvailable = true;
+    private Listener mListener;
+    private int mTitleResId;
 
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
@@ -46,9 +49,6 @@
 
         Context context = getContext();
 
-        int titleResId = R.string.set_wallpaper_dialog_message;
-        final Listener callback = (Listener) getTargetFragment();
-
         @SuppressWarnings("RestrictTo")
         View layout =
                 View.inflate(
@@ -56,42 +56,33 @@
                         R.layout.dialog_set_wallpaper,
                         null);
 
-        AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
-                .setTitle(titleResId)
+        AlertDialog dialog = new AlertDialog.Builder(context, R.style.LightDialogTheme)
+                .setTitle(mTitleResId)
                 .setView(layout)
                 .create();
 
         mSetHomeWallpaperButton = layout.findViewById(R.id.set_home_wallpaper_button);
-        mSetHomeWallpaperButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                callback.onSetHomeScreen();
-                dismiss();
-            }
+        mSetHomeWallpaperButton.setOnClickListener(v -> {
+            mListener.onSetHomeScreen();
+            dismiss();
         });
         ButtonDrawableSetterCompat.setDrawableToButtonStart(
                 mSetHomeWallpaperButton,
                 context.getDrawable(R.drawable.ic_home_24px));
 
         mSetLockWallpaperButton = layout.findViewById(R.id.set_lock_wallpaper_button);
-        mSetLockWallpaperButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                callback.onSetLockScreen();
-                dismiss();
-            }
+        mSetLockWallpaperButton.setOnClickListener(v -> {
+            mListener.onSetLockScreen();
+            dismiss();
         });
         ButtonDrawableSetterCompat.setDrawableToButtonStart(
                 mSetLockWallpaperButton,
                 context.getDrawable(R.drawable.ic_lock_outline_24px));
 
         mSetBothWallpaperButton = layout.findViewById(R.id.set_both_wallpaper_button);
-        mSetBothWallpaperButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                callback.onSetBoth();
-                dismiss();
-            }
+        mSetBothWallpaperButton.setOnClickListener(v -> {
+            mListener.onSetBoth();
+            dismiss();
         });
         ButtonDrawableSetterCompat.setDrawableToButtonStart(
                 mSetBothWallpaperButton,
@@ -107,6 +98,14 @@
         updateButtonsVisibility();
     }
 
+    public void setTitleResId(@StringRes int titleResId) {
+        mTitleResId = titleResId;
+    }
+
+    public void setListener(Listener listener) {
+        mListener = listener;
+    }
+
     private void updateButtonsVisibility() {
         if (mSetHomeWallpaperButton != null) {
             mSetHomeWallpaperButton.setVisibility(mHomeAvailable ? View.VISIBLE : View.GONE);