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.os.Bundle;
21 
22 import com.android.wallpaper.R;
23 
24 import androidx.annotation.NonNull;
25 import androidx.appcompat.app.AlertDialog;
26 import androidx.fragment.app.DialogFragment;
27 
28 /**
29  * Dialog fragment which communicates a message that loading the wallpaper failed with an OK button,
30  * when clicked will navigate the user back to the previous activity.
31  */
32 public class LoadWallpaperErrorDialogFragment extends DialogFragment {
33 
newInstance()34     public static LoadWallpaperErrorDialogFragment newInstance() {
35         return new LoadWallpaperErrorDialogFragment();
36     }
37 
38     @Override
39     @NonNull
onCreateDialog(Bundle savedInstanceState)40     public Dialog onCreateDialog(Bundle savedInstanceState) {
41         super.onCreateDialog(savedInstanceState);
42 
43         return new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme)
44                 .setMessage(R.string.load_wallpaper_error_message)
45                 .setPositiveButton(android.R.string.ok,
46                         new DialogInterface.OnClickListener() {
47                             @Override
48                             public void onClick(DialogInterface dialogInterface, int i) {
49                                 Listener callback = (Listener) getTargetFragment();
50                                 callback.onClickOk();
51                             }
52                         }
53                 )
54                 .create();
55     }
56 
57     @Override
58     public void onDismiss(DialogInterface dialogInterface) {
59         super.onDismiss(dialogInterface);
60 
61         // Treat a dismissal by user click outside the dialog foreground the same as the user clicking
62         // "OK" to dismiss the dialog.
63         Listener callback = (Listener) getTargetFragment();
64         callback.onClickOk();
65     }
66 
67     /**
68      * Interface which clients of this DialogFragment should implement in order to handle user actions
69      * on the dialog's buttons.
70      */
71     public interface Listener {
72         void onClickOk();
73     }
74 }
75