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