Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2010 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.os.UserManager;
     25 import android.preference.Preference;
     26 import android.preference.PreferenceScreen;
     27 import android.preference.SwitchPreference;
     28 
     29 import com.android.settings.R;
     30 
     31 /**
     32  * NfcEnabler is a helper to manage the Nfc on/off checkbox preference. It is
     33  * turns on/off Nfc and ensures the summary of the preference reflects the
     34  * current state.
     35  */
     36 public class NfcEnabler implements Preference.OnPreferenceChangeListener {
     37     private final Context mContext;
     38     private final SwitchPreference mSwitch;
     39     private final PreferenceScreen mAndroidBeam;
     40     private final NfcAdapter mNfcAdapter;
     41     private final IntentFilter mIntentFilter;
     42     private boolean mBeamDisallowed;
     43 
     44     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     45         @Override
     46         public void onReceive(Context context, Intent intent) {
     47             String action = intent.getAction();
     48             if (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED.equals(action)) {
     49                 handleNfcStateChanged(intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
     50                         NfcAdapter.STATE_OFF));
     51             }
     52         }
     53     };
     54 
     55     public NfcEnabler(Context context, SwitchPreference switchPreference,
     56             PreferenceScreen androidBeam) {
     57         mContext = context;
     58         mSwitch = switchPreference;
     59         mAndroidBeam = androidBeam;
     60         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
     61         mBeamDisallowed = ((UserManager) mContext.getSystemService(Context.USER_SERVICE))
     62                 .hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM);
     63 
     64         if (mNfcAdapter == null) {
     65             // NFC is not supported
     66             mSwitch.setEnabled(false);
     67             mAndroidBeam.setEnabled(false);
     68             mIntentFilter = null;
     69             return;
     70         }
     71         if (mBeamDisallowed) {
     72             mAndroidBeam.setEnabled(false);
     73         }
     74         mIntentFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
     75     }
     76 
     77     public void resume() {
     78         if (mNfcAdapter == null) {
     79             return;
     80         }
     81         handleNfcStateChanged(mNfcAdapter.getAdapterState());
     82         mContext.registerReceiver(mReceiver, mIntentFilter);
     83         mSwitch.setOnPreferenceChangeListener(this);
     84     }
     85 
     86     public void pause() {
     87         if (mNfcAdapter == null) {
     88             return;
     89         }
     90         mContext.unregisterReceiver(mReceiver);
     91         mSwitch.setOnPreferenceChangeListener(null);
     92     }
     93 
     94     public boolean onPreferenceChange(Preference preference, Object value) {
     95         // Turn NFC on/off
     96 
     97         final boolean desiredState = (Boolean) value;
     98         mSwitch.setEnabled(false);
     99 
    100         if (desiredState) {
    101             mNfcAdapter.enable();
    102         } else {
    103             mNfcAdapter.disable();
    104         }
    105 
    106         return false;
    107     }
    108 
    109     private void handleNfcStateChanged(int newState) {
    110         switch (newState) {
    111         case NfcAdapter.STATE_OFF:
    112             mSwitch.setChecked(false);
    113             mSwitch.setEnabled(true);
    114             mAndroidBeam.setEnabled(false);
    115             mAndroidBeam.setSummary(R.string.android_beam_disabled_summary);
    116             break;
    117         case NfcAdapter.STATE_ON:
    118             mSwitch.setChecked(true);
    119             mSwitch.setEnabled(true);
    120             mAndroidBeam.setEnabled(!mBeamDisallowed);
    121             if (mNfcAdapter.isNdefPushEnabled() && !mBeamDisallowed) {
    122                 mAndroidBeam.setSummary(R.string.android_beam_on_summary);
    123             } else {
    124                 mAndroidBeam.setSummary(R.string.android_beam_off_summary);
    125             }
    126             break;
    127         case NfcAdapter.STATE_TURNING_ON:
    128             mSwitch.setChecked(true);
    129             mSwitch.setEnabled(false);
    130             mAndroidBeam.setEnabled(false);
    131             break;
    132         case NfcAdapter.STATE_TURNING_OFF:
    133             mSwitch.setChecked(false);
    134             mSwitch.setEnabled(false);
    135             mAndroidBeam.setEnabled(false);
    136             break;
    137         }
    138     }
    139 }
    140