1 /*
2  * Copyright (C) 2013 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.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.content.pm.UserInfo;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import androidx.annotation.VisibleForTesting;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.R;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.core.lifecycle.Lifecycle;
38 import com.android.settingslib.search.SearchIndexable;
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 @SearchIndexable
44 public class PaymentSettings extends DashboardFragment {
45     public static final String TAG = "PaymentSettings";
46 
47     private PaymentBackend mPaymentBackend;
48 
49     @Override
getLogTag()50     protected String getLogTag() {
51         return TAG;
52     }
53 
54     @Override
getMetricsCategory()55     public int getMetricsCategory() {
56         return SettingsEnums.NFC_PAYMENT;
57     }
58 
59     @Override
getPreferenceScreenResId()60     protected int getPreferenceScreenResId() {
61         return R.xml.nfc_payment_settings;
62     }
63 
64     @Override
createPreferenceControllers(Context context)65     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
66         return buildPreferenceControllers(context, getSettingsLifecycle());
67     }
68 
buildPreferenceControllers(Context context, Lifecycle lifecycle)69     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
70             Lifecycle lifecycle) {
71         final List<AbstractPreferenceController> controllers = new ArrayList<>();
72         controllers.add(new NfcDefaultPaymentPreferenceController(context, lifecycle));
73 
74         return controllers;
75     }
76 
77     @Override
onAttach(Context context)78     public void onAttach(Context context) {
79         super.onAttach(context);
80         mPaymentBackend = new PaymentBackend(getActivity());
81 
82         use(NfcForegroundPreferenceController.class).setPaymentBackend(mPaymentBackend);
83     }
84 
85     @Override
onViewCreated(View view, Bundle savedInstanceState)86     public void onViewCreated(View view, Bundle savedInstanceState) {
87         super.onViewCreated(view, savedInstanceState);
88         if (isShowEmptyImage(getPreferenceScreen())) {
89             View emptyView = getActivity().getLayoutInflater().inflate(
90                     R.layout.nfc_payment_empty, null, false);
91             ((ViewGroup) view.findViewById(android.R.id.list_container)).addView(emptyView);
92         }
93     }
94 
95     @Override
onResume()96     public void onResume() {
97         super.onResume();
98         mPaymentBackend.onResume();
99     }
100 
101     @Override
onPause()102     public void onPause() {
103         super.onPause();
104         mPaymentBackend.onPause();
105     }
106 
107     @VisibleForTesting
isShowEmptyImage(PreferenceScreen screen)108     boolean isShowEmptyImage(PreferenceScreen screen) {
109         for (int i = 0; i < screen.getPreferenceCount(); i++) {
110             final Preference preference = screen.getPreference(i);
111             if(preference.isVisible()) {
112                 return false;
113             }
114         }
115         return true;
116     }
117 
118     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
119             new BaseSearchIndexProvider(R.xml.nfc_payment_settings) {
120 
121                 @Override
122                 protected boolean isPageSearchEnabled(Context context) {
123                     final UserManager userManager = context.getSystemService(UserManager.class);
124                     final UserInfo myUserInfo = userManager.getUserInfo(UserHandle.myUserId());
125                     if (myUserInfo.isGuest()) {
126                         return false;
127                     }
128                     final PackageManager pm = context.getPackageManager();
129                     return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
130                 }
131             };
132 }
133