blob: 31d56e03ef77695dbceb40b87bed78dc156d9b5e [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.picker;
17
Chuck Liaof646e052020-09-24 12:24:59 +080018import android.app.AlertDialog;
Jon Miranda16ea1b12017-12-12 14:52:48 -080019import android.app.Dialog;
20import android.content.Context;
Santiago Etchebehereed9d1622019-12-05 15:19:22 -080021import android.content.DialogInterface;
Jon Miranda16ea1b12017-12-12 14:52:48 -080022import android.os.Bundle;
Jon Miranda16ea1b12017-12-12 14:52:48 -080023import android.view.View;
24import android.widget.Button;
Pierre-Louise04db2c2021-04-10 16:49:10 +020025import android.widget.TextView;
Jon Miranda16ea1b12017-12-12 14:52:48 -080026
Santiago Etchebehereed9d1622019-12-05 15:19:22 -080027import androidx.annotation.NonNull;
Santiago Etchebehere32305d72019-03-25 14:32:40 -070028import androidx.annotation.StringRes;
Sunny Goyal8600a3f2018-08-15 12:48:01 -070029import androidx.appcompat.view.ContextThemeWrapper;
30import androidx.fragment.app.DialogFragment;
31
Santiago Etchebehere32305d72019-03-25 14:32:40 -070032import com.android.wallpaper.R;
Clément Julliard11ab2072019-04-23 10:04:15 -070033import com.android.wallpaper.module.WallpaperPersister;
Santiago Etchebehere32305d72019-03-25 14:32:40 -070034
Jon Miranda16ea1b12017-12-12 14:52:48 -080035/**
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 */
39public class SetWallpaperDialogFragment extends DialogFragment {
40
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070041 private Button mSetHomeWallpaperButton;
42 private Button mSetLockWallpaperButton;
43 private Button mSetBothWallpaperButton;
44
45 private boolean mHomeAvailable = true;
Santiago Etchebehere371906d2019-04-22 18:14:34 -030046 private boolean mLockAvailable = true;
Santiago Etchebehere32305d72019-03-25 14:32:40 -070047 private Listener mListener;
48 private int mTitleResId;
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +080049 private boolean mWithItemSelected;
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070050
Santiago Etchebehere943fe402019-05-23 11:09:51 -070051 public SetWallpaperDialogFragment() {
52 setRetainInstance(true);
53 }
54
Jon Miranda16ea1b12017-12-12 14:52:48 -080055 @Override
56 public Dialog onCreateDialog(Bundle savedInstanceState) {
57 super.onCreateDialog(savedInstanceState);
58
59 Context context = getContext();
60
Jon Miranda16ea1b12017-12-12 14:52:48 -080061 @SuppressWarnings("RestrictTo")
62 View layout =
63 View.inflate(
64 new ContextThemeWrapper(getActivity(), R.style.LightDialogTheme),
65 R.layout.dialog_set_wallpaper,
66 null);
67
Pierre-Louise04db2c2021-04-10 16:49:10 +020068 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);
Santiago Etchebehere32305d72019-03-25 14:32:40 -070074 AlertDialog dialog = new AlertDialog.Builder(context, R.style.LightDialogTheme)
Pierre-Louise04db2c2021-04-10 16:49:10 +020075 .setCustomTitle(customTitleView)
Jon Miranda16ea1b12017-12-12 14:52:48 -080076 .setView(layout)
77 .create();
78
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070079 mSetHomeWallpaperButton = layout.findViewById(R.id.set_home_wallpaper_button);
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +080080 mSetHomeWallpaperButton.setOnClickListener(
81 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_HOME_SCREEN));
Jon Miranda16ea1b12017-12-12 14:52:48 -080082
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070083 mSetLockWallpaperButton = layout.findViewById(R.id.set_lock_wallpaper_button);
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +080084 mSetLockWallpaperButton.setOnClickListener(
85 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_LOCK_SCREEN));
Jon Miranda16ea1b12017-12-12 14:52:48 -080086
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070087 mSetBothWallpaperButton = layout.findViewById(R.id.set_both_wallpaper_button);
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +080088 mSetBothWallpaperButton.setOnClickListener(
89 v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_BOTH));
Jon Miranda16ea1b12017-12-12 14:52:48 -080090
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -070091 updateButtonsVisibility();
92
Jon Miranda16ea1b12017-12-12 14:52:48 -080093 return dialog;
94 }
95
Santiago Etchebehereed9d1622019-12-05 15:19:22 -080096 @Override
97 public void onDismiss(@NonNull DialogInterface dialog) {
98 super.onDismiss(dialog);
99 if (mListener != null) {
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +0800100 mListener.onDialogDismissed(mWithItemSelected);
Santiago Etchebehereed9d1622019-12-05 15:19:22 -0800101 }
102 }
103
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -0700104 public void setHomeOptionAvailable(boolean homeAvailable) {
105 mHomeAvailable = homeAvailable;
106 updateButtonsVisibility();
107 }
108
Santiago Etchebehere371906d2019-04-22 18:14:34 -0300109 public void setLockOptionAvailable(boolean lockAvailable) {
110 mLockAvailable = lockAvailable;
111 updateButtonsVisibility();
112 }
113
Santiago Etchebehere32305d72019-03-25 14:32:40 -0700114 public void setTitleResId(@StringRes int titleResId) {
115 mTitleResId = titleResId;
116 }
117
118 public void setListener(Listener listener) {
119 mListener = listener;
120 }
121
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -0700122 private void updateButtonsVisibility() {
123 if (mSetHomeWallpaperButton != null) {
124 mSetHomeWallpaperButton.setVisibility(mHomeAvailable ? View.VISIBLE : View.GONE);
125 }
Santiago Etchebehere371906d2019-04-22 18:14:34 -0300126 if (mSetLockWallpaperButton != null) {
127 mSetLockWallpaperButton.setVisibility(mLockAvailable ? View.VISIBLE : View.GONE);
128 }
Santiago Etchebehere8f118fe2018-06-11 16:57:10 -0700129 }
130
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +0800131 private void onSetWallpaperButtonClick(int destination) {
132 mWithItemSelected = true;
133 mListener.onSet(destination);
134 dismiss();
135 }
136
Jon Miranda16ea1b12017-12-12 14:52:48 -0800137 /**
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +0800138 * Interface which clients of this DialogFragment should implement in order to handle user
139 * actions on the dialog's clickable elements.
Jon Miranda16ea1b12017-12-12 14:52:48 -0800140 */
141 public interface Listener {
Clément Julliard11ab2072019-04-23 10:04:15 -0700142 void onSet(int destination);
Santiago Etchebehereed9d1622019-12-05 15:19:22 -0800143
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.
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +0800147 *
148 * @param withItemSelected true if the dialog is dismissed with item selected
Santiago Etchebehereed9d1622019-12-05 15:19:22 -0800149 */
Chihhang Chuangd5bc1fc2019-12-16 16:28:04 +0800150 default void onDialogDismissed(boolean withItemSelected) {}
Jon Miranda16ea1b12017-12-12 14:52:48 -0800151 }
152}