-
Notifications
You must be signed in to change notification settings - Fork 1
/
PermissionHelper.java
127 lines (100 loc) · 4.26 KB
/
PermissionHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.tf.reusable.demoapplication.util;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
/**
* Created by kamran on 8/28/16.
*/
public class PermissionHelper {
private static final int PERMISSION_REQUEST_CODE = 0;
public static void askForAllPermissions(Activity activity) {
try {
String[] permissions = getRequiredPermissions(activity);
ArrayList<String> permissionsToBeAsked = new ArrayList<>();
if (permissions != null) {
for (String permission : permissions) {
if (!isPermissionGranted(activity, permission) && isDangerous(activity, permission)) {
permissionsToBeAsked.add(permission);
}
}
}
if (permissionsToBeAsked.size() > 0) {
askForPermission(activity, permissionsToBeAsked.toArray(new String[]{}));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
private static String[] getRequiredPermissions(Context context) throws PackageManager.NameNotFoundException {
return context.getPackageManager()
.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS)
.requestedPermissions;
}
public static boolean isPermissionGranted(Context context, String permission) {
return Build.VERSION.SDK_INT <= 22
|| ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
public static boolean areAllPermissionsGranted(Context context) {
boolean areGranted = true;
try {
String[] permissions = getRequiredPermissions(context);
for (String permission : permissions) {
areGranted = areGranted
&& (!isDangerous(context, permission) || isPermissionGranted(context, permission));
}
} catch (PackageManager.NameNotFoundException e) {
Logger.printStackTrace(e);
return false;
}
return areGranted;
}
public static void askForPermission(Activity activity, String permissions) {
askForPermission(activity, new String[]{permissions});
}
public static void askForPermission(Activity activity, String... permissions) {
if (Build.VERSION.SDK_INT >= 22) {
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
}
}
private static boolean isDangerous(Context context, String permission) {
boolean isDangerous = false;
try {
isDangerous = context.getPackageManager().getPermissionInfo(permission, 0).protectionLevel
== PermissionInfo.PROTECTION_DANGEROUS;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return isDangerous;
}
public static boolean isAnyPermissionDeniedByUser(Activity activity) {
boolean isChecked = false;
if (Build.VERSION.SDK_INT >= 22) {
return false;
}
try {
String[] requiredPermissions = getRequiredPermissions(activity);
for (String requiredPermission : requiredPermissions) {
if (isDangerous(activity, requiredPermission)) {
isChecked = isChecked || activity.shouldShowRequestPermissionRationale(requiredPermission);
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return isChecked;
}
public static void showPermissionSettings(Activity activity, int code) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
activity.startActivityForResult(intent, code);
}
}