| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 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 | |
| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| Nick Pelly | 454d2ef | 2010-10-12 13:07:06 -0700 | [diff] [blame] | 20 | import android.nfc.NfcAdapter; |
| Stanley Wang | 10b7a7d | 2021-01-29 01:17:43 +0800 | [diff] [blame] | 21 | |
| Edgar Wang | cfaa4ea | 2025-06-03 04:35:02 +0000 | [diff] [blame] | 22 | import androidx.preference.TwoStatePreference; |
| Chihhang Chuang | 3b387a0 | 2018-03-29 17:26:04 +0800 | [diff] [blame] | 23 | |
| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 24 | /** |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 25 | * NfcEnabler is a helper to manage the Nfc on/off checkbox preference. It turns on/off Nfc |
| 26 | * and ensures the summary of the preference reflects the current state. |
| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 27 | */ |
| Chihhang Chuang | 3b387a0 | 2018-03-29 17:26:04 +0800 | [diff] [blame] | 28 | public class NfcEnabler extends BaseNfcEnabler { |
| Edgar Wang | cfaa4ea | 2025-06-03 04:35:02 +0000 | [diff] [blame] | 29 | private final TwoStatePreference mPreference; |
| Brad Fitzpatrick | d36d650 | 2010-10-18 14:04:24 -0700 | [diff] [blame] | 30 | |
| Edgar Wang | cfaa4ea | 2025-06-03 04:35:02 +0000 | [diff] [blame] | 31 | public NfcEnabler(Context context, TwoStatePreference preference) { |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 32 | super(context); |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 33 | mPreference = preference; |
| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 36 | @Override |
| 37 | protected void handleNfcStateChanged(int newState) { |
| Nick Pelly | 9894d4a | 2011-08-09 07:10:16 -0700 | [diff] [blame] | 38 | switch (newState) { |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 39 | case NfcAdapter.STATE_OFF: |
| Jacky Wang | 041bc11 | 2025-01-20 21:19:46 +0800 | [diff] [blame] | 40 | mPreference.setChecked(false); |
| Roshan Pius | 3ebfcea | 2023-03-29 10:48:23 -0700 | [diff] [blame] | 41 | mPreference.setEnabled(true); |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 42 | break; |
| 43 | case NfcAdapter.STATE_ON: |
| Jacky Wang | 041bc11 | 2025-01-20 21:19:46 +0800 | [diff] [blame] | 44 | mPreference.setChecked(true); |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 45 | mPreference.setEnabled(true); |
| 46 | break; |
| 47 | case NfcAdapter.STATE_TURNING_ON: |
| Jacky Wang | 041bc11 | 2025-01-20 21:19:46 +0800 | [diff] [blame] | 48 | mPreference.setChecked(true); |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 49 | mPreference.setEnabled(false); |
| 50 | break; |
| 51 | case NfcAdapter.STATE_TURNING_OFF: |
| Jacky Wang | 041bc11 | 2025-01-20 21:19:46 +0800 | [diff] [blame] | 52 | mPreference.setChecked(false); |
| HJ ChangLiao | b247311 | 2018-03-21 16:01:31 +0800 | [diff] [blame] | 53 | mPreference.setEnabled(false); |
| 54 | break; |
| Nick Pelly | 9894d4a | 2011-08-09 07:10:16 -0700 | [diff] [blame] | 55 | } |
| Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 56 | } |
| Amith Yamasani | 1bdb787 | 2010-10-14 17:20:59 -0700 | [diff] [blame] | 57 | } |