1 /*
2  * Copyright (C) 2017 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 package com.android.wallpaper.picker;
17 
18 import android.app.AlertDialog;
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.TextView;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.StringRes;
29 import androidx.appcompat.view.ContextThemeWrapper;
30 import androidx.fragment.app.DialogFragment;
31 
32 import com.android.wallpaper.R;
33 import com.android.wallpaper.module.WallpaperPersister;
34 
35 /**
36  * Dialog fragment which shows the "Set wallpaper" destination dialog for N+ devices. Lets user
37  * choose whether to set the wallpaper on the home screen, lock screen, or both.
38  */
39 public class SetWallpaperDialogFragment extends DialogFragment {
40 
41     private Button mSetHomeWallpaperButton;
42     private Button mSetLockWallpaperButton;
43     private Button mSetBothWallpaperButton;
44 
45     private boolean mHomeAvailable = true;
46     private boolean mLockAvailable = true;
47     private Listener mListener;
48     private int mTitleResId;
49     private boolean mWithItemSelected;
50 
SetWallpaperDialogFragment()51     public SetWallpaperDialogFragment() {
52         setRetainInstance(true);
53     }
54 
55     @Override
onCreateDialog(Bundle savedInstanceState)56     public Dialog onCreateDialog(Bundle savedInstanceState) {
57         super.onCreateDialog(savedInstanceState);
58 
59         Context context = getContext();
60 
61         @SuppressWarnings("RestrictTo")
62         View layout =
63                 View.inflate(
64                         new ContextThemeWrapper(getActivity(), R.style.LightDialogTheme),
65                         R.layout.dialog_set_wallpaper,
66                         null);
67 
68         View options = layout.findViewById(R.id.dialog_set_wallpaper_options);
69         options.setClipToOutline(true);
70 
71         View customTitleView = View.inflate(context, R.layout.dialog_set_wallpaper_title,  null);
72         TextView title = customTitleView.findViewById(R.id.dialog_set_wallpaper_title);
73         title.setText(mTitleResId);
74         AlertDialog dialog = new AlertDialog.Builder(context, R.style.LightDialogTheme)
75                 .setCustomTitle(customTitleView)
76                 .setView(layout)
77                 .create();
78 
79         mSetHomeWallpaperButton = layout.findViewById(R.id.set_home_wallpaper_button);
80         mSetHomeWallpaperButton.setOnClickListener(
81                 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_HOME_SCREEN));
82 
83         mSetLockWallpaperButton = layout.findViewById(R.id.set_lock_wallpaper_button);
84         mSetLockWallpaperButton.setOnClickListener(
85                 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_LOCK_SCREEN));
86 
87         mSetBothWallpaperButton = layout.findViewById(R.id.set_both_wallpaper_button);
88         mSetBothWallpaperButton.setOnClickListener(
89                 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_BOTH));
90 
91         updateButtonsVisibility();
92 
93         return dialog;
94     }
95 
96     @Override
onDismiss(@onNull DialogInterface dialog)97     public void onDismiss(@NonNull DialogInterface dialog) {
98         super.onDismiss(dialog);
99         if (mListener != null) {
100             mListener.onDialogDismissed(mWithItemSelected);
101         }
102     }
103 
setHomeOptionAvailable(boolean homeAvailable)104     public void setHomeOptionAvailable(boolean homeAvailable) {
105         mHomeAvailable = homeAvailable;
106         updateButtonsVisibility();
107     }
108 
setLockOptionAvailable(boolean lockAvailable)109     public void setLockOptionAvailable(boolean lockAvailable) {
110         mLockAvailable = lockAvailable;
111         updateButtonsVisibility();
112     }
113 
setTitleResId(@tringRes int titleResId)114     public void setTitleResId(@StringRes int titleResId) {
115         mTitleResId = titleResId;
116     }
117 
setListener(Listener listener)118     public void setListener(Listener listener) {
119         mListener = listener;
120     }
121 
updateButtonsVisibility()122     private void updateButtonsVisibility() {
123         if (mSetHomeWallpaperButton != null) {
124             mSetHomeWallpaperButton.setVisibility(mHomeAvailable ? View.VISIBLE : View.GONE);
125         }
126         if (mSetLockWallpaperButton != null) {
127             mSetLockWallpaperButton.setVisibility(mLockAvailable ? View.VISIBLE : View.GONE);
128         }
129     }
130 
onSetWallpaperButtonClick(int destination)131     private void onSetWallpaperButtonClick(int destination) {
132         mWithItemSelected = true;
133         mListener.onSet(destination);
134         dismiss();
135     }
136 
137     /**
138      * Interface which clients of this DialogFragment should implement in order to handle user
139      * actions on the dialog's clickable elements.
140      */
141     public interface Listener {
onSet(int destination)142         void onSet(int destination);
143 
144         /**
145          * Called when the dialog is closed, both because of dismissal and for a selection
146          * being set, so it'll be called even after onSet is called.
147          *
148          * @param withItemSelected true if the dialog is dismissed with item selected
149          */
onDialogDismissed(boolean withItemSelected)150         default void onDialogDismissed(boolean withItemSelected) {}
151     }
152 }
153