1 /*
2  * Copyright (C) 2020 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.connecteddevice;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.nfc.NfcAdapter;
22 import android.os.UserManager;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.BasePreferenceController;
26 
27 /**
28  * Controller that used to show NFC and payment features
29  */
30 public class NfcAndPaymentFragmentController extends BasePreferenceController {
31     private final NfcAdapter mNfcAdapter;
32     private final PackageManager mPackageManager;
33     private final UserManager mUserManager;
34 
NfcAndPaymentFragmentController(Context context, String preferenceKey)35     public NfcAndPaymentFragmentController(Context context, String preferenceKey) {
36         super(context, preferenceKey);
37 
38         mPackageManager = context.getPackageManager();
39         mUserManager = context.getSystemService(UserManager.class);
40         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
46                 || !mPackageManager.hasSystemFeature(
47                 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
48             return UNSUPPORTED_ON_DEVICE;
49         }
50         return AVAILABLE;
51     }
52 
53     @Override
getSummary()54     public CharSequence getSummary() {
55         if (mNfcAdapter != null) {
56             if (mNfcAdapter.isEnabled()) {
57                 return mContext.getText(R.string.nfc_setting_on);
58             } else {
59                 return mContext.getText(R.string.nfc_setting_off);
60             }
61         }
62         return null;
63     }
64 }
65