summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/nfc/NfcEnabler.java
blob: 1facab9fa1f94ef116751f2d041f026edea662c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.nfc;

import com.android.settings.R;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.preference.Preference;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
import android.util.Log;

/**
 * NfcEnabler is a helper to manage the Nfc on/off checkbox preference. It is
 * turns on/off Nfc and ensures the summary of the preference reflects the
 * current state.
 */
public class NfcEnabler implements Preference.OnPreferenceChangeListener {
    private static final String TAG = "NfcEnabler";

    private final Context mContext;
    private final CheckBoxPreference mCheckbox;
    private final NfcAdapter mNfcAdapter;

    private boolean mNfcState;

    public NfcEnabler(Context context, CheckBoxPreference checkBoxPreference) {
        mContext = context;
        mCheckbox = checkBoxPreference;
        mNfcAdapter = NfcAdapter.getDefaultAdapter();

        if (mNfcAdapter == null) {
            // NFC is not supported
            mCheckbox.setEnabled(false);
        }
    }

    public void resume() {
        if (mNfcAdapter == null) {
            return;
        }
		mCheckbox.setOnPreferenceChangeListener(this);
		mNfcState = Settings.System.getInt(mContext.getContentResolver(),
		        Settings.System.NFC_ON, 0) != 0;
		updateUi();
    }

    public void pause() {
        if (mNfcAdapter == null) {
            return;
        }
        mCheckbox.setOnPreferenceChangeListener(null);
    }

    public boolean onPreferenceChange(Preference preference, Object value) {
        // Turn on/off Nfc
        mNfcState = (Boolean) value;
        setEnabled();

        return false;
    }

    private void setEnabled() {
        if (mNfcState) {
            if (!mNfcAdapter.enableTagDiscovery()) {
                Log.w(TAG, "NFC enabling failed");
				mNfcState = false;
            }
        } else {
            if (!mNfcAdapter.disableTagDiscovery()) {
                Log.w(TAG, "NFC disabling failed");
				mNfcState = true;
            }
        }
		updateUi();
	}

    private void updateUi() {
        mCheckbox.setChecked(mNfcState);
    }
}