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 validateUserId(int userId)29 public static void validateUserId(int userId) { 30 if (userId != UserHandle.getCallingUserId()) { 31 throw new SecurityException("userId passed in is not the calling user."); 32 } 33 } 34 enforceAdminPermissions(Context context)35 public static void enforceAdminPermissions(Context context) { 36 context.enforceCallingOrSelfPermission(ADMIN_PERM, ADMIN_PERM_ERROR); 37 } 38 39 enforceUserPermissions(Context context)40 public static void enforceUserPermissions(Context context) { 41 context.enforceCallingOrSelfPermission(NFC_PERMISSION, NFC_PERM_ERROR); 42 } 43 enforcePreferredPaymentInfoPermissions(Context context)44 public static void enforcePreferredPaymentInfoPermissions(Context context) { 45 context.enforceCallingOrSelfPermission(NFC_PREFERRED_PAYMENT_INFO_PERMISSION, 46 NFC_PREFERRED_PAYMENT_INFO_PERM_ERROR); 47 } 48 } 49