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.AlertDialog;
19 import android.app.Dialog;
20 import android.content.DialogInterface;
21 import android.os.Bundle;
22 
23 import com.android.wallpaper.R;
24 import com.android.wallpaper.model.WallpaperRotationInitializer.NetworkPreference;
25 
26 import androidx.annotation.NonNull;
27 import androidx.fragment.app.DialogFragment;
28 
29 /**
30  * Dialog fragment which communicates that starting a wallpaper rotation failed and gives the user
31  * an option to retry starting the rotation.
32  */
33 public class StartRotationErrorDialogFragment extends DialogFragment {
34 
35     private static final String ARG_NETWORK_PREFERENCE = "network_preference";
36 
newInstance( @etworkPreference int networkPreference)37     public static StartRotationErrorDialogFragment newInstance(
38             @NetworkPreference int networkPreference) {
39         StartRotationErrorDialogFragment dialogFragment = new StartRotationErrorDialogFragment();
40         Bundle args = new Bundle();
41         args.putInt(ARG_NETWORK_PREFERENCE, networkPreference);
42         dialogFragment.setArguments(args);
43         return dialogFragment;
44     }
45 
46     @NonNull
47     @Override
onCreateDialog(Bundle savedInstanceState)48     public Dialog onCreateDialog(Bundle savedInstanceState) {
49         super.onCreateDialog(savedInstanceState);
50 
51         @NetworkPreference final int networkPreference = getArguments().getInt(ARG_NETWORK_PREFERENCE);
52 
53         return new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
54                 .setMessage(getResources().getString(R.string.start_rotation_error_message))
55                 .setPositiveButton(com.android.wallpaper.R.string.try_again,
56                         new DialogInterface.OnClickListener() {
57                             @Override
58                             public void onClick(DialogInterface dialogInterface, int i) {
59                                 Listener callback = (Listener) getTargetFragment();
60                                 callback.retryStartRotation(networkPreference);
61                             }
62                         }
63                 )
64                 .setNegativeButton(android.R.string.cancel, null)
65                 .create();
66     }
67 
68     /**
69      * Interface which clients of this DialogFragment should implement in order to handle user actions
70      * on the dialog's buttons.
71      */
72     public interface Listener {
73         void retryStartRotation(@NetworkPreference int networkPreference);
74     }
75 }
76