File indexing completed on 2024-12-22 04:41:42

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 package org.kde.kdeconnect.UserInterface;
0008 
0009 import android.os.Bundle;
0010 
0011 import androidx.annotation.Nullable;
0012 import androidx.core.app.ActivityCompat;
0013 
0014 import org.kde.kdeconnect_tp.R;
0015 
0016 public class PermissionsAlertDialogFragment extends AlertDialogFragment {
0017     private static final String KEY_PERMISSIONS = "Permissions";
0018     private static final String KEY_REQUEST_CODE = "RequestCode";
0019 
0020     private String[] permissions;
0021     private int requestCode;
0022 
0023     public PermissionsAlertDialogFragment() {
0024     }
0025 
0026     @Override
0027     public void onCreate(@Nullable Bundle savedInstanceState) {
0028         super.onCreate(savedInstanceState);
0029 
0030         Bundle args = getArguments();
0031 
0032         if (args == null || !args.containsKey(KEY_PERMISSIONS)) {
0033             throw new RuntimeException("You must call Builder.setPermission() to set the array of needed permissions");
0034         }
0035 
0036         permissions = args.getStringArray(KEY_PERMISSIONS);
0037         requestCode = args.getInt(KEY_REQUEST_CODE, 0);
0038 
0039         setCallback(new Callback() {
0040             @Override
0041             public void onPositiveButtonClicked() {
0042                 ActivityCompat.requestPermissions(requireActivity(), permissions, requestCode);
0043             }
0044         });
0045     }
0046 
0047     public static class Builder extends AlertDialogFragment.AbstractBuilder<Builder, PermissionsAlertDialogFragment> {
0048 
0049         public Builder() {
0050             setPositiveButton(R.string.ok);
0051             setNegativeButton(R.string.cancel);
0052         }
0053 
0054         @Override
0055         public Builder getThis() {
0056             return this;
0057         }
0058 
0059         public Builder setPermissions(String[] permissions) {
0060             args.putStringArray(KEY_PERMISSIONS, permissions);
0061 
0062             return getThis();
0063         }
0064 
0065         public Builder setRequestCode(int requestCode) {
0066             args.putInt(KEY_REQUEST_CODE, requestCode);
0067 
0068             return getThis();
0069         }
0070 
0071         @Override
0072         protected PermissionsAlertDialogFragment createFragment() {
0073             return new PermissionsAlertDialogFragment();
0074         }
0075     }
0076 }