-
Notifications
You must be signed in to change notification settings - Fork 12
/
PermissionUtils.java
210 lines (167 loc) · 6.61 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package droidmentor.permissionexample;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Jaison on 25/08/16.
*/
public class PermissionUtils
{
Context context;
Activity current_activity;
PermissionResultCallback permissionResultCallback;
ArrayList<String> permission_list=new ArrayList<>();
ArrayList<String> listPermissionsNeeded=new ArrayList<>();
String dialog_content="";
int req_code;
public PermissionUtils(Context context)
{
this.context=context;
this.current_activity= (Activity) context;
permissionResultCallback= (PermissionResultCallback) context;
}
/**
* Check the API Level & Permission
*
* @param permissions
* @param dialog_content
* @param request_code
*/
public void check_permission(ArrayList<String> permissions, String dialog_content, int request_code)
{
this.permission_list=permissions;
this.dialog_content=dialog_content;
this.req_code=request_code;
if(Build.VERSION.SDK_INT >= 23)
{
if (checkAndRequestPermissions(permissions, request_code))
{
permissionResultCallback.PermissionGranted(request_code);
Log.i("all permissions", "granted");
Log.i("proceed", "to callback");
}
}
else
{
permissionResultCallback.PermissionGranted(request_code);
Log.i("all permissions", "granted");
Log.i("proceed", "to callback");
}
}
/**
* Check and request the Permissions
*
* @param permissions
* @param request_code
* @return
*/
private boolean checkAndRequestPermissions(ArrayList<String> permissions,int request_code) {
if(permissions.size()>0)
{
listPermissionsNeeded = new ArrayList<>();
for(int i=0;i<permissions.size();i++)
{
int hasPermission = ContextCompat.checkSelfPermission(current_activity,permissions.get(i));
if (hasPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(permissions.get(i));
}
}
if (!listPermissionsNeeded.isEmpty())
{
ActivityCompat.requestPermissions(current_activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),request_code);
return false;
}
}
return true;
}
/**
*
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
switch (requestCode)
{
case 1:
if(grantResults.length>0)
{
Map<String, Integer> perms = new HashMap<>();
for (int i = 0; i < permissions.length; i++)
{
perms.put(permissions[i], grantResults[i]);
}
final ArrayList<String> pending_permissions=new ArrayList<>();
for (int i = 0; i < listPermissionsNeeded.size(); i++)
{
if (perms.get(listPermissionsNeeded.get(i)) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(current_activity,listPermissionsNeeded.get(i)))
pending_permissions.add(listPermissionsNeeded.get(i));
else
{
Log.i("Go to settings","and enable permissions");
permissionResultCallback.NeverAskAgain(req_code);
Toast.makeText(current_activity, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
return;
}
}
}
if(pending_permissions.size()>0)
{
showMessageOKCancel(dialog_content,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
check_permission(permission_list,dialog_content,req_code);
break;
case DialogInterface.BUTTON_NEGATIVE:
Log.i("permisson","not fully given");
if(permission_list.size()==pending_permissions.size())
permissionResultCallback.PermissionDenied(req_code);
else
permissionResultCallback.PartialPermissionGranted(req_code,pending_permissions);
break;
}
}
});
}
else
{
Log.i("all","permissions granted");
Log.i("proceed","to next step");
permissionResultCallback.PermissionGranted(req_code);
}
}
break;
}
}
/**
* Explain why the app needs permissions
*
* @param message
* @param okListener
*/
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(current_activity)
.setMessage(message)
.setPositiveButton("Ok", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
}