-
Notifications
You must be signed in to change notification settings - Fork 4
/
PermissionUtils.java
70 lines (65 loc) · 2.59 KB
/
PermissionUtils.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
package com.bytesizebit.androidutils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
/******
* Android Utils
* Created by Shahar Barsheshet on 20/06/16.
* bytesizebit@gmail.com
* www.bytesizebit.com
******/
public class PermissionUtils {
public static final int RC_CAMERA_PERM = 10;
public static final int RC_LOCATION_PERM = 11;
public static final int RC_MIC_PERM = 12;
public static final int RC_CALL_PERM = 13;
public static final int RC_CONTACT_PERM = 14;
/**
* Open the app settings to enable permissions
*
* @param context the calling Activity or Fragment.
*/
public static void openPermissionsSettings(Context context) {
if (context == null) {
return;
}
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
/**
* Check if user denied permissions with the flag NEVER ASK AGAIN.
*
* @param object the calling Activity or Fragment.
* @param deniedPerms the set of denied permissions.
* @return {@code true} if user denied at least one permission with the flag NEVER ASK AGAIN.
*/
public static boolean checkDeniedPermissionsNeverAskAgain(Object object, String deniedPerms) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean shouldShowRationale;
shouldShowRationale = shouldShowRequestPermissionRationale(object, deniedPerms);
return !shouldShowRationale;
}
return false;
}
@TargetApi(23)
private static boolean shouldShowRequestPermissionRationale(Object object, String perm) {
if (object instanceof Activity) {
return ActivityCompat.shouldShowRequestPermissionRationale((Activity) object, perm);
} else if (object instanceof Fragment) {
return ((Fragment) object).shouldShowRequestPermissionRationale(perm);
} else
return object instanceof android.app.Fragment && ((android.app.Fragment) object).shouldShowRequestPermissionRationale(perm);
}
}