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