Adding Nfc Tag App Preference setting to special_access settings

In the settings app, allow users to change the preference of the Nfc Tag apps.

Bug: 244272155
Test: make RunSettingsRoboTests ROBOTEST_FILTER=NfcTagAppsPreferenceControllerTest
Change-Id: I28903fae8935613a0e8618da21ca44e98b8801d5
This commit is contained in:
George
2023-01-02 18:13:43 +08:00
parent a72e62b3a3
commit 2c5d1f8d29
12 changed files with 469 additions and 7 deletions

View File

@@ -0,0 +1,136 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.nfc;
import static com.android.settingslib.applications.ApplicationsState.AppEntry;
import static com.android.settingslib.applications.ApplicationsState.AppFilter;
import android.app.ActivityManager;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import com.android.settings.applications.AppStateBaseBridge;
import com.android.settingslib.applications.ApplicationsState;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Filter to display only in the Tag preference listed Apps on Nfc Tag Apps page.
*/
public class AppStateNfcTagAppsBridge extends AppStateBaseBridge{
private static final String TAG = "AppStateNfcTagAppsBridge";
private final Context mContext;
private final NfcAdapter mNfcAdapter;
// preference list cache
private static Map<Integer, Map<String, Boolean>> sList = new HashMap<>();
public AppStateNfcTagAppsBridge(Context context, ApplicationsState appState,
Callback callback) {
super(appState, callback);
mContext = context;
mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
if (mNfcAdapter != null && mNfcAdapter.isTagIntentAppPreferenceSupported()) {
UserManager um = mContext.createContextAsUser(
UserHandle.of(ActivityManager.getCurrentUser()), 0)
.getSystemService(UserManager.class);
List<UserHandle> luh = um.getEnabledProfiles();
for (UserHandle uh : luh) {
int userId = uh.getIdentifier();
sList.put(userId, mNfcAdapter.getTagIntentAppPreferenceForUser(userId));
}
}
}
/**
* Update the system and cached tag app preference lists.
*/
public boolean updateApplist(int userId, String pkg, boolean allowed) {
if (mNfcAdapter.setTagIntentAppPreferenceForUser(
userId, pkg, allowed) == NfcAdapter.TAG_INTENT_APP_PREF_RESULT_SUCCESS) {
sList.put(userId, mNfcAdapter.getTagIntentAppPreferenceForUser(userId));
return true;
} else {
return false;
}
}
@Override
protected void loadAllExtraInfo() {
final List<ApplicationsState.AppEntry> allApps = mAppSession.getAllApps();
for (int i = 0; i < allApps.size(); i++) {
ApplicationsState.AppEntry app = allApps.get(i);
this.updateExtraInfo(app, app.info.packageName, app.info.uid);
}
}
@Override
protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
// Display package if is in the app preference list.
int userId = UserHandle.getUserId(uid);
Map<String, Boolean> map = sList.getOrDefault(userId, new HashMap<>());
if (map.containsKey(pkg)) {
app.extraInfo = new NfcTagAppState(/* exist */ true, /* allowed */ map.get(pkg));
} else {
app.extraInfo = new NfcTagAppState(/* exist */ false, /* allowed */ false);
}
}
/**
* Class to denote the nfc tag app preference state of the AppEntry
*/
public static class NfcTagAppState {
private boolean mIsExisted;
private boolean mIsAllowed;
public NfcTagAppState(boolean exist, boolean allowed) {
mIsExisted = exist;
mIsAllowed = allowed;
}
public boolean isExisted() {
return mIsExisted;
}
public boolean isAllowed() {
return mIsAllowed;
}
}
public static final AppFilter FILTER_APPS_NFC_TAG =
new AppFilter() {
@Override
public void init() {
}
@Override
public boolean filterApp(AppEntry entry) {
if (entry.extraInfo == null) {
Log.d(TAG, "[" + entry.info.packageName + "]" + " has No extra info.");
return false;
}
NfcTagAppState state = (NfcTagAppState) entry.extraInfo;
return state.isExisted();
}
};
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.nfc;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.applications.AppInfoWithHeader;
import com.android.settings.nfc.AppStateNfcTagAppsBridge.NfcTagAppState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
/**
* Class for displaying app info of the Nfc Tag App
*/
public class ChangeNfcTagAppsStateDetails extends AppInfoWithHeader
implements OnPreferenceChangeListener {
private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
private static final String LOG_TAG = "ChangeNfcTagAppsStateDetails";
private AppStateNfcTagAppsBridge mAppBridge;
private SwitchPreference mSwitchPref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getActivity();
mAppBridge = new AppStateNfcTagAppsBridge(context, mState, null);
// find preferences
addPreferencesFromResource(R.xml.change_nfc_tag_apps_details);
mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
// set title/summary for all of them
mSwitchPref.setTitle(R.string.change_nfc_tag_apps_detail_switch);
// install event listeners
mSwitchPref.setOnPreferenceChangeListener(this);
}
@Override
protected AlertDialog createDialog(int id, int errorCode) {
return null;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.CONFIG_NFC_TAG_APP_PREF;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean enable = (Boolean) newValue;
if (preference == mSwitchPref) {
if (mAppBridge != null && mAppBridge.updateApplist(mUserId, mPackageName, enable)) {
refreshUi();
return true;
} else {
Log.e(LOG_TAG, "Set [" + mPackageName + "]" + " failed.");
return false;
}
}
return false;
}
@Override
protected boolean refreshUi() {
if (mPackageInfo == null || mPackageInfo.applicationInfo == null) {
return false;
}
retrieveAppEntry();
NfcTagAppState state;
if (mAppEntry.extraInfo instanceof NfcTagAppState) {
state = (NfcTagAppState) mAppEntry.extraInfo;
} else {
state = new NfcTagAppState(/* exist */ false, /* allowed */ false);
}
mSwitchPref.setChecked(state.isAllowed());
mSwitchPref.setEnabled(state.isExisted());
return true;
}
/** Returns the summary string for this setting preference. */
public static CharSequence getSummary(Context context, AppEntry entry) {
NfcTagAppState state;
if (entry.extraInfo instanceof NfcTagAppState) {
state = (NfcTagAppState) entry.extraInfo;
} else {
state = new NfcTagAppState(/* exist */ false, /* allowed */ false);
}
return context.getString(state.isAllowed()
? R.string.app_permission_summary_allowed
: R.string.app_permission_summary_not_allowed);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.nfc;
import android.content.Context;
import android.nfc.NfcAdapter;
import com.android.settings.core.BasePreferenceController;
/**
* A PreferenceController handling the logic for the Nfc Tag App preference
*/
public class NfcTagAppsPreferenceController extends BasePreferenceController {
private NfcAdapter mNfcAdapter;
public NfcTagAppsPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mNfcAdapter = NfcAdapter.getDefaultAdapter(context.getApplicationContext());
}
@Override
public int getAvailabilityStatus() {
if (mNfcAdapter != null) {
return mNfcAdapter.isTagIntentAppPreferenceSupported()
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
return UNSUPPORTED_ON_DEVICE;
}
}