1 /*
2  * Copyright (C) 2023 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.os.Bundle;
22 import android.util.Log;
23 
24 import androidx.appcompat.app.AlertDialog;
25 import androidx.preference.Preference;
26 import androidx.preference.Preference.OnPreferenceChangeListener;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.settings.R;
30 import com.android.settings.applications.AppInfoWithHeader;
31 import com.android.settings.nfc.AppStateNfcTagAppsBridge.NfcTagAppState;
32 import com.android.settingslib.applications.ApplicationsState.AppEntry;
33 
34 /**
35  * Class for displaying app info of the Nfc Tag App
36  */
37 public class ChangeNfcTagAppsStateDetails extends AppInfoWithHeader
38         implements OnPreferenceChangeListener {
39 
40     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
41     private static final String LOG_TAG = "ChangeNfcTagAppsStateDetails";
42 
43     private AppStateNfcTagAppsBridge mAppBridge;
44     private TwoStatePreference mSwitchPref;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         final Context context = getActivity();
50         mAppBridge = new AppStateNfcTagAppsBridge(context, mState, null);
51 
52         // find preferences
53         addPreferencesFromResource(R.xml.change_nfc_tag_apps_details);
54         mSwitchPref = (TwoStatePreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
55 
56         // set title/summary for all of them
57         mSwitchPref.setTitle(R.string.change_nfc_tag_apps_detail_switch);
58 
59         // install event listeners
60         mSwitchPref.setOnPreferenceChangeListener(this);
61 
62     }
63 
64     @Override
createDialog(int id, int errorCode)65     protected AlertDialog createDialog(int id, int errorCode) {
66         return null;
67     }
68 
69     @Override
getMetricsCategory()70     public int getMetricsCategory() {
71         return SettingsEnums.CONFIG_NFC_TAG_APP_PREF;
72     }
73 
74     @Override
onPreferenceChange(Preference preference, Object newValue)75     public boolean onPreferenceChange(Preference preference, Object newValue) {
76         Boolean enable = (Boolean) newValue;
77         if (preference == mSwitchPref) {
78             if (mAppBridge != null && mAppBridge.updateApplist(mUserId, mPackageName, enable)) {
79                 refreshUi();
80                 return true;
81             } else {
82                 Log.e(LOG_TAG, "Set [" + mPackageName + "]" + " failed.");
83                 return false;
84             }
85         }
86         return false;
87     }
88 
89     @Override
refreshUi()90     protected boolean refreshUi() {
91         if (mPackageInfo == null || mPackageInfo.applicationInfo == null) {
92             return false;
93         }
94         retrieveAppEntry();
95         NfcTagAppState state;
96         if (mAppEntry.extraInfo instanceof NfcTagAppState) {
97             state = (NfcTagAppState) mAppEntry.extraInfo;
98         } else {
99             state = new NfcTagAppState(/* exist */ false, /* allowed */ false);
100         }
101         mSwitchPref.setChecked(state.isAllowed());
102         mSwitchPref.setEnabled(state.isExisted());
103         return true;
104     }
105 
106     /** Returns the summary string for this setting preference. */
getSummary(Context context, AppEntry entry)107     public static CharSequence getSummary(Context context, AppEntry entry) {
108         NfcTagAppState state;
109         if (entry.extraInfo instanceof NfcTagAppState) {
110             state = (NfcTagAppState) entry.extraInfo;
111         } else {
112             state = new NfcTagAppState(/* exist */ false, /* allowed */ false);
113         }
114         return context.getString(state.isAllowed()
115                 ? R.string.app_permission_summary_allowed
116                 : R.string.app_permission_summary_not_allowed);
117     }
118 }
119