Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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 | |
Chuck Liao | f646e05 | 2020-09-24 12:24:59 +0800 | [diff] [blame] | 18 | import android.app.AlertDialog; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 19 | import android.app.Dialog; |
| 20 | import android.content.Context; |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 21 | import android.content.DialogInterface; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 22 | import android.os.Bundle; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.widget.Button; |
Pierre-Louis | e04db2c | 2021-04-10 16:49:10 +0200 | [diff] [blame] | 25 | import android.widget.TextView; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 26 | |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 27 | import androidx.annotation.NonNull; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 28 | import androidx.annotation.StringRes; |
Sunny Goyal | 8600a3f | 2018-08-15 12:48:01 -0700 | [diff] [blame] | 29 | import androidx.appcompat.view.ContextThemeWrapper; |
| 30 | import androidx.fragment.app.DialogFragment; |
| 31 | |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 32 | import com.android.wallpaper.R; |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 33 | import com.android.wallpaper.module.WallpaperPersister; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 34 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 41 | private Button mSetHomeWallpaperButton; |
| 42 | private Button mSetLockWallpaperButton; |
| 43 | private Button mSetBothWallpaperButton; |
| 44 | |
| 45 | private boolean mHomeAvailable = true; |
Santiago Etchebehere | 371906d | 2019-04-22 18:14:34 -0300 | [diff] [blame] | 46 | private boolean mLockAvailable = true; |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 47 | private Listener mListener; |
| 48 | private int mTitleResId; |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 49 | private boolean mWithItemSelected; |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 50 | |
Santiago Etchebehere | 943fe40 | 2019-05-23 11:09:51 -0700 | [diff] [blame] | 51 | public SetWallpaperDialogFragment() { |
| 52 | setRetainInstance(true); |
| 53 | } |
| 54 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 55 | @Override |
| 56 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 57 | super.onCreateDialog(savedInstanceState); |
| 58 | |
| 59 | Context context = getContext(); |
| 60 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 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 | |
Pierre-Louis | e04db2c | 2021-04-10 16:49:10 +0200 | [diff] [blame] | 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); |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 74 | AlertDialog dialog = new AlertDialog.Builder(context, R.style.LightDialogTheme) |
Pierre-Louis | e04db2c | 2021-04-10 16:49:10 +0200 | [diff] [blame] | 75 | .setCustomTitle(customTitleView) |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 76 | .setView(layout) |
| 77 | .create(); |
| 78 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 79 | mSetHomeWallpaperButton = layout.findViewById(R.id.set_home_wallpaper_button); |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 80 | mSetHomeWallpaperButton.setOnClickListener( |
| 81 | v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_HOME_SCREEN)); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 82 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 83 | mSetLockWallpaperButton = layout.findViewById(R.id.set_lock_wallpaper_button); |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 84 | mSetLockWallpaperButton.setOnClickListener( |
| 85 | v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_LOCK_SCREEN)); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 86 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 87 | mSetBothWallpaperButton = layout.findViewById(R.id.set_both_wallpaper_button); |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 88 | mSetBothWallpaperButton.setOnClickListener( |
| 89 | v -> onSetWallpaperButtonClick(WallpaperPersister.DEST_BOTH)); |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 90 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 91 | updateButtonsVisibility(); |
| 92 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 93 | return dialog; |
| 94 | } |
| 95 | |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public void onDismiss(@NonNull DialogInterface dialog) { |
| 98 | super.onDismiss(dialog); |
| 99 | if (mListener != null) { |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 100 | mListener.onDialogDismissed(mWithItemSelected); |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 104 | public void setHomeOptionAvailable(boolean homeAvailable) { |
| 105 | mHomeAvailable = homeAvailable; |
| 106 | updateButtonsVisibility(); |
| 107 | } |
| 108 | |
Santiago Etchebehere | 371906d | 2019-04-22 18:14:34 -0300 | [diff] [blame] | 109 | public void setLockOptionAvailable(boolean lockAvailable) { |
| 110 | mLockAvailable = lockAvailable; |
| 111 | updateButtonsVisibility(); |
| 112 | } |
| 113 | |
Santiago Etchebehere | 32305d7 | 2019-03-25 14:32:40 -0700 | [diff] [blame] | 114 | public void setTitleResId(@StringRes int titleResId) { |
| 115 | mTitleResId = titleResId; |
| 116 | } |
| 117 | |
| 118 | public void setListener(Listener listener) { |
| 119 | mListener = listener; |
| 120 | } |
| 121 | |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 122 | private void updateButtonsVisibility() { |
| 123 | if (mSetHomeWallpaperButton != null) { |
| 124 | mSetHomeWallpaperButton.setVisibility(mHomeAvailable ? View.VISIBLE : View.GONE); |
| 125 | } |
Santiago Etchebehere | 371906d | 2019-04-22 18:14:34 -0300 | [diff] [blame] | 126 | if (mSetLockWallpaperButton != null) { |
| 127 | mSetLockWallpaperButton.setVisibility(mLockAvailable ? View.VISIBLE : View.GONE); |
| 128 | } |
Santiago Etchebehere | 8f118fe | 2018-06-11 16:57:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 131 | private void onSetWallpaperButtonClick(int destination) { |
| 132 | mWithItemSelected = true; |
| 133 | mListener.onSet(destination); |
| 134 | dismiss(); |
| 135 | } |
| 136 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 137 | /** |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 138 | * Interface which clients of this DialogFragment should implement in order to handle user |
| 139 | * actions on the dialog's clickable elements. |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 140 | */ |
| 141 | public interface Listener { |
Clément Julliard | 11ab207 | 2019-04-23 10:04:15 -0700 | [diff] [blame] | 142 | void onSet(int destination); |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 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. |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 147 | * |
| 148 | * @param withItemSelected true if the dialog is dismissed with item selected |
Santiago Etchebehere | ed9d162 | 2019-12-05 15:19:22 -0800 | [diff] [blame] | 149 | */ |
Chihhang Chuang | d5bc1fc | 2019-12-16 16:28:04 +0800 | [diff] [blame] | 150 | default void onDialogDismissed(boolean withItemSelected) {} |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 151 | } |
| 152 | } |