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 static com.android.wallpaper.model.WallpaperRotationInitializer.NETWORK_PREFERENCE_CELLULAR_OK; 19 import static com.android.wallpaper.model.WallpaperRotationInitializer.NETWORK_PREFERENCE_WIFI_ONLY; 20 21 import android.app.AlertDialog; 22 import android.app.Dialog; 23 import android.os.Bundle; 24 import android.text.Html; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.widget.CheckBox; 28 import android.widget.TextView; 29 30 import androidx.annotation.NonNull; 31 import androidx.annotation.Nullable; 32 33 import androidx.appcompat.view.ContextThemeWrapper; 34 import androidx.fragment.app.DialogFragment; 35 36 import com.android.wallpaper.R; 37 import com.android.wallpaper.module.InjectorProvider; 38 39 /** 40 * Dialog which allows user to start a wallpaper rotation or cancel, as well as providing an option 41 * whether to rotate wallpapers on wifi-only connections or not. 42 */ 43 public class StartRotationDialogFragment extends DialogFragment { 44 private static final String KEY_IS_WIFI_ONLY_CHECKED = "key_is_wifi_only_checked"; 45 private static final String KEY_IS_LIVE_WALLPAPER_PREVIEW_NEEDED = "key_is_live_wallpaper_needed"; 46 private static final boolean DEFAULT_IS_WIFI_ONLY = true; 47 48 private boolean mIsWifiOnlyChecked; 49 private boolean mIsLiveWallpaperPreviewNeeded; 50 newInstance(boolean isLiveWallpaperPreviewNeeded)51 public static StartRotationDialogFragment newInstance(boolean isLiveWallpaperPreviewNeeded) { 52 StartRotationDialogFragment dialogFragment = new StartRotationDialogFragment(); 53 Bundle args = new Bundle(); 54 args.putBoolean(KEY_IS_LIVE_WALLPAPER_PREVIEW_NEEDED, isLiveWallpaperPreviewNeeded); 55 dialogFragment.setArguments(args); 56 return dialogFragment; 57 } 58 59 @Override onCreate(@ullable Bundle savedInstanceState)60 public void onCreate(@Nullable Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 63 Bundle args = getArguments(); 64 if (args != null) { 65 mIsLiveWallpaperPreviewNeeded = args.getBoolean(KEY_IS_LIVE_WALLPAPER_PREVIEW_NEEDED); 66 } 67 68 if (savedInstanceState == null) { 69 mIsWifiOnlyChecked = DEFAULT_IS_WIFI_ONLY; 70 } else { 71 mIsWifiOnlyChecked = savedInstanceState.getBoolean(KEY_IS_WIFI_ONLY_CHECKED); 72 } 73 } 74 75 @NonNull 76 @Override onCreateDialog(Bundle savedInstanceState)77 public Dialog onCreateDialog(Bundle savedInstanceState) { 78 LayoutInflater inflater = getActivity().getLayoutInflater(); 79 View dialogView = inflater.inflate(R.layout.dialog_start_rotation, null); 80 81 TextView bodyText = dialogView.findViewById(R.id.start_rotation_dialog_subhead); 82 bodyText.setText(Html.fromHtml(getResources().getString(getBodyTextResourceId()))); 83 84 final CheckBox wifiOnlyCheckBox = dialogView.findViewById( 85 R.id.start_rotation_wifi_only_checkbox); 86 87 boolean hasTelephony = InjectorProvider.getInjector().getSystemFeatureChecker() 88 .hasTelephony(getContext()); 89 90 // Only show the "WiFi only" checkbox if the device supports a cellular data connection. 91 if (hasTelephony) { 92 wifiOnlyCheckBox.setChecked(mIsWifiOnlyChecked); 93 wifiOnlyCheckBox.setOnClickListener( 94 v -> mIsWifiOnlyChecked = wifiOnlyCheckBox.isChecked()); 95 } else { 96 wifiOnlyCheckBox.setVisibility(View.GONE); 97 } 98 99 return new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme) 100 .setView(dialogView) 101 .setNegativeButton(android.R.string.cancel, null /* listener */) 102 .setPositiveButton(getPositiveButtonTextResourceId(), 103 (dialog, which) -> { 104 RotationStarter callback = (RotationStarter) getTargetFragment(); 105 callback.startRotation( 106 mIsWifiOnlyChecked 107 ? NETWORK_PREFERENCE_WIFI_ONLY 108 : NETWORK_PREFERENCE_CELLULAR_OK); 109 }) 110 .create(); 111 } 112 113 @Override onSaveInstanceState(Bundle outState)114 public void onSaveInstanceState(Bundle outState) { 115 super.onSaveInstanceState(outState); 116 outState.putBoolean(KEY_IS_WIFI_ONLY_CHECKED, mIsWifiOnlyChecked); 117 } 118 getBodyTextResourceId()119 private int getBodyTextResourceId() { 120 return mIsLiveWallpaperPreviewNeeded 121 ? R.string.start_rotation_dialog_body_live_wallpaper_needed 122 : R.string.start_rotation_dialog_body; 123 } 124 getPositiveButtonTextResourceId()125 private int getPositiveButtonTextResourceId() { 126 return mIsLiveWallpaperPreviewNeeded 127 ? R.string.start_rotation_dialog_continue 128 : android.R.string.ok; 129 } 130 } 131