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.setChecked(desiredState); 102 mSwitch.setEnabled(false); 103 104 if (desiredState) { 105 mNfcAdapter.enable(); 106 } else { 107 mNfcAdapter.disable(); 108 } 109 110 return false; 111 } 112 113 private void handleNfcStateChanged(int newState) { 114 switch (newState) { 115 case NfcAdapter.STATE_OFF: 116 mSwitch.setChecked(false); 117 mSwitch.setEnabled(true); 118 mAndroidBeam.setEnabled(false); 119 mAndroidBeam.setSummary(R.string.android_beam_disabled_summary); 120 break; 121 case NfcAdapter.STATE_ON: 122 mSwitch.setChecked(true); 123 mSwitch.setEnabled(true); 124 if (mBeamDisallowedBySystem) { 125 mAndroidBeam.setDisabledByAdmin(null); 126 mAndroidBeam.setEnabled(false); 127 } else { 128 mAndroidBeam.checkRestrictionAndSetDisabled(UserManager.DISALLOW_OUTGOING_BEAM); 129 } 130 if (mNfcAdapter.isNdefPushEnabled() && mAndroidBeam.isEnabled()) { 131 mAndroidBeam.setSummary(R.string.android_beam_on_summary); 132 } else { 133 mAndroidBeam.setSummary(R.string.android_beam_off_summary); 134 } 135 break; 136 case NfcAdapter.STATE_TURNING_ON: 137 mSwitch.setChecked(true); 138 mSwitch.setEnabled(false); 139 mAndroidBeam.setEnabled(false); 140 break; 141 case NfcAdapter.STATE_TURNING_OFF: 142 mSwitch.setChecked(false); 143 mSwitch.setEnabled(false); 144 mAndroidBeam.setEnabled(false); 145 break; 146 } 147 } 148 } 149