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