1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ex.chips; 18 19 import android.Manifest; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.os.Build; 23 import android.os.Process; 24 import android.support.annotation.Nullable; 25 26 public class ChipsUtil { 27 28 /** 29 * Listener that gets notified when we check whether we have permission. 30 */ 31 public interface PermissionsCheckListener { onPermissionCheck(String permission, boolean granted)32 void onPermissionCheck(String permission, boolean granted); 33 } 34 35 /** 36 * Permissions required by Chips library. 37 */ 38 public static final String[] REQUIRED_PERMISSIONS = 39 new String[] { Manifest.permission.READ_CONTACTS }; 40 41 /** 42 * Returns true when the caller can use Chips UI in its environment. 43 */ supportsChipsUi()44 public static boolean supportsChipsUi() { 45 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; 46 } 47 48 /** 49 * Whether we are running on M or later version. 50 * 51 * <p>This is interesting for us because new permission model is introduced in M and we need to 52 * check if we have {@link #REQUIRED_PERMISSIONS}. 53 */ isRunningMOrLater()54 public static boolean isRunningMOrLater() { 55 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; 56 } 57 58 /** 59 * Returns {@link PackageManager#PERMISSION_GRANTED} if given permission is granted, or 60 * {@link PackageManager#PERMISSION_DENIED} if not. 61 */ checkPermission(Context context, String permission)62 public static int checkPermission(Context context, String permission) { 63 if (isRunningMOrLater()) { 64 // TODO: Use "context.checkSelfPermission(permission)" once it's safe to move to M sdk 65 return context.checkPermission(permission, Process.myPid(), Process.myUid()); 66 } else { 67 // Assume that we have permission before M. 68 return PackageManager.PERMISSION_GRANTED; 69 } 70 } 71 72 /** 73 * Returns true if all permissions in {@link #REQUIRED_PERMISSIONS} are granted. 74 * 75 * <p>If {@link PermissionsCheckListener} is specified it will be called for every 76 * {@link #checkPermission} call. 77 */ hasPermissions(Context context, @Nullable PermissionsCheckListener permissionsCheckListener)78 public static boolean hasPermissions(Context context, 79 @Nullable PermissionsCheckListener permissionsCheckListener) { 80 for (String permission : REQUIRED_PERMISSIONS) { 81 final boolean granted = 82 checkPermission(context, permission) == PackageManager.PERMISSION_GRANTED; 83 if (permissionsCheckListener != null) { 84 permissionsCheckListener.onPermissionCheck(permission, granted); 85 } 86 if (!granted) { 87 return false; 88 } 89 } 90 return true; 91 } 92 } 93