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