blob: 89a7d02c9f5c4285ee99918581cdbfee6cac1358 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.picker;
17
18import android.app.Activity;
19import android.app.Dialog;
20import android.content.DialogInterface;
21import android.os.Bundle;
Jon Miranda16ea1b12017-12-12 14:52:48 -080022
23import com.android.wallpaper.R;
24import com.android.wallpaper.module.WallpaperPersister.Destination;
25
Sunny Goyal8600a3f2018-08-15 12:48:01 -070026import androidx.appcompat.app.AlertDialog;
27import androidx.fragment.app.DialogFragment;
28import androidx.fragment.app.Fragment;
29
Jon Miranda16ea1b12017-12-12 14:52:48 -080030/**
31 * Dialog fragment which communicates a message that setting the wallpaper failed with an option to
32 * try again.
33 */
34public class SetWallpaperErrorDialogFragment extends DialogFragment {
35
36 private static final String ARG_MESSAGE = "message";
37 private static final String ARG_WALLPAPER_DESTINATION = "destination";
38
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
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}