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