Android系统移植与调试之------->如何修改Android手机NFC模块,使黑屏时候能够使用NFC
2015-10-19 20:31
1001 查看
我们都知道在不修改源代码的情况下,只能是解锁之后才能使用NFC功能。而在锁屏和黑屏2个状态下是没办法用NFC的,但是最近有个客户要求手机在黑屏状态下能够使用NFC,因此我们需要去修改Android源代码关于NFC模块。
最开始可以通过查看分析源代码,找到到NfcService的相关代码,如下: packages\apps\Nfc\src\com\android\nfc\NfcService.java
找到186行,这句是定义NFC能够使用的屏幕最小状态
// minimum screen state that enables NFC polling
static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
这几个状态分别是:
SCREEN_STATE_OFF黑屏状态
SCREEN_STATE_ON_LOCKED屏幕亮了,但是是锁屏状态
SCREEN_STATE_ON_UNLOCKED 屏幕亮了,并且是解锁状态
代码定义如下,在packages\apps\Nfc\src\com\android\nfc\ScreenStateHelper中定义
static final int SCREEN_STATE_UNKNOWN = 0;
static final int SCREEN_STATE_OFF = 1;
static final int SCREEN_STATE_ON_LOCKED = 2;
static final int SCREEN_STATE_ON_UNLOCKED = 3;
上面的这个最小状态在NfcService.java的第1706行,computeDiscoveryParameters(int screenState)方法中被调用,用来判断的,方法代码如下:
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
if (screenState >= NFC_POLLING_MODE) {//这里被调用
// Check if reader-mode is enabled
if (mReaderModeParams != null) {
int techMask = 0;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_A) != 0)
techMask |= NFC_POLL_A;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_B) != 0)
techMask |= NFC_POLL_B;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_F) != 0)
techMask |= NFC_POLL_F;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_V) != 0)
techMask |= NFC_POLL_ISO15693;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_BARCODE) != 0)
techMask |= NFC_POLL_KOVIO;
Log.d(TAG, " mReaderModeParams != null paramsBuilder.setTechMask:"+techMask);
paramsBuilder.setTechMask(techMask);
paramsBuilder.setEnableReaderMode(true);
} else {
Log.d(TAG, " mReaderModeParams == null paramsBuilder.setTechMask:"+NfcDiscoveryParameters.NFC_POLL_DEFAULT +" NFC_POLL_DEFAULT");
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
paramsBuilder.setEnableP2p(mIsNdefPushEnabled);
}
} else if (screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED && mInProvisionMode) {
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
// enable P2P for MFM/EDU/Corp provisioning
paramsBuilder.setEnableP2p(true);
} else if (screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mNfcUnlockManager.isLockscreenPollingEnabled()) {
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED setTechMask ");
// For lock-screen tags, no low-power polling
paramsBuilder.setTechMask(mNfcUnlockManager.getLockscreenPollMask());
paramsBuilder.setEnableLowPowerDiscovery(false);
paramsBuilder.setEnableP2p(false);
}
if (mIsHceCapable && mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED) {
// Host routing is always enabled at lock screen or later
Log.d(TAG, " >= SCREEN_STATE_ON_LOCKED paramsBuilder.setEnableHostRouting(true) ");
paramsBuilder.setEnableHostRouting(true);
}
return paramsBuilder.build();
}因此如果要改成黑屏状态下可以使用NFC的话,只要将变量NFC_POLLING_MODE改成
ScreenStateHelper.SCREEN_STATE_OFF即可,代码如下:
// minimum screen state that enables NFC polling
//static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_OFF;
但是这样的话,手机会一直不休眠,观察电池的电流和电压发现,一直在跳动,这样在黑屏状态下,手机不会休眠,会很耗电,因此还要优化。
客户的要求是:当双击物理按键Camera键的时候,可以在黑屏状态下使用NFC十分钟,十分钟之类,差不多关于NFC的工作完成了,之后将状态改回来,即:只能在解锁状态下使用NFC,这样的话就可以黑屏使用NFC又节电。
因此,思路如下:
1、接收物理按键Camera键发送的广播,来判断是双击,并将NFC_POLLING_MODE的最小模式改为ScreenStateHelper.SCREEN_STATE_OFF。
2、需要写一个定时器来处理十分钟之后将NFC_POLLING_MODE的最小模式改为会原来的ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED。
因此,首先先定义几个常量,从第185行static final int NFC_POLLING_MODE= ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;处开始修改,修改代码如下:
// minimum screen state that enables NFC polling
// edited by ouyang [2015-10-19] start
// static final int NFC_POLLING_MODE= ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
//默认为要解锁才能使用NFC
static final int NFC_POLLING_MODE_DEFALUT = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
//在黑屏情况下也可以使用NFC
static final int NFC_POLLING_MODE_SCREEN_OFF = ScreenStateHelper.SCREEN_STATE_OFF;
//默认能使用NFC时的屏幕状态
static int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
public static int getNFC_POLLING_MODE() {
return NFC_POLLING_MODE;
}
public static void setNFC_POLLING_MODE(int mNFC_POLLING_MODE) {
NFC_POLLING_MODE = mNFC_POLLING_MODE;
}
//是否是双击Camera键
static boolean isDoublePress=false;
//从黑屏可用NFC恢复到要解锁才能用NFC的时间
static final int TIME_TO_Restore_Default_Values=(60*1000)*10;//10分钟 10*1000*60
// edited by ouyang [2015-10-19] end
第二步:写一个广播接收者来处理物理按键Camera,按下和松开时发出的广播。
因为是要判断双击Camera,所以这里只要接收松开Camera键时发出的广播即可。这个广播是公司自己定义的,定义的广播为:"com.runbo.camera.key.up"。所以现在处理这个广播。因为代码中本来就动态注册了一个广播接收者,因此我们在这个广播接收者种再注册一个Intent即可。代码如下:在第450行
// Intents for all users
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_USER_SWITCHED);
//added by ouyang start [2015-10-19]
//Camera物理键按下后松开 发出的广播
filter.addAction("com.runbo.camera.key.up");
//added by ouyang end [2015-10-19]
registerForAirplaneMode(filter);
mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null);
这样我们处理这个双击Camera键可以在mReceiver中处理了,在mReceiver中的onReceive方法中,判断action是否是"com.runbo.camera.key.up",2295行代码如下:
//added by ouyang start [2015-10-19] Camera物理键按下后松开
else if (action.equals("com.runbo.camera.key.up")) {
Log.d("oyp", "<----com.runbo.camera.key.up---->");
Handler checkHandler=new Handler();
Handler restoreHandler=new Handler();
//单击
if (!isDoublePress) {
isDoublePress=true;//500ms之类再单击Camera键的话,就是双击了,直接进入 else语句块
//500ms后触发该线程,查看是单击还是双击
Runnable CheckDoubleRunnable=new Runnable(){
@Override
public void run() {
if (isDoublePress) {
Log.i("oyp", "<----Single Press the Camera Key---->"); //显示为单击
}else{
Log.i("oyp", "<----Double Press the Camera Key---->"); //显示为双击
}
isDoublePress=false;//500ms后在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
}
};
checkHandler.postDelayed(CheckDoubleRunnable, 500);// 500ms内两次单击,触发双击
}
// 500ms内两次单击,触发双击
else{
isDoublePress=false;//双击后,将该值设为false,下次在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
//设置在屏幕关闭情况下仍然可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_SCREEN_OFF);
applyRouting(true);
Log.d("oyp", "2、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
//10分钟后触发该线程,恢复原来值
Runnable RestoreDefaultValues=new Runnable(){
@Override
public void run() {
//设置在屏幕解锁情况下可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
applyRouting(true);
Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
}
};
restoreHandler.removeCallbacks(RestoreDefaultValues);//先取消定时器
restoreHandler.postDelayed(RestoreDefaultValues, TIME_TO_Restore_Default_Values);//10分钟后恢复原来值
}
}
//added by ouyang end [2015-10-19]
还要将computeDiscoveryParameters()方法中的判断语句改掉,1733行代码如下:
// if (screenState >= NFC_POLLING_MODE) {
//edited by ouyang [2015-10-19 11:13:17]
if (screenState >= getNFC_POLLING_MODE()) {
====================================================================================
上面代码用Handler做十分钟定时器的时候,时间不准确,改用AlarmManager做定时器,下面是修改的代码//added by ouyang start [2015-11-4] 40分钟后恢复NFC默认值的广播
else if (action.equals("com.runbo.camera.nfc.restore")) {
//设置在屏幕解锁情况下可以使用NFC
Log.d("oyp", "<----com.runbo.camera.nfc.restore---->");
setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
applyRouting(true);
Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String time=sdf.format(date);
Log.d("oyp", "time after="+time);
}
//added by ouyang start [2015-10-19] Camera物理键按下后松开
else if (action.equals("com.runbo.camera.key.up")) {
Log.d("oyp", "<----com.runbo.camera.key.up---->");
Handler checkHandler=new Handler();
Handler restoreHandler=new Handler();
//单击
if (!isDoublePress) {
isDoublePress=true;//500ms之类再单击Camera键的话,就是双击了,直接进入 else语句块
//500ms后触发该线程,查看是单击还是双击
Runnable CheckDoubleRunnable=new Runnable(){
@Override
public void run() {
if (isDoublePress) {
Log.i("oyp", "<----Single Press the Camera Key---->"); //显示为单击
}else{
Log.i("oyp", "<----Double Press the Camera Key---->"); //显示为双击
}
isDoublePress=false;//500ms后在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
}
};
checkHandler.postDelayed(CheckDoubleRunnable, 500);// 500ms内两次单击,触发双击
}
// 500ms内两次单击,触发双击
else{
isDoublePress=false;//双击后,将该值设为false,下次在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
//设置在屏幕关闭情况下仍然可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_SCREEN_OFF);
applyRouting(true);
Log.d("oyp", "2、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
//使用AlarmManager来做定时
AlarmManager aManager = (AlarmManager)context.getSystemService(Service.ALARM_SERVICE);
// 指定启动AlarmActivity组件
Intent nfcRestoreIntent = new Intent("com.runbo.camera.nfc.restore");
// 创建PendingIntent对象
PendingIntent pi = PendingIntent.getBroadcast(context, 0, nfcRestoreIntent, 0);
//设定一个40分钟后的时间
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE,TIME_TO_Restore_Default_Values);
//先取消定时器
//aManager.cancel(pi);
// 设置AlarmManager将在Calendar对应的时间启动指定组件
aManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String time=sdf.format(date);
Log.d("oyp", "time before="+time);
// //10分钟后触发该线程,恢复原来值
// Runnable RestoreDefaultValues=new Runnable(){
// @Override
// public void run() {
// //设置在屏幕解锁情况下可以使用NFC
// setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
// applyRouting(true);
// Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
// }
// };
// restoreHandler.removeCallbacks(RestoreDefaultValues);//先取消定时器
// restoreHandler.postDelayed(RestoreDefaultValues, TIME_TO_Restore_Default_Values);//10分钟后恢复原来值
}
}
//added by ouyang end [2015-10-19]
将Handler的代码注释掉了,改用AlarmManager来做定时器,到达指定的时间后,发送一个“com.runbo.camera.nfc.restore”广播,这个广播也让该广播接收者来接收,因此动态注册广播的代码改成: //如果是Hanbang的定制软件
if (android.os.SystemProperties.isHanbangVersion()) {
//接收Camera物理键按下后松开,发出的广播
filter.addAction("com.runbo.camera.key.up");
//接收NFC恢复默认值的广播
filter.addAction("com.runbo.camera.nfc.restore");
}
//added by ouyang end [2015-10-19]
因为之前使用秒来计时,现在使用分钟来计时,因此TIME_TO_Restore_Default_Values改成40,即40分钟 //从黑屏可用NFC恢复到要解锁才能用NFC的时间
static final int TIME_TO_Restore_Default_Values=40; //40分钟
====================================================================================
下面呈上修改后的代码和没修改的代码,经验证,完美!
修改之后的代码如下:
* All work that might turn the NFC adapter on or off must be done
* through this task, to keep the handling of mState simple.
* In other words, mState is only modified in these tasks (and we
* don't need a lock to read it in these tasks).
*
* These tasks are all done on the same AsyncTask background
* thread, so they are serialized. Each task may temporarily transition
* mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in
* either STATE_ON or STATE_OFF. This way each task can be guaranteed
* of starting in either STATE_OFF or STATE_ON, without needing to hold
* NfcService.this for the entire task.
*
* AsyncTask's are also implicitly queued. This is useful for corner
* cases like turning airplane mode on while TASK_ENABLE is in progress.
* The TASK_DISABLE triggered by airplane mode will be correctly executed
* immediately after TASK_ENABLE is complete. This seems like the most sane
* way to deal with these situations.
*
* {@link #TASK_ENABLE} enables the NFC adapter, without changing
* preferences
*
{@link #TASK_DISABLE} disables the NFC adapter, without changing
* preferences
*
{@link #TASK_BOOT} does first boot work and may enable NFC
*/
class EnableDisableTask extends AsyncTask {
/// M: @ {
EnableDisableTask() {
MtkNfcAddonSequence.getInstance().setScenario(MtkNfcAddonSequence.DISABLE_CARD_MODE_OFF);
}
EnableDisableTask(int scenario) {
MtkNfcAddonSequence.getInstance().setScenario(scenario);
}
/// }
@Override
protected Void doInBackground(Integer... params) {
// Sanity check mState
switch (mState) {
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_TURNING_ON:
Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " +
mState);
return null;
}
/* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND,
* override with the default. THREAD_PRIORITY_BACKGROUND causes
* us to service software I2C too slow for firmware download
* with the NXP PN544.
* TODO: move this to the DAL I2C layer in libnfc-nxp, since this
* problem only occurs on I2C platforms using PN544
*/
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
switch (params[0].intValue()) {
case TASK_ENABLE:
enableInternal();
break;
case TASK_DISABLE:
disableInternal();
break;
case TASK_BOOT:
Log.d(TAG, "checking on firmware download");
boolean airplaneOverride = mPrefs.getBoolean(PREF_AIRPLANE_OVERRIDE, false);
if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) &&
(!mIsAirplaneSensitive || !isAirplaneModeOn() || airplaneOverride)) {
Log.d(TAG, "NFC is on. Doing normal stuff");
enableInternal();
} else {
Log.d(TAG, "NFC is off. Checking firmware version");
mDeviceHost.checkFirmware();
}
if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) {
Log.i(TAG, "First Boot");
mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false);
mPrefsEditor.apply();
}
break;
}
// Restore default AsyncTask priority
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return null;
}
/**
* Enable NFC adapter functions.
* Does not toggle preferences.
*/
boolean enableInternal() {
if (mState == NfcAdapter.STATE_ON) {
return true;
}
/// M: @ {
// TODO:: updateState to on, move synchronized object
synchronized (NfcService.this) {
Log.i(TAG, "Enabling NFC");
updateState(NfcAdapter.STATE_TURNING_ON);
WatchDogThread watchDog = new WatchDogThread("enableInternal", INIT_WATCHDOG_MS);
watchDog.start();
try {
mRoutingWakeLock.acquire();
try {
if (!mDeviceHost.initialize()) {
Log.w(TAG, "Error enabling NFC");
updateState(NfcAdapter.STATE_OFF);
return false;
}
} finally {
mRoutingWakeLock.release();
}
} finally {
watchDog.cancel();
}
if (mIsHceCapable) {
// Generate the initial card emulation routing table
mCardEmulationManager.onNfcEnabled();
}
//synchronized (NfcService.this) {
/// }
mObjectMap.clear();
mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true);
updateState(NfcAdapter.STATE_ON);
/// M: @ {
mScreenState = mScreenStateHelper.checkScreenState();
/// }
}
initSoundPool();
/* Start polling loop */
applyRouting(true);
/// M: @ {
Log.d(TAG, "showNotification()");
//start weixj at 20151013 Don't show the NFC notification
if(!SystemProperties.isJ5Version()){
NfcStatusNotificationUi.getInstance().showNotification();
}
/// }
//end
return true;
}
/**
* Disable all NFC adapter functions.
* Does not toggle preferences.
*/
boolean disableInternal() {
if (mState == NfcAdapter.STATE_OFF) {
return true;
}
Log.i(TAG, "Disabling NFC");
updateState(NfcAdapter.STATE_TURNING_OFF);
/* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog.
* Implemented with a new thread (instead of a Handler or AsyncTask),
* because the UI Thread and AsyncTask thread-pools can also get hung
* when the NFC controller stops responding */
WatchDogThread watchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS);
watchDog.start();
if (mIsHceCapable) {
mCardEmulationManager.onNfcDisabled();
}
/// M: @ {
boolean result = false;
synchronized (NfcService.this) {
mP2pLinkManager.enableDisable(false, false);
/* The NFC-EE may still be opened by another process,
* and a transceive() could still be in progress on
* another Binder thread.
* Give it a while to finish existing operations
* before we close it.
*/
Long startTime = SystemClock.elapsedRealtime();
do {
synchronized (NfcService.this) {
if (mOpenEe == null)
break;
}
try {
Thread.sleep(WAIT_FOR_NFCEE_POLL_MS);
} catch (InterruptedException e) {
// Ignore
}
} while (SystemClock.elapsedRealtime() - startTime < WAIT_FOR_NFCEE_OPERATIONS_MS);
synchronized (NfcService.this) {
if (mOpenEe != null) {
try {
_nfcEeClose(-1, mOpenEe.binder);
} catch (IOException e) { }
}
}
// Stop watchdog if tag present
// A convenient way to stop the watchdog properly consists of
// disconnecting the tag. The polling loop shall be stopped before
// to avoid the tag being discovered again.
maybeDisconnectTarget();
mNfcDispatcher.setForegroundDispatch(null, null, null);
result = mDeviceHost.deinitialize();
if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result);
//mNfcPollingEnabled = false;/// MTK: potential bug fix
}
/// }
watchDog.cancel();
synchronized (NfcService.this) {
mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters();
updateState(NfcAdapter.STATE_OFF);
}
/// M: @ {
//releaseSoundPool();
Log.d(TAG, "hideNotification()");
NfcStatusNotificationUi.getInstance().hideNotification();
/// }
return result;
}
void updateState(int newState) {
synchronized (NfcService.this) {
if (newState == mState) {
return;
}
mState = newState;
Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState);
mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
}
}
}
void saveNfcOnSetting(boolean on) {
synchronized (NfcService.this) {
mPrefsEditor.putBoolean(PREF_NFC_ON, on);
mPrefsEditor.apply();
}
}
public void playSound(int sound) {
synchronized (this) {
if (mSoundPool == null) {
Log.w(TAG, "Not playing sound when NFC is disabled");
return;
}
switch (sound) {
case SOUND_START:
mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_END:
mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_ERROR:
mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
}
}
}
synchronized int getUserId() {
return mUserId;
}
void setBeamShareActivityState(boolean enabled) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
// Propagate the state change to all user profiles related to the current
// user. Note that the list returned by getUserProfiles contains the
// current user.
List luh = um.getUserProfiles();
for (UserHandle uh : luh){
enforceBeamShareActivityPolicy(mContext, uh, enabled);
}
}
void enforceBeamShareActivityPolicy(Context context, UserHandle uh,
boolean isGlobalEnabled){
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
boolean isActiveForUser =
(!um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM, uh)) &&
isGlobalEnabled;
if (DBG){
Log.d(TAG, "Enforcing a policy change on user: " + uh +
", isActiveForUser = " + isActiveForUser);
}
try {
mIpm.setComponentEnabledSetting(new ComponentName(
BeamShareActivity.class.getPackageName$(),
BeamShareActivity.class.getName()),
isActiveForUser ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
uh.getIdentifier());
} catch (RemoteException e) {
Log.w(TAG, "Unable to change Beam status for user " + uh);
}
}
final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
saveNfcOnSetting(true);
if (mIsAirplaneSensitive && isAirplaneModeOn()) {
if (!mIsAirplaneToggleable) {
Log.i(TAG, "denying enable() request (airplane mode)");
return false;
}
// Make sure the override survives a reboot
mPrefsEditor.putBoolean(PREF_AIRPLANE_OVERRIDE, true);
mPrefsEditor.apply();
}
new EnableDisableTask().execute(TASK_ENABLE);
return true;
}
@Override
public boolean disable(boolean saveState) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
if (saveState) {
saveNfcOnSetting(false);
}
new EnableDisableTask().execute(TASK_DISABLE);
return true;
}
@Override
public void pausePolling(int timeoutInMs) {
NfcPermissions.enforceAdminPermissions(mContext);
if (timeoutInMs <= 0 || timeoutInMs > MAX_POLLING_PAUSE_TIMEOUT) {
Log.e(TAG, "Refusing to pause polling for " + timeoutInMs + "ms.");
return;
}
synchronized (NfcService.this) {
mPollingPaused = true;
mDeviceHost.disableDiscovery();
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_RESUME_POLLING), timeoutInMs);
}
}
@Override
public void resumePolling() {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mPollingPaused) {
return;
}
mHandler.removeMessages(MSG_RESUME_POLLING);
mPollingPaused = false;
new ApplyRoutingTask().execute();
}
}
@Override
public boolean isNdefPushEnabled() throws RemoteException {
synchronized (NfcService.this) {
return mState == NfcAdapter.STATE_ON && mIsNdefPushEnabled;
}
}
@Override
public boolean enableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "enabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true);
mPrefsEditor.apply();
mIsNdefPushEnabled = true;
setBeamShareActivityState(true);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(true, true);
}
}
return true;
}
@Override
public boolean disableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "disabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false);
mPrefsEditor.apply();
mIsNdefPushEnabled = false;
setBeamShareActivityState(false);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(false, true);
}
}
return true;
}
@Override
public void setForegroundDispatch(PendingIntent intent,
IntentFilter[] filters, TechListParcel techListsParcel) {
NfcPermissions.enforceUserPermissions(mContext);
// Short-cut the disable path
if (intent == null && filters == null && techListsParcel == null) {
mNfcDispatcher.setForegroundDispatch(null, null, null);
return;
}
// Validate the IntentFilters
if (filters != null) {
if (filters.length == 0) {
filters = null;
} else {
for (IntentFilter filter : filters) {
if (filter == null) {
throw new IllegalArgumentException("null IntentFilter");
}
}
}
}
// Validate the tech lists
String[][] techLists = null;
if (techListsParcel != null) {
techLists = techListsParcel.getTechLists();
}
mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
}
@Override
public void setAppCallback(IAppCallback callback) {
NfcPermissions.enforceUserPermissions(mContext);
// don't allow Beam for managed profiles, or devices with a device owner or policy owner
UserInfo userInfo = mUserManager.getUserInfo(UserHandle.getCallingUserId());
if(!mUserManager.hasUserRestriction(
UserManager.DISALLOW_OUTGOING_BEAM, userInfo.getUserHandle())) {
mP2pLinkManager.setNdefCallback(callback, Binder.getCallingUid());
} else if (DBG) {
Log.d(TAG, "Disabling default Beam behavior");
}
}
@Override
public void verifyNfcPermission() {
NfcPermissions.enforceUserPermissions(mContext);
}
@Override
public void invokeBeam() {
NfcPermissions.enforceUserPermissions(mContext);
if (mForegroundUtils.isInForeground(Binder.getCallingUid())) {
mP2pLinkManager.onManualBeamInvoke(null);
} else {
Log.e(TAG, "Calling activity not in foreground.");
}
}
@Override
public void invokeBeamInternal(BeamShareData shareData) {
NfcPermissions.enforceAdminPermissions(mContext);
Message msg = Message.obtain();
msg.what = MSG_INVOKE_BEAM;
msg.obj = shareData;
// We have to send this message delayed for two reasons:
// 1) This is an IPC call from BeamShareActivity, which is
// running when the user has invoked Beam through the
// share menu. As soon as BeamShareActivity closes, the UI
// will need some time to rebuild the original Activity.
// Waiting here for a while gives a better chance of the UI
// having been rebuilt, which means the screenshot that the
// Beam animation is using will be more accurate.
// 2) Similarly, because the Activity that launched BeamShareActivity
// with an ACTION_SEND intent is now in paused state, the NDEF
// callbacks that it has registered may no longer be valid.
// Allowing the original Activity to resume will make sure we
// it has a chance to re-register the NDEF message / callback,
// so we share the right data.
//
// Note that this is somewhat of a hack because the delay may not actually
// be long enough for 2) on very slow devices, but there's no better
// way to do this right now without additional framework changes.
mHandler.sendMessageDelayed(msg, INVOKE_BEAM_DELAY_MS);
}
@Override
public INfcTag getNfcTagInterface() throws RemoteException {
return mNfcTagService;
}
@Override
public INfcCardEmulation getNfcCardEmulationInterface() {
if (mIsHceCapable) {
return mCardEmulationManager.getNfcCardEmulationInterface();
} else {
return null;
}
}
@Override
public int getState() throws RemoteException {
synchronized (NfcService.this) {
return mState;
}
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
NfcService.this.dump(fd, pw, args);
}
@Override
public void dispatch(Tag tag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mNfcDispatcher.dispatchTag(tag);
}
@Override
public void setP2pModes(int initiatorModes, int targetModes) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mDeviceHost.setP2pInitiatorModes(initiatorModes);
mDeviceHost.setP2pTargetModes(targetModes);
applyRouting(true);
}
@Override
public void setReaderMode(IBinder binder, IAppCallback callback, int flags, Bundle extras)
throws RemoteException {
Log.e(TAG, "================================");
Log.e(TAG, "==== setReaderMode flags:"+flags);
synchronized (NfcService.this) {
if (flags != 0) {
try {
mReaderModeParams = new ReaderModeParams();
mReaderModeParams.callback = callback;
mReaderModeParams.flags = flags;
mReaderModeParams.presenceCheckDelay = extras != null
? (extras.getInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY,
DEFAULT_PRESENCE_CHECK_DELAY))
: DEFAULT_PRESENCE_CHECK_DELAY;
binder.linkToDeath(mReaderModeDeathRecipient, 0);
} catch (RemoteException e) {
Log.e(TAG, "Remote binder has already died.");
return;
}
} else {
try {
mReaderModeParams = null;
binder.unlinkToDeath(mReaderModeDeathRecipient, 0);
} catch (NoSuchElementException e) {
Log.e(TAG, "Reader mode Binder was never registered.");
}
}
applyRouting(false);
}
}
@Override
public INfcAdapterExtras getNfcAdapterExtrasInterface(String pkg) throws RemoteException {
NfcService.this.enforceNfceeAdminPerm(pkg);
return mExtrasService;
}
/// M: GSMA@ {
@Override
public INfcAdapterGsmaExtras getNfcAdapterGsmaExtrasInterface() throws RemoteException {
//NfcService.this.enforceNfceeAdminPerm(pkg);
return NfcGsmaExtraService.getInstance();
}
/// }
@Override
public void addNfcUnlockHandler(INfcUnlockHandler unlockHandler, int[] techList) {
NfcPermissions.enforceAdminPermissions(mContext);
int lockscreenPollMask = computeLockscreenPollMask(techList);
synchronized (NfcService.this) {
mNfcUnlockManager.addUnlockHandler(unlockHandler, lockscreenPollMask);
}
applyRouting(false);
}
@Override
public void removeNfcUnlockHandler(INfcUnlockHandler token) throws RemoteException {
synchronized (NfcService.this) {
mNfcUnlockManager.removeUnlockHandler(token.asBinder());
}
applyRouting(false);
}
private int computeLockscreenPollMask(int[] techList) {
Map techCodeToMask = new HashMap();
techCodeToMask.put(TagTechnology.NFC_A, NfcService.NFC_POLL_A);
techCodeToMask.put(TagTechnology.NFC_B, NfcService.NFC_POLL_B);
techCodeToMask.put(TagTechnology.NFC_V, NfcService.NFC_POLL_ISO15693);
techCodeToMask.put(TagTechnology.NFC_F, NfcService.NFC_POLL_F);
techCodeToMask.put(TagTechnology.NFC_BARCODE, NfcService.NFC_POLL_KOVIO);
int mask = 0;
for (int i = 0; i < techList.length; i++) {
if (techCodeToMask.containsKey(techList[i])) {
mask |= techCodeToMask.get(techList[i]).intValue();
}
}
return mask;
}
/// M: @ {
public int getModeFlag(int mode) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
return MtkNfcAddonSequence.getInstance().getModeFlag(mode, NfcService.this);
}
public void setModeFlag(int mode, int flag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
MtkNfcAddonSequence.getInstance().setModeFlag(isNfcEnabled(), mode, flag, NfcService.this);
}
/// }
}
final class ReaderModeDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {
synchronized (NfcService.this) {
Log.e(TAG, "binderDied() mReaderModeParams:"+mReaderModeParams);
if (mReaderModeParams != null) {
mReaderModeParams = null;
applyRouting(false);
}
}
}
}
final class TagService extends INfcTag.Stub {
@Override
public int close(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "close(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
/* Remove the device from the hmap */
unregisterObject(nativeHandle);
tag.disconnect();
return ErrorCodes.SUCCESS;
}
/* Restart polling loop for notification */
applyRouting(true);
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int connect(int nativeHandle, int technology) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag = null;
Log.d(TAG, "connect(..) nativeHandle:"+nativeHandle+" technology:"+technology);
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_DISCONNECT;
}
if (!tag.isPresent()) {
return ErrorCodes.ERROR_DISCONNECT;
}
// Note that on most tags, all technologies are behind a single
// handle. This means that the connect at the lower levels
// will do nothing, as the tag is already connected to that handle.
if (tag.connect(technology)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
@Override
public int reconnect(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "reconnect(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
if (tag.reconnect()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int[] getTechList(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
TagEndpoint tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
return tag.getTechList();
}
return null;
}
@Override
public boolean isPresent(int nativeHandle) throws RemoteException {
TagEndpoint tag = null;
Log.d(TAG, "isPresent(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return false;
}
return tag.isPresent();
}
@Override
public boolean isNdef(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "isNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
int[] ndefInfo = new int[2];
if (tag == null) {
return false;
}
return tag.checkNdef(ndefInfo);
}
@Override
public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw)
throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "transceive(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
byte[] response;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// Check if length is within limits
if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) {
return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null);
}
int[] targetLost = new int[1];
response = tag.transceive(data, raw, targetLost);
int result;
if (response != null) {
result = TransceiveResult.RESULT_SUCCESS;
} else if (targetLost[0] == 1) {
result = TransceiveResult.RESULT_TAGLOST;
} else {
result = TransceiveResult.RESULT_FAILURE;
}
return new TransceiveResult(result, response);
}
return null;
}
@Override
public NdefMessage ndefRead(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
Log.d(TAG, "ndefRead(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
byte[] buf = tag.readNdef();
if (buf == null) {
return null;
}
/* Create an NdefMessage */
try {
return new NdefMessage(buf);
} catch (FormatException e) {
return null;
}
}
return null;
}
@Override
public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (msg == null) return ErrorCodes.ERROR_INVALID_PARAM;
if (tag.writeNdef(msg.toByteArray())) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public boolean ndefIsWritable(int nativeHandle) throws RemoteException {
throw new UnsupportedOperationException();
}
@Override
public int ndefMakeReadOnly(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.makeReadOnly()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public int formatNdef(int nativeHandle, byte[] key) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "formatNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.formatNdef(key)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public Tag rediscover(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "rediscover(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// For now the prime usecase for rediscover() is to be able
// to access the NDEF technology after formatting without
// having to remove the tag from the field, or similar
// to have access to NdefFormatable in case low-level commands
// were used to remove NDEF. So instead of doing a full stack
// rediscover (which is poorly supported at the moment anyway),
// we simply remove these two technologies and detect them
// again.
tag.removeTechnology(TagTechnology.NDEF);
tag.removeTechnology(TagTechnology.NDEF_FORMATABLE);
tag.findAndReadNdef();
// Build a new Tag object to return
Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
tag.getTechExtras(), tag.getHandle(), this);
return newTag;
}
return null;
}
@Override
public int setTimeout(int tech, int timeout) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "setTimeout(..) tech:"+tech+" timeout:"+timeout);
boolean success = mDeviceHost.setTimeout(tech, timeout);
if (success) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_INVALID_PARAM;
}
}
@Override
public int getTimeout(int tech) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
return mDeviceHost.getTimeout(tech);
}
@Override
public void resetTimeouts() throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
mDeviceHost.resetTimeouts();
}
@Override
public boolean canMakeReadOnly(int ndefType) throws RemoteException {
return mDeviceHost.canMakeReadOnly(ndefType);
}
@Override
public int getMaxTransceiveLength(int tech) throws RemoteException {
return mDeviceHost.getMaxTransceiveLength(tech);
}
@Override
public boolean getExtendedLengthApdusSupported() throws RemoteException {
return mDeviceHost.getExtendedLengthApdusSupported();
}
}
boolean isNfcEnabledOrShuttingDown() {
synchronized (this) {
return (mState == NfcAdapter.STATE_ON || mState == NfcAdapter.STATE_TURNING_OFF);
}
}
boolean isNfcEnabled() {
synchronized (this) {
return mState == NfcAdapter.STATE_ON;
}
}
class WatchDogThread extends Thread {
final Object mCancelWaiter = new Object();
final int mTimeout;
boolean mCanceled = false;
public WatchDogThread(String threadName, int timeout) {
super(threadName);
mTimeout = timeout;
}
@Override
public void run() {
try {
synchronized (mCancelWaiter) {
mCancelWaiter.wait(mTimeout);
if (mCanceled) {
return;
}
}
} catch (InterruptedException e) {
// Should not happen; fall-through to abort.
Log.w(TAG, "Watchdog thread interruped.");
interrupt();
}
Log.e(TAG, "Watchdog triggered, aborting.");
mDeviceHost.doAbort();
}
public synchronized void cancel() {
synchronized (mCancelWaiter) {
mCanceled = true;
mCancelWaiter.notify();
}
}
}
static byte[] hexStringToBytes(String s) {
if (s == null || s.length() == 0) return null;
int len = s.length();
if (len % 2 != 0) {
s = '0' + s;
len++;
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
/**
* Read mScreenState and apply NFC-C polling and NFC-EE routing
*/
void applyRouting(boolean force) {
Log.d("oyp", "<----applyRouting()---->");
synchronized (this) {
/// M: @ {
if (!isNfcEnabledOrShuttingDown() || mOpenEe != null) {
return;
}
/// }
WatchDogThread watchDog = new WatchDogThread("applyRouting", ROUTING_WATCHDOG_MS);
if (mInProvisionMode) {
mInProvisionMode = Settings.Secure.getInt(mContentResolver,
Settings.Global.DEVICE_PROVISIONED, 0) == 0;
if (!mInProvisionMode) {
// Notify dispatcher it's fine to dispatch to any package now
// and allow handover transfers.
mNfcDispatcher.disableProvisioningMode();
}
}
// Special case: if we're transitioning to unlocked state while
// still talking to a tag, postpone re-configuration.
if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && isTagPresent()) {
Log.d(TAG, "Not updating discovery parameters, tag connected.");
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RESUME_POLLING),
APPLY_ROUTING_RETRY_TIMEOUT_MS);
return;
}
/// M: @ {
// configure NFC-EE routing
if (mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
if (force || !mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE ON");
mNfceeRouteEnabled = true;
mNativeNfcManager.deselectSecureElement();
}
} else {
if (force || mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE OFF");
mNfceeRouteEnabled = false;//edited by ouyang [2015-10-16] start
if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) { /// M: only deselect when using Google Wallet
mNativeNfcManager.deselectSecureElement();
}
}
}
/// }
try {
watchDog.start();
// Compute new polling parameters
NfcDiscoveryParameters newParams = computeDiscoveryParameters(mScreenState);
if (force || !newParams.equals(mCurrentDiscoveryParameters)) {
Log.d(TAG, "newParams.shouldEnableDiscovery()");
if (newParams.shouldEnableDiscovery()) {
Log.d(TAG, "mCurrentDiscoveryParameters.shouldEnableDiscovery()");
boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery();
Log.d(TAG, "shouldRestart:"+shouldRestart);
mDeviceHost.enableDiscovery(newParams, shouldRestart);
} else {
mDeviceHost.disableDiscovery();
}
mCurrentDiscoveryParameters = newParams;
} else {
Log.d(TAG, "Discovery configuration equal, not updating.");
}
} finally {
watchDog.cancel();
}
}
}
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
// if (screenState >= NFC_POLLING_MODE) {
//edited by ouyang [2015-10-19 11:13:17]
if (screenState >= getNFC_POLLING_MODE()) {
// Check if reader-mode is enabled
Log.d("oyp", "1、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
修改之前的代码如下:
* All work that might turn the NFC adapter on or off must be done
* through this task, to keep the handling of mState simple.
* In other words, mState is only modified in these tasks (and we
* don't need a lock to read it in these tasks).
*
* These tasks are all done on the same AsyncTask background
* thread, so they are serialized. Each task may temporarily transition
* mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in
* either STATE_ON or STATE_OFF. This way each task can be guaranteed
* of starting in either STATE_OFF or STATE_ON, without needing to hold
* NfcService.this for the entire task.
*
* AsyncTask's are also implicitly queued. This is useful for corner
* cases like turning airplane mode on while TASK_ENABLE is in progress.
* The TASK_DISABLE triggered by airplane mode will be correctly executed
* immediately after TASK_ENABLE is complete. This seems like the most sane
* way to deal with these situations.
*
* {@link #TASK_ENABLE} enables the NFC adapter, without changing
* preferences
*
{@link #TASK_DISABLE} disables the NFC adapter, without changing
* preferences
*
{@link #TASK_BOOT} does first boot work and may enable NFC
*/
class EnableDisableTask extends AsyncTask {
/// M: @ {
EnableDisableTask() {
MtkNfcAddonSequence.getInstance().setScenario(MtkNfcAddonSequence.DISABLE_CARD_MODE_OFF);
}
EnableDisableTask(int scenario) {
MtkNfcAddonSequence.getInstance().setScenario(scenario);
}
/// }
@Override
protected Void doInBackground(Integer... params) {
// Sanity check mState
switch (mState) {
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_TURNING_ON:
Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " +
mState);
return null;
}
/* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND,
* override with the default. THREAD_PRIORITY_BACKGROUND causes
* us to service software I2C too slow for firmware download
* with the NXP PN544.
* TODO: move this to the DAL I2C layer in libnfc-nxp, since this
* problem only occurs on I2C platforms using PN544
*/
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
switch (params[0].intValue()) {
case TASK_ENABLE:
enableInternal();
break;
case TASK_DISABLE:
disableInternal();
break;
case TASK_BOOT:
Log.d(TAG, "checking on firmware download");
boolean airplaneOverride = mPrefs.getBoolean(PREF_AIRPLANE_OVERRIDE, false);
if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) &&
(!mIsAirplaneSensitive || !isAirplaneModeOn() || airplaneOverride)) {
Log.d(TAG, "NFC is on. Doing normal stuff");
enableInternal();
} else {
Log.d(TAG, "NFC is off. Checking firmware version");
mDeviceHost.checkFirmware();
}
if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) {
Log.i(TAG, "First Boot");
mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false);
mPrefsEditor.apply();
}
break;
}
// Restore default AsyncTask priority
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return null;
}
/**
* Enable NFC adapter functions.
* Does not toggle preferences.
*/
boolean enableInternal() {
if (mState == NfcAdapter.STATE_ON) {
return true;
}
/// M: @ {
// TODO:: updateState to on, move synchronized object
synchronized (NfcService.this) {
Log.i(TAG, "Enabling NFC");
updateState(NfcAdapter.STATE_TURNING_ON);
WatchDogThread watchDog = new WatchDogThread("enableInternal", INIT_WATCHDOG_MS);
watchDog.start();
try {
mRoutingWakeLock.acquire();
try {
if (!mDeviceHost.initialize()) {
Log.w(TAG, "Error enabling NFC");
updateState(NfcAdapter.STATE_OFF);
return false;
}
} finally {
mRoutingWakeLock.release();
}
} finally {
watchDog.cancel();
}
if (mIsHceCapable) {
// Generate the initial card emulation routing table
mCardEmulationManager.onNfcEnabled();
}
//synchronized (NfcService.this) {
/// }
mObjectMap.clear();
mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true);
updateState(NfcAdapter.STATE_ON);
/// M: @ {
mScreenState = mScreenStateHelper.checkScreenState();
/// }
}
initSoundPool();
/* Start polling loop */
applyRouting(true);
/// M: @ {
Log.d(TAG, "showNotification()");
//start weixj at 20151013 Don't show the NFC notification
if(!SystemProperties.isJ5Version()){
NfcStatusNotificationUi.getInstance().showNotification();
}
/// }
//end
return true;
}
/**
* Disable all NFC adapter functions.
* Does not toggle preferences.
*/
boolean disableInternal() {
if (mState == NfcAdapter.STATE_OFF) {
return true;
}
Log.i(TAG, "Disabling NFC");
updateState(NfcAdapter.STATE_TURNING_OFF);
/* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog.
* Implemented with a new thread (instead of a Handler or AsyncTask),
* because the UI Thread and AsyncTask thread-pools can also get hung
* when the NFC controller stops responding */
WatchDogThread watchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS);
watchDog.start();
if (mIsHceCapable) {
mCardEmulationManager.onNfcDisabled();
}
/// M: @ {
boolean result = false;
synchronized (NfcService.this) {
mP2pLinkManager.enableDisable(false, false);
/* The NFC-EE may still be opened by another process,
* and a transceive() could still be in progress on
* another Binder thread.
* Give it a while to finish existing operations
* before we close it.
*/
Long startTime = SystemClock.elapsedRealtime();
do {
synchronized (NfcService.this) {
if (mOpenEe == null)
break;
}
try {
Thread.sleep(WAIT_FOR_NFCEE_POLL_MS);
} catch (InterruptedException e) {
// Ignore
}
} while (SystemClock.elapsedRealtime() - startTime < WAIT_FOR_NFCEE_OPERATIONS_MS);
synchronized (NfcService.this) {
if (mOpenEe != null) {
try {
_nfcEeClose(-1, mOpenEe.binder);
} catch (IOException e) { }
}
}
// Stop watchdog if tag present
// A convenient way to stop the watchdog properly consists of
// disconnecting the tag. The polling loop shall be stopped before
// to avoid the tag being discovered again.
maybeDisconnectTarget();
mNfcDispatcher.setForegroundDispatch(null, null, null);
result = mDeviceHost.deinitialize();
if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result);
//mNfcPollingEnabled = false;/// MTK: potential bug fix
}
/// }
watchDog.cancel();
synchronized (NfcService.this) {
mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters();
updateState(NfcAdapter.STATE_OFF);
}
/// M: @ {
//releaseSoundPool();
Log.d(TAG, "hideNotification()");
NfcStatusNotificationUi.getInstance().hideNotification();
/// }
return result;
}
void updateState(int newState) {
synchronized (NfcService.this) {
if (newState == mState) {
return;
}
mState = newState;
Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState);
mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
}
}
}
void saveNfcOnSetting(boolean on) {
synchronized (NfcService.this) {
mPrefsEditor.putBoolean(PREF_NFC_ON, on);
mPrefsEditor.apply();
}
}
public void playSound(int sound) {
synchronized (this) {
if (mSoundPool == null) {
Log.w(TAG, "Not playing sound when NFC is disabled");
return;
}
switch (sound) {
case SOUND_START:
mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_END:
mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_ERROR:
mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
}
}
}
synchronized int getUserId() {
return mUserId;
}
void setBeamShareActivityState(boolean enabled) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
// Propagate the state change to all user profiles related to the current
// user. Note that the list returned by getUserProfiles contains the
// current user.
List luh = um.getUserProfiles();
for (UserHandle uh : luh){
enforceBeamShareActivityPolicy(mContext, uh, enabled);
}
}
void enforceBeamShareActivityPolicy(Context context, UserHandle uh,
boolean isGlobalEnabled){
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
boolean isActiveForUser =
(!um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM, uh)) &&
isGlobalEnabled;
if (DBG){
Log.d(TAG, "Enforcing a policy change on user: " + uh +
", isActiveForUser = " + isActiveForUser);
}
try {
mIpm.setComponentEnabledSetting(new ComponentName(
BeamShareActivity.class.getPackageName$(),
BeamShareActivity.class.getName()),
isActiveForUser ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
uh.getIdentifier());
} catch (RemoteException e) {
Log.w(TAG, "Unable to change Beam status for user " + uh);
}
}
final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
saveNfcOnSetting(true);
if (mIsAirplaneSensitive && isAirplaneModeOn()) {
if (!mIsAirplaneToggleable) {
Log.i(TAG, "denying enable() request (airplane mode)");
return false;
}
// Make sure the override survives a reboot
mPrefsEditor.putBoolean(PREF_AIRPLANE_OVERRIDE, true);
mPrefsEditor.apply();
}
new EnableDisableTask().execute(TASK_ENABLE);
return true;
}
@Override
public boolean disable(boolean saveState) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
if (saveState) {
saveNfcOnSetting(false);
}
new EnableDisableTask().execute(TASK_DISABLE);
return true;
}
@Override
public void pausePolling(int timeoutInMs) {
NfcPermissions.enforceAdminPermissions(mContext);
if (timeoutInMs <= 0 || timeoutInMs > MAX_POLLING_PAUSE_TIMEOUT) {
Log.e(TAG, "Refusing to pause polling for " + timeoutInMs + "ms.");
return;
}
synchronized (NfcService.this) {
mPollingPaused = true;
mDeviceHost.disableDiscovery();
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_RESUME_POLLING), timeoutInMs);
}
}
@Override
public void resumePolling() {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mPollingPaused) {
return;
}
mHandler.removeMessages(MSG_RESUME_POLLING);
mPollingPaused = false;
new ApplyRoutingTask().execute();
}
}
@Override
public boolean isNdefPushEnabled() throws RemoteException {
synchronized (NfcService.this) {
return mState == NfcAdapter.STATE_ON && mIsNdefPushEnabled;
}
}
@Override
public boolean enableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "enabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true);
mPrefsEditor.apply();
mIsNdefPushEnabled = true;
setBeamShareActivityState(true);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(true, true);
}
}
return true;
}
@Override
public boolean disableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "disabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false);
mPrefsEditor.apply();
mIsNdefPushEnabled = false;
setBeamShareActivityState(false);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(false, true);
}
}
return true;
}
@Override
public void setForegroundDispatch(PendingIntent intent,
IntentFilter[] filters, TechListParcel techListsParcel) {
NfcPermissions.enforceUserPermissions(mContext);
// Short-cut the disable path
if (intent == null && filters == null && techListsParcel == null) {
mNfcDispatcher.setForegroundDispatch(null, null, null);
return;
}
// Validate the IntentFilters
if (filters != null) {
if (filters.length == 0) {
filters = null;
} else {
for (IntentFilter filter : filters) {
if (filter == null) {
throw new IllegalArgumentException("null IntentFilter");
}
}
}
}
// Validate the tech lists
String[][] techLists = null;
if (techListsParcel != null) {
techLists = techListsParcel.getTechLists();
}
mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
}
@Override
public void setAppCallback(IAppCallback callback) {
NfcPermissions.enforceUserPermissions(mContext);
// don't allow Beam for managed profiles, or devices with a device owner or policy owner
UserInfo userInfo = mUserManager.getUserInfo(UserHandle.getCallingUserId());
if(!mUserManager.hasUserRestriction(
UserManager.DISALLOW_OUTGOING_BEAM, userInfo.getUserHandle())) {
mP2pLinkManager.setNdefCallback(callback, Binder.getCallingUid());
} else if (DBG) {
Log.d(TAG, "Disabling default Beam behavior");
}
}
@Override
public void verifyNfcPermission() {
NfcPermissions.enforceUserPermissions(mContext);
}
@Override
public void invokeBeam() {
NfcPermissions.enforceUserPermissions(mContext);
if (mForegroundUtils.isInForeground(Binder.getCallingUid())) {
mP2pLinkManager.onManualBeamInvoke(null);
} else {
Log.e(TAG, "Calling activity not in foreground.");
}
}
@Override
public void invokeBeamInternal(BeamShareData shareData) {
NfcPermissions.enforceAdminPermissions(mContext);
Message msg = Message.obtain();
msg.what = MSG_INVOKE_BEAM;
msg.obj = shareData;
// We have to send this message delayed for two reasons:
// 1) This is an IPC call from BeamShareActivity, which is
// running when the user has invoked Beam through the
// share menu. As soon as BeamShareActivity closes, the UI
// will need some time to rebuild the original Activity.
// Waiting here for a while gives a better chance of the UI
// having been rebuilt, which means the screenshot that the
// Beam animation is using will be more accurate.
// 2) Similarly, because the Activity that launched BeamShareActivity
// with an ACTION_SEND intent is now in paused state, the NDEF
// callbacks that it has registered may no longer be valid.
// Allowing the original Activity to resume will make sure we
// it has a chance to re-register the NDEF message / callback,
// so we share the right data.
//
// Note that this is somewhat of a hack because the delay may not actually
// be long enough for 2) on very slow devices, but there's no better
// way to do this right now without additional framework changes.
mHandler.sendMessageDelayed(msg, INVOKE_BEAM_DELAY_MS);
}
@Override
public INfcTag getNfcTagInterface() throws RemoteException {
return mNfcTagService;
}
@Override
public INfcCardEmulation getNfcCardEmulationInterface() {
if (mIsHceCapable) {
return mCardEmulationManager.getNfcCardEmulationInterface();
} else {
return null;
}
}
@Override
public int getState() throws RemoteException {
synchronized (NfcService.this) {
return mState;
}
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
NfcService.this.dump(fd, pw, args);
}
@Override
public void dispatch(Tag tag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mNfcDispatcher.dispatchTag(tag);
}
@Override
public void setP2pModes(int initiatorModes, int targetModes) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mDeviceHost.setP2pInitiatorModes(initiatorModes);
mDeviceHost.setP2pTargetModes(targetModes);
applyRouting(true);
}
@Override
public void setReaderMode(IBinder binder, IAppCallback callback, int flags, Bundle extras)
throws RemoteException {
Log.e(TAG, "================================");
Log.e(TAG, "==== setReaderMode flags:"+flags);
synchronized (NfcService.this) {
if (flags != 0) {
try {
mReaderModeParams = new ReaderModeParams();
mReaderModeParams.callback = callback;
mReaderModeParams.flags = flags;
mReaderModeParams.presenceCheckDelay = extras != null
? (extras.getInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY,
DEFAULT_PRESENCE_CHECK_DELAY))
: DEFAULT_PRESENCE_CHECK_DELAY;
binder.linkToDeath(mReaderModeDeathRecipient, 0);
} catch (RemoteException e) {
Log.e(TAG, "Remote binder has already died.");
return;
}
} else {
try {
mReaderModeParams = null;
binder.unlinkToDeath(mReaderModeDeathRecipient, 0);
} catch (NoSuchElementException e) {
Log.e(TAG, "Reader mode Binder was never registered.");
}
}
applyRouting(false);
}
}
@Override
public INfcAdapterExtras getNfcAdapterExtrasInterface(String pkg) throws RemoteException {
NfcService.this.enforceNfceeAdminPerm(pkg);
return mExtrasService;
}
/// M: GSMA@ {
@Override
public INfcAdapterGsmaExtras getNfcAdapterGsmaExtrasInterface() throws RemoteException {
//NfcService.this.enforceNfceeAdminPerm(pkg);
return NfcGsmaExtraService.getInstance();
}
/// }
@Override
public void addNfcUnlockHandler(INfcUnlockHandler unlockHandler, int[] techList) {
NfcPermissions.enforceAdminPermissions(mContext);
int lockscreenPollMask = computeLockscreenPollMask(techList);
synchronized (NfcService.this) {
mNfcUnlockManager.addUnlockHandler(unlockHandler, lockscreenPollMask);
}
applyRouting(false);
}
@Override
public void removeNfcUnlockHandler(INfcUnlockHandler token) throws RemoteException {
synchronized (NfcService.this) {
mNfcUnlockManager.removeUnlockHandler(token.asBinder());
}
applyRouting(false);
}
private int computeLockscreenPollMask(int[] techList) {
Map techCodeToMask = new HashMap();
techCodeToMask.put(TagTechnology.NFC_A, NfcService.NFC_POLL_A);
techCodeToMask.put(TagTechnology.NFC_B, NfcService.NFC_POLL_B);
techCodeToMask.put(TagTechnology.NFC_V, NfcService.NFC_POLL_ISO15693);
techCodeToMask.put(TagTechnology.NFC_F, NfcService.NFC_POLL_F);
techCodeToMask.put(TagTechnology.NFC_BARCODE, NfcService.NFC_POLL_KOVIO);
int mask = 0;
for (int i = 0; i < techList.length; i++) {
if (techCodeToMask.containsKey(techList[i])) {
mask |= techCodeToMask.get(techList[i]).intValue();
}
}
return mask;
}
/// M: @ {
public int getModeFlag(int mode) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
return MtkNfcAddonSequence.getInstance().getModeFlag(mode, NfcService.this);
}
public void setModeFlag(int mode, int flag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
MtkNfcAddonSequence.getInstance().setModeFlag(isNfcEnabled(), mode, flag, NfcService.this);
}
/// }
}
final class ReaderModeDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {
synchronized (NfcService.this) {
Log.e(TAG, "binderDied() mReaderModeParams:"+mReaderModeParams);
if (mReaderModeParams != null) {
mReaderModeParams = null;
applyRouting(false);
}
}
}
}
final class TagService extends INfcTag.Stub {
@Override
public int close(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "close(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
/* Remove the device from the hmap */
unregisterObject(nativeHandle);
tag.disconnect();
return ErrorCodes.SUCCESS;
}
/* Restart polling loop for notification */
applyRouting(true);
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int connect(int nativeHandle, int technology) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag = null;
Log.d(TAG, "connect(..) nativeHandle:"+nativeHandle+" technology:"+technology);
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_DISCONNECT;
}
if (!tag.isPresent()) {
return ErrorCodes.ERROR_DISCONNECT;
}
// Note that on most tags, all technologies are behind a single
// handle. This means that the connect at the lower levels
// will do nothing, as the tag is already connected to that handle.
if (tag.connect(technology)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
@Override
public int reconnect(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "reconnect(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
if (tag.reconnect()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int[] getTechList(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
TagEndpoint tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
return tag.getTechList();
}
return null;
}
@Override
public boolean isPresent(int nativeHandle) throws RemoteException {
TagEndpoint tag = null;
Log.d(TAG, "isPresent(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return false;
}
return tag.isPresent();
}
@Override
public boolean isNdef(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "isNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
int[] ndefInfo = new int[2];
if (tag == null) {
return false;
}
return tag.checkNdef(ndefInfo);
}
@Override
public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw)
throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "transceive(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
byte[] response;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// Check if length is within limits
if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) {
return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null);
}
int[] targetLost = new int[1];
response = tag.transceive(data, raw, targetLost);
int result;
if (response != null) {
result = TransceiveResult.RESULT_SUCCESS;
} else if (targetLost[0] == 1) {
result = TransceiveResult.RESULT_TAGLOST;
} else {
result = TransceiveResult.RESULT_FAILURE;
}
return new TransceiveResult(result, response);
}
return null;
}
@Override
public NdefMessage ndefRead(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
Log.d(TAG, "ndefRead(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
byte[] buf = tag.readNdef();
if (buf == null) {
return null;
}
/* Create an NdefMessage */
try {
return new NdefMessage(buf);
} catch (FormatException e) {
return null;
}
}
return null;
}
@Override
public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (msg == null) return ErrorCodes.ERROR_INVALID_PARAM;
if (tag.writeNdef(msg.toByteArray())) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public boolean ndefIsWritable(int nativeHandle) throws RemoteException {
throw new UnsupportedOperationException();
}
@Override
public int ndefMakeReadOnly(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.makeReadOnly()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public int formatNdef(int nativeHandle, byte[] key) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "formatNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.formatNdef(key)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public Tag rediscover(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "rediscover(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// For now the prime usecase for rediscover() is to be able
// to access the NDEF technology after formatting without
// having to remove the tag from the field, or similar
// to have access to NdefFormatable in case low-level commands
// were used to remove NDEF. So instead of doing a full stack
// rediscover (which is poorly supported at the moment anyway),
// we simply remove these two technologies and detect them
// again.
tag.removeTechnology(TagTechnology.NDEF);
tag.removeTechnology(TagTechnology.NDEF_FORMATABLE);
tag.findAndReadNdef();
// Build a new Tag object to return
Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
tag.getTechExtras(), tag.getHandle(), this);
return newTag;
}
return null;
}
@Override
public int setTimeout(int tech, int timeout) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "setTimeout(..) tech:"+tech+" timeout:"+timeout);
boolean success = mDeviceHost.setTimeout(tech, timeout);
if (success) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_INVALID_PARAM;
}
}
@Override
public int getTimeout(int tech) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
return mDeviceHost.getTimeout(tech);
}
@Override
public void resetTimeouts() throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
mDeviceHost.resetTimeouts();
}
@Override
public boolean canMakeReadOnly(int ndefType) throws RemoteException {
return mDeviceHost.canMakeReadOnly(ndefType);
}
@Override
public int getMaxTransceiveLength(int tech) throws RemoteException {
return mDeviceHost.getMaxTransceiveLength(tech);
}
@Override
public boolean getExtendedLengthApdusSupported() throws RemoteException {
return mDeviceHost.getExtendedLengthApdusSupported();
}
}
boolean isNfcEnabledOrShuttingDown() {
synchronized (this) {
return (mState == NfcAdapter.STATE_ON || mState == NfcAdapter.STATE_TURNING_OFF);
}
}
boolean isNfcEnabled() {
synchronized (this) {
return mState == NfcAdapter.STATE_ON;
}
}
class WatchDogThread extends Thread {
final Object mCancelWaiter = new Object();
final int mTimeout;
boolean mCanceled = false;
public WatchDogThread(String threadName, int timeout) {
super(threadName);
mTimeout = timeout;
}
@Override
public void run() {
try {
synchronized (mCancelWaiter) {
mCancelWaiter.wait(mTimeout);
if (mCanceled) {
return;
}
}
} catch (InterruptedException e) {
// Should not happen; fall-through to abort.
Log.w(TAG, "Watchdog thread interruped.");
interrupt();
}
Log.e(TAG, "Watchdog triggered, aborting.");
mDeviceHost.doAbort();
}
public synchronized void cancel() {
synchronized (mCancelWaiter) {
mCanceled = true;
mCancelWaiter.notify();
}
}
}
static byte[] hexStringToBytes(String s) {
if (s == null || s.length() == 0) return null;
int len = s.length();
if (len % 2 != 0) {
s = '0' + s;
len++;
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
/**
* Read mScreenState and apply NFC-C polling and NFC-EE routing
*/
void applyRouting(boolean force) {
synchronized (this) {
/// M: @ {
if (!isNfcEnabledOrShuttingDown() || mOpenEe != null) {
return;
}
/// }
WatchDogThread watchDog = new WatchDogThread("applyRouting", ROUTING_WATCHDOG_MS);
if (mInProvisionMode) {
mInProvisionMode = Settings.Secure.getInt(mContentResolver,
Settings.Global.DEVICE_PROVISIONED, 0) == 0;
if (!mInProvisionMode) {
// Notify dispatcher it's fine to dispatch to any package now
// and allow handover transfers.
mNfcDispatcher.disableProvisioningMode();
}
}
// Special case: if we're transitioning to unlocked state while
// still talking to a tag, postpone re-configuration.
if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && isTagPresent()) {
Log.d(TAG, "Not updating discovery parameters, tag connected.");
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RESUME_POLLING),
APPLY_ROUTING_RETRY_TIMEOUT_MS);
return;
}
/// M: @ {
// configure NFC-EE routing
if (mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
if (force || !mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE ON");
mNfceeRouteEnabled = true;
mNativeNfcManager.deselectSecureElement();
}
} else {
if (force || mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE OFF");
mNfceeRouteEnabled = false;
if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) { /// M: only deselect when using Google Wallet
mNativeNfcManager.deselectSecureElement();
}
}
}
/// }
try {
watchDog.start();
// Compute new polling parameters
NfcDiscoveryParameters newParams = computeDiscoveryParameters(mScreenState);
if (force || !newParams.equals(mCurrentDiscoveryParameters)) {
Log.d(TAG, "newParams.shouldEnableDiscovery()");
if (newParams.shouldEnableDiscovery()) {
Log.d(TAG, "mCurrentDiscoveryParameters.shouldEnableDiscovery()");
boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery();
Log.d(TAG, "shouldRestart:"+shouldRestart);
mDeviceHost.enableDiscovery(newParams, shouldRestart);
} else {
mDeviceHost.disableDiscovery();
}
mCurrentDiscoveryParameters = newParams;
} else {
Log.d(TAG, "Discovery configuration equal, not updating.");
}
} finally {
watchDog.cancel();
}
}
}
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
if (screenState >= NFC_POLLING_MODE) {
// Check if reader-mode is enabled
if (mReaderModeParams != null) {
int techMask = 0;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_A) != 0)
techMask |= NFC_POLL_A;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_B) != 0)
techMask |= NFC_POLL_B;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_F) != 0)
techMask |= NFC_POLL_F;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_V) != 0)
techMask |= NFC_POLL_ISO15693;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_BARCODE) != 0)
techMask |= NFC_POLL_KOVIO;
Log.d(TAG, " mReaderModeParams != null paramsBuilder.setTechMask:"+techMask);
paramsBuilder.setTechMask(techMask);
paramsBuilder.setEnableReaderMode(true);
} else {
Log.d(TAG, " mReaderModeParams == null paramsBuilder.setTechMask:"+NfcDiscoveryParameters.NFC_POLL_DEFAULT +" NFC_POLL_DEFAULT");
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
paramsBuilder.setEnableP2p(mIsNdefPushEnabled);
}
} else if (screenState == ScreenStateHelper.SCREEN_STAT
==================================================================================== 作者:欧阳鹏 欢迎转载,与人分享是进步的源泉! 转载请保留原文地址:http://blog.csdn.net/ouyang_peng====================================================================================
最开始可以通过查看分析源代码,找到到NfcService的相关代码,如下: packages\apps\Nfc\src\com\android\nfc\NfcService.java
找到186行,这句是定义NFC能够使用的屏幕最小状态
// minimum screen state that enables NFC polling
static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
这几个状态分别是:
SCREEN_STATE_OFF黑屏状态
SCREEN_STATE_ON_LOCKED屏幕亮了,但是是锁屏状态
SCREEN_STATE_ON_UNLOCKED 屏幕亮了,并且是解锁状态
代码定义如下,在packages\apps\Nfc\src\com\android\nfc\ScreenStateHelper中定义
static final int SCREEN_STATE_UNKNOWN = 0;
static final int SCREEN_STATE_OFF = 1;
static final int SCREEN_STATE_ON_LOCKED = 2;
static final int SCREEN_STATE_ON_UNLOCKED = 3;
上面的这个最小状态在NfcService.java的第1706行,computeDiscoveryParameters(int screenState)方法中被调用,用来判断的,方法代码如下:
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
if (screenState >= NFC_POLLING_MODE) {//这里被调用
// Check if reader-mode is enabled
if (mReaderModeParams != null) {
int techMask = 0;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_A) != 0)
techMask |= NFC_POLL_A;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_B) != 0)
techMask |= NFC_POLL_B;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_F) != 0)
techMask |= NFC_POLL_F;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_V) != 0)
techMask |= NFC_POLL_ISO15693;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_BARCODE) != 0)
techMask |= NFC_POLL_KOVIO;
Log.d(TAG, " mReaderModeParams != null paramsBuilder.setTechMask:"+techMask);
paramsBuilder.setTechMask(techMask);
paramsBuilder.setEnableReaderMode(true);
} else {
Log.d(TAG, " mReaderModeParams == null paramsBuilder.setTechMask:"+NfcDiscoveryParameters.NFC_POLL_DEFAULT +" NFC_POLL_DEFAULT");
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
paramsBuilder.setEnableP2p(mIsNdefPushEnabled);
}
} else if (screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED && mInProvisionMode) {
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
// enable P2P for MFM/EDU/Corp provisioning
paramsBuilder.setEnableP2p(true);
} else if (screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mNfcUnlockManager.isLockscreenPollingEnabled()) {
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED setTechMask ");
// For lock-screen tags, no low-power polling
paramsBuilder.setTechMask(mNfcUnlockManager.getLockscreenPollMask());
paramsBuilder.setEnableLowPowerDiscovery(false);
paramsBuilder.setEnableP2p(false);
}
if (mIsHceCapable && mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED) {
// Host routing is always enabled at lock screen or later
Log.d(TAG, " >= SCREEN_STATE_ON_LOCKED paramsBuilder.setEnableHostRouting(true) ");
paramsBuilder.setEnableHostRouting(true);
}
return paramsBuilder.build();
}因此如果要改成黑屏状态下可以使用NFC的话,只要将变量NFC_POLLING_MODE改成
ScreenStateHelper.SCREEN_STATE_OFF即可,代码如下:
// minimum screen state that enables NFC polling
//static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_OFF;
但是这样的话,手机会一直不休眠,观察电池的电流和电压发现,一直在跳动,这样在黑屏状态下,手机不会休眠,会很耗电,因此还要优化。
客户的要求是:当双击物理按键Camera键的时候,可以在黑屏状态下使用NFC十分钟,十分钟之类,差不多关于NFC的工作完成了,之后将状态改回来,即:只能在解锁状态下使用NFC,这样的话就可以黑屏使用NFC又节电。
因此,思路如下:
1、接收物理按键Camera键发送的广播,来判断是双击,并将NFC_POLLING_MODE的最小模式改为ScreenStateHelper.SCREEN_STATE_OFF。
2、需要写一个定时器来处理十分钟之后将NFC_POLLING_MODE的最小模式改为会原来的ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED。
因此,首先先定义几个常量,从第185行static final int NFC_POLLING_MODE= ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;处开始修改,修改代码如下:
// minimum screen state that enables NFC polling
// edited by ouyang [2015-10-19] start
// static final int NFC_POLLING_MODE= ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
//默认为要解锁才能使用NFC
static final int NFC_POLLING_MODE_DEFALUT = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
//在黑屏情况下也可以使用NFC
static final int NFC_POLLING_MODE_SCREEN_OFF = ScreenStateHelper.SCREEN_STATE_OFF;
//默认能使用NFC时的屏幕状态
static int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED;
public static int getNFC_POLLING_MODE() {
return NFC_POLLING_MODE;
}
public static void setNFC_POLLING_MODE(int mNFC_POLLING_MODE) {
NFC_POLLING_MODE = mNFC_POLLING_MODE;
}
//是否是双击Camera键
static boolean isDoublePress=false;
//从黑屏可用NFC恢复到要解锁才能用NFC的时间
static final int TIME_TO_Restore_Default_Values=(60*1000)*10;//10分钟 10*1000*60
// edited by ouyang [2015-10-19] end
第二步:写一个广播接收者来处理物理按键Camera,按下和松开时发出的广播。
因为是要判断双击Camera,所以这里只要接收松开Camera键时发出的广播即可。这个广播是公司自己定义的,定义的广播为:"com.runbo.camera.key.up"。所以现在处理这个广播。因为代码中本来就动态注册了一个广播接收者,因此我们在这个广播接收者种再注册一个Intent即可。代码如下:在第450行
// Intents for all users
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_USER_SWITCHED);
//added by ouyang start [2015-10-19]
//Camera物理键按下后松开 发出的广播
filter.addAction("com.runbo.camera.key.up");
//added by ouyang end [2015-10-19]
registerForAirplaneMode(filter);
mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null);
这样我们处理这个双击Camera键可以在mReceiver中处理了,在mReceiver中的onReceive方法中,判断action是否是"com.runbo.camera.key.up",2295行代码如下:
//added by ouyang start [2015-10-19] Camera物理键按下后松开
else if (action.equals("com.runbo.camera.key.up")) {
Log.d("oyp", "<----com.runbo.camera.key.up---->");
Handler checkHandler=new Handler();
Handler restoreHandler=new Handler();
//单击
if (!isDoublePress) {
isDoublePress=true;//500ms之类再单击Camera键的话,就是双击了,直接进入 else语句块
//500ms后触发该线程,查看是单击还是双击
Runnable CheckDoubleRunnable=new Runnable(){
@Override
public void run() {
if (isDoublePress) {
Log.i("oyp", "<----Single Press the Camera Key---->"); //显示为单击
}else{
Log.i("oyp", "<----Double Press the Camera Key---->"); //显示为双击
}
isDoublePress=false;//500ms后在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
}
};
checkHandler.postDelayed(CheckDoubleRunnable, 500);// 500ms内两次单击,触发双击
}
// 500ms内两次单击,触发双击
else{
isDoublePress=false;//双击后,将该值设为false,下次在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
//设置在屏幕关闭情况下仍然可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_SCREEN_OFF);
applyRouting(true);
Log.d("oyp", "2、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
//10分钟后触发该线程,恢复原来值
Runnable RestoreDefaultValues=new Runnable(){
@Override
public void run() {
//设置在屏幕解锁情况下可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
applyRouting(true);
Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
}
};
restoreHandler.removeCallbacks(RestoreDefaultValues);//先取消定时器
restoreHandler.postDelayed(RestoreDefaultValues, TIME_TO_Restore_Default_Values);//10分钟后恢复原来值
}
}
//added by ouyang end [2015-10-19]
还要将computeDiscoveryParameters()方法中的判断语句改掉,1733行代码如下:
// if (screenState >= NFC_POLLING_MODE) {
//edited by ouyang [2015-10-19 11:13:17]
if (screenState >= getNFC_POLLING_MODE()) {
====================================================================================
上面代码用Handler做十分钟定时器的时候,时间不准确,改用AlarmManager做定时器,下面是修改的代码//added by ouyang start [2015-11-4] 40分钟后恢复NFC默认值的广播
else if (action.equals("com.runbo.camera.nfc.restore")) {
//设置在屏幕解锁情况下可以使用NFC
Log.d("oyp", "<----com.runbo.camera.nfc.restore---->");
setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
applyRouting(true);
Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String time=sdf.format(date);
Log.d("oyp", "time after="+time);
}
//added by ouyang start [2015-10-19] Camera物理键按下后松开
else if (action.equals("com.runbo.camera.key.up")) {
Log.d("oyp", "<----com.runbo.camera.key.up---->");
Handler checkHandler=new Handler();
Handler restoreHandler=new Handler();
//单击
if (!isDoublePress) {
isDoublePress=true;//500ms之类再单击Camera键的话,就是双击了,直接进入 else语句块
//500ms后触发该线程,查看是单击还是双击
Runnable CheckDoubleRunnable=new Runnable(){
@Override
public void run() {
if (isDoublePress) {
Log.i("oyp", "<----Single Press the Camera Key---->"); //显示为单击
}else{
Log.i("oyp", "<----Double Press the Camera Key---->"); //显示为双击
}
isDoublePress=false;//500ms后在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
}
};
checkHandler.postDelayed(CheckDoubleRunnable, 500);// 500ms内两次单击,触发双击
}
// 500ms内两次单击,触发双击
else{
isDoublePress=false;//双击后,将该值设为false,下次在单击Camera键的话 依旧是单击 ,还是进入了 if语句块
//设置在屏幕关闭情况下仍然可以使用NFC
setNFC_POLLING_MODE(NFC_POLLING_MODE_SCREEN_OFF);
applyRouting(true);
Log.d("oyp", "2、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
//使用AlarmManager来做定时
AlarmManager aManager = (AlarmManager)context.getSystemService(Service.ALARM_SERVICE);
// 指定启动AlarmActivity组件
Intent nfcRestoreIntent = new Intent("com.runbo.camera.nfc.restore");
// 创建PendingIntent对象
PendingIntent pi = PendingIntent.getBroadcast(context, 0, nfcRestoreIntent, 0);
//设定一个40分钟后的时间
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE,TIME_TO_Restore_Default_Values);
//先取消定时器
//aManager.cancel(pi);
// 设置AlarmManager将在Calendar对应的时间启动指定组件
aManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String time=sdf.format(date);
Log.d("oyp", "time before="+time);
// //10分钟后触发该线程,恢复原来值
// Runnable RestoreDefaultValues=new Runnable(){
// @Override
// public void run() {
// //设置在屏幕解锁情况下可以使用NFC
// setNFC_POLLING_MODE(NFC_POLLING_MODE_DEFALUT);
// applyRouting(true);
// Log.d("oyp", "3、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
// }
// };
// restoreHandler.removeCallbacks(RestoreDefaultValues);//先取消定时器
// restoreHandler.postDelayed(RestoreDefaultValues, TIME_TO_Restore_Default_Values);//10分钟后恢复原来值
}
}
//added by ouyang end [2015-10-19]
将Handler的代码注释掉了,改用AlarmManager来做定时器,到达指定的时间后,发送一个“com.runbo.camera.nfc.restore”广播,这个广播也让该广播接收者来接收,因此动态注册广播的代码改成: //如果是Hanbang的定制软件
if (android.os.SystemProperties.isHanbangVersion()) {
//接收Camera物理键按下后松开,发出的广播
filter.addAction("com.runbo.camera.key.up");
//接收NFC恢复默认值的广播
filter.addAction("com.runbo.camera.nfc.restore");
}
//added by ouyang end [2015-10-19]
因为之前使用秒来计时,现在使用分钟来计时,因此TIME_TO_Restore_Default_Values改成40,即40分钟 //从黑屏可用NFC恢复到要解锁才能用NFC的时间
static final int TIME_TO_Restore_Default_Values=40; //40分钟
====================================================================================
下面呈上修改后的代码和没修改的代码,经验证,完美!
修改之后的代码如下:
/* * Copyright (C) 2014 MediaTek Inc. * Modification based on code covered by the mentioned copyright * and/or permission notice(s). */ /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.nfc; import android.app.ActivityManager; import android.app.Application; import android.app.KeyguardManager; import android.app.PendingIntent; import android.app.admin.DevicePolicyManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.IPackageManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.content.res.Resources.NotFoundException; import android.media.AudioManager; import android.media.SoundPool; import android.nfc.BeamShareData; import android.nfc.ErrorCodes; import android.nfc.FormatException; import android.nfc.IAppCallback; import android.nfc.INfcAdapter; import android.nfc.INfcAdapterExtras; import android.nfc.INfcCardEmulation; import android.nfc.INfcTag; import android.nfc.INfcUnlockHandler; import android.nfc.NdefMessage; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.TechListParcel; import android.nfc.TransceiveResult; import android.nfc.tech.Ndef; import android.nfc.tech.TagTechnology; import android.os.AsyncTask; import android.os.Binder; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.PowerManager; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; import android.util.Log; import com.android.nfc.DeviceHost.DeviceHostListener; import com.android.nfc.DeviceHost.LlcpConnectionlessSocket; import com.android.nfc.DeviceHost.LlcpServerSocket; import com.android.nfc.DeviceHost.LlcpSocket; import com.android.nfc.DeviceHost.NfcDepEndpoint; import com.android.nfc.DeviceHost.TagEndpoint; import com.android.nfc.cardemulation.CardEmulationManager; import com.android.nfc.dhimpl.NativeNfcManager; import com.android.nfc.handover.HandoverDataParser; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Arrays; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; /// M: @ { import android.os.SystemClock; import com.mediatek.nfc.handover.MtkNfcEntry; import com.android.nfc.dhimpl.NativeNfcSecureElement; import java.io.IOException; import java.util.HashSet; import java.util.concurrent.ExecutionException; import com.mediatek.nfc.addon.MtkNfcAddonSequence; import com.mediatek.nfc.addon.NfcStatusNotificationUi; import com.mediatek.nfc.addon.Utility; import android.widget.Toast; import android.database.ContentObserver; import android.net.Uri; import com.mediatek.nfc.gsmahandset.NfcGsmaExtraService; import android.nfc.INfcAdapterGsmaExtras; import android.os.SystemProperties; /// } public class NfcService implements DeviceHostListener { static final boolean DBG = true; static final String TAG = "NfcService"; public static final String SERVICE_NAME = "nfc"; public static final String PREF = "NfcServicePrefs"; static final String PREF_NFC_ON = "nfc_on"; static final boolean NFC_ON_DEFAULT = true; static final String PREF_NDEF_PUSH_ON = "ndef_push_on"; static final boolean NDEF_PUSH_ON_DEFAULT = true; static final String PREF_FIRST_BEAM = "first_beam"; static final String PREF_FIRST_BOOT = "first_boot"; static final String PREF_AIRPLANE_OVERRIDE = "airplane_override"; /// M: @ { public static final String NFC_HCE_ON = "nfc_hce_on"; //value type: int,0 for Off, 1 for on /// } static final int MSG_NDEF_TAG = 0; static final int MSG_LLCP_LINK_ACTIVATION = 1; static final int MSG_LLCP_LINK_DEACTIVATED = 2; static final int MSG_MOCK_NDEF = 3; static final int MSG_LLCP_LINK_FIRST_PACKET = 4; static final int MSG_ROUTE_AID = 5; static final int MSG_UNROUTE_AID = 6; static final int MSG_COMMIT_ROUTING = 7; static final int MSG_INVOKE_BEAM = 8; static final int MSG_RF_FIELD_ACTIVATED = 9; static final int MSG_RF_FIELD_DEACTIVATED = 10; static final int MSG_RESUME_POLLING = 11; static final long MAX_POLLING_PAUSE_TIMEOUT = 40000; static final int TASK_ENABLE = 1; static final int TASK_DISABLE = 2; static final int TASK_BOOT = 3; // Polling technology masks static final int NFC_POLL_A = 0x01; static final int NFC_POLL_B = 0x02; static final int NFC_POLL_F = 0x04; static final int NFC_POLL_ISO15693 = 0x08; static final int NFC_POLL_B_PRIME = 0x10; static final int NFC_POLL_KOVIO = 0x20; /// M: @ { // Copied from com.android.nfc_extras to avoid library dependency // Must keep in sync with com.android.nfc_extras static final int ROUTE_OFF = 1; static final int ROUTE_ON_WHEN_SCREEN_ON = 2; // Return values from NfcEe.open() - these are 1:1 mapped // to the thrown EE_EXCEPTION_ exceptions in nfc-extras. static final int EE_ERROR_IO = -1; static final int EE_ERROR_ALREADY_OPEN = -2; static final int EE_ERROR_INIT = -3; static final int EE_ERROR_LISTEN_MODE = -4; static final int EE_ERROR_EXT_FIELD = -5; static final int EE_ERROR_NFC_DISABLED = -6; // Amount of time to wait before closing the NFCEE connection // in a disable/shutdown scenario. static final int WAIT_FOR_NFCEE_OPERATIONS_MS = 5000; // Polling interval for waiting on NFCEE operations static final int WAIT_FOR_NFCEE_POLL_MS = 100; /// } // minimum screen state that enables NFC polling // edited by ouyang [2015-10-19] start // static final int NFC_POLLING_MODE= ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; //默认为要解锁才能使用NFC static final int NFC_POLLING_MODE_DEFALUT = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; //在黑屏情况下也可以使用NFC static final int NFC_POLLING_MODE_SCREEN_OFF = ScreenStateHelper.SCREEN_STATE_OFF; //默认能使用NFC时的屏幕状态 static int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; public static int getNFC_POLLING_MODE() { return NFC_POLLING_MODE; } public static void setNFC_POLLING_MODE(int mNFC_POLLING_MODE) { NFC_POLLING_MODE = mNFC_POLLING_MODE; } //是否是双击Camera键 static boolean isDoublePress=false; //从黑屏可用NFC恢复到要解锁才能用NFC的时间 static final int TIME_TO_Restore_Default_Values=(60*1000)*10;//10分钟 10*1000*60 // edited by ouyang [2015-10-19] end // Time to wait for NFC controller to initialize before watchdog // goes off. This time is chosen large, because firmware download // may be a part of initialization. static final int INIT_WATCHDOG_MS = 90000; // Time to wait for routing to be applied before watchdog // goes off static final int ROUTING_WATCHDOG_MS = 10000; // Default delay used for presence checks static final int DEFAULT_PRESENCE_CHECK_DELAY = 125; // The amount of time we wait before manually launching // the Beam animation when called through the share menu. static final int INVOKE_BEAM_DELAY_MS = 1000; // RF field events as defined in NFC extras public static final String ACTION_RF_FIELD_ON_DETECTED = "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; public static final String ACTION_RF_FIELD_OFF_DETECTED = "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; // for use with playSound() public static final int SOUND_START = 0; public static final int SOUND_END = 1; public static final int SOUND_ERROR = 2; public static final String ACTION_LLCP_UP = "com.android.nfc.action.LLCP_UP"; public static final String ACTION_LLCP_DOWN = "com.android.nfc.action.LLCP_DOWN"; // Timeout to re-apply routing if a tag was present and we postponed it private static final int APPLY_ROUTING_RETRY_TIMEOUT_MS = 5000; private final UserManager mUserManager; // NFC Execution Environment // fields below are protected by this private final ReaderModeDeathRecipient mReaderModeDeathRecipient = new ReaderModeDeathRecipient(); private final NfcUnlockManager mNfcUnlockManager; private final NfceeAccessControl mNfceeAccessControl; List mInstalledPackages; // cached version of installed packages // fields below are used in multiple threads and protected by synchronized(this) final HashMap mObjectMap = new HashMap(); int mScreenState; boolean mInProvisionMode; // whether we're in setup wizard and enabled NFC provisioning boolean mIsNdefPushEnabled; NfcDiscoveryParameters mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters(); ReaderModeParams mReaderModeParams; // mState is protected by this, however it is only modified in onCreate() // and the default AsyncTask thread so it is read unprotected from that // thread int mState; // one of NfcAdapter.STATE_ON, STATE_TURNING_ON, etc // fields below are final after onCreate() Context mContext; private DeviceHost mDeviceHost; private SharedPreferences mPrefs; private SharedPreferences.Editor mPrefsEditor; private PowerManager.WakeLock mRoutingWakeLock; int mStartSound; int mEndSound; int mErrorSound; SoundPool mSoundPool; // playback synchronized on this P2pLinkManager mP2pLinkManager; TagService mNfcTagService; NfcAdapterService mNfcAdapter; boolean mIsAirplaneSensitive; boolean mIsAirplaneToggleable; boolean mIsDebugBuild; boolean mIsHceCapable; boolean mPollingPaused; private NfcDispatcher mNfcDispatcher; private PowerManager mPowerManager; private KeyguardManager mKeyguard; private HandoverDataParser mHandoverDataParser; private ContentResolver mContentResolver; private CardEmulationManager mCardEmulationManager; private ScreenStateHelper mScreenStateHelper; private ForegroundUtils mForegroundUtils; private int mUserId; private static NfcService sService; public static NfcService getInstance() { return sService; } @Override public void onRemoteEndpointDiscovered(TagEndpoint tag) { sendMessage(NfcService.MSG_NDEF_TAG, tag); } /** * Notifies transaction */ @Override public void onHostCardEmulationActivated() { /// M: @ { if (mCardEmulationManager != null && mIsHceOn) { mCardEmulationManager.onHostCardEmulationActivated(); } /// } } @Override public void onHostCardEmulationData(byte[] data) { /// M: @ { if (mCardEmulationManager != null && mIsHceOn) { mCardEmulationManager.onHostCardEmulationData(data); } /// } } @Override public void onHostCardEmulationDeactivated() { if (mCardEmulationManager != null) { mCardEmulationManager.onHostCardEmulationDeactivated(); } } /** * Notifies P2P Device detected, to activate LLCP link */ @Override public void onLlcpLinkActivated(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device); } /** * Notifies P2P Device detected, to activate LLCP link */ @Override public void onLlcpLinkDeactivated(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device); } /** * Notifies P2P Device detected, first packet received over LLCP link */ @Override public void onLlcpFirstPacketReceived(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_FIRST_PACKET, device); } @Override public void onRemoteFieldActivated() { sendMessage(NfcService.MSG_RF_FIELD_ACTIVATED, null); } public void onRemoteFieldDeactivated() { sendMessage(NfcService.MSG_RF_FIELD_DEACTIVATED, null); } final class ReaderModeParams { public int flags; public IAppCallback callback; public int presenceCheckDelay; } public NfcService(Application nfcApplication) { mUserId = ActivityManager.getCurrentUser(); mContext = nfcApplication; mNfcTagService = new TagService(); mNfcAdapter = new NfcAdapterService(); Log.i(TAG, "Starting NFC service"); sService = this; mScreenStateHelper = new ScreenStateHelper(mContext); mContentResolver = mContext.getContentResolver(); /// M: @{ mNativeNfcManager = new NativeNfcManager(mContext, this); mDeviceHost = (DeviceHost) mNativeNfcManager; /// } mNfcUnlockManager = NfcUnlockManager.getInstance(); mHandoverDataParser = new HandoverDataParser(); boolean isNfcProvisioningEnabled = false; try { isNfcProvisioningEnabled = mContext.getResources().getBoolean( R.bool.enable_nfc_provisioning); } catch (NotFoundException e) { } if (isNfcProvisioningEnabled) { mInProvisionMode = Settings.Secure.getInt(mContentResolver, Settings.Global.DEVICE_PROVISIONED, 0) == 0; } else { mInProvisionMode = false; } mNfcDispatcher = new NfcDispatcher(mContext, mHandoverDataParser, mInProvisionMode); mP2pLinkManager = new P2pLinkManager(mContext, mHandoverDataParser, mDeviceHost.getDefaultLlcpMiu(), mDeviceHost.getDefaultLlcpRwSize()); mPrefs = mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE); mPrefsEditor = mPrefs.edit(); mNfceeAccessControl = new NfceeAccessControl(mContext); mState = NfcAdapter.STATE_OFF; mIsNdefPushEnabled = mPrefs.getBoolean(PREF_NDEF_PUSH_ON, NDEF_PUSH_ON_DEFAULT); setBeamShareActivityState(mIsNdefPushEnabled); mIsDebugBuild = "userdebug".equals(Build.TYPE) || "eng".equals(Build.TYPE); mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mRoutingWakeLock = mPowerManager.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "NfcService:mRoutingWakeLock"); mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); mScreenState = mScreenStateHelper.checkScreenState(); /// M: @ { if (NativeNfcManager.sIsUnsupportedChip) { return; } /// } ServiceManager.addService(SERVICE_NAME, mNfcAdapter); // Intents for all users IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_PRESENT); filter.addAction(Intent.ACTION_USER_SWITCHED); //added by ouyang start [2015-10-19] //Camera物理键按下后松开 发出的广播 filter.addAction("com.runbo.camera.key.up"); //added by ouyang end [2015-10-19] registerForAirplaneMode(filter); mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); IntentFilter ownerFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); ownerFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); mContext.registerReceiver(mOwnerReceiver, ownerFilter); ownerFilter = new IntentFilter(); ownerFilter.addAction(Intent.ACTION_PACKAGE_ADDED); ownerFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); ownerFilter.addDataScheme("package"); mContext.registerReceiver(mOwnerReceiver, ownerFilter); IntentFilter policyFilter = new IntentFilter(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); mContext.registerReceiverAsUser(mPolicyReceiver, UserHandle.ALL, policyFilter, null, null); updatePackageCache(); PackageManager pm = mContext.getPackageManager(); mIsHceCapable = pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION); if (mIsHceCapable) { mCardEmulationManager = new CardEmulationManager(mContext); } mForegroundUtils = ForegroundUtils.getInstance(); /// M: { initAddon(); /// } new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks } void initSoundPool() { synchronized (this) { if (mSoundPool == null) { mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); /// M: bug-fix for SoundPool issues @ { Utility.SoundPoolListener soundPoolListener = new Utility.SoundPoolListener(mSoundPool); mStartSound = mSoundPool.load(mContext, R.raw.start, 1); mEndSound = mSoundPool.load(mContext, R.raw.end, 1); mErrorSound = mSoundPool.load(mContext, R.raw.error, 1); soundPoolListener.waitForSamplesReady(new int[] {mStartSound, mEndSound, mErrorSound}); /// } } } } void releaseSoundPool() { synchronized (this) { if (mSoundPool != null) { mSoundPool.release(); mSoundPool = null; } } } void registerForAirplaneMode(IntentFilter filter) { final String airplaneModeRadios = Settings.System.getString(mContentResolver, Settings.Global.AIRPLANE_MODE_RADIOS); final String toggleableRadios = Settings.System.getString(mContentResolver, Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); mIsAirplaneSensitive = airplaneModeRadios == null ? true : airplaneModeRadios.contains(Settings.Global.RADIO_NFC); mIsAirplaneToggleable = toggleableRadios == null ? false : toggleableRadios.contains(Settings.Global.RADIO_NFC); if (mIsAirplaneSensitive) { filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); } } void updatePackageCache() { PackageManager pm = mContext.getPackageManager(); List packages = pm.getInstalledPackages(0, UserHandle.USER_OWNER); synchronized (this) { mInstalledPackages = packages; } } /** * Manages tasks that involve turning on/off the NFC controller. *
* All work that might turn the NFC adapter on or off must be done
* through this task, to keep the handling of mState simple.
* In other words, mState is only modified in these tasks (and we
* don't need a lock to read it in these tasks).
*
* These tasks are all done on the same AsyncTask background
* thread, so they are serialized. Each task may temporarily transition
* mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in
* either STATE_ON or STATE_OFF. This way each task can be guaranteed
* of starting in either STATE_OFF or STATE_ON, without needing to hold
* NfcService.this for the entire task.
*
* AsyncTask's are also implicitly queued. This is useful for corner
* cases like turning airplane mode on while TASK_ENABLE is in progress.
* The TASK_DISABLE triggered by airplane mode will be correctly executed
* immediately after TASK_ENABLE is complete. This seems like the most sane
* way to deal with these situations.
*
* {@link #TASK_ENABLE} enables the NFC adapter, without changing
* preferences
*
{@link #TASK_DISABLE} disables the NFC adapter, without changing
* preferences
*
{@link #TASK_BOOT} does first boot work and may enable NFC
*/
class EnableDisableTask extends AsyncTask {
/// M: @ {
EnableDisableTask() {
MtkNfcAddonSequence.getInstance().setScenario(MtkNfcAddonSequence.DISABLE_CARD_MODE_OFF);
}
EnableDisableTask(int scenario) {
MtkNfcAddonSequence.getInstance().setScenario(scenario);
}
/// }
@Override
protected Void doInBackground(Integer... params) {
// Sanity check mState
switch (mState) {
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_TURNING_ON:
Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " +
mState);
return null;
}
/* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND,
* override with the default. THREAD_PRIORITY_BACKGROUND causes
* us to service software I2C too slow for firmware download
* with the NXP PN544.
* TODO: move this to the DAL I2C layer in libnfc-nxp, since this
* problem only occurs on I2C platforms using PN544
*/
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
switch (params[0].intValue()) {
case TASK_ENABLE:
enableInternal();
break;
case TASK_DISABLE:
disableInternal();
break;
case TASK_BOOT:
Log.d(TAG, "checking on firmware download");
boolean airplaneOverride = mPrefs.getBoolean(PREF_AIRPLANE_OVERRIDE, false);
if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) &&
(!mIsAirplaneSensitive || !isAirplaneModeOn() || airplaneOverride)) {
Log.d(TAG, "NFC is on. Doing normal stuff");
enableInternal();
} else {
Log.d(TAG, "NFC is off. Checking firmware version");
mDeviceHost.checkFirmware();
}
if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) {
Log.i(TAG, "First Boot");
mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false);
mPrefsEditor.apply();
}
break;
}
// Restore default AsyncTask priority
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return null;
}
/**
* Enable NFC adapter functions.
* Does not toggle preferences.
*/
boolean enableInternal() {
if (mState == NfcAdapter.STATE_ON) {
return true;
}
/// M: @ {
// TODO:: updateState to on, move synchronized object
synchronized (NfcService.this) {
Log.i(TAG, "Enabling NFC");
updateState(NfcAdapter.STATE_TURNING_ON);
WatchDogThread watchDog = new WatchDogThread("enableInternal", INIT_WATCHDOG_MS);
watchDog.start();
try {
mRoutingWakeLock.acquire();
try {
if (!mDeviceHost.initialize()) {
Log.w(TAG, "Error enabling NFC");
updateState(NfcAdapter.STATE_OFF);
return false;
}
} finally {
mRoutingWakeLock.release();
}
} finally {
watchDog.cancel();
}
if (mIsHceCapable) {
// Generate the initial card emulation routing table
mCardEmulationManager.onNfcEnabled();
}
//synchronized (NfcService.this) {
/// }
mObjectMap.clear();
mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true);
updateState(NfcAdapter.STATE_ON);
/// M: @ {
mScreenState = mScreenStateHelper.checkScreenState();
/// }
}
initSoundPool();
/* Start polling loop */
applyRouting(true);
/// M: @ {
Log.d(TAG, "showNotification()");
//start weixj at 20151013 Don't show the NFC notification
if(!SystemProperties.isJ5Version()){
NfcStatusNotificationUi.getInstance().showNotification();
}
/// }
//end
return true;
}
/**
* Disable all NFC adapter functions.
* Does not toggle preferences.
*/
boolean disableInternal() {
if (mState == NfcAdapter.STATE_OFF) {
return true;
}
Log.i(TAG, "Disabling NFC");
updateState(NfcAdapter.STATE_TURNING_OFF);
/* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog.
* Implemented with a new thread (instead of a Handler or AsyncTask),
* because the UI Thread and AsyncTask thread-pools can also get hung
* when the NFC controller stops responding */
WatchDogThread watchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS);
watchDog.start();
if (mIsHceCapable) {
mCardEmulationManager.onNfcDisabled();
}
/// M: @ {
boolean result = false;
synchronized (NfcService.this) {
mP2pLinkManager.enableDisable(false, false);
/* The NFC-EE may still be opened by another process,
* and a transceive() could still be in progress on
* another Binder thread.
* Give it a while to finish existing operations
* before we close it.
*/
Long startTime = SystemClock.elapsedRealtime();
do {
synchronized (NfcService.this) {
if (mOpenEe == null)
break;
}
try {
Thread.sleep(WAIT_FOR_NFCEE_POLL_MS);
} catch (InterruptedException e) {
// Ignore
}
} while (SystemClock.elapsedRealtime() - startTime < WAIT_FOR_NFCEE_OPERATIONS_MS);
synchronized (NfcService.this) {
if (mOpenEe != null) {
try {
_nfcEeClose(-1, mOpenEe.binder);
} catch (IOException e) { }
}
}
// Stop watchdog if tag present
// A convenient way to stop the watchdog properly consists of
// disconnecting the tag. The polling loop shall be stopped before
// to avoid the tag being discovered again.
maybeDisconnectTarget();
mNfcDispatcher.setForegroundDispatch(null, null, null);
result = mDeviceHost.deinitialize();
if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result);
//mNfcPollingEnabled = false;/// MTK: potential bug fix
}
/// }
watchDog.cancel();
synchronized (NfcService.this) {
mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters();
updateState(NfcAdapter.STATE_OFF);
}
/// M: @ {
//releaseSoundPool();
Log.d(TAG, "hideNotification()");
NfcStatusNotificationUi.getInstance().hideNotification();
/// }
return result;
}
void updateState(int newState) {
synchronized (NfcService.this) {
if (newState == mState) {
return;
}
mState = newState;
Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState);
mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
}
}
}
void saveNfcOnSetting(boolean on) {
synchronized (NfcService.this) {
mPrefsEditor.putBoolean(PREF_NFC_ON, on);
mPrefsEditor.apply();
}
}
public void playSound(int sound) {
synchronized (this) {
if (mSoundPool == null) {
Log.w(TAG, "Not playing sound when NFC is disabled");
return;
}
switch (sound) {
case SOUND_START:
mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_END:
mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_ERROR:
mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
}
}
}
synchronized int getUserId() {
return mUserId;
}
void setBeamShareActivityState(boolean enabled) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
// Propagate the state change to all user profiles related to the current
// user. Note that the list returned by getUserProfiles contains the
// current user.
List luh = um.getUserProfiles();
for (UserHandle uh : luh){
enforceBeamShareActivityPolicy(mContext, uh, enabled);
}
}
void enforceBeamShareActivityPolicy(Context context, UserHandle uh,
boolean isGlobalEnabled){
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
boolean isActiveForUser =
(!um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM, uh)) &&
isGlobalEnabled;
if (DBG){
Log.d(TAG, "Enforcing a policy change on user: " + uh +
", isActiveForUser = " + isActiveForUser);
}
try {
mIpm.setComponentEnabledSetting(new ComponentName(
BeamShareActivity.class.getPackageName$(),
BeamShareActivity.class.getName()),
isActiveForUser ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
uh.getIdentifier());
} catch (RemoteException e) {
Log.w(TAG, "Unable to change Beam status for user " + uh);
}
}
final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
saveNfcOnSetting(true);
if (mIsAirplaneSensitive && isAirplaneModeOn()) {
if (!mIsAirplaneToggleable) {
Log.i(TAG, "denying enable() request (airplane mode)");
return false;
}
// Make sure the override survives a reboot
mPrefsEditor.putBoolean(PREF_AIRPLANE_OVERRIDE, true);
mPrefsEditor.apply();
}
new EnableDisableTask().execute(TASK_ENABLE);
return true;
}
@Override
public boolean disable(boolean saveState) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
if (saveState) {
saveNfcOnSetting(false);
}
new EnableDisableTask().execute(TASK_DISABLE);
return true;
}
@Override
public void pausePolling(int timeoutInMs) {
NfcPermissions.enforceAdminPermissions(mContext);
if (timeoutInMs <= 0 || timeoutInMs > MAX_POLLING_PAUSE_TIMEOUT) {
Log.e(TAG, "Refusing to pause polling for " + timeoutInMs + "ms.");
return;
}
synchronized (NfcService.this) {
mPollingPaused = true;
mDeviceHost.disableDiscovery();
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_RESUME_POLLING), timeoutInMs);
}
}
@Override
public void resumePolling() {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mPollingPaused) {
return;
}
mHandler.removeMessages(MSG_RESUME_POLLING);
mPollingPaused = false;
new ApplyRoutingTask().execute();
}
}
@Override
public boolean isNdefPushEnabled() throws RemoteException {
synchronized (NfcService.this) {
return mState == NfcAdapter.STATE_ON && mIsNdefPushEnabled;
}
}
@Override
public boolean enableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "enabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true);
mPrefsEditor.apply();
mIsNdefPushEnabled = true;
setBeamShareActivityState(true);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(true, true);
}
}
return true;
}
@Override
public boolean disableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "disabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false);
mPrefsEditor.apply();
mIsNdefPushEnabled = false;
setBeamShareActivityState(false);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(false, true);
}
}
return true;
}
@Override
public void setForegroundDispatch(PendingIntent intent,
IntentFilter[] filters, TechListParcel techListsParcel) {
NfcPermissions.enforceUserPermissions(mContext);
// Short-cut the disable path
if (intent == null && filters == null && techListsParcel == null) {
mNfcDispatcher.setForegroundDispatch(null, null, null);
return;
}
// Validate the IntentFilters
if (filters != null) {
if (filters.length == 0) {
filters = null;
} else {
for (IntentFilter filter : filters) {
if (filter == null) {
throw new IllegalArgumentException("null IntentFilter");
}
}
}
}
// Validate the tech lists
String[][] techLists = null;
if (techListsParcel != null) {
techLists = techListsParcel.getTechLists();
}
mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
}
@Override
public void setAppCallback(IAppCallback callback) {
NfcPermissions.enforceUserPermissions(mContext);
// don't allow Beam for managed profiles, or devices with a device owner or policy owner
UserInfo userInfo = mUserManager.getUserInfo(UserHandle.getCallingUserId());
if(!mUserManager.hasUserRestriction(
UserManager.DISALLOW_OUTGOING_BEAM, userInfo.getUserHandle())) {
mP2pLinkManager.setNdefCallback(callback, Binder.getCallingUid());
} else if (DBG) {
Log.d(TAG, "Disabling default Beam behavior");
}
}
@Override
public void verifyNfcPermission() {
NfcPermissions.enforceUserPermissions(mContext);
}
@Override
public void invokeBeam() {
NfcPermissions.enforceUserPermissions(mContext);
if (mForegroundUtils.isInForeground(Binder.getCallingUid())) {
mP2pLinkManager.onManualBeamInvoke(null);
} else {
Log.e(TAG, "Calling activity not in foreground.");
}
}
@Override
public void invokeBeamInternal(BeamShareData shareData) {
NfcPermissions.enforceAdminPermissions(mContext);
Message msg = Message.obtain();
msg.what = MSG_INVOKE_BEAM;
msg.obj = shareData;
// We have to send this message delayed for two reasons:
// 1) This is an IPC call from BeamShareActivity, which is
// running when the user has invoked Beam through the
// share menu. As soon as BeamShareActivity closes, the UI
// will need some time to rebuild the original Activity.
// Waiting here for a while gives a better chance of the UI
// having been rebuilt, which means the screenshot that the
// Beam animation is using will be more accurate.
// 2) Similarly, because the Activity that launched BeamShareActivity
// with an ACTION_SEND intent is now in paused state, the NDEF
// callbacks that it has registered may no longer be valid.
// Allowing the original Activity to resume will make sure we
// it has a chance to re-register the NDEF message / callback,
// so we share the right data.
//
// Note that this is somewhat of a hack because the delay may not actually
// be long enough for 2) on very slow devices, but there's no better
// way to do this right now without additional framework changes.
mHandler.sendMessageDelayed(msg, INVOKE_BEAM_DELAY_MS);
}
@Override
public INfcTag getNfcTagInterface() throws RemoteException {
return mNfcTagService;
}
@Override
public INfcCardEmulation getNfcCardEmulationInterface() {
if (mIsHceCapable) {
return mCardEmulationManager.getNfcCardEmulationInterface();
} else {
return null;
}
}
@Override
public int getState() throws RemoteException {
synchronized (NfcService.this) {
return mState;
}
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
NfcService.this.dump(fd, pw, args);
}
@Override
public void dispatch(Tag tag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mNfcDispatcher.dispatchTag(tag);
}
@Override
public void setP2pModes(int initiatorModes, int targetModes) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mDeviceHost.setP2pInitiatorModes(initiatorModes);
mDeviceHost.setP2pTargetModes(targetModes);
applyRouting(true);
}
@Override
public void setReaderMode(IBinder binder, IAppCallback callback, int flags, Bundle extras)
throws RemoteException {
Log.e(TAG, "================================");
Log.e(TAG, "==== setReaderMode flags:"+flags);
synchronized (NfcService.this) {
if (flags != 0) {
try {
mReaderModeParams = new ReaderModeParams();
mReaderModeParams.callback = callback;
mReaderModeParams.flags = flags;
mReaderModeParams.presenceCheckDelay = extras != null
? (extras.getInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY,
DEFAULT_PRESENCE_CHECK_DELAY))
: DEFAULT_PRESENCE_CHECK_DELAY;
binder.linkToDeath(mReaderModeDeathRecipient, 0);
} catch (RemoteException e) {
Log.e(TAG, "Remote binder has already died.");
return;
}
} else {
try {
mReaderModeParams = null;
binder.unlinkToDeath(mReaderModeDeathRecipient, 0);
} catch (NoSuchElementException e) {
Log.e(TAG, "Reader mode Binder was never registered.");
}
}
applyRouting(false);
}
}
@Override
public INfcAdapterExtras getNfcAdapterExtrasInterface(String pkg) throws RemoteException {
NfcService.this.enforceNfceeAdminPerm(pkg);
return mExtrasService;
}
/// M: GSMA@ {
@Override
public INfcAdapterGsmaExtras getNfcAdapterGsmaExtrasInterface() throws RemoteException {
//NfcService.this.enforceNfceeAdminPerm(pkg);
return NfcGsmaExtraService.getInstance();
}
/// }
@Override
public void addNfcUnlockHandler(INfcUnlockHandler unlockHandler, int[] techList) {
NfcPermissions.enforceAdminPermissions(mContext);
int lockscreenPollMask = computeLockscreenPollMask(techList);
synchronized (NfcService.this) {
mNfcUnlockManager.addUnlockHandler(unlockHandler, lockscreenPollMask);
}
applyRouting(false);
}
@Override
public void removeNfcUnlockHandler(INfcUnlockHandler token) throws RemoteException {
synchronized (NfcService.this) {
mNfcUnlockManager.removeUnlockHandler(token.asBinder());
}
applyRouting(false);
}
private int computeLockscreenPollMask(int[] techList) {
Map techCodeToMask = new HashMap();
techCodeToMask.put(TagTechnology.NFC_A, NfcService.NFC_POLL_A);
techCodeToMask.put(TagTechnology.NFC_B, NfcService.NFC_POLL_B);
techCodeToMask.put(TagTechnology.NFC_V, NfcService.NFC_POLL_ISO15693);
techCodeToMask.put(TagTechnology.NFC_F, NfcService.NFC_POLL_F);
techCodeToMask.put(TagTechnology.NFC_BARCODE, NfcService.NFC_POLL_KOVIO);
int mask = 0;
for (int i = 0; i < techList.length; i++) {
if (techCodeToMask.containsKey(techList[i])) {
mask |= techCodeToMask.get(techList[i]).intValue();
}
}
return mask;
}
/// M: @ {
public int getModeFlag(int mode) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
return MtkNfcAddonSequence.getInstance().getModeFlag(mode, NfcService.this);
}
public void setModeFlag(int mode, int flag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
MtkNfcAddonSequence.getInstance().setModeFlag(isNfcEnabled(), mode, flag, NfcService.this);
}
/// }
}
final class ReaderModeDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {
synchronized (NfcService.this) {
Log.e(TAG, "binderDied() mReaderModeParams:"+mReaderModeParams);
if (mReaderModeParams != null) {
mReaderModeParams = null;
applyRouting(false);
}
}
}
}
final class TagService extends INfcTag.Stub {
@Override
public int close(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "close(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
/* Remove the device from the hmap */
unregisterObject(nativeHandle);
tag.disconnect();
return ErrorCodes.SUCCESS;
}
/* Restart polling loop for notification */
applyRouting(true);
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int connect(int nativeHandle, int technology) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag = null;
Log.d(TAG, "connect(..) nativeHandle:"+nativeHandle+" technology:"+technology);
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_DISCONNECT;
}
if (!tag.isPresent()) {
return ErrorCodes.ERROR_DISCONNECT;
}
// Note that on most tags, all technologies are behind a single
// handle. This means that the connect at the lower levels
// will do nothing, as the tag is already connected to that handle.
if (tag.connect(technology)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
@Override
public int reconnect(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "reconnect(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
if (tag.reconnect()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int[] getTechList(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
TagEndpoint tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
return tag.getTechList();
}
return null;
}
@Override
public boolean isPresent(int nativeHandle) throws RemoteException {
TagEndpoint tag = null;
Log.d(TAG, "isPresent(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return false;
}
return tag.isPresent();
}
@Override
public boolean isNdef(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "isNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
int[] ndefInfo = new int[2];
if (tag == null) {
return false;
}
return tag.checkNdef(ndefInfo);
}
@Override
public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw)
throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "transceive(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
byte[] response;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// Check if length is within limits
if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) {
return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null);
}
int[] targetLost = new int[1];
response = tag.transceive(data, raw, targetLost);
int result;
if (response != null) {
result = TransceiveResult.RESULT_SUCCESS;
} else if (targetLost[0] == 1) {
result = TransceiveResult.RESULT_TAGLOST;
} else {
result = TransceiveResult.RESULT_FAILURE;
}
return new TransceiveResult(result, response);
}
return null;
}
@Override
public NdefMessage ndefRead(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
Log.d(TAG, "ndefRead(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
byte[] buf = tag.readNdef();
if (buf == null) {
return null;
}
/* Create an NdefMessage */
try {
return new NdefMessage(buf);
} catch (FormatException e) {
return null;
}
}
return null;
}
@Override
public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (msg == null) return ErrorCodes.ERROR_INVALID_PARAM;
if (tag.writeNdef(msg.toByteArray())) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public boolean ndefIsWritable(int nativeHandle) throws RemoteException {
throw new UnsupportedOperationException();
}
@Override
public int ndefMakeReadOnly(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.makeReadOnly()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public int formatNdef(int nativeHandle, byte[] key) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "formatNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.formatNdef(key)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public Tag rediscover(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "rediscover(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// For now the prime usecase for rediscover() is to be able
// to access the NDEF technology after formatting without
// having to remove the tag from the field, or similar
// to have access to NdefFormatable in case low-level commands
// were used to remove NDEF. So instead of doing a full stack
// rediscover (which is poorly supported at the moment anyway),
// we simply remove these two technologies and detect them
// again.
tag.removeTechnology(TagTechnology.NDEF);
tag.removeTechnology(TagTechnology.NDEF_FORMATABLE);
tag.findAndReadNdef();
// Build a new Tag object to return
Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
tag.getTechExtras(), tag.getHandle(), this);
return newTag;
}
return null;
}
@Override
public int setTimeout(int tech, int timeout) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "setTimeout(..) tech:"+tech+" timeout:"+timeout);
boolean success = mDeviceHost.setTimeout(tech, timeout);
if (success) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_INVALID_PARAM;
}
}
@Override
public int getTimeout(int tech) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
return mDeviceHost.getTimeout(tech);
}
@Override
public void resetTimeouts() throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
mDeviceHost.resetTimeouts();
}
@Override
public boolean canMakeReadOnly(int ndefType) throws RemoteException {
return mDeviceHost.canMakeReadOnly(ndefType);
}
@Override
public int getMaxTransceiveLength(int tech) throws RemoteException {
return mDeviceHost.getMaxTransceiveLength(tech);
}
@Override
public boolean getExtendedLengthApdusSupported() throws RemoteException {
return mDeviceHost.getExtendedLengthApdusSupported();
}
}
boolean isNfcEnabledOrShuttingDown() {
synchronized (this) {
return (mState == NfcAdapter.STATE_ON || mState == NfcAdapter.STATE_TURNING_OFF);
}
}
boolean isNfcEnabled() {
synchronized (this) {
return mState == NfcAdapter.STATE_ON;
}
}
class WatchDogThread extends Thread {
final Object mCancelWaiter = new Object();
final int mTimeout;
boolean mCanceled = false;
public WatchDogThread(String threadName, int timeout) {
super(threadName);
mTimeout = timeout;
}
@Override
public void run() {
try {
synchronized (mCancelWaiter) {
mCancelWaiter.wait(mTimeout);
if (mCanceled) {
return;
}
}
} catch (InterruptedException e) {
// Should not happen; fall-through to abort.
Log.w(TAG, "Watchdog thread interruped.");
interrupt();
}
Log.e(TAG, "Watchdog triggered, aborting.");
mDeviceHost.doAbort();
}
public synchronized void cancel() {
synchronized (mCancelWaiter) {
mCanceled = true;
mCancelWaiter.notify();
}
}
}
static byte[] hexStringToBytes(String s) {
if (s == null || s.length() == 0) return null;
int len = s.length();
if (len % 2 != 0) {
s = '0' + s;
len++;
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
/**
* Read mScreenState and apply NFC-C polling and NFC-EE routing
*/
void applyRouting(boolean force) {
Log.d("oyp", "<----applyRouting()---->");
synchronized (this) {
/// M: @ {
if (!isNfcEnabledOrShuttingDown() || mOpenEe != null) {
return;
}
/// }
WatchDogThread watchDog = new WatchDogThread("applyRouting", ROUTING_WATCHDOG_MS);
if (mInProvisionMode) {
mInProvisionMode = Settings.Secure.getInt(mContentResolver,
Settings.Global.DEVICE_PROVISIONED, 0) == 0;
if (!mInProvisionMode) {
// Notify dispatcher it's fine to dispatch to any package now
// and allow handover transfers.
mNfcDispatcher.disableProvisioningMode();
}
}
// Special case: if we're transitioning to unlocked state while
// still talking to a tag, postpone re-configuration.
if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && isTagPresent()) {
Log.d(TAG, "Not updating discovery parameters, tag connected.");
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RESUME_POLLING),
APPLY_ROUTING_RETRY_TIMEOUT_MS);
return;
}
/// M: @ {
// configure NFC-EE routing
if (mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
if (force || !mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE ON");
mNfceeRouteEnabled = true;
mNativeNfcManager.deselectSecureElement();
}
} else {
if (force || mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE OFF");
mNfceeRouteEnabled = false;//edited by ouyang [2015-10-16] start
if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) { /// M: only deselect when using Google Wallet
mNativeNfcManager.deselectSecureElement();
}
}
}
/// }
try {
watchDog.start();
// Compute new polling parameters
NfcDiscoveryParameters newParams = computeDiscoveryParameters(mScreenState);
if (force || !newParams.equals(mCurrentDiscoveryParameters)) {
Log.d(TAG, "newParams.shouldEnableDiscovery()");
if (newParams.shouldEnableDiscovery()) {
Log.d(TAG, "mCurrentDiscoveryParameters.shouldEnableDiscovery()");
boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery();
Log.d(TAG, "shouldRestart:"+shouldRestart);
mDeviceHost.enableDiscovery(newParams, shouldRestart);
} else {
mDeviceHost.disableDiscovery();
}
mCurrentDiscoveryParameters = newParams;
} else {
Log.d(TAG, "Discovery configuration equal, not updating.");
}
} finally {
watchDog.cancel();
}
}
}
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
// if (screenState >= NFC_POLLING_MODE) {
//edited by ouyang [2015-10-19 11:13:17]
if (screenState >= getNFC_POLLING_MODE()) {
// Check if reader-mode is enabled
Log.d("oyp", "1、NFC_POLLING_MODE="+getNFC_POLLING_MODE());
修改之前的代码如下:
/* * Copyright (C) 2014 MediaTek Inc. * Modification based on code covered by the mentioned copyright * and/or permission notice(s). */ /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.nfc; import android.app.ActivityManager; import android.app.Application; import android.app.KeyguardManager; import android.app.PendingIntent; import android.app.admin.DevicePolicyManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.IPackageManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.content.res.Resources.NotFoundException; import android.media.AudioManager; import android.media.SoundPool; import android.nfc.BeamShareData; import android.nfc.ErrorCodes; import android.nfc.FormatException; import android.nfc.IAppCallback; import android.nfc.INfcAdapter; import android.nfc.INfcAdapterExtras; import android.nfc.INfcCardEmulation; import android.nfc.INfcTag; import android.nfc.INfcUnlockHandler; import android.nfc.NdefMessage; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.TechListParcel; import android.nfc.TransceiveResult; import android.nfc.tech.Ndef; import android.nfc.tech.TagTechnology; import android.os.AsyncTask; import android.os.Binder; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.PowerManager; import android.os.Process; import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; import android.util.Log; import com.android.nfc.DeviceHost.DeviceHostListener; import com.android.nfc.DeviceHost.LlcpConnectionlessSocket; import com.android.nfc.DeviceHost.LlcpServerSocket; import com.android.nfc.DeviceHost.LlcpSocket; import com.android.nfc.DeviceHost.NfcDepEndpoint; import com.android.nfc.DeviceHost.TagEndpoint; import com.android.nfc.cardemulation.CardEmulationManager; import com.android.nfc.dhimpl.NativeNfcManager; import com.android.nfc.handover.HandoverDataParser; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Arrays; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; /// M: @ { import android.os.SystemClock; import com.mediatek.nfc.handover.MtkNfcEntry; import com.android.nfc.dhimpl.NativeNfcSecureElement; import java.io.IOException; import java.util.HashSet; import java.util.concurrent.ExecutionException; import com.mediatek.nfc.addon.MtkNfcAddonSequence; import com.mediatek.nfc.addon.NfcStatusNotificationUi; import com.mediatek.nfc.addon.Utility; import android.widget.Toast; import android.database.ContentObserver; import android.net.Uri; import com.mediatek.nfc.gsmahandset.NfcGsmaExtraService; import android.nfc.INfcAdapterGsmaExtras; import android.os.SystemProperties; /// } public class NfcService implements DeviceHostListener { static final boolean DBG = true; static final String TAG = "NfcService"; public static final String SERVICE_NAME = "nfc"; public static final String PREF = "NfcServicePrefs"; static final String PREF_NFC_ON = "nfc_on"; static final boolean NFC_ON_DEFAULT = true; static final String PREF_NDEF_PUSH_ON = "ndef_push_on"; static final boolean NDEF_PUSH_ON_DEFAULT = true; static final String PREF_FIRST_BEAM = "first_beam"; static final String PREF_FIRST_BOOT = "first_boot"; static final String PREF_AIRPLANE_OVERRIDE = "airplane_override"; /// M: @ { public static final String NFC_HCE_ON = "nfc_hce_on"; //value type: int,0 for Off, 1 for on /// } static final int MSG_NDEF_TAG = 0; static final int MSG_LLCP_LINK_ACTIVATION = 1; static final int MSG_LLCP_LINK_DEACTIVATED = 2; static final int MSG_MOCK_NDEF = 3; static final int MSG_LLCP_LINK_FIRST_PACKET = 4; static final int MSG_ROUTE_AID = 5; static final int MSG_UNROUTE_AID = 6; static final int MSG_COMMIT_ROUTING = 7; static final int MSG_INVOKE_BEAM = 8; static final int MSG_RF_FIELD_ACTIVATED = 9; static final int MSG_RF_FIELD_DEACTIVATED = 10; static final int MSG_RESUME_POLLING = 11; static final long MAX_POLLING_PAUSE_TIMEOUT = 40000; static final int TASK_ENABLE = 1; static final int TASK_DISABLE = 2; static final int TASK_BOOT = 3; // Polling technology masks static final int NFC_POLL_A = 0x01; static final int NFC_POLL_B = 0x02; static final int NFC_POLL_F = 0x04; static final int NFC_POLL_ISO15693 = 0x08; static final int NFC_POLL_B_PRIME = 0x10; static final int NFC_POLL_KOVIO = 0x20; /// M: @ { // Copied from com.android.nfc_extras to avoid library dependency // Must keep in sync with com.android.nfc_extras static final int ROUTE_OFF = 1; static final int ROUTE_ON_WHEN_SCREEN_ON = 2; // Return values from NfcEe.open() - these are 1:1 mapped // to the thrown EE_EXCEPTION_ exceptions in nfc-extras. static final int EE_ERROR_IO = -1; static final int EE_ERROR_ALREADY_OPEN = -2; static final int EE_ERROR_INIT = -3; static final int EE_ERROR_LISTEN_MODE = -4; static final int EE_ERROR_EXT_FIELD = -5; static final int EE_ERROR_NFC_DISABLED = -6; // Amount of time to wait before closing the NFCEE connection // in a disable/shutdown scenario. static final int WAIT_FOR_NFCEE_OPERATIONS_MS = 5000; // Polling interval for waiting on NFCEE operations static final int WAIT_FOR_NFCEE_POLL_MS = 100; /// } // minimum screen state that enables NFC polling static final int NFC_POLLING_MODE = ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED; // Time to wait for NFC controller to initialize before watchdog // goes off. This time is chosen large, because firmware download // may be a part of initialization. static final int INIT_WATCHDOG_MS = 90000; // Time to wait for routing to be applied before watchdog // goes off static final int ROUTING_WATCHDOG_MS = 10000; // Default delay used for presence checks static final int DEFAULT_PRESENCE_CHECK_DELAY = 125; // The amount of time we wait before manually launching // the Beam animation when called through the share menu. static final int INVOKE_BEAM_DELAY_MS = 1000; // RF field events as defined in NFC extras public static final String ACTION_RF_FIELD_ON_DETECTED = "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; public static final String ACTION_RF_FIELD_OFF_DETECTED = "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED"; // for use with playSound() public static final int SOUND_START = 0; public static final int SOUND_END = 1; public static final int SOUND_ERROR = 2; public static final String ACTION_LLCP_UP = "com.android.nfc.action.LLCP_UP"; public static final String ACTION_LLCP_DOWN = "com.android.nfc.action.LLCP_DOWN"; // Timeout to re-apply routing if a tag was present and we postponed it private static final int APPLY_ROUTING_RETRY_TIMEOUT_MS = 5000; private final UserManager mUserManager; // NFC Execution Environment // fields below are protected by this private final ReaderModeDeathRecipient mReaderModeDeathRecipient = new ReaderModeDeathRecipient(); private final NfcUnlockManager mNfcUnlockManager; private final NfceeAccessControl mNfceeAccessControl; List mInstalledPackages; // cached version of installed packages // fields below are used in multiple threads and protected by synchronized(this) final HashMap mObjectMap = new HashMap(); int mScreenState; boolean mInProvisionMode; // whether we're in setup wizard and enabled NFC provisioning boolean mIsNdefPushEnabled; NfcDiscoveryParameters mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters(); ReaderModeParams mReaderModeParams; // mState is protected by this, however it is only modified in onCreate() // and the default AsyncTask thread so it is read unprotected from that // thread int mState; // one of NfcAdapter.STATE_ON, STATE_TURNING_ON, etc // fields below are final after onCreate() Context mContext; private DeviceHost mDeviceHost; private SharedPreferences mPrefs; private SharedPreferences.Editor mPrefsEditor; private PowerManager.WakeLock mRoutingWakeLock; int mStartSound; int mEndSound; int mErrorSound; SoundPool mSoundPool; // playback synchronized on this P2pLinkManager mP2pLinkManager; TagService mNfcTagService; NfcAdapterService mNfcAdapter; boolean mIsAirplaneSensitive; boolean mIsAirplaneToggleable; boolean mIsDebugBuild; boolean mIsHceCapable; boolean mPollingPaused; private NfcDispatcher mNfcDispatcher; private PowerManager mPowerManager; private KeyguardManager mKeyguard; private HandoverDataParser mHandoverDataParser; private ContentResolver mContentResolver; private CardEmulationManager mCardEmulationManager; private ScreenStateHelper mScreenStateHelper; private ForegroundUtils mForegroundUtils; private int mUserId; private static NfcService sService; public static NfcService getInstance() { return sService; } @Override public void onRemoteEndpointDiscovered(TagEndpoint tag) { sendMessage(NfcService.MSG_NDEF_TAG, tag); } /** * Notifies transaction */ @Override public void onHostCardEmulationActivated() { /// M: @ { if (mCardEmulationManager != null && mIsHceOn) { mCardEmulationManager.onHostCardEmulationActivated(); } /// } } @Override public void onHostCardEmulationData(byte[] data) { /// M: @ { if (mCardEmulationManager != null && mIsHceOn) { mCardEmulationManager.onHostCardEmulationData(data); } /// } } @Override public void onHostCardEmulationDeactivated() { if (mCardEmulationManager != null) { mCardEmulationManager.onHostCardEmulationDeactivated(); } } /** * Notifies P2P Device detected, to activate LLCP link */ @Override public void onLlcpLinkActivated(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device); } /** * Notifies P2P Device detected, to activate LLCP link */ @Override public void onLlcpLinkDeactivated(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device); } /** * Notifies P2P Device detected, first packet received over LLCP link */ @Override public void onLlcpFirstPacketReceived(NfcDepEndpoint device) { sendMessage(NfcService.MSG_LLCP_LINK_FIRST_PACKET, device); } @Override public void onRemoteFieldActivated() { sendMessage(NfcService.MSG_RF_FIELD_ACTIVATED, null); } public void onRemoteFieldDeactivated() { sendMessage(NfcService.MSG_RF_FIELD_DEACTIVATED, null); } final class ReaderModeParams { public int flags; public IAppCallback callback; public int presenceCheckDelay; } public NfcService(Application nfcApplication) { mUserId = ActivityManager.getCurrentUser(); mContext = nfcApplication; mNfcTagService = new TagService(); mNfcAdapter = new NfcAdapterService(); Log.i(TAG, "Starting NFC service"); sService = this; mScreenStateHelper = new ScreenStateHelper(mContext); mContentResolver = mContext.getContentResolver(); /// M: @{ mNativeNfcManager = new NativeNfcManager(mContext, this); mDeviceHost = (DeviceHost) mNativeNfcManager; /// } mNfcUnlockManager = NfcUnlockManager.getInstance(); mHandoverDataParser = new HandoverDataParser(); boolean isNfcProvisioningEnabled = false; try { isNfcProvisioningEnabled = mContext.getResources().getBoolean( R.bool.enable_nfc_provisioning); } catch (NotFoundException e) { } if (isNfcProvisioningEnabled) { mInProvisionMode = Settings.Secure.getInt(mContentResolver, Settings.Global.DEVICE_PROVISIONED, 0) == 0; } else { mInProvisionMode = false; } mNfcDispatcher = new NfcDispatcher(mContext, mHandoverDataParser, mInProvisionMode); mP2pLinkManager = new P2pLinkManager(mContext, mHandoverDataParser, mDeviceHost.getDefaultLlcpMiu(), mDeviceHost.getDefaultLlcpRwSize()); mPrefs = mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE); mPrefsEditor = mPrefs.edit(); mNfceeAccessControl = new NfceeAccessControl(mContext); mState = NfcAdapter.STATE_OFF; mIsNdefPushEnabled = mPrefs.getBoolean(PREF_NDEF_PUSH_ON, NDEF_PUSH_ON_DEFAULT); setBeamShareActivityState(mIsNdefPushEnabled); mIsDebugBuild = "userdebug".equals(Build.TYPE) || "eng".equals(Build.TYPE); mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mRoutingWakeLock = mPowerManager.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "NfcService:mRoutingWakeLock"); mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); mScreenState = mScreenStateHelper.checkScreenState(); /// M: @ { if (NativeNfcManager.sIsUnsupportedChip) { return; } /// } ServiceManager.addService(SERVICE_NAME, mNfcAdapter); // Intents for all users IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_PRESENT); filter.addAction(Intent.ACTION_USER_SWITCHED); registerForAirplaneMode(filter); mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, null); IntentFilter ownerFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); ownerFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); mContext.registerReceiver(mOwnerReceiver, ownerFilter); ownerFilter = new IntentFilter(); ownerFilter.addAction(Intent.ACTION_PACKAGE_ADDED); ownerFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); ownerFilter.addDataScheme("package"); mContext.registerReceiver(mOwnerReceiver, ownerFilter); IntentFilter policyFilter = new IntentFilter(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); mContext.registerReceiverAsUser(mPolicyReceiver, UserHandle.ALL, policyFilter, null, null); updatePackageCache(); PackageManager pm = mContext.getPackageManager(); mIsHceCapable = pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION); if (mIsHceCapable) { mCardEmulationManager = new CardEmulationManager(mContext); } mForegroundUtils = ForegroundUtils.getInstance(); /// M: { initAddon(); /// } new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks } void initSoundPool() { synchronized (this) { if (mSoundPool == null) { mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); /// M: bug-fix for SoundPool issues @ { Utility.SoundPoolListener soundPoolListener = new Utility.SoundPoolListener(mSoundPool); mStartSound = mSoundPool.load(mContext, R.raw.start, 1); mEndSound = mSoundPool.load(mContext, R.raw.end, 1); mErrorSound = mSoundPool.load(mContext, R.raw.error, 1); soundPoolListener.waitForSamplesReady(new int[] {mStartSound, mEndSound, mErrorSound}); /// } } } } void releaseSoundPool() { synchronized (this) { if (mSoundPool != null) { mSoundPool.release(); mSoundPool = null; } } } void registerForAirplaneMode(IntentFilter filter) { final String airplaneModeRadios = Settings.System.getString(mContentResolver, Settings.Global.AIRPLANE_MODE_RADIOS); final String toggleableRadios = Settings.System.getString(mContentResolver, Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); mIsAirplaneSensitive = airplaneModeRadios == null ? true : airplaneModeRadios.contains(Settings.Global.RADIO_NFC); mIsAirplaneToggleable = toggleableRadios == null ? false : toggleableRadios.contains(Settings.Global.RADIO_NFC); if (mIsAirplaneSensitive) { filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); } } void updatePackageCache() { PackageManager pm = mContext.getPackageManager(); List packages = pm.getInstalledPackages(0, UserHandle.USER_OWNER); synchronized (this) { mInstalledPackages = packages; } } /** * Manages tasks that involve turning on/off the NFC controller. *
* All work that might turn the NFC adapter on or off must be done
* through this task, to keep the handling of mState simple.
* In other words, mState is only modified in these tasks (and we
* don't need a lock to read it in these tasks).
*
* These tasks are all done on the same AsyncTask background
* thread, so they are serialized. Each task may temporarily transition
* mState to STATE_TURNING_OFF or STATE_TURNING_ON, but must exit in
* either STATE_ON or STATE_OFF. This way each task can be guaranteed
* of starting in either STATE_OFF or STATE_ON, without needing to hold
* NfcService.this for the entire task.
*
* AsyncTask's are also implicitly queued. This is useful for corner
* cases like turning airplane mode on while TASK_ENABLE is in progress.
* The TASK_DISABLE triggered by airplane mode will be correctly executed
* immediately after TASK_ENABLE is complete. This seems like the most sane
* way to deal with these situations.
*
* {@link #TASK_ENABLE} enables the NFC adapter, without changing
* preferences
*
{@link #TASK_DISABLE} disables the NFC adapter, without changing
* preferences
*
{@link #TASK_BOOT} does first boot work and may enable NFC
*/
class EnableDisableTask extends AsyncTask {
/// M: @ {
EnableDisableTask() {
MtkNfcAddonSequence.getInstance().setScenario(MtkNfcAddonSequence.DISABLE_CARD_MODE_OFF);
}
EnableDisableTask(int scenario) {
MtkNfcAddonSequence.getInstance().setScenario(scenario);
}
/// }
@Override
protected Void doInBackground(Integer... params) {
// Sanity check mState
switch (mState) {
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_TURNING_ON:
Log.e(TAG, "Processing EnableDisable task " + params[0] + " from bad state " +
mState);
return null;
}
/* AsyncTask sets this thread to THREAD_PRIORITY_BACKGROUND,
* override with the default. THREAD_PRIORITY_BACKGROUND causes
* us to service software I2C too slow for firmware download
* with the NXP PN544.
* TODO: move this to the DAL I2C layer in libnfc-nxp, since this
* problem only occurs on I2C platforms using PN544
*/
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
switch (params[0].intValue()) {
case TASK_ENABLE:
enableInternal();
break;
case TASK_DISABLE:
disableInternal();
break;
case TASK_BOOT:
Log.d(TAG, "checking on firmware download");
boolean airplaneOverride = mPrefs.getBoolean(PREF_AIRPLANE_OVERRIDE, false);
if (mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT) &&
(!mIsAirplaneSensitive || !isAirplaneModeOn() || airplaneOverride)) {
Log.d(TAG, "NFC is on. Doing normal stuff");
enableInternal();
} else {
Log.d(TAG, "NFC is off. Checking firmware version");
mDeviceHost.checkFirmware();
}
if (mPrefs.getBoolean(PREF_FIRST_BOOT, true)) {
Log.i(TAG, "First Boot");
mPrefsEditor.putBoolean(PREF_FIRST_BOOT, false);
mPrefsEditor.apply();
}
break;
}
// Restore default AsyncTask priority
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return null;
}
/**
* Enable NFC adapter functions.
* Does not toggle preferences.
*/
boolean enableInternal() {
if (mState == NfcAdapter.STATE_ON) {
return true;
}
/// M: @ {
// TODO:: updateState to on, move synchronized object
synchronized (NfcService.this) {
Log.i(TAG, "Enabling NFC");
updateState(NfcAdapter.STATE_TURNING_ON);
WatchDogThread watchDog = new WatchDogThread("enableInternal", INIT_WATCHDOG_MS);
watchDog.start();
try {
mRoutingWakeLock.acquire();
try {
if (!mDeviceHost.initialize()) {
Log.w(TAG, "Error enabling NFC");
updateState(NfcAdapter.STATE_OFF);
return false;
}
} finally {
mRoutingWakeLock.release();
}
} finally {
watchDog.cancel();
}
if (mIsHceCapable) {
// Generate the initial card emulation routing table
mCardEmulationManager.onNfcEnabled();
}
//synchronized (NfcService.this) {
/// }
mObjectMap.clear();
mP2pLinkManager.enableDisable(mIsNdefPushEnabled, true);
updateState(NfcAdapter.STATE_ON);
/// M: @ {
mScreenState = mScreenStateHelper.checkScreenState();
/// }
}
initSoundPool();
/* Start polling loop */
applyRouting(true);
/// M: @ {
Log.d(TAG, "showNotification()");
//start weixj at 20151013 Don't show the NFC notification
if(!SystemProperties.isJ5Version()){
NfcStatusNotificationUi.getInstance().showNotification();
}
/// }
//end
return true;
}
/**
* Disable all NFC adapter functions.
* Does not toggle preferences.
*/
boolean disableInternal() {
if (mState == NfcAdapter.STATE_OFF) {
return true;
}
Log.i(TAG, "Disabling NFC");
updateState(NfcAdapter.STATE_TURNING_OFF);
/* Sometimes mDeviceHost.deinitialize() hangs, use a watch-dog.
* Implemented with a new thread (instead of a Handler or AsyncTask),
* because the UI Thread and AsyncTask thread-pools can also get hung
* when the NFC controller stops responding */
WatchDogThread watchDog = new WatchDogThread("disableInternal", ROUTING_WATCHDOG_MS);
watchDog.start();
if (mIsHceCapable) {
mCardEmulationManager.onNfcDisabled();
}
/// M: @ {
boolean result = false;
synchronized (NfcService.this) {
mP2pLinkManager.enableDisable(false, false);
/* The NFC-EE may still be opened by another process,
* and a transceive() could still be in progress on
* another Binder thread.
* Give it a while to finish existing operations
* before we close it.
*/
Long startTime = SystemClock.elapsedRealtime();
do {
synchronized (NfcService.this) {
if (mOpenEe == null)
break;
}
try {
Thread.sleep(WAIT_FOR_NFCEE_POLL_MS);
} catch (InterruptedException e) {
// Ignore
}
} while (SystemClock.elapsedRealtime() - startTime < WAIT_FOR_NFCEE_OPERATIONS_MS);
synchronized (NfcService.this) {
if (mOpenEe != null) {
try {
_nfcEeClose(-1, mOpenEe.binder);
} catch (IOException e) { }
}
}
// Stop watchdog if tag present
// A convenient way to stop the watchdog properly consists of
// disconnecting the tag. The polling loop shall be stopped before
// to avoid the tag being discovered again.
maybeDisconnectTarget();
mNfcDispatcher.setForegroundDispatch(null, null, null);
result = mDeviceHost.deinitialize();
if (DBG) Log.d(TAG, "mDeviceHost.deinitialize() = " + result);
//mNfcPollingEnabled = false;/// MTK: potential bug fix
}
/// }
watchDog.cancel();
synchronized (NfcService.this) {
mCurrentDiscoveryParameters = NfcDiscoveryParameters.getNfcOffParameters();
updateState(NfcAdapter.STATE_OFF);
}
/// M: @ {
//releaseSoundPool();
Log.d(TAG, "hideNotification()");
NfcStatusNotificationUi.getInstance().hideNotification();
/// }
return result;
}
void updateState(int newState) {
synchronized (NfcService.this) {
if (newState == mState) {
return;
}
mState = newState;
Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, mState);
mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
}
}
}
void saveNfcOnSetting(boolean on) {
synchronized (NfcService.this) {
mPrefsEditor.putBoolean(PREF_NFC_ON, on);
mPrefsEditor.apply();
}
}
public void playSound(int sound) {
synchronized (this) {
if (mSoundPool == null) {
Log.w(TAG, "Not playing sound when NFC is disabled");
return;
}
switch (sound) {
case SOUND_START:
mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_END:
mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
case SOUND_ERROR:
mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f);
break;
}
}
}
synchronized int getUserId() {
return mUserId;
}
void setBeamShareActivityState(boolean enabled) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
// Propagate the state change to all user profiles related to the current
// user. Note that the list returned by getUserProfiles contains the
// current user.
List luh = um.getUserProfiles();
for (UserHandle uh : luh){
enforceBeamShareActivityPolicy(mContext, uh, enabled);
}
}
void enforceBeamShareActivityPolicy(Context context, UserHandle uh,
boolean isGlobalEnabled){
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
IPackageManager mIpm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
boolean isActiveForUser =
(!um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM, uh)) &&
isGlobalEnabled;
if (DBG){
Log.d(TAG, "Enforcing a policy change on user: " + uh +
", isActiveForUser = " + isActiveForUser);
}
try {
mIpm.setComponentEnabledSetting(new ComponentName(
BeamShareActivity.class.getPackageName$(),
BeamShareActivity.class.getName()),
isActiveForUser ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
uh.getIdentifier());
} catch (RemoteException e) {
Log.w(TAG, "Unable to change Beam status for user " + uh);
}
}
final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
saveNfcOnSetting(true);
if (mIsAirplaneSensitive && isAirplaneModeOn()) {
if (!mIsAirplaneToggleable) {
Log.i(TAG, "denying enable() request (airplane mode)");
return false;
}
// Make sure the override survives a reboot
mPrefsEditor.putBoolean(PREF_AIRPLANE_OVERRIDE, true);
mPrefsEditor.apply();
}
new EnableDisableTask().execute(TASK_ENABLE);
return true;
}
@Override
public boolean disable(boolean saveState) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
if (saveState) {
saveNfcOnSetting(false);
}
new EnableDisableTask().execute(TASK_DISABLE);
return true;
}
@Override
public void pausePolling(int timeoutInMs) {
NfcPermissions.enforceAdminPermissions(mContext);
if (timeoutInMs <= 0 || timeoutInMs > MAX_POLLING_PAUSE_TIMEOUT) {
Log.e(TAG, "Refusing to pause polling for " + timeoutInMs + "ms.");
return;
}
synchronized (NfcService.this) {
mPollingPaused = true;
mDeviceHost.disableDiscovery();
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_RESUME_POLLING), timeoutInMs);
}
}
@Override
public void resumePolling() {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mPollingPaused) {
return;
}
mHandler.removeMessages(MSG_RESUME_POLLING);
mPollingPaused = false;
new ApplyRoutingTask().execute();
}
}
@Override
public boolean isNdefPushEnabled() throws RemoteException {
synchronized (NfcService.this) {
return mState == NfcAdapter.STATE_ON && mIsNdefPushEnabled;
}
}
@Override
public boolean enableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "enabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, true);
mPrefsEditor.apply();
mIsNdefPushEnabled = true;
setBeamShareActivityState(true);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(true, true);
}
}
return true;
}
@Override
public boolean disableNdefPush() throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
synchronized (NfcService.this) {
if (!mIsNdefPushEnabled) {
return true;
}
Log.i(TAG, "disabling NDEF Push");
mPrefsEditor.putBoolean(PREF_NDEF_PUSH_ON, false);
mPrefsEditor.apply();
mIsNdefPushEnabled = false;
setBeamShareActivityState(false);
if (isNfcEnabled()) {
mP2pLinkManager.enableDisable(false, true);
}
}
return true;
}
@Override
public void setForegroundDispatch(PendingIntent intent,
IntentFilter[] filters, TechListParcel techListsParcel) {
NfcPermissions.enforceUserPermissions(mContext);
// Short-cut the disable path
if (intent == null && filters == null && techListsParcel == null) {
mNfcDispatcher.setForegroundDispatch(null, null, null);
return;
}
// Validate the IntentFilters
if (filters != null) {
if (filters.length == 0) {
filters = null;
} else {
for (IntentFilter filter : filters) {
if (filter == null) {
throw new IllegalArgumentException("null IntentFilter");
}
}
}
}
// Validate the tech lists
String[][] techLists = null;
if (techListsParcel != null) {
techLists = techListsParcel.getTechLists();
}
mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
}
@Override
public void setAppCallback(IAppCallback callback) {
NfcPermissions.enforceUserPermissions(mContext);
// don't allow Beam for managed profiles, or devices with a device owner or policy owner
UserInfo userInfo = mUserManager.getUserInfo(UserHandle.getCallingUserId());
if(!mUserManager.hasUserRestriction(
UserManager.DISALLOW_OUTGOING_BEAM, userInfo.getUserHandle())) {
mP2pLinkManager.setNdefCallback(callback, Binder.getCallingUid());
} else if (DBG) {
Log.d(TAG, "Disabling default Beam behavior");
}
}
@Override
public void verifyNfcPermission() {
NfcPermissions.enforceUserPermissions(mContext);
}
@Override
public void invokeBeam() {
NfcPermissions.enforceUserPermissions(mContext);
if (mForegroundUtils.isInForeground(Binder.getCallingUid())) {
mP2pLinkManager.onManualBeamInvoke(null);
} else {
Log.e(TAG, "Calling activity not in foreground.");
}
}
@Override
public void invokeBeamInternal(BeamShareData shareData) {
NfcPermissions.enforceAdminPermissions(mContext);
Message msg = Message.obtain();
msg.what = MSG_INVOKE_BEAM;
msg.obj = shareData;
// We have to send this message delayed for two reasons:
// 1) This is an IPC call from BeamShareActivity, which is
// running when the user has invoked Beam through the
// share menu. As soon as BeamShareActivity closes, the UI
// will need some time to rebuild the original Activity.
// Waiting here for a while gives a better chance of the UI
// having been rebuilt, which means the screenshot that the
// Beam animation is using will be more accurate.
// 2) Similarly, because the Activity that launched BeamShareActivity
// with an ACTION_SEND intent is now in paused state, the NDEF
// callbacks that it has registered may no longer be valid.
// Allowing the original Activity to resume will make sure we
// it has a chance to re-register the NDEF message / callback,
// so we share the right data.
//
// Note that this is somewhat of a hack because the delay may not actually
// be long enough for 2) on very slow devices, but there's no better
// way to do this right now without additional framework changes.
mHandler.sendMessageDelayed(msg, INVOKE_BEAM_DELAY_MS);
}
@Override
public INfcTag getNfcTagInterface() throws RemoteException {
return mNfcTagService;
}
@Override
public INfcCardEmulation getNfcCardEmulationInterface() {
if (mIsHceCapable) {
return mCardEmulationManager.getNfcCardEmulationInterface();
} else {
return null;
}
}
@Override
public int getState() throws RemoteException {
synchronized (NfcService.this) {
return mState;
}
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
NfcService.this.dump(fd, pw, args);
}
@Override
public void dispatch(Tag tag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mNfcDispatcher.dispatchTag(tag);
}
@Override
public void setP2pModes(int initiatorModes, int targetModes) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
mDeviceHost.setP2pInitiatorModes(initiatorModes);
mDeviceHost.setP2pTargetModes(targetModes);
applyRouting(true);
}
@Override
public void setReaderMode(IBinder binder, IAppCallback callback, int flags, Bundle extras)
throws RemoteException {
Log.e(TAG, "================================");
Log.e(TAG, "==== setReaderMode flags:"+flags);
synchronized (NfcService.this) {
if (flags != 0) {
try {
mReaderModeParams = new ReaderModeParams();
mReaderModeParams.callback = callback;
mReaderModeParams.flags = flags;
mReaderModeParams.presenceCheckDelay = extras != null
? (extras.getInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY,
DEFAULT_PRESENCE_CHECK_DELAY))
: DEFAULT_PRESENCE_CHECK_DELAY;
binder.linkToDeath(mReaderModeDeathRecipient, 0);
} catch (RemoteException e) {
Log.e(TAG, "Remote binder has already died.");
return;
}
} else {
try {
mReaderModeParams = null;
binder.unlinkToDeath(mReaderModeDeathRecipient, 0);
} catch (NoSuchElementException e) {
Log.e(TAG, "Reader mode Binder was never registered.");
}
}
applyRouting(false);
}
}
@Override
public INfcAdapterExtras getNfcAdapterExtrasInterface(String pkg) throws RemoteException {
NfcService.this.enforceNfceeAdminPerm(pkg);
return mExtrasService;
}
/// M: GSMA@ {
@Override
public INfcAdapterGsmaExtras getNfcAdapterGsmaExtrasInterface() throws RemoteException {
//NfcService.this.enforceNfceeAdminPerm(pkg);
return NfcGsmaExtraService.getInstance();
}
/// }
@Override
public void addNfcUnlockHandler(INfcUnlockHandler unlockHandler, int[] techList) {
NfcPermissions.enforceAdminPermissions(mContext);
int lockscreenPollMask = computeLockscreenPollMask(techList);
synchronized (NfcService.this) {
mNfcUnlockManager.addUnlockHandler(unlockHandler, lockscreenPollMask);
}
applyRouting(false);
}
@Override
public void removeNfcUnlockHandler(INfcUnlockHandler token) throws RemoteException {
synchronized (NfcService.this) {
mNfcUnlockManager.removeUnlockHandler(token.asBinder());
}
applyRouting(false);
}
private int computeLockscreenPollMask(int[] techList) {
Map techCodeToMask = new HashMap();
techCodeToMask.put(TagTechnology.NFC_A, NfcService.NFC_POLL_A);
techCodeToMask.put(TagTechnology.NFC_B, NfcService.NFC_POLL_B);
techCodeToMask.put(TagTechnology.NFC_V, NfcService.NFC_POLL_ISO15693);
techCodeToMask.put(TagTechnology.NFC_F, NfcService.NFC_POLL_F);
techCodeToMask.put(TagTechnology.NFC_BARCODE, NfcService.NFC_POLL_KOVIO);
int mask = 0;
for (int i = 0; i < techList.length; i++) {
if (techCodeToMask.containsKey(techList[i])) {
mask |= techCodeToMask.get(techList[i]).intValue();
}
}
return mask;
}
/// M: @ {
public int getModeFlag(int mode) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
return MtkNfcAddonSequence.getInstance().getModeFlag(mode, NfcService.this);
}
public void setModeFlag(int mode, int flag) throws RemoteException {
NfcPermissions.enforceAdminPermissions(mContext);
MtkNfcAddonSequence.getInstance().setModeFlag(isNfcEnabled(), mode, flag, NfcService.this);
}
/// }
}
final class ReaderModeDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {
synchronized (NfcService.this) {
Log.e(TAG, "binderDied() mReaderModeParams:"+mReaderModeParams);
if (mReaderModeParams != null) {
mReaderModeParams = null;
applyRouting(false);
}
}
}
}
final class TagService extends INfcTag.Stub {
@Override
public int close(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "close(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
/* Remove the device from the hmap */
unregisterObject(nativeHandle);
tag.disconnect();
return ErrorCodes.SUCCESS;
}
/* Restart polling loop for notification */
applyRouting(true);
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int connect(int nativeHandle, int technology) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag = null;
Log.d(TAG, "connect(..) nativeHandle:"+nativeHandle+" technology:"+technology);
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_DISCONNECT;
}
if (!tag.isPresent()) {
return ErrorCodes.ERROR_DISCONNECT;
}
// Note that on most tags, all technologies are behind a single
// handle. This means that the connect at the lower levels
// will do nothing, as the tag is already connected to that handle.
if (tag.connect(technology)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
@Override
public int reconnect(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "reconnect(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
if (tag.reconnect()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_DISCONNECT;
}
}
return ErrorCodes.ERROR_DISCONNECT;
}
@Override
public int[] getTechList(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
TagEndpoint tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
return tag.getTechList();
}
return null;
}
@Override
public boolean isPresent(int nativeHandle) throws RemoteException {
TagEndpoint tag = null;
Log.d(TAG, "isPresent(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return false;
}
return tag.isPresent();
}
@Override
public boolean isNdef(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "isNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
int[] ndefInfo = new int[2];
if (tag == null) {
return false;
}
return tag.checkNdef(ndefInfo);
}
@Override
public TransceiveResult transceive(int nativeHandle, byte[] data, boolean raw)
throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "transceive(..) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
byte[] response;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// Check if length is within limits
if (data.length > getMaxTransceiveLength(tag.getConnectedTechnology())) {
return new TransceiveResult(TransceiveResult.RESULT_EXCEEDED_LENGTH, null);
}
int[] targetLost = new int[1];
response = tag.transceive(data, raw, targetLost);
int result;
if (response != null) {
result = TransceiveResult.RESULT_SUCCESS;
} else if (targetLost[0] == 1) {
result = TransceiveResult.RESULT_TAGLOST;
} else {
result = TransceiveResult.RESULT_FAILURE;
}
return new TransceiveResult(result, response);
}
return null;
}
@Override
public NdefMessage ndefRead(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
Log.d(TAG, "ndefRead(..) nativeHandle:"+nativeHandle);
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
byte[] buf = tag.readNdef();
if (buf == null) {
return null;
}
/* Create an NdefMessage */
try {
return new NdefMessage(buf);
} catch (FormatException e) {
return null;
}
}
return null;
}
@Override
public int ndefWrite(int nativeHandle, NdefMessage msg) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (msg == null) return ErrorCodes.ERROR_INVALID_PARAM;
if (tag.writeNdef(msg.toByteArray())) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public boolean ndefIsWritable(int nativeHandle) throws RemoteException {
throw new UnsupportedOperationException();
}
@Override
public int ndefMakeReadOnly(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.makeReadOnly()) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public int formatNdef(int nativeHandle, byte[] key) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "formatNdef(.) nativeHandle:"+nativeHandle);
TagEndpoint tag;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return ErrorCodes.ERROR_NOT_INITIALIZED;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag == null) {
return ErrorCodes.ERROR_IO;
}
if (tag.formatNdef(key)) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_IO;
}
}
@Override
public Tag rediscover(int nativeHandle) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "rediscover(.) nativeHandle:"+nativeHandle);
TagEndpoint tag = null;
// Check if NFC is enabled
if (!isNfcEnabled()) {
return null;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
if (tag != null) {
// For now the prime usecase for rediscover() is to be able
// to access the NDEF technology after formatting without
// having to remove the tag from the field, or similar
// to have access to NdefFormatable in case low-level commands
// were used to remove NDEF. So instead of doing a full stack
// rediscover (which is poorly supported at the moment anyway),
// we simply remove these two technologies and detect them
// again.
tag.removeTechnology(TagTechnology.NDEF);
tag.removeTechnology(TagTechnology.NDEF_FORMATABLE);
tag.findAndReadNdef();
// Build a new Tag object to return
Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
tag.getTechExtras(), tag.getHandle(), this);
return newTag;
}
return null;
}
@Override
public int setTimeout(int tech, int timeout) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
Log.d(TAG, "setTimeout(..) tech:"+tech+" timeout:"+timeout);
boolean success = mDeviceHost.setTimeout(tech, timeout);
if (success) {
return ErrorCodes.SUCCESS;
} else {
return ErrorCodes.ERROR_INVALID_PARAM;
}
}
@Override
public int getTimeout(int tech) throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
return mDeviceHost.getTimeout(tech);
}
@Override
public void resetTimeouts() throws RemoteException {
NfcPermissions.enforceUserPermissions(mContext);
mDeviceHost.resetTimeouts();
}
@Override
public boolean canMakeReadOnly(int ndefType) throws RemoteException {
return mDeviceHost.canMakeReadOnly(ndefType);
}
@Override
public int getMaxTransceiveLength(int tech) throws RemoteException {
return mDeviceHost.getMaxTransceiveLength(tech);
}
@Override
public boolean getExtendedLengthApdusSupported() throws RemoteException {
return mDeviceHost.getExtendedLengthApdusSupported();
}
}
boolean isNfcEnabledOrShuttingDown() {
synchronized (this) {
return (mState == NfcAdapter.STATE_ON || mState == NfcAdapter.STATE_TURNING_OFF);
}
}
boolean isNfcEnabled() {
synchronized (this) {
return mState == NfcAdapter.STATE_ON;
}
}
class WatchDogThread extends Thread {
final Object mCancelWaiter = new Object();
final int mTimeout;
boolean mCanceled = false;
public WatchDogThread(String threadName, int timeout) {
super(threadName);
mTimeout = timeout;
}
@Override
public void run() {
try {
synchronized (mCancelWaiter) {
mCancelWaiter.wait(mTimeout);
if (mCanceled) {
return;
}
}
} catch (InterruptedException e) {
// Should not happen; fall-through to abort.
Log.w(TAG, "Watchdog thread interruped.");
interrupt();
}
Log.e(TAG, "Watchdog triggered, aborting.");
mDeviceHost.doAbort();
}
public synchronized void cancel() {
synchronized (mCancelWaiter) {
mCanceled = true;
mCancelWaiter.notify();
}
}
}
static byte[] hexStringToBytes(String s) {
if (s == null || s.length() == 0) return null;
int len = s.length();
if (len % 2 != 0) {
s = '0' + s;
len++;
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
/**
* Read mScreenState and apply NFC-C polling and NFC-EE routing
*/
void applyRouting(boolean force) {
synchronized (this) {
/// M: @ {
if (!isNfcEnabledOrShuttingDown() || mOpenEe != null) {
return;
}
/// }
WatchDogThread watchDog = new WatchDogThread("applyRouting", ROUTING_WATCHDOG_MS);
if (mInProvisionMode) {
mInProvisionMode = Settings.Secure.getInt(mContentResolver,
Settings.Global.DEVICE_PROVISIONED, 0) == 0;
if (!mInProvisionMode) {
// Notify dispatcher it's fine to dispatch to any package now
// and allow handover transfers.
mNfcDispatcher.disableProvisioningMode();
}
}
// Special case: if we're transitioning to unlocked state while
// still talking to a tag, postpone re-configuration.
if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && isTagPresent()) {
Log.d(TAG, "Not updating discovery parameters, tag connected.");
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RESUME_POLLING),
APPLY_ROUTING_RETRY_TIMEOUT_MS);
return;
}
/// M: @ {
// configure NFC-EE routing
if (mScreenState >= ScreenStateHelper.SCREEN_STATE_ON_LOCKED &&
mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
if (force || !mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE ON");
mNfceeRouteEnabled = true;
mNativeNfcManager.deselectSecureElement();
}
} else {
if (force || mNfceeRouteEnabled) {
Log.d(TAG, "NFC-EE OFF");
mNfceeRouteEnabled = false;
if (mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) { /// M: only deselect when using Google Wallet
mNativeNfcManager.deselectSecureElement();
}
}
}
/// }
try {
watchDog.start();
// Compute new polling parameters
NfcDiscoveryParameters newParams = computeDiscoveryParameters(mScreenState);
if (force || !newParams.equals(mCurrentDiscoveryParameters)) {
Log.d(TAG, "newParams.shouldEnableDiscovery()");
if (newParams.shouldEnableDiscovery()) {
Log.d(TAG, "mCurrentDiscoveryParameters.shouldEnableDiscovery()");
boolean shouldRestart = mCurrentDiscoveryParameters.shouldEnableDiscovery();
Log.d(TAG, "shouldRestart:"+shouldRestart);
mDeviceHost.enableDiscovery(newParams, shouldRestart);
} else {
mDeviceHost.disableDiscovery();
}
mCurrentDiscoveryParameters = newParams;
} else {
Log.d(TAG, "Discovery configuration equal, not updating.");
}
} finally {
watchDog.cancel();
}
}
}
private NfcDiscoveryParameters computeDiscoveryParameters(int screenState) {
Log.d(TAG, "computeDiscoveryParameters() screenState:"+describeScreenState(screenState));
if(screenState == ScreenStateHelper.SCREEN_STATE_ON_LOCKED)
Log.d(TAG, "!!!! SCREEN_STATE_ON_LOCKED,, mNfcUnlockManager.isLockscreenPollingEnabled():"+mNfcUnlockManager.isLockscreenPollingEnabled());
// Recompute discovery parameters based on screen state
NfcDiscoveryParameters.Builder paramsBuilder = NfcDiscoveryParameters.newBuilder();
// Polling
if (screenState >= NFC_POLLING_MODE) {
// Check if reader-mode is enabled
if (mReaderModeParams != null) {
int techMask = 0;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_A) != 0)
techMask |= NFC_POLL_A;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_B) != 0)
techMask |= NFC_POLL_B;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_F) != 0)
techMask |= NFC_POLL_F;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_V) != 0)
techMask |= NFC_POLL_ISO15693;
if ((mReaderModeParams.flags & NfcAdapter.FLAG_READER_NFC_BARCODE) != 0)
techMask |= NFC_POLL_KOVIO;
Log.d(TAG, " mReaderModeParams != null paramsBuilder.setTechMask:"+techMask);
paramsBuilder.setTechMask(techMask);
paramsBuilder.setEnableReaderMode(true);
} else {
Log.d(TAG, " mReaderModeParams == null paramsBuilder.setTechMask:"+NfcDiscoveryParameters.NFC_POLL_DEFAULT +" NFC_POLL_DEFAULT");
paramsBuilder.setTechMask(NfcDiscoveryParameters.NFC_POLL_DEFAULT);
paramsBuilder.setEnableP2p(mIsNdefPushEnabled);
}
} else if (screenState == ScreenStateHelper.SCREEN_STAT
==================================================================================== 作者:欧阳鹏 欢迎转载,与人分享是进步的源泉! 转载请保留原文地址:http://blog.csdn.net/ouyang_peng====================================================================================
相关文章推荐
- Android M Permission 学习笔记
- 智能家居Android技术系列之——蓝牙那些事
- android练习:使用get方法发送请求
- Android基础入门教程——9.1 使用SoundPool播放音效(Duang~)
- Android开发者指南(9) —— ProGuard
- Android之二维码生成和识别
- 一天掌握Android JNI本地编程 快速入门
- android 49 广播接收者中启动其他组件
- Android:Material Design - Style - Color
- Android Dialog去掉黑色背景
- android studio编译卡在gradle不能退出
- Android布局-TableLayout表格布局
- Android全面理解Context
- android中操纵sqlite数据库
- Android Studio如何设置代码自动提示
- Android进阶笔记05:View、SurfaceView 和GLSurfaceView 的关系和区别
- android studio快捷键大全
- Android Studio 恢复小窗口停靠模式(Docked Mode)
- Android 6.0 新增API 简介(4)
- Android提示框Dialog