Snap for 7175096 from 6698d3b2c8 to sc-v2-release

Change-Id: I973fbf0979599966ae6f634a39e0c28017a470ff
This commit is contained in:
android-build-team Robot
2021-02-28 00:08:12 +00:00
20 changed files with 814 additions and 221 deletions

View File

@@ -5316,7 +5316,7 @@
<string name="daltonizer_mode_tritanomaly_summary">Blue-yellow</string>
<!-- Title for the accessibility preference and switch of the Reduce Brightness feature. [CHAR LIMIT=NONE] -->
<string name="reduce_bright_colors_preference_title">Reduce Brightness</string>
<string name="reduce_bright_colors_preference_title">Reduce brightness</string>
<!-- Summary for the accessibility preference to configure Reduce Brightness feature. [CHAR LIMIT=NONE] -->
<string name="reduce_bright_colors_preference_summary" product="default">Make screen darker than your phone\u2019s minimum brightness</string>
<!-- Summary for the accessibility preference to configure Reduce Brightness feature. [CHAR LIMIT=NONE] -->

View File

@@ -31,15 +31,22 @@
android:title="@string/notification_access_detail_switch"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.ApprovalPreferenceController"/>
<MultiSelectListPreference
android:key="notification_type_filter"
android:title="@string/notification_listener_type_title"
android:entries="@array/notif_types_titles"
android:entryValues="@array/notif_types_values"
android:summary="%s"
android:persistent="false"
style="@style/SettingsMultiSelectListPreference"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.TypeFilterPreferenceController"/>/>
<CheckBoxPreference
android:key="type_filter_ongoing"
android:title="@string/notif_type_ongoing"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.OngoingTypeFilterPreferenceController"/>/>
<CheckBoxPreference
android:key="type_filter_conversation"
android:title="@string/notif_type_conversation"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.ConversationTypeFilterPreferenceController"/>/>
<CheckBoxPreference
android:key="type_filter_alerting"
android:title="@string/notif_type_alerting"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.AlertingTypeFilterPreferenceController"/>/>
<CheckBoxPreference
android:key="type_filter_silent"
android:title="@string/notif_type_silent"
settings:controller="com.android.settings.applications.specialaccess.notificationaccess.SilentTypeFilterPreferenceController"/>/>
<Preference
android:key="bridged_apps"

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ALERTING;
import android.content.Context;
public class AlertingTypeFilterPreferenceController extends TypeFilterPreferenceController {
private static final String TAG = "AlertFilterPrefCntlr";
public AlertingTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return FLAG_FILTER_TYPE_ALERTING;
}
}

View File

@@ -20,6 +20,7 @@ import android.os.UserHandle;
import android.service.notification.NotificationListenerFilter;
import androidx.annotation.VisibleForTesting;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
@@ -130,9 +131,9 @@ public class BridgedAppsPreferenceController extends BasePreferenceController im
}
final String prefKey = entry.info.packageName + "|" + entry.info.uid;
appsKeySet.add(prefKey);
SwitchPreference preference = mScreen.findPreference(prefKey);
CheckBoxPreference preference = mScreen.findPreference(prefKey);
if (preference == null) {
preference = new SwitchPreference(mScreen.getContext());
preference = new CheckBoxPreference(mScreen.getContext());
preference.setIcon(entry.icon);
preference.setTitle(entry.label);
preference.setKey(prefKey);
@@ -172,7 +173,7 @@ public class BridgedAppsPreferenceController extends BasePreferenceController im
}
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof SwitchPreference) {
if (preference instanceof CheckBoxPreference) {
String packageName = preference.getKey().substring(0, preference.getKey().indexOf("|"));
int uid = Integer.parseInt(preference.getKey().substring(
preference.getKey().indexOf("|") + 1));

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_CONVERSATIONS;
import android.content.Context;
public class ConversationTypeFilterPreferenceController extends TypeFilterPreferenceController {
private static final String TAG = "ConvFilterPrefCntlr";
public ConversationTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return FLAG_FILTER_TYPE_CONVERSATIONS;
}
}

View File

@@ -21,6 +21,7 @@ import static com.android.settings.applications.AppInfoBase.ARG_PACKAGE_NAME;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -49,7 +50,9 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
@@ -60,6 +63,7 @@ public class NotificationAccessDetails extends DashboardFragment {
private NotificationListenerFilter mNlf;
private ComponentName mComponentName;
private CharSequence mServiceName;
protected ServiceInfo mServiceInfo;
protected PackageInfo mPackageInfo;
protected int mUserId;
protected String mPackageName;
@@ -96,11 +100,19 @@ public class NotificationAccessDetails extends DashboardFragment {
.setPackageInfo(mPackageInfo)
.setPm(context.getPackageManager())
.setServiceName(mServiceName);
use(TypeFilterPreferenceController.class)
.setNm(new NotificationBackend())
getPreferenceControllers().forEach(controllers -> {
controllers.forEach(controller -> {
if (controller instanceof TypeFilterPreferenceController) {
TypeFilterPreferenceController tfpc =
(TypeFilterPreferenceController) controller;
tfpc.setNm(new NotificationBackend())
.setCn(mComponentName)
.setServiceInfo(mServiceInfo)
.setUserId(mUserId);
}
});
});
}
@Override
public int getMetricsCategory() {
@@ -205,20 +217,34 @@ public class NotificationAccessDetails extends DashboardFragment {
// along to keep business logic out of this file
public void disable(final ComponentName cn) {
final PreferenceScreen screen = getPreferenceScreen();
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
controller.disable(cn);
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
TypeFilterPreferenceController dependent1 = use(TypeFilterPreferenceController.class);
dependent1.updateState(screen.findPreference(dependent1.getPreferenceKey()));
ApprovalPreferenceController apc = use(ApprovalPreferenceController.class);
apc.disable(cn);
apc.updateState(screen.findPreference(apc.getPreferenceKey()));
getPreferenceControllers().forEach(controllers -> {
controllers.forEach(controller -> {
if (controller instanceof TypeFilterPreferenceController) {
TypeFilterPreferenceController tfpc =
(TypeFilterPreferenceController) controller;
tfpc.updateState(screen.findPreference(tfpc.getPreferenceKey()));
}
});
});
}
protected void enable(ComponentName cn) {
final PreferenceScreen screen = getPreferenceScreen();
ApprovalPreferenceController controller = use(ApprovalPreferenceController.class);
controller.enable(cn);
controller.updateState(screen.findPreference(controller.getPreferenceKey()));
TypeFilterPreferenceController dependent1 = use(TypeFilterPreferenceController.class);
dependent1.updateState(screen.findPreference(dependent1.getPreferenceKey()));
ApprovalPreferenceController apc = use(ApprovalPreferenceController.class);
apc.enable(cn);
apc.updateState(screen.findPreference(apc.getPreferenceKey()));
getPreferenceControllers().forEach(controllers -> {
controllers.forEach(controller -> {
if (controller instanceof TypeFilterPreferenceController) {
TypeFilterPreferenceController tfpc =
(TypeFilterPreferenceController) controller;
tfpc.updateState(screen.findPreference(tfpc.getPreferenceKey()));
}
});
});
}
// To save binder calls, load this in the fragment rather than each preference controller
@@ -239,6 +265,7 @@ public class NotificationAccessDetails extends DashboardFragment {
if (Objects.equals(mComponentName, info.getComponentName())) {
mIsNls = true;
mServiceName = info.loadLabel(mPm);
mServiceInfo = info;
break;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ONGOING;
import android.content.Context;
public class OngoingTypeFilterPreferenceController extends TypeFilterPreferenceController {
private static final String TAG = "OngoingFilterPrefCntlr";
public OngoingTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return FLAG_FILTER_TYPE_ONGOING;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import android.content.Context;
public class SilentTypeFilterPreferenceController extends TypeFilterPreferenceController {
private static final String TAG = "SilentFilterPrefCntlr";
public SilentTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return FLAG_FILTER_TYPE_SILENT;
}
}

View File

@@ -16,35 +16,31 @@
package com.android.settings.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ALERTING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_CONVERSATIONS;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ONGOING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.service.notification.NotificationListenerFilter;
import android.service.notification.NotificationListenerService;
import android.text.TextUtils;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.notification.NotificationBackend;
import java.util.HashSet;
import java.util.Set;
public class TypeFilterPreferenceController extends BasePreferenceController implements
public abstract class TypeFilterPreferenceController extends BasePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String TAG = "TypeFilterPrefCntlr";
private static final String XML_SEPARATOR = ",";
private ComponentName mCn;
private int mUserId;
private NotificationBackend mNm;
private NotificationListenerFilter mNlf;
private ServiceInfo mSi;
public TypeFilterPreferenceController(Context context, String key) {
super(context, key);
@@ -65,6 +61,13 @@ public class TypeFilterPreferenceController extends BasePreferenceController imp
return this;
}
public TypeFilterPreferenceController setServiceInfo(ServiceInfo si) {
mSi = si;
return this;
}
abstract protected int getType();
@Override
public int getAvailabilityStatus() {
if (mNm.isNotificationListenerAccessGranted(mCn)) {
@@ -74,71 +77,62 @@ public class TypeFilterPreferenceController extends BasePreferenceController imp
}
}
@Override
public void updateState(Preference pref) {
mNlf = mNm.getListenerFilter(mCn, mUserId);
Set<String> values = new HashSet<>();
Set<String> entries = new HashSet<>();
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ONGOING)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_ONGOING));
entries.add(mContext.getString(R.string.notif_type_ongoing));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_CONVERSATIONS)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_CONVERSATIONS));
entries.add(mContext.getString(R.string.notif_type_conversation));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ALERTING)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_ALERTING));
entries.add(mContext.getString(R.string.notif_type_alerting));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_SILENT)) {
values.add(String.valueOf(FLAG_FILTER_TYPE_SILENT));
entries.add(mContext.getString(R.string.notif_type_silent));
}
final MultiSelectListPreference preference = (MultiSelectListPreference) pref;
preference.setValues(values);
super.updateState(preference);
pref.setEnabled(getAvailabilityStatus() == AVAILABLE);
}
private boolean hasFlag(int value, int flag) {
return (value & flag) != 0;
}
public CharSequence getSummary() {
Set<String> entries = new HashSet<>();
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ONGOING)) {
entries.add(mContext.getString(R.string.notif_type_ongoing));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_CONVERSATIONS)) {
entries.add(mContext.getString(R.string.notif_type_conversation));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_ALERTING)) {
entries.add(mContext.getString(R.string.notif_type_alerting));
}
if (hasFlag(mNlf.getTypes(), FLAG_FILTER_TYPE_SILENT)) {
entries.add(mContext.getString(R.string.notif_type_silent));
}
return String.join(System.lineSeparator(), entries);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// retrieve latest in case the package filter has changed
mNlf = mNm.getListenerFilter(mCn, mUserId);
Set<String> set = (Set<String>) newValue;
boolean enabled = (boolean) newValue;
int newFilter = 0;
for (String filterType : set) {
newFilter |= Integer.parseInt(filterType);
int newFilter = mNlf.getTypes();
if (enabled) {
newFilter |= getType();
} else {
newFilter &= ~getType();
}
mNlf.setTypes(newFilter);
preference.setSummary(getSummary());
mNm.setListenerFilter(mCn, mUserId, mNlf);
return true;
}
@Override
public void updateState(Preference pref) {
mNlf = mNm.getListenerFilter(mCn, mUserId);
CheckBoxPreference check = (CheckBoxPreference) pref;
check.setChecked(hasFlag(mNlf.getTypes(), getType()));
boolean disableRequestedByApp = false;
if (mSi != null) {
if (mSi.metaData != null && mSi.metaData.containsKey(
NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES)) {
String typeList = mSi.metaData.get(
NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES).toString();
if (typeList != null) {
int types = 0;
String[] typeStrings = typeList.split(XML_SEPARATOR);
for (int i = 0; i < typeStrings.length; i++) {
if (TextUtils.isEmpty(typeStrings[i])) {
continue;
}
try {
types |= Integer.parseInt(typeStrings[i]);
} catch (NumberFormatException e) {
// skip
}
}
if (hasFlag(types, getType())) {
disableRequestedByApp = true;
}
}
}
}
// Apps can prevent a category from being turned on, but not turned off
boolean disabledByApp = disableRequestedByApp && !check.isChecked();
pref.setEnabled(getAvailabilityStatus() == AVAILABLE && !disabledByApp);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2021 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.utils;
import android.os.Process;
import android.security.keystore.AndroidKeyStoreProvider;
import android.security.keystore.KeyProperties;
import android.security.keystore2.AndroidKeyStoreLoadStoreParameter;
import android.util.Log;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
/**
* This class provides a portable and unified way to load the content of AndroidKeyStore through
* public API.
* @hide
*/
public class AndroidKeystoreAliasLoader {
private static final String TAG = "SettingsKeystoreUtils";
private final Collection<String> mKeyCertAliases;
private final Collection<String> mCaCertAliases;
/**
* This Constructor loads all aliases of asymmetric keys pairs and certificates in the
* AndroidKeyStore within the given namespace.
* Viable namespaces are {@link KeyProperties#NAMESPACE_WIFI},
* {@link KeyProperties#NAMESPACE_APPLICATION}, or null. The latter two are equivalent in
* that they will load the keystore content of the app's own namespace. In case of settings,
* this is the namespace of the AID_SYSTEM.
*
* @param namespace {@link KeyProperties#NAMESPACE_WIFI},
* {@link KeyProperties#NAMESPACE_APPLICATION}, or null
* @hide
*/
public AndroidKeystoreAliasLoader(Integer namespace) {
mKeyCertAliases = new ArrayList<>();
mCaCertAliases = new ArrayList<>();
KeyStore keyStore = null;
final Enumeration<String> aliases;
try {
if (namespace != null && namespace != KeyProperties.NAMESPACE_APPLICATION) {
if (AndroidKeyStoreProvider.isKeystore2Enabled()) {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(new AndroidKeyStoreLoadStoreParameter(namespace));
} else {
// In the legacy case we pass in the WIFI UID because that is the only
// possible special namespace that existed as of this writing,
// and new namespaces must only be added using the new mechanism.
keyStore = AndroidKeyStoreProvider.getKeyStoreForUid(Process.WIFI_UID);
}
} else {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
}
aliases = keyStore.aliases();
} catch (Exception e) {
Log.e(TAG, "Failed to open Android Keystore.", e);
// Will return empty lists.
return;
}
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
try {
Key key = keyStore.getKey(alias, null);
if (key != null) {
if (key instanceof PrivateKey) {
mKeyCertAliases.add(alias);
}
} else {
if (keyStore.getCertificate(alias) != null) {
mCaCertAliases.add(alias);
}
}
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
Log.e(TAG, "Failed to load alias: "
+ alias + " from Android Keystore. Ignoring.", e);
}
}
}
/**
* Returns the aliases of the key pairs and certificates stored in the Android KeyStore at the
* time the constructor was called.
* @return Collection of keystore aliases.
* @hide
*/
public Collection<String> getKeyCertAliases() {
return mKeyCertAliases;
}
/**
* Returns the aliases of the trusted certificates stored in the Android KeyStore at the
* time the constructor was called.
* @return Collection of keystore aliases.
* @hide
*/
public Collection<String> getCaCertAliases() {
return mCaCertAliases;
}
}

View File

@@ -34,8 +34,7 @@ import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.security.Credentials;
import android.security.KeyStore;
import android.security.keystore.KeyProperties;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.Editable;
@@ -72,6 +71,7 @@ import com.android.net.module.util.ProxyUtils;
import com.android.settings.ProxySelector;
import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.Utils;
import com.android.settingslib.utils.ThreadUtils;
@@ -80,7 +80,7 @@ import com.android.settingslib.wifi.AccessPoint;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -1031,15 +1031,17 @@ public class WifiConfigController implements TextWatcher,
if (refreshCertificates) {
loadSims();
final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
Credentials.CA_CERTIFICATE,
androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
false /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
loadCertificates(
mEapUserCertSpinner,
Credentials.USER_PRIVATE_KEY,
androidKeystoreAliasLoader.getKeyCertAliases(),
mDoNotProvideEapUserCertString,
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -1122,10 +1124,13 @@ public class WifiConfigController implements TextWatcher,
} else if (caCerts.length == 1) {
setSelection(mEapCaCertSpinner, caCerts[0]);
} else {
final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
getAndroidKeystoreAliasLoader();
// Reload the cert spinner with an extra "multiple certificates added" item.
loadCertificates(
mEapCaCertSpinner,
Credentials.CA_CERTIFICATE,
androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
true /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
@@ -1444,8 +1449,8 @@ public class WifiConfigController implements TextWatcher,
}
@VisibleForTesting
KeyStore getKeyStore() {
return KeyStore.getInstance();
AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
return new AndroidKeystoreAliasLoader(KeyProperties.NAMESPACE_WIFI);
}
@VisibleForTesting
@@ -1489,7 +1494,7 @@ public class WifiConfigController implements TextWatcher,
@VisibleForTesting
void loadCertificates(
Spinner spinner,
String prefix,
Collection<String> choices,
String noCertificateString,
boolean showMultipleCerts,
boolean showUsePreinstalledCertOption) {
@@ -1504,14 +1509,8 @@ public class WifiConfigController implements TextWatcher,
certs.add(mUseSystemCertsString);
}
String[] certificateNames = null;
try {
certificateNames = getKeyStore().list(prefix, android.os.Process.WIFI_UID);
} catch (Exception e) {
Log.e(TAG, "can't get the certificate list from KeyStore");
}
if (certificateNames != null && certificateNames.length != 0) {
certs.addAll(Arrays.stream(certificateNames)
if (choices != null && choices.size() != 0) {
certs.addAll(choices.stream()
.filter(certificateName -> {
for (String undesired : UNDESIRED_CERTIFICATES) {
if (certificateName.startsWith(undesired)) {

View File

@@ -32,8 +32,7 @@ import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.security.Credentials;
import android.security.KeyStore;
import android.security.keystore.KeyProperties;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.Editable;
@@ -70,6 +69,7 @@ import com.android.net.module.util.ProxyUtils;
import com.android.settings.ProxySelector;
import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.details2.WifiPrivacyPreferenceController2;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.Utils;
@@ -80,7 +80,7 @@ import com.android.wifitrackerlib.WifiEntry.ConnectedInfo;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -994,15 +994,17 @@ public class WifiConfigController2 implements TextWatcher,
if (refreshCertificates) {
loadSims();
final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
Credentials.CA_CERTIFICATE,
androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
false /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
loadCertificates(
mEapUserCertSpinner,
Credentials.USER_PRIVATE_KEY,
androidKeystoreAliasLoader.getKeyCertAliases(),
mDoNotProvideEapUserCertString,
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -1087,9 +1089,11 @@ public class WifiConfigController2 implements TextWatcher,
setSelection(mEapCaCertSpinner, caCerts[0]);
} else {
// Reload the cert spinner with an extra "multiple certificates added" item.
final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
Credentials.CA_CERTIFICATE,
androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
true /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
@@ -1408,8 +1412,8 @@ public class WifiConfigController2 implements TextWatcher,
}
@VisibleForTesting
KeyStore getKeyStore() {
return KeyStore.getInstance();
AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
return new AndroidKeystoreAliasLoader(KeyProperties.NAMESPACE_WIFI);
}
@VisibleForTesting
@@ -1453,7 +1457,7 @@ public class WifiConfigController2 implements TextWatcher,
@VisibleForTesting
void loadCertificates(
Spinner spinner,
String prefix,
Collection<String> choices,
String noCertificateString,
boolean showMultipleCerts,
boolean showUsePreinstalledCertOption) {
@@ -1468,14 +1472,8 @@ public class WifiConfigController2 implements TextWatcher,
certs.add(mUseSystemCertsString);
}
String[] certificateNames = null;
try {
certificateNames = getKeyStore().list(prefix, android.os.Process.WIFI_UID);
} catch (Exception e) {
Log.e(TAG, "can't get the certificate list from KeyStore");
}
if (certificateNames != null && certificateNames.length != 0) {
certs.addAll(Arrays.stream(certificateNames)
if (choices != null && choices.size() != 0) {
certs.addAll(choices.stream()
.filter(certificateName -> {
for (String undesired : UNDESIRED_CERTIFICATES) {
if (certificateName.startsWith(undesired)) {

View File

@@ -18,9 +18,6 @@ package com.android.settings.wifi;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -33,9 +30,6 @@ import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
import android.os.ServiceSpecificException;
import android.security.Credentials;
import android.security.KeyStore;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -51,9 +45,12 @@ import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.details2.WifiPrivacyPreferenceController2;
import com.android.wifitrackerlib.WifiEntry;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -79,7 +76,7 @@ public class WifiConfigController2Test {
@Mock
private WifiEntry mWifiEntry;
@Mock
private KeyStore mKeyStore;
private AndroidKeystoreAliasLoader mAndroidKeystoreAliasLoader;
private View mView;
private Spinner mHiddenSettingsSpinner;
private Spinner mEapCaCertSpinner;
@@ -285,28 +282,12 @@ public class WifiConfigController2Test {
assertThat(mController.getSignalString()).isNull();
}
@Test
public void loadCertificates_keyStoreListFail_shouldNotCrash() {
// Set up
when(mWifiEntry.getSecurity()).thenReturn(WifiEntry.SECURITY_EAP);
when(mKeyStore.list(anyString()))
.thenThrow(new ServiceSpecificException(-1, "permission error"));
mController = new TestWifiConfigController2(mConfigUiBase, mView, mWifiEntry,
WifiConfigUiBase2.MODE_CONNECT);
// Verify that the EAP method menu is visible.
assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.VISIBLE);
// No Crash
}
@Test
public void loadCertificates_undesiredCertificates_shouldNotLoadUndesiredCertificates() {
final Spinner spinner = new Spinner(mContext);
when(mKeyStore.list(anyString())).thenReturn(WifiConfigController2.UNDESIRED_CERTIFICATES);
mController.loadCertificates(spinner,
"prefix",
Arrays.asList(WifiConfigController.UNDESIRED_CERTIFICATES),
"doNotProvideEapUserCertString",
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -432,8 +413,8 @@ public class WifiConfigController2Test {
}
@Override
KeyStore getKeyStore() {
return mKeyStore;
AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
return mAndroidKeystoreAliasLoader;
}
}
@@ -882,6 +863,7 @@ public class WifiConfigController2Test {
String savedUserCertificate) {
final WifiConfiguration mockWifiConfig = mock(WifiConfiguration.class);
final WifiEnterpriseConfig mockWifiEnterpriseConfig = mock(WifiEnterpriseConfig.class);
mockWifiConfig.enterpriseConfig = mockWifiEnterpriseConfig;
when(mWifiEntry.isSaved()).thenReturn(true);
when(mWifiEntry.getSecurity()).thenReturn(WifiEntry.SECURITY_EAP);
@@ -892,15 +874,15 @@ public class WifiConfigController2Test {
String[] savedCaCertificates = new String[]{savedCaCertificate};
when(mockWifiEnterpriseConfig.getCaCertificateAliases())
.thenReturn(savedCaCertificates);
when(mKeyStore.list(eq(Credentials.CA_CERTIFICATE), anyInt()))
.thenReturn(savedCaCertificates);
when(mAndroidKeystoreAliasLoader.getCaCertAliases())
.thenReturn(ImmutableList.of(savedCaCertificate));
}
if (savedUserCertificate != null) {
String[] savedUserCertificates = new String[]{savedUserCertificate};
when(mockWifiEnterpriseConfig.getClientCertificateAlias())
.thenReturn(savedUserCertificate);
when(mKeyStore.list(eq(Credentials.USER_PRIVATE_KEY), anyInt()))
.thenReturn(savedUserCertificates);
when(mAndroidKeystoreAliasLoader.getKeyCertAliases())
.thenReturn(ImmutableList.of(savedUserCertificate));
}
mController = new TestWifiConfigController2(mConfigUiBase, mView, mWifiEntry,

View File

@@ -21,7 +21,6 @@ import static com.android.settings.wifi.WifiConfigController.PRIVACY_SPINNER_IND
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -34,8 +33,6 @@ import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
import android.os.ServiceSpecificException;
import android.security.KeyStore;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -77,8 +74,6 @@ public class WifiConfigControllerTest {
private Context mContext;
@Mock
private AccessPoint mAccessPoint;
@Mock
private KeyStore mKeyStore;
private View mView;
private Spinner mHiddenSettingsSpinner;
private ShadowSubscriptionManager mShadowSubscriptionManager;
@@ -266,28 +261,12 @@ public class WifiConfigControllerTest {
assertThat(mController.getSignalString()).isNull();
}
@Test
public void loadCertificates_keyStoreListFail_shouldNotCrash() {
// Set up
when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_EAP);
when(mKeyStore.list(anyString()))
.thenThrow(new ServiceSpecificException(-1, "permission error"));
mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
WifiConfigUiBase.MODE_CONNECT);
// Verify that the EAP method menu is visible.
assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.VISIBLE);
// No Crash
}
@Test
public void loadCertificates_undesiredCertificates_shouldNotLoadUndesiredCertificates() {
final Spinner spinner = new Spinner(mContext);
when(mKeyStore.list(anyString())).thenReturn(WifiConfigController.UNDESIRED_CERTIFICATES);
mController.loadCertificates(spinner,
"prefix",
Arrays.asList(WifiConfigController.UNDESIRED_CERTIFICATES),
"doNotProvideEapUserCertString",
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -412,8 +391,6 @@ public class WifiConfigControllerTest {
super(parent, view, accessPoint, mode, wifiManager);
}
@Override
KeyStore getKeyStore() { return mKeyStore; }
}
@Test

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ALERTING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class AlertingTypeFilterPreferenceControllerTest {
private Context mContext;
private AlertingTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new AlertingTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_ALERTING);
}
}

View File

@@ -35,10 +35,10 @@ import android.os.Looper;
import android.service.notification.NotificationListenerFilter;
import android.util.ArraySet;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -122,7 +122,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
SwitchPreference p = mock(SwitchPreference.class);
CheckBoxPreference p = mock(CheckBoxPreference.class);
when(p.getKey()).thenReturn("pkg|12300");
mScreen.addPreference(p);
@@ -163,7 +163,7 @@ public class BridgedAppsPreferenceControllerTest {
mController.onRebuildComplete(entries);
SwitchPreference actual = mScreen.findPreference("pkg|12300");
CheckBoxPreference actual = mScreen.findPreference("pkg|12300");
assertThat(actual.isChecked()).isTrue();
assertThat(actual.getTitle()).isEqualTo("hi");
@@ -180,7 +180,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
SwitchPreference pref = new SwitchPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
pref.setKey("pkg|567");
mController.onPreferenceChange(pref, false);
@@ -206,7 +206,7 @@ public class BridgedAppsPreferenceControllerTest {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
SwitchPreference pref = new SwitchPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
pref.setKey("pkg|567");
mController.onPreferenceChange(pref, true);

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_CONVERSATIONS;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class ConversationTypeFilterPreferenceControllerTest {
private Context mContext;
private ConversationTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new ConversationTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_ONGOING;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class OngoingTypeFilterPreferenceControllerTest {
private Context mContext;
private OngoingTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new OngoingTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_ONGOING);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 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.applications.specialaccess.notificationaccess;
import static android.service.notification.NotificationListenerService.FLAG_FILTER_TYPE_SILENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.notification.NotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class SilentTypeFilterPreferenceControllerTest {
private Context mContext;
private SilentTypeFilterPreferenceController mController;
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new SilentTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void getType() {
assertThat(mController.getType()).isEqualTo(FLAG_FILTER_TYPE_SILENT);
}
}

View File

@@ -28,10 +28,13 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.service.notification.NotificationListenerFilter;
import android.service.notification.NotificationListenerService;
import android.util.ArraySet;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.CheckBoxPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -54,23 +57,76 @@ public class TypeFilterPreferenceControllerTest {
@Mock
NotificationBackend mNm;
ComponentName mCn = new ComponentName("a", "b");
ServiceInfo mSi = new ServiceInfo();
private static class TestTypeFilterPreferenceController extends TypeFilterPreferenceController {
public TestTypeFilterPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected int getType() {
return 32;
}
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new TypeFilterPreferenceController(mContext, "key");
mController = new TestTypeFilterPreferenceController(mContext, "key");
mController.setCn(mCn);
mController.setNm(mNm);
mController.setServiceInfo(mSi);
mController.setUserId(0);
}
@Test
public void updateState_enabled() {
public void updateState_enabled_noMetaData() {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_notTheDisableFilter() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence("test", "value");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_disableFilter_notThisField() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_enabled_metaData_disableFilter_thisField_stateIsChecked() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2,32");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(
new NotificationListenerFilter(32, new ArraySet<>()));
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
@@ -80,57 +136,86 @@ public class TypeFilterPreferenceControllerTest {
public void updateState_disabled() {
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(false);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(new NotificationListenerFilter());
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isEnabled()).isFalse();
}
@Test
public void updateState() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_SILENT, new ArraySet<>());
public void updateState_disabled_metaData_disableFilter_thisField_stateIsNotChecked() {
mSi.metaData = new Bundle();
mSi.metaData.putCharSequence(NotificationListenerService.META_DATA_DISABLED_FILTER_TYPES,
"1,2,32");
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
NotificationListenerFilter before = new NotificationListenerFilter(4, new ArraySet<>());
when(mNm.getListenerFilter(mCn, 0)).thenReturn(before);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.updateState(pref);
assertThat(pref.getValues()).containsExactlyElementsIn(
new String[] {String.valueOf(FLAG_FILTER_TYPE_ONGOING),
String.valueOf(FLAG_FILTER_TYPE_SILENT)});
assertThat(pref.getSummary()).isNotNull();
assertThat(pref.isChecked()).isFalse();
assertThat(pref.isEnabled()).isFalse();
}
@Test
public void getSummary() {
public void updateState_checked() {
NotificationListenerFilter nlf = new NotificationListenerFilter(mController.getType(),
new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isChecked()).isTrue();
}
@Test
public void updateState_unchecked() {
NotificationListenerFilter nlf = new NotificationListenerFilter(mController.getType() - 1,
new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.updateState(pref);
assertThat(pref.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_true() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS, new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.updateState(pref);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
assertThat(mController.getSummary().toString()).ignoringCase().contains("ongoing");
assertThat(mController.getSummary().toString()).ignoringCase().contains("conversation");
}
@Test
public void onPreferenceChange() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS, new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
MultiSelectListPreference pref = new MultiSelectListPreference(mContext);
mController.onPreferenceChange(pref, Set.of("8", "1", "4"));
mController.onPreferenceChange(pref, true);
ArgumentCaptor<NotificationListenerFilter> captor =
ArgumentCaptor.forClass(NotificationListenerFilter.class);
verify(mNm).setListenerFilter(eq(mCn), eq(0), captor.capture());
assertThat(captor.getValue().getTypes()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS
| FLAG_FILTER_TYPE_SILENT | FLAG_FILTER_TYPE_ONGOING);
| FLAG_FILTER_TYPE_ONGOING | mController.getType());
}
@Test
public void onPreferenceChange_false() {
NotificationListenerFilter nlf = new NotificationListenerFilter(FLAG_FILTER_TYPE_ONGOING
| FLAG_FILTER_TYPE_CONVERSATIONS | mController.getType(), new ArraySet<>());
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
when(mNm.getListenerFilter(mCn, 0)).thenReturn(nlf);
CheckBoxPreference pref = new CheckBoxPreference(mContext);
mController.onPreferenceChange(pref, false);
ArgumentCaptor<NotificationListenerFilter> captor =
ArgumentCaptor.forClass(NotificationListenerFilter.class);
verify(mNm).setListenerFilter(eq(mCn), eq(0), captor.capture());
assertThat(captor.getValue().getTypes()).isEqualTo(FLAG_FILTER_TYPE_CONVERSATIONS
| FLAG_FILTER_TYPE_ONGOING);
}
}