• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.android_scripting.facade;
18 
19 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
20 import com.googlecode.android_scripting.rpc.Rpc;
21 
22 import android.app.Service;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.nfc.NfcAdapter;
28 import android.nfc.NfcManager;
29 
30 /**
31  * Access NFC functions.
32  */
33 public class NfcManagerFacade extends RpcReceiver {
34 
35     private final Service mService;
36     private final NfcManager mNfcManager;
37     private final NfcAdapter mNfc;
38     private final EventFacade mEventFacade;
39     private final IntentFilter mStateChangeFilter;
40     private boolean mTrackingStateChange;
41 
NfcManagerFacade(FacadeManager manager)42     public NfcManagerFacade(FacadeManager manager) {
43         super(manager);
44         mService = manager.getService();
45         mNfcManager = (NfcManager) mService.getSystemService(Context.NFC_SERVICE);
46         mNfc = mNfcManager.getDefaultAdapter();
47         mEventFacade = manager.getReceiver(EventFacade.class);
48         mStateChangeFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
49     }
50 
51     private final BroadcastReceiver mNfcStateReceiver = new BroadcastReceiver() {
52         @Override
53         public void onReceive(Context context, Intent intent) {
54             String action = intent.getAction();
55             if (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED.equals(action)) {
56                 int nfcState = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
57                         NfcAdapter.STATE_OFF);
58                 if (nfcState == NfcAdapter.STATE_ON) {
59                     mEventFacade.postEvent("NfcStateOn", null);
60                 } else if (nfcState == NfcAdapter.STATE_OFF) {
61                     mEventFacade.postEvent("NfcStateOff", null);
62                 }
63             }
64         }
65     };
66 
67     @Rpc(description = "Check if NFC hardware is enabled.")
nfcIsEnabled()68     public Boolean nfcIsEnabled() {
69         return mNfc.isEnabled();
70     }
71 
72     @Rpc(description = "Asynchronous call to enable NFC hardware.")
nfcEnable()73     public Boolean nfcEnable() {
74         return mNfc.enable();
75     }
76 
77     @Rpc(description = "Asynchronous call to disable NFC hardware.")
nfcDisable()78     public Boolean nfcDisable() {
79         return mNfc.disable();
80     }
81 
82     @Rpc(description = "Start tracking NFC hardware state changes.")
nfcStartTrackingStateChange()83     public void nfcStartTrackingStateChange() {
84         mService.registerReceiver(mNfcStateReceiver, mStateChangeFilter);
85         mTrackingStateChange = true;
86     }
87 
88     @Rpc(description = "Stop tracking NFC hardware state changes.")
nfcStopTrackingStateChange()89     public void nfcStopTrackingStateChange() {
90         mService.unregisterReceiver(mNfcStateReceiver);
91         mTrackingStateChange = false;
92     }
93 
94     @Override
shutdown()95     public void shutdown() {
96         if (mTrackingStateChange == true) {
97             nfcStopTrackingStateChange();
98         }
99     }
100 
101 }
102