permissionsList = new ArrayList<>();
185 |
186 | for (SinglePermission singlePermission : notGrantedPermissionsList) {
187 |
188 | permissionsList.add(singlePermission.getPermissionName());
189 | }
190 |
191 | if (fragment != null) {
192 |
193 | fragment.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), mRequestCode);
194 | } else {
195 |
196 | activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), mRequestCode);
197 | }
198 | }
199 |
200 | } catch (NullPointerException e) {
201 |
202 | Log.e(TAG, "The activity or fragment is null", e);
203 | }
204 |
205 | } else {
206 | //if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ) {
207 | mGrantAction.call(0);
208 | //}
209 |
210 | }
211 | }
212 |
213 | /**
214 | * Called if all the permissions were granted
215 | *
216 | * @param grantAction the grant action
217 | * @return the permission builder
218 | */
219 | public PermissionBuilder onPermissionsGranted(OnGrantAction grantAction) {
220 | mGrantAction = grantAction;
221 |
222 | return this;
223 | }
224 |
225 | /**
226 | * Called if there is at least one denied permission
227 | *
228 | * @param denyAction the deny action
229 | * @return the permission builder
230 | */
231 | public PermissionBuilder onPermissionsDenied(OnDenyAction denyAction) {
232 | mDenyAction = denyAction;
233 |
234 | return this;
235 | }
236 |
237 |
238 | /**
239 | * On rationale dismissed permission builder.
240 | *
241 | * @param onRationaleDismissedAction the on rationale dismissed action
242 | * @return the permission builder
243 | */
244 | public PermissionBuilder onRationaleDismissed(OnRationaleDismissedAction onRationaleDismissedAction) {
245 | mRationaleDismissedAction = onRationaleDismissedAction;
246 |
247 | return this;
248 | }
249 |
250 | /**
251 | * One approach you might use is to provide an explanation only if the user
252 | * has already turned down that permission build. If a user keeps trying
253 | * to use functionality that requires a permission, but keeps turning down
254 | * the permission build, that probably shows that the user doesn't understand
255 | * why the app needs the permission to provide that functionality.
256 | * In a situation like that, it's probably a good idea to show an explanation.
257 | *
258 | * @param titleResx the title resx
259 | * @param descriptionResx the description resx
260 | * @param styleResx the style resx
261 | */
262 | public void showRational(int titleResx, int descriptionResx, int styleResx) {
263 | int mTitle = titleResx != 0 ? titleResx : com.firetrap.permissionhelper.R.string.rationale_title;
264 |
265 | DialogHelper.showAlertDialog((Activity) context, mTitle, descriptionResx, com.firetrap.permissionhelper.R.string.rationale_retry, rationale_dismiss, new DialogInterface.OnClickListener() {
266 | @Override
267 | public void onClick(DialogInterface dialog, int which) {
268 | retry();
269 | }
270 | }, new DialogInterface.OnClickListener() {
271 | @Override
272 | public void onClick(DialogInterface dialogInterface, int i) {
273 | dialogInterface.dismiss();
274 | mRationaleDismissedAction.call();
275 | }
276 | }, styleResx);
277 | }
278 |
279 | /**
280 | * This Method should be called from {@link AppCompatActivity#onRequestPermissionsResult(int, String[], int[])
281 | * onRequestPermissionsResult}** with all the same incoming operands
282 | *
283 | * {@code
284 | *
285 | * public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
286 | * if (mStoragePermissionRequest != null)
287 | * mStoragePermissionRequest.onRequestPermissionsResult(requestCode, permissions,grantResults);
288 | * }
289 | * }**
290 | *
291 | *
292 | * @param requestCode the request code
293 | * @param permissions the permissions
294 | * @param grantResults the grant results
295 | */
296 | public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
297 | if (mRequestCode == requestCode) {
298 |
299 | for (int i = 0; i < permissions.length; i++) {
300 |
301 | if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
302 |
303 | boolean shouldShowRational = ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, notGrantedPermissionsList.get(i).getPermissionName());
304 | if (mDenyAction != null) {
305 |
306 | if (BuildConfig.DEBUG) Log.i(TAG, "Calling Deny action");
307 |
308 | mDenyAction.call(requestCode, shouldShowRational);
309 | } else {
310 |
311 | Log.i(TAG, "Error calling OnDenyAction, had you implemented OnDenyAction?");
312 | }
313 | // Terminate if there is at least one deny
314 | return;
315 | }
316 | }
317 |
318 | // there has not been any deny
319 | if (mGrantAction != null) {
320 |
321 | mGrantAction.call(requestCode);
322 | } else {
323 |
324 | Log.i(TAG, "Error calling onGrantAction, had you implemented onGrantAction?");
325 | }
326 |
327 | }
328 | }
329 | }
330 | }
331 |
332 |
--------------------------------------------------------------------------------