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