1 /*
2  * Copyright (C) 2014 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.example.android.permissionrequest;
17 
18 import android.app.AlertDialog;
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.text.TextUtils;
24 
25 /**
26  * Prompts the user to confirm permission request.
27  */
28 public class ConfirmationDialogFragment extends DialogFragment {
29 
30     private static final String ARG_RESOURCES = "resources";
31 
32     /**
33      * Creates a new instance of ConfirmationDialogFragment.
34      *
35      * @param resources The list of resources requested by PermissionRequeste.
36      * @return A new instance.
37      */
newInstance(String[] resources)38     public static ConfirmationDialogFragment newInstance(String[] resources) {
39         ConfirmationDialogFragment fragment = new ConfirmationDialogFragment();
40         Bundle args = new Bundle();
41         args.putStringArray(ARG_RESOURCES, resources);
42         fragment.setArguments(args);
43         return fragment;
44     }
45 
46     @Override
onCreateDialog(Bundle savedInstanceState)47     public Dialog onCreateDialog(Bundle savedInstanceState) {
48         String[] resources = getArguments().getStringArray(ARG_RESOURCES);
49         return new AlertDialog.Builder(getActivity())
50                 .setMessage(getString(R.string.confirmation, TextUtils.join("\n", resources)))
51                 .setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
52                     @Override
53                     public void onClick(DialogInterface dialog, int which) {
54                         ((Listener) getParentFragment()).onConfirmation(false);
55                     }
56                 })
57                 .setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
58                     @Override
59                     public void onClick(DialogInterface dialog, int which) {
60                         ((Listener) getParentFragment()).onConfirmation(true);
61                     }
62                 })
63                 .create();
64     }
65 
66     /**
67      * Callback for the user's response.
68      */
69     public interface Listener {
70 
71         /**
72          * Called when the PermissoinRequest is allowed or denied by the user.
73          *
74          * @param allowed True if the user allowed the request.
75          */
76         public void onConfirmation(boolean allowed);
77     }
78 
79 }
80