Bug 1241887 - (Part 1) Allow to use Permission class with every Context object. r=nalexander
--- a/mobile/android/base/java/org/mozilla/gecko/permissions/PermissionBlock.java
+++ b/mobile/android/base/java/org/mozilla/gecko/permissions/PermissionBlock.java
@@ -12,25 +12,25 @@ import android.content.Context;
import android.support.annotation.NonNull;
/**
* Helper class to run code blocks depending on whether a user has granted or denied certain runtime permissions.
*/
public class PermissionBlock {
private final PermissionsHelper helper;
- private Activity activity;
+ private Context context;
private String[] permissions;
private boolean onUIThread;
private Runnable onPermissionsGranted;
private Runnable onPermissionsDenied;
private boolean doNotPrompt;
- /* package-private */ PermissionBlock(Activity activity, PermissionsHelper helper) {
- this.activity = activity;
+ /* package-private */ PermissionBlock(Context context, PermissionsHelper helper) {
+ this.context = context;
this.helper = helper;
}
/**
* Determine whether the app has been granted the specified permissions.
*/
public PermissionBlock withPermissions(@NonNull String... permissions) {
this.permissions = permissions;
@@ -65,28 +65,32 @@ public class PermissionBlock {
return this;
}
/**
* Execute the specified runnable if the app has been granted all permissions. Calling this method will prompt the
* user if needed.
*/
public void run(@NonNull Runnable onPermissionsGranted) {
+ if (!doNotPrompt && !(context instanceof Activity)) {
+ throw new IllegalStateException("You need to either specify doNotPrompt() or pass in an Activity context");
+ }
+
this.onPermissionsGranted = onPermissionsGranted;
- if (hasPermissions(activity)) {
+ if (hasPermissions(context)) {
onPermissionsGranted();
} else if (doNotPrompt) {
onPermissionsDenied();
} else {
- Permissions.prompt(activity, this);
+ Permissions.prompt((Activity) context, this);
}
// This reference is no longer needed. Let's clear it now to avoid memory leaks.
- activity = null;
+ context = null;
}
/**
* Execute this fallback if at least one permission has not been granted.
*/
public PermissionBlock andFallback(@NonNull Runnable onPermissionsDenied) {
this.onPermissionsDenied = onPermissionsDenied;
return this;
--- a/mobile/android/base/java/org/mozilla/gecko/permissions/Permissions.java
+++ b/mobile/android/base/java/org/mozilla/gecko/permissions/Permissions.java
@@ -1,16 +1,17 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.permissions;
import android.app.Activity;
+import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import org.mozilla.gecko.util.ThreadUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -41,19 +42,22 @@ import java.util.concurrent.FutureTask;
public class Permissions {
private static final Queue<PermissionBlock> waiting = new LinkedList<>();
private static final Queue<PermissionBlock> prompt = new LinkedList<>();
private static PermissionsHelper permissionHelper = new PermissionsHelper();
/**
* Entry point for checking (and optionally prompting for) runtime permissions.
+ *
+ * Note: The provided context needs to be an Activity context in order to prompt. Use doNotPrompt()
+ * for all other contexts.
*/
- public static PermissionBlock from(@NonNull Activity activity) {
- return new PermissionBlock(activity, permissionHelper);
+ public static PermissionBlock from(@NonNull Context context) {
+ return new PermissionBlock(context, permissionHelper);
}
/**
* This method will block until the specified permissions have been granted or denied by the user.
* If needed the user will be prompted.
*
* @return true if all of the permissions have been granted. False if any of the permissions have been denied.
*/