Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | package com.android.soundrecorder.util; |
| 31 | |
| 32 | import android.Manifest; |
| 33 | import android.app.Activity; |
| 34 | import android.content.pm.PackageManager; |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 35 | import android.app.AlertDialog; |
| 36 | import android.content.DialogInterface; |
| 37 | import android.content.Intent; |
| 38 | import android.net.Uri; |
| 39 | import android.os.Bundle; |
| 40 | import android.provider.Settings; |
| 41 | import android.util.Log; |
| 42 | |
| 43 | import com.android.soundrecorder.R; |
| 44 | import com.android.soundrecorder.SoundRecorder; |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 45 | |
| 46 | import java.util.ArrayList; |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 47 | import java.util.List; |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 48 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 49 | |
| 50 | public class PermissionUtils extends Activity{ |
| 51 | |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 52 | public enum PermissionType { |
| 53 | RECORD, PLAY |
| 54 | } |
| 55 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 56 | private final static String TAG = "PermissionUtils"; |
| 57 | private static final String PACKAGE_URL_SCHEME = "package:"; |
| 58 | private static final int REQUEST_CODE = 200; |
| 59 | private static final String PERVIOUS_INTENT = "pervious_intent"; |
| 60 | private static final String REQUEST_PERMISSIONS = "request_permissions"; |
| 61 | private String[] mRequestedPermissons; |
| 62 | private Intent mPreviousIntent; |
xiujuan.yuan | ea992c3 | 2022-07-15 10:59:33 +0800 | [diff] [blame] | 63 | private static final String EXIT_AFTER_RECORD = "exit_after_record"; |
| 64 | private static final int RESULT_GRANTED = 99; |
| 65 | private static final int RESULT_NOT_GRANTED = 100; |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 66 | |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 67 | private static String[] RECORD_PERMISSIONS = { |
| 68 | Manifest.permission.READ_PHONE_STATE, Manifest.permission.RECORD_AUDIO, |
hoffc | 4394dc0 | 2018-01-26 09:46:31 +0800 | [diff] [blame] | 69 | Manifest.permission.CAPTURE_AUDIO_OUTPUT, |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 70 | Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE |
| 71 | }; |
| 72 | |
| 73 | private static String[] PLAY_PERMISSIONS = { |
sxia | 4b3f3d4 | 2020-09-17 17:43:14 +0800 | [diff] [blame] | 74 | Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 77 | @Override |
| 78 | protected void onCreate(Bundle savedInstanceState) { |
| 79 | super.onCreate(savedInstanceState); |
| 80 | mPreviousIntent = (Intent)getIntent().getExtras().get(PERVIOUS_INTENT); |
| 81 | mRequestedPermissons = getIntent().getStringArrayExtra(REQUEST_PERMISSIONS); |
| 82 | if (savedInstanceState == null && mRequestedPermissons != null) { |
| 83 | String[] neededPermissions = |
| 84 | checkRequestedPermission(PermissionUtils.this, mRequestedPermissons); |
| 85 | if (neededPermissions != null && neededPermissions.length != 0) { |
| 86 | requestPermissions(neededPermissions, REQUEST_CODE); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 92 | public static String[] getOperationPermissions(PermissionType type) { |
| 93 | switch (type) { |
| 94 | case RECORD: |
| 95 | return RECORD_PERMISSIONS; |
| 96 | case PLAY: |
| 97 | return PLAY_PERMISSIONS; |
| 98 | default: |
| 99 | return null; |
| 100 | } |
| 101 | } |
| 102 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 103 | public static boolean checkAndRequestPermission(Activity activity, String[] permissions) { |
xiujuan.yuan | 1a82128 | 2022-07-18 15:39:38 +0800 | [diff] [blame] | 104 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 105 | String[] neededPermissions = checkRequestedPermission(activity, permissions); |
| 106 | if (neededPermissions.length == 0) { |
| 107 | return false; |
| 108 | } else { |
| 109 | Intent intent = new Intent(); |
| 110 | intent.putExtra(PERVIOUS_INTENT, activity.getIntent()); |
| 111 | intent.putExtra(REQUEST_PERMISSIONS, permissions); |
| 112 | intent.setClass(activity, PermissionUtils.class); |
xiujuan.yuan | ea992c3 | 2022-07-15 10:59:33 +0800 | [diff] [blame] | 113 | activity.startActivityForResult(intent,RESULT_GRANTED); |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private static String[] checkRequestedPermission(Activity activity, String[] permissionName) { |
| 119 | boolean isPermissionGranted = true; |
| 120 | List<String> needRequestPermission = new ArrayList<String>(); |
| 121 | for (String tmp : permissionName) { |
| 122 | isPermissionGranted = (PackageManager.PERMISSION_GRANTED == |
| 123 | activity.checkSelfPermission(tmp)); |
| 124 | if (!isPermissionGranted) { |
| 125 | needRequestPermission.add(tmp); |
| 126 | } |
| 127 | } |
| 128 | String[] needRequestPermissionArray = new String[needRequestPermission.size()]; |
| 129 | needRequestPermission.toArray(needRequestPermissionArray); |
| 130 | return needRequestPermissionArray; |
| 131 | } |
| 132 | |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 133 | public static boolean checkPermissions(final Activity activity, PermissionType type, |
| 134 | int operationHandle) { |
| 135 | String[] permissions = getOperationPermissions(type); |
| 136 | return checkPermissions(activity, permissions, operationHandle); |
| 137 | } |
| 138 | |
| 139 | public static boolean checkPermissions(final Activity activity, String[] permissions, |
| 140 | int operationHandle) { |
| 141 | if (permissions == null) |
| 142 | return true; |
| 143 | boolean isPermissionGranted = true; |
| 144 | ArrayList<String> permissionList = new ArrayList<String>(); |
| 145 | for (String permission : permissions) { |
| 146 | if (PackageManager.PERMISSION_GRANTED != activity.checkSelfPermission(permission)) { |
| 147 | permissionList.add(permission); |
| 148 | isPermissionGranted = false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!isPermissionGranted) { |
| 153 | String[] permissionArray = new String[permissionList.size()]; |
| 154 | permissionList.toArray(permissionArray); |
| 155 | activity.requestPermissions(permissionArray, operationHandle); |
| 156 | } |
| 157 | |
| 158 | return isPermissionGranted; |
| 159 | } |
| 160 | |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 161 | |
| 162 | @Override |
| 163 | public void onRequestPermissionsResult(int requestCode, String[] permissions, |
| 164 | int[] grantResults) { |
| 165 | boolean isAllPermissionsGranted = true; |
| 166 | if (requestCode != REQUEST_CODE || permissions == null |
| 167 | || grantResults == null || |
| 168 | permissions.length == 0 || grantResults.length == 0) { |
| 169 | isAllPermissionsGranted = false; |
| 170 | } else { |
| 171 | for (int i : grantResults) { |
| 172 | if (i != PackageManager.PERMISSION_GRANTED) |
| 173 | isAllPermissionsGranted = false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if(isAllPermissionsGranted) { |
xiujuan.yuan | ea992c3 | 2022-07-15 10:59:33 +0800 | [diff] [blame] | 178 | setResult(RESULT_GRANTED); |
| 179 | finish(); |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 180 | } else { |
| 181 | showMissingPermissionDialog(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private void showMissingPermissionDialog() { |
| 186 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 187 | |
| 188 | builder.setMessage(R.string.dialog_content); |
| 189 | builder.setTitle(R.string.dialog_title_help); |
| 190 | |
| 191 | builder.setPositiveButton(R.string.dialog_button_settings, |
| 192 | new DialogInterface.OnClickListener() { |
| 193 | @Override |
| 194 | public void onClick(DialogInterface dialog, int which) { |
| 195 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 196 | intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName())); |
| 197 | startActivity(intent); |
| 198 | finish(); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | builder.setNegativeButton(R.string.dialog_button_quit, |
| 203 | new DialogInterface.OnClickListener() { |
| 204 | @Override |
| 205 | public void onClick(DialogInterface dialog, int which) { |
xiujuan.yuan | ea992c3 | 2022-07-15 10:59:33 +0800 | [diff] [blame] | 206 | setResult(RESULT_NOT_GRANTED); |
sxia | b1bad73 | 2021-11-04 15:05:07 +0800 | [diff] [blame] | 207 | finish(); |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | builder.show(); |
| 212 | } |
| 213 | |
Chao Zhang | 3be696e | 2016-05-25 12:29:44 +0800 | [diff] [blame] | 214 | public static boolean checkPermissionResult(String[] permissions, int[] grantResults) { |
| 215 | if (permissions == null || grantResults == null || permissions.length == 0 |
| 216 | || grantResults.length == 0) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | for (int result : grantResults) { |
| 221 | if (result != PackageManager.PERMISSION_GRANTED) { |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | } |