[Settings] Adjust code for performance

Reorder code flow to reduce redundent work.

Bug: 213836977
Change-Id: Ieb1a15e867dcc5c65e8092d9f5c5eb38245138c9
Test: Junit VpnPreferenceControllerTest
This commit is contained in:
Bonian Chen
2022-01-18 05:11:42 +00:00
parent 13f22fd6c5
commit 9d56033f5f

View File

@@ -30,7 +30,6 @@ import android.provider.SettingsSlicesContract;
import android.security.Credentials; import android.security.Credentials;
import android.security.LegacyVpnProfileStore; import android.security.LegacyVpnProfileStore;
import android.util.Log; import android.util.Log;
import android.util.SparseArray;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -140,6 +139,7 @@ public class VpnPreferenceController extends AbstractPreferenceController
} }
VpnConfig vpn = vpnManager.getVpnConfig(uid); VpnConfig vpn = vpnManager.getVpnConfig(uid);
if ((vpn != null) && vpn.legacy) { if ((vpn != null) && vpn.legacy) {
// Copied from SystemUI::SecurityControllerImpl
// Legacy VPNs should do nothing if the network is disconnected. Third-party // Legacy VPNs should do nothing if the network is disconnected. Third-party
// VPN warnings need to continue as traffic can still go to the app. // VPN warnings need to continue as traffic can still go to the app.
final LegacyVpnInfo legacyVpn = vpnManager.getLegacyVpnInfo(uid); final LegacyVpnInfo legacyVpn = vpnManager.getLegacyVpnInfo(uid);
@@ -158,34 +158,19 @@ public class VpnPreferenceController extends AbstractPreferenceController
} }
protected int getNumberOfNonLegacyVpn(UserManager userManager, VpnManager vpnManager) { protected int getNumberOfNonLegacyVpn(UserManager userManager, VpnManager vpnManager) {
// Copied from SystemUI::SecurityControllerImpl // Converted from SystemUI::SecurityControllerImpl
SparseArray<VpnConfig> vpns = new SparseArray<>(); return (int) userManager.getUsers().stream()
final List<UserInfo> users = userManager.getUsers(); .map(user -> vpnManager.getVpnConfig(user.id))
int connectedLegacyVpnCount = 0; .filter(cfg -> (cfg != null) && (!cfg.legacy))
for (UserInfo user : users) { .count();
VpnConfig cfg = vpnManager.getVpnConfig(user.id);
if (cfg == null) {
continue;
} else if (cfg.legacy) {
// Legacy VPNs should do nothing if the network is disconnected. Third-party
// VPN warnings need to continue as traffic can still go to the app.
final LegacyVpnInfo legacyVpn = vpnManager.getLegacyVpnInfo(user.id);
if (legacyVpn == null || legacyVpn.state != LegacyVpnInfo.STATE_CONNECTED) {
continue;
} else {
connectedLegacyVpnCount++;
}
}
vpns.put(user.id, cfg);
}
return vpns.size() - connectedLegacyVpnCount;
} }
protected String getInsecureVpnSummaryOverride(UserManager userManager, protected String getInsecureVpnSummaryOverride(UserManager userManager,
VpnManager vpnManager) { VpnManager vpnManager) {
// Optionally add warning icon if an insecure VPN is present. // Optionally add warning icon if an insecure VPN is present.
if (mPreference instanceof VpnInfoPreference) { if (mPreference instanceof VpnInfoPreference) {
final int insecureVpnCount = getInsecureVpnCount(); String [] legacyVpnProfileKeys = LegacyVpnProfileStore.list(Credentials.VPN);
final int insecureVpnCount = getInsecureVpnCount(legacyVpnProfileKeys);
boolean isInsecureVPN = insecureVpnCount > 0; boolean isInsecureVPN = insecureVpnCount > 0;
((VpnInfoPreference) mPreference).setInsecureVpn(isInsecureVPN); ((VpnInfoPreference) mPreference).setInsecureVpn(isInsecureVPN);
@@ -193,11 +178,14 @@ public class VpnPreferenceController extends AbstractPreferenceController
if (isInsecureVPN) { if (isInsecureVPN) {
// Add the users and the number of legacy vpns to determine if there is more than // Add the users and the number of legacy vpns to determine if there is more than
// one vpn, since there can be more than one VPN per user. // one vpn, since there can be more than one VPN per user.
final int vpnCount = getNumberOfNonLegacyVpn(userManager, vpnManager) int vpnCount = legacyVpnProfileKeys.length;
+ LegacyVpnProfileStore.list(Credentials.VPN).length; if (vpnCount <= 1) {
vpnCount += getNumberOfNonLegacyVpn(userManager, vpnManager);
if (vpnCount == 1) { if (vpnCount == 1) {
return mContext.getString(R.string.vpn_settings_insecure_single); return mContext.getString(R.string.vpn_settings_insecure_single);
} else if (insecureVpnCount == 1) { }
}
if (insecureVpnCount == 1) {
return mContext.getString( return mContext.getString(
R.string.vpn_settings_single_insecure_multiple_total, R.string.vpn_settings_single_insecure_multiple_total,
insecureVpnCount); insecureVpnCount);
@@ -229,10 +217,10 @@ public class VpnPreferenceController extends AbstractPreferenceController
} }
@VisibleForTesting @VisibleForTesting
protected int getInsecureVpnCount() { protected int getInsecureVpnCount(String [] legacyVpnProfileKeys) {
final Function<String, VpnProfile> keyToProfile = key -> final Function<String, VpnProfile> keyToProfile = key ->
VpnProfile.decode(key, LegacyVpnProfileStore.get(Credentials.VPN + key)); VpnProfile.decode(key, LegacyVpnProfileStore.get(Credentials.VPN + key));
return (int) Arrays.stream(LegacyVpnProfileStore.list(Credentials.VPN)) return (int) Arrays.stream(legacyVpnProfileKeys)
.map(keyToProfile) .map(keyToProfile)
// Return whether any profile is an insecure type. // Return whether any profile is an insecure type.
.filter(profile -> VpnProfile.isLegacyType(profile.type)) .filter(profile -> VpnProfile.isLegacyType(profile.type))