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.Activity;
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.module.WallpaperPersister.Destination;
25 
26 import androidx.appcompat.app.AlertDialog;
27 import androidx.fragment.app.DialogFragment;
28 import androidx.fragment.app.Fragment;
29 
30 /**
31  * Dialog fragment which communicates a message that setting the wallpaper failed with an option to
32  * try again.
33  */
34 public class SetWallpaperErrorDialogFragment extends DialogFragment {
35 
36     private static final String ARG_MESSAGE = "message";
37     private static final String ARG_WALLPAPER_DESTINATION = "destination";
38 
newInstance(int messageId, @Destination int wallpaperDestination)39     public static SetWallpaperErrorDialogFragment newInstance(int messageId,
40                                                               @Destination int wallpaperDestination) {
41         SetWallpaperErrorDialogFragment dialogFrag = new SetWallpaperErrorDialogFragment();
42         Bundle args = new Bundle();
43         args.putInt(ARG_MESSAGE, messageId);
44         args.putInt(ARG_WALLPAPER_DESTINATION, wallpaperDestination);
45         dialogFrag.setArguments(args);
46         return dialogFrag;
47     }
48 
49     @Override
onCreateDialog(Bundle savedInstanceState)50     public Dialog onCreateDialog(Bundle savedInstanceState) {
51         super.onCreateDialog(savedInstanceState);
52 
53         int message = getArguments().getInt(ARG_MESSAGE);
54         @Destination final int wallpaperDestination = getArguments().getInt(ARG_WALLPAPER_DESTINATION);
55 
56         return new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
57                 .setMessage(message)
58                 .setPositiveButton(R.string.try_again,
59                         new DialogInterface.OnClickListener() {
60                             @Override
61                             public void onClick(DialogInterface dialogInterface, int i) {
62                                 // The component hosting this DialogFragment could be either a Fragment or an
63                                 // Activity, so check if a target Fragment was explicitly set--if not then the
64                                 // appropriate Listener would be the containing Activity.
65                                 Fragment fragment = getTargetFragment();
66                                 Activity activity = getActivity();
67 
68                                 Listener callback = (Listener) (fragment == null ? activity : fragment);
69 
70                                 if (callback != null) {
71                                     callback.onClickTryAgain(wallpaperDestination);
72                                 }
73                             }
74                         }
75                 )
76                 .setNegativeButton(android.R.string.cancel, null)
77                 .create();
78     }
79 
80     /**
81      * Interface which clients of this DialogFragment should implement in order to handle user actions
82      * on the dialog's buttons.
83      */
84     public interface Listener {
85         void onClickTryAgain(@Destination int wallpaperDestination);
86     }
87 }
88