Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.nfc;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.nfc.NfcAdapter;
     24 import android.support.v7.preference.Preference;
     25 
     26 /**
     27  * BaseNfcEnabler is a abstract helper to manage the Nfc state for Nfc and Android Beam
     28  * preference. It will receive intent and update state to ensure preference show correct state.
     29  */
     30 public abstract class BaseNfcEnabler {
     31     private final Context mContext;
     32     protected final NfcAdapter mNfcAdapter;
     33     private final IntentFilter mIntentFilter;
     34 
     35     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     36         @Override
     37         public void onReceive(Context context, Intent intent) {
     38             String action = intent.getAction();
     39             if (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED.equals(action)) {
     40                 handleNfcStateChanged(intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
     41                         NfcAdapter.STATE_OFF));
     42             }
     43         }
     44     };
     45 
     46     public BaseNfcEnabler(Context context) {
     47         mContext = context;
     48         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
     49 
     50         if (!isNfcAvailable()) {
     51             // NFC is not supported
     52             mIntentFilter = null;
     53             return;
     54         }
     55         mIntentFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
     56     }
     57 
     58     public void resume() {
     59         if (!isNfcAvailable()) {
     60             return;
     61         }
     62         handleNfcStateChanged(mNfcAdapter.getAdapterState());
     63         mContext.registerReceiver(mReceiver, mIntentFilter);
     64     }
     65 
     66     public void pause() {
     67         if (!isNfcAvailable()) {
     68             return;
     69         }
     70         mContext.unregisterReceiver(mReceiver);
     71     }
     72 
     73     public boolean isNfcAvailable() {
     74         return mNfcAdapter != null;
     75     }
     76 
     77     protected abstract void handleNfcStateChanged(int newState);
     78 }
     79