1 package com.android.nfc; 2 3 4 import android.content.Context; 5 import android.os.UserHandle; 6 7 public class NfcPermissions { 8 9 /** 10 * NFC ADMIN permission - only for system apps 11 */ 12 private static final String ADMIN_PERM = android.Manifest.permission.WRITE_SECURE_SETTINGS; 13 private static final String ADMIN_PERM_ERROR = "WRITE_SECURE_SETTINGS permission required"; 14 15 /** 16 * Regular NFC permission 17 */ 18 static final String NFC_PERMISSION = android.Manifest.permission.NFC; 19 private static final String NFC_PERM_ERROR = "NFC permission required"; 20 21 /** 22 * NFC PREFERRED PAYMENT INFO permission 23 */ 24 static final String NFC_PREFERRED_PAYMENT_INFO_PERMISSION = 25 android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO; 26 private static final String NFC_PREFERRED_PAYMENT_INFO_PERM_ERROR = 27 "NFC_PREFERRED_PAYMENT_INFO permission required"; 28 29 /** 30 * NFC SET CONTROLLER ALWAYS ON permission 31 */ 32 static final String NFC_SET_CONTROLLER_ALWAYS_ON = 33 android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON; 34 private static final String NFC_SET_CONTROLLER_ALWAYS_ON_ERROR = 35 "NFC_SET_CONTROLLER_ALWAYS_ON permission required"; 36 validateUserId(int userId)37 public static void validateUserId(int userId) { 38 if (userId != UserHandle.getCallingUserId()) { 39 throw new SecurityException("userId passed in is not the calling user."); 40 } 41 } 42 enforceAdminPermissions(Context context)43 public static void enforceAdminPermissions(Context context) { 44 context.enforceCallingOrSelfPermission(ADMIN_PERM, ADMIN_PERM_ERROR); 45 } 46 47 enforceUserPermissions(Context context)48 public static void enforceUserPermissions(Context context) { 49 context.enforceCallingOrSelfPermission(NFC_PERMISSION, NFC_PERM_ERROR); 50 } 51 enforcePreferredPaymentInfoPermissions(Context context)52 public static void enforcePreferredPaymentInfoPermissions(Context context) { 53 context.enforceCallingOrSelfPermission(NFC_PREFERRED_PAYMENT_INFO_PERMISSION, 54 NFC_PREFERRED_PAYMENT_INFO_PERM_ERROR); 55 } 56 57 /** 58 * Permission check for android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON 59 */ enforceSetControllerAlwaysOnPermissions(Context context)60 public static void enforceSetControllerAlwaysOnPermissions(Context context) { 61 context.enforceCallingOrSelfPermission(NFC_SET_CONTROLLER_ALWAYS_ON, 62 NFC_SET_CONTROLLER_ALWAYS_ON_ERROR); 63 } 64 } 65