summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/util
diff options
context:
space:
mode:
authorYorke Lee <yorkelee@google.com>2015-07-17 15:25:32 -0700
committerYorke Lee <yorkelee@google.com>2015-07-17 15:31:03 -0700
commitf33d1a718271e2eecd1316d88f5727be8a6da455 (patch)
tree66905caddba141fef72329e75e06625bbb788d39 /src/com/android/contacts/common/util
parent51f53811614d9185c8de800ed2403e9f3245b581 (diff)
downloadpackages_apps_ContactsCommon-f33d1a718271e2eecd1316d88f5727be8a6da455.tar.gz
packages_apps_ContactsCommon-f33d1a718271e2eecd1316d88f5727be8a6da455.tar.bz2
packages_apps_ContactsCommon-f33d1a718271e2eecd1316d88f5727be8a6da455.zip
Add utility methods to listen for permission granted broadcasts
Bug: 22174668 Change-Id: I527bec3e607b0677c587c704333216ecdabe7558
Diffstat (limited to 'src/com/android/contacts/common/util')
-rw-r--r--src/com/android/contacts/common/util/PermissionsUtil.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/com/android/contacts/common/util/PermissionsUtil.java b/src/com/android/contacts/common/util/PermissionsUtil.java
index 07480e31..43d09177 100644
--- a/src/com/android/contacts/common/util/PermissionsUtil.java
+++ b/src/com/android/contacts/common/util/PermissionsUtil.java
@@ -17,8 +17,12 @@
package com.android.contacts.common.util;
import android.Manifest.permission;
+import android.content.BroadcastReceiver;
import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.content.pm.PackageManager;
+import android.support.v4.content.LocalBroadcastManager;
/**
* Utility class to help with runtime permissions.
@@ -48,4 +52,45 @@ public class PermissionsUtil {
public static boolean hasPermission(Context context, String permission) {
return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
+
+ /**
+ * Rudimentary methods wrapping the use of a LocalBroadcastManager to simplify the process
+ * of notifying other classes when a particular fragment is notified that a permission is
+ * granted.
+ *
+ * To be notified when a permission has been granted, create a new broadcast receiver
+ * and register it using {@link #registerPermissionReceiver(Context, BroadcastReceiver, String)}
+ *
+ * E.g.
+ *
+ * final BroadcastReceiver receiver = new BroadcastReceiver() {
+ * @Override
+ * public void onReceive(Context context, Intent intent) {
+ * refreshContactsView();
+ * }
+ * }
+ *
+ * PermissionsUtil.registerPermissionReceiver(getActivity(), receiver, READ_CONTACTS);
+ *
+ * If you register to listen for multiple permissions, you can identify which permission was
+ * granted by inspecting {@link Intent#getAction()}.
+ *
+ * In the fragment that requests for the permission, be sure to call
+ * {@link #notifyPermissionGranted(Context, String)} when the permission is granted so that
+ * any interested listeners are notified of the change.
+ */
+ public static void registerPermissionReceiver(Context context, BroadcastReceiver receiver,
+ String permission) {
+ final IntentFilter filter = new IntentFilter(permission);
+ LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter);
+ }
+
+ public static void unregisterPermissionReceiver(Context context, BroadcastReceiver receiver) {
+ LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver);
+ }
+
+ public static void notifyPermissionGranted(Context context, String permission) {
+ final Intent intent = new Intent(permission);
+ LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+ }
}