2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.nfc;
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;
29 import com.android.settings.R;
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
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;
44 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
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));
55 public NfcEnabler(Context context, SwitchPreference switchPreference,
56 PreferenceScreen androidBeam) {
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);
64 if (mNfcAdapter == null) {
65 // NFC is not supported
66 mSwitch.setEnabled(false);
67 mAndroidBeam.setEnabled(false);
71 if (mBeamDisallowed) {
72 mAndroidBeam.setEnabled(false);
74 mIntentFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
77 public void resume() {
78 if (mNfcAdapter == null) {
81 handleNfcStateChanged(mNfcAdapter.getAdapterState());
82 mContext.registerReceiver(mReceiver, mIntentFilter);
83 mSwitch.setOnPreferenceChangeListener(this);
87 if (mNfcAdapter == null) {
90 mContext.unregisterReceiver(mReceiver);
91 mSwitch.setOnPreferenceChangeListener(null);
94 public boolean onPreferenceChange(Preference preference, Object value) {
97 final boolean desiredState = (Boolean) value;
98 mSwitch.setEnabled(false);
101 mNfcAdapter.enable();
103 mNfcAdapter.disable();
109 private void handleNfcStateChanged(int 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);
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);
124 mAndroidBeam.setSummary(R.string.android_beam_off_summary);
127 case NfcAdapter.STATE_TURNING_ON:
128 mSwitch.setChecked(true);
129 mSwitch.setEnabled(false);
130 mAndroidBeam.setEnabled(false);
132 case NfcAdapter.STATE_TURNING_OFF:
133 mSwitch.setChecked(false);
134 mSwitch.setEnabled(false);
135 mAndroidBeam.setEnabled(false);