1 /*
2  * Copyright (C) 2018 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.Context;
20 import android.nfc.NfcAdapter;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import com.android.settings.R;
25 import com.android.settingslib.RestrictedLockUtilsInternal;
26 import com.android.settingslib.RestrictedPreference;
27 
28 /**
29  * AndroidBeanEnabler is a helper to manage the Android Beam preference. It turns on/off
30  * Android Beam and ensures the summary of the preference reflects the current state.
31  */
32 public class AndroidBeamEnabler extends BaseNfcEnabler {
33     private final boolean mBeamDisallowedBySystem;
34     private final RestrictedPreference mPreference;
35 
AndroidBeamEnabler(Context context, RestrictedPreference preference)36     public AndroidBeamEnabler(Context context, RestrictedPreference preference) {
37         super(context);
38         mPreference = preference;
39         mBeamDisallowedBySystem = RestrictedLockUtilsInternal.hasBaseUserRestriction(context,
40                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId());
41         if (!isNfcAvailable()) {
42             // NFC is not supported
43             mPreference.setEnabled(false);
44             return;
45         }
46         if (mBeamDisallowedBySystem) {
47             mPreference.setEnabled(false);
48         }
49     }
50 
51     @Override
handleNfcStateChanged(int newState)52     protected void handleNfcStateChanged(int newState) {
53         switch (newState) {
54             case NfcAdapter.STATE_OFF:
55                 mPreference.setEnabled(false);
56                 mPreference.setSummary(R.string.nfc_disabled_summary);
57                 break;
58             case NfcAdapter.STATE_ON:
59                 if (mBeamDisallowedBySystem) {
60                     mPreference.setDisabledByAdmin(null);
61                     mPreference.setEnabled(false);
62                 } else {
63                     mPreference.checkRestrictionAndSetDisabled(UserManager.DISALLOW_OUTGOING_BEAM);
64                 }
65                 if (mNfcAdapter.isNdefPushEnabled() && mPreference.isEnabled()) {
66                     mPreference.setSummary(R.string.android_beam_on_summary);
67                 } else {
68                     mPreference.setSummary(R.string.android_beam_off_summary);
69                 }
70                 break;
71             case NfcAdapter.STATE_TURNING_ON:
72                 mPreference.setEnabled(false);
73                 break;
74             case NfcAdapter.STATE_TURNING_OFF:
75                 mPreference.setEnabled(false);
76                 break;
77         }
78     }
79 }
80