Merge "RESTRICT AUTOMERGE Refactoring settings UI" into tm-qpr-dev
This commit is contained in:
@@ -625,9 +625,6 @@
|
|||||||
<!-- The extra value for battery tip -->
|
<!-- The extra value for battery tip -->
|
||||||
<integer name="config_battery_extra_tip_value">12</integer>
|
<integer name="config_battery_extra_tip_value">12</integer>
|
||||||
|
|
||||||
<!-- Whether to enable the advanced vpn feature. The default is not to. -->
|
|
||||||
<bool name="config_advanced_vpn_enabled">false</bool>
|
|
||||||
|
|
||||||
<!-- An array of uid name for which packages exempt from Wi-Fi permission check. -->
|
<!-- An array of uid name for which packages exempt from Wi-Fi permission check. -->
|
||||||
<string-array name="config_exempt_wifi_permission_uid_name" translatable="false">
|
<string-array name="config_exempt_wifi_permission_uid_name" translatable="false">
|
||||||
<item>@string/config_settingsintelligence_package_name</item>
|
<item>@string/config_settingsintelligence_package_name</item>
|
||||||
|
@@ -16,9 +16,4 @@
|
|||||||
|
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:title="@string/vpn_title">
|
android:title="@string/vpn_title">
|
||||||
<PreferenceCategory
|
|
||||||
android:key="advanced_vpn_group"/>
|
|
||||||
|
|
||||||
<PreferenceCategory
|
|
||||||
android:key="vpn_group"/>
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
@@ -45,7 +45,6 @@ import com.android.settings.security.SecurityFeatureProvider;
|
|||||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||||
import com.android.settings.slices.SlicesFeatureProvider;
|
import com.android.settings.slices.SlicesFeatureProvider;
|
||||||
import com.android.settings.users.UserFeatureProvider;
|
import com.android.settings.users.UserFeatureProvider;
|
||||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
|
||||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
@@ -178,11 +177,6 @@ public abstract class FeatureFactory {
|
|||||||
*/
|
*/
|
||||||
public abstract AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider();
|
public abstract AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider();
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves implementation for advanced vpn feature.
|
|
||||||
*/
|
|
||||||
public abstract AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider();
|
|
||||||
|
|
||||||
public static final class FactoryNotFoundException extends RuntimeException {
|
public static final class FactoryNotFoundException extends RuntimeException {
|
||||||
public FactoryNotFoundException(Throwable throwable) {
|
public FactoryNotFoundException(Throwable throwable) {
|
||||||
super("Unable to create factory. Did you misconfigure Proguard?", throwable);
|
super("Unable to create factory. Did you misconfigure Proguard?", throwable);
|
||||||
|
@@ -71,8 +71,6 @@ import com.android.settings.slices.SlicesFeatureProvider;
|
|||||||
import com.android.settings.slices.SlicesFeatureProviderImpl;
|
import com.android.settings.slices.SlicesFeatureProviderImpl;
|
||||||
import com.android.settings.users.UserFeatureProvider;
|
import com.android.settings.users.UserFeatureProvider;
|
||||||
import com.android.settings.users.UserFeatureProviderImpl;
|
import com.android.settings.users.UserFeatureProviderImpl;
|
||||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
|
||||||
import com.android.settings.vpn2.AdvancedVpnFeatureProviderImpl;
|
|
||||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||||
import com.android.settings.wifi.WifiTrackerLibProviderImpl;
|
import com.android.settings.wifi.WifiTrackerLibProviderImpl;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
@@ -108,7 +106,6 @@ public class FeatureFactoryImpl extends FeatureFactory {
|
|||||||
private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
|
private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
|
||||||
private AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
private AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||||
private AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
private AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||||
private AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SupportFeatureProvider getSupportFeatureProvider(Context context) {
|
public SupportFeatureProvider getSupportFeatureProvider(Context context) {
|
||||||
@@ -337,12 +334,4 @@ public class FeatureFactoryImpl extends FeatureFactory {
|
|||||||
}
|
}
|
||||||
return mAccessibilityMetricsFeatureProvider;
|
return mAccessibilityMetricsFeatureProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
|
||||||
if (mAdvancedVpnFeatureProvider == null) {
|
|
||||||
mAdvancedVpnFeatureProvider = new AdvancedVpnFeatureProviderImpl();
|
|
||||||
}
|
|
||||||
return mAdvancedVpnFeatureProvider;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2022 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.vpn2;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Feature Provider used in vpn usage
|
|
||||||
*/
|
|
||||||
public interface AdvancedVpnFeatureProvider {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns package name of advanced vpn.
|
|
||||||
*/
|
|
||||||
String getAdvancedVpnPackageName();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns {@code true} advanced vpn is supported.
|
|
||||||
*/
|
|
||||||
boolean isAdvancedVpnSupported(Context context);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the title of advanced vpn preference group.
|
|
||||||
*/
|
|
||||||
String getAdvancedVpnPreferenceGroupTitle(Context context);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the title of vpn preference group.
|
|
||||||
*/
|
|
||||||
String getVpnPreferenceGroupTitle(Context context);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns {@code true} advanced vpn is removable.
|
|
||||||
*/
|
|
||||||
boolean isAdvancedVpnRemovable();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns {@code true} if the disconnect dialog is enabled when advanced vpn is connected.
|
|
||||||
*/
|
|
||||||
boolean isDisconnectDialogEnabled();
|
|
||||||
}
|
|
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2022 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.vpn2;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Feature provider implementation for advanced vpn.
|
|
||||||
*/
|
|
||||||
public class AdvancedVpnFeatureProviderImpl implements AdvancedVpnFeatureProvider {
|
|
||||||
@Override
|
|
||||||
public String getAdvancedVpnPackageName() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAdvancedVpnSupported(Context context) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAdvancedVpnPreferenceGroupTitle(Context context) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getVpnPreferenceGroupTitle(Context context) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAdvancedVpnRemovable() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDisconnectDialogEnabled() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -48,7 +48,6 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
import com.android.settings.core.SubSettingLauncher;
|
import com.android.settings.core.SubSettingLauncher;
|
||||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
|
||||||
import com.android.settingslib.RestrictedLockUtils;
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
import com.android.settingslib.RestrictedPreference;
|
import com.android.settingslib.RestrictedPreference;
|
||||||
@@ -72,7 +71,6 @@ public class AppManagementFragment extends SettingsPreferenceFragment
|
|||||||
private PackageManager mPackageManager;
|
private PackageManager mPackageManager;
|
||||||
private DevicePolicyManager mDevicePolicyManager;
|
private DevicePolicyManager mDevicePolicyManager;
|
||||||
private VpnManager mVpnManager;
|
private VpnManager mVpnManager;
|
||||||
private AdvancedVpnFeatureProvider mFeatureProvider;
|
|
||||||
|
|
||||||
// VPN app info
|
// VPN app info
|
||||||
private final int mUserId = UserHandle.myUserId();
|
private final int mUserId = UserHandle.myUserId();
|
||||||
@@ -124,7 +122,6 @@ public class AppManagementFragment extends SettingsPreferenceFragment
|
|||||||
mPackageManager = getContext().getPackageManager();
|
mPackageManager = getContext().getPackageManager();
|
||||||
mDevicePolicyManager = getContext().getSystemService(DevicePolicyManager.class);
|
mDevicePolicyManager = getContext().getSystemService(DevicePolicyManager.class);
|
||||||
mVpnManager = getContext().getSystemService(VpnManager.class);
|
mVpnManager = getContext().getSystemService(VpnManager.class);
|
||||||
mFeatureProvider = FeatureFactory.getFactory(getContext()).getAdvancedVpnFeatureProvider();
|
|
||||||
|
|
||||||
mPreferenceAlwaysOn = (RestrictedSwitchPreference) findPreference(KEY_ALWAYS_ON_VPN);
|
mPreferenceAlwaysOn = (RestrictedSwitchPreference) findPreference(KEY_ALWAYS_ON_VPN);
|
||||||
mPreferenceLockdown = (RestrictedSwitchPreference) findPreference(KEY_LOCKDOWN_VPN);
|
mPreferenceLockdown = (RestrictedSwitchPreference) findPreference(KEY_LOCKDOWN_VPN);
|
||||||
@@ -286,16 +283,7 @@ public class AppManagementFragment extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
private void updateRestrictedViews() {
|
||||||
void updateRestrictedViews() {
|
|
||||||
if (mFeatureProvider.isAdvancedVpnSupported(getContext())
|
|
||||||
&& !mFeatureProvider.isAdvancedVpnRemovable()
|
|
||||||
&& TextUtils.equals(mPackageName, mFeatureProvider.getAdvancedVpnPackageName())) {
|
|
||||||
mPreferenceForget.setVisible(false);
|
|
||||||
} else {
|
|
||||||
mPreferenceForget.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAdded()) {
|
if (isAdded()) {
|
||||||
mPreferenceAlwaysOn.checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN,
|
mPreferenceAlwaysOn.checkRestrictionAndSetDisabled(UserManager.DISALLOW_CONFIG_VPN,
|
||||||
mUserId);
|
mUserId);
|
||||||
@@ -326,14 +314,6 @@ public class AppManagementFragment extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
void init(String packageName, AdvancedVpnFeatureProvider featureProvider,
|
|
||||||
RestrictedPreference preference) {
|
|
||||||
mPackageName = packageName;
|
|
||||||
mFeatureProvider = featureProvider;
|
|
||||||
mPreferenceForget = preference;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getAlwaysOnVpnPackage() {
|
private String getAlwaysOnVpnPackage() {
|
||||||
return mVpnManager.getAlwaysOnVpnPackageForUser(mUserId);
|
return mVpnManager.getAlwaysOnVpnPackageForUser(mUserId);
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,6 @@ import android.app.AppOpsManager;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
@@ -43,7 +42,6 @@ import android.os.UserHandle;
|
|||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.security.Credentials;
|
import android.security.Credentials;
|
||||||
import android.security.LegacyVpnProfileStore;
|
import android.security.LegacyVpnProfileStore;
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.ArrayMap;
|
import android.util.ArrayMap;
|
||||||
import android.util.ArraySet;
|
import android.util.ArraySet;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -54,7 +52,6 @@ import android.view.MenuItem;
|
|||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceGroup;
|
import androidx.preference.PreferenceGroup;
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.internal.annotations.GuardedBy;
|
import com.android.internal.annotations.GuardedBy;
|
||||||
import com.android.internal.net.LegacyVpnInfo;
|
import com.android.internal.net.LegacyVpnInfo;
|
||||||
@@ -62,7 +59,6 @@ import com.android.internal.net.VpnConfig;
|
|||||||
import com.android.internal.net.VpnProfile;
|
import com.android.internal.net.VpnProfile;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.RestrictedSettingsFragment;
|
import com.android.settings.RestrictedSettingsFragment;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
|
||||||
import com.android.settings.widget.GearPreference;
|
import com.android.settings.widget.GearPreference;
|
||||||
import com.android.settings.widget.GearPreference.OnGearClickListener;
|
import com.android.settings.widget.GearPreference.OnGearClickListener;
|
||||||
import com.android.settingslib.RestrictedLockUtilsInternal;
|
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||||
@@ -83,12 +79,9 @@ import java.util.Set;
|
|||||||
public class VpnSettings extends RestrictedSettingsFragment implements
|
public class VpnSettings extends RestrictedSettingsFragment implements
|
||||||
Handler.Callback, Preference.OnPreferenceClickListener {
|
Handler.Callback, Preference.OnPreferenceClickListener {
|
||||||
private static final String LOG_TAG = "VpnSettings";
|
private static final String LOG_TAG = "VpnSettings";
|
||||||
private static final boolean DEBUG = Log.isLoggable(LOG_TAG, Log.DEBUG);
|
|
||||||
|
|
||||||
private static final int RESCAN_MESSAGE = 0;
|
private static final int RESCAN_MESSAGE = 0;
|
||||||
private static final int RESCAN_INTERVAL_MS = 1000;
|
private static final int RESCAN_INTERVAL_MS = 1000;
|
||||||
private static final String ADVANCED_VPN_GROUP_KEY = "advanced_vpn_group";
|
|
||||||
private static final String VPN_GROUP_KEY = "vpn_group";
|
|
||||||
|
|
||||||
private static final NetworkRequest VPN_REQUEST = new NetworkRequest.Builder()
|
private static final NetworkRequest VPN_REQUEST = new NetworkRequest.Builder()
|
||||||
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
||||||
@@ -109,9 +102,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
private LegacyVpnInfo mConnectedLegacyVpn;
|
private LegacyVpnInfo mConnectedLegacyVpn;
|
||||||
|
|
||||||
private boolean mUnavailable;
|
private boolean mUnavailable;
|
||||||
private AdvancedVpnFeatureProvider mFeatureProvider;
|
|
||||||
private PreferenceScreen mPreferenceScreen;
|
|
||||||
private boolean mIsAdvancedVpnSupported;
|
|
||||||
|
|
||||||
public VpnSettings() {
|
public VpnSettings() {
|
||||||
super(UserManager.DISALLOW_CONFIG_VPN);
|
super(UserManager.DISALLOW_CONFIG_VPN);
|
||||||
@@ -129,14 +119,11 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
|
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
|
||||||
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
mVpnManager = (VpnManager) getSystemService(Context.VPN_MANAGEMENT_SERVICE);
|
mVpnManager = (VpnManager) getSystemService(Context.VPN_MANAGEMENT_SERVICE);
|
||||||
mFeatureProvider = FeatureFactory.getFactory(getContext()).getAdvancedVpnFeatureProvider();
|
|
||||||
mIsAdvancedVpnSupported = mFeatureProvider.isAdvancedVpnSupported(getContext());
|
|
||||||
|
|
||||||
mUnavailable = isUiRestricted();
|
mUnavailable = isUiRestricted();
|
||||||
setHasOptionsMenu(!mUnavailable);
|
setHasOptionsMenu(!mUnavailable);
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.vpn_settings2);
|
addPreferencesFromResource(R.xml.vpn_settings2);
|
||||||
mPreferenceScreen = getPreferenceScreen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -242,8 +229,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
|
|
||||||
// Run heavy RPCs before switching to UI thread
|
// Run heavy RPCs before switching to UI thread
|
||||||
final List<VpnProfile> vpnProfiles = loadVpnProfiles();
|
final List<VpnProfile> vpnProfiles = loadVpnProfiles();
|
||||||
final List<AppVpnInfo> vpnApps = getVpnApps(context, /* includeProfiles */ true,
|
final List<AppVpnInfo> vpnApps = getVpnApps(context, /* includeProfiles */ true);
|
||||||
mFeatureProvider);
|
|
||||||
|
|
||||||
final Map<String, LegacyVpnInfo> connectedLegacyVpns = getConnectedLegacyVpns();
|
final Map<String, LegacyVpnInfo> connectedLegacyVpns = getConnectedLegacyVpns();
|
||||||
final Set<AppVpnInfo> connectedAppVpns = getConnectedAppVpns();
|
final Set<AppVpnInfo> connectedAppVpns = getConnectedAppVpns();
|
||||||
@@ -279,7 +265,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
|
|
||||||
private final VpnSettings mSettings;
|
private final VpnSettings mSettings;
|
||||||
|
|
||||||
UpdatePreferences(VpnSettings settings) {
|
public UpdatePreferences(VpnSettings settings) {
|
||||||
mSettings = settings;
|
mSettings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,14 +332,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trim out deleted VPN preferences
|
// Trim out deleted VPN preferences
|
||||||
if (DEBUG) {
|
mSettings.setShownPreferences(updates);
|
||||||
Log.d(LOG_TAG, "isAdvancedVpnSupported() : " + mSettings.mIsAdvancedVpnSupported);
|
|
||||||
}
|
|
||||||
if (mSettings.mIsAdvancedVpnSupported) {
|
|
||||||
mSettings.setShownAdvancedPreferences(updates);
|
|
||||||
} else {
|
|
||||||
mSettings.setShownPreferences(updates);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,61 +343,12 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
|
|
||||||
@VisibleForTesting @UiThread
|
@VisibleForTesting @UiThread
|
||||||
public void setShownPreferences(final Collection<Preference> updates) {
|
public void setShownPreferences(final Collection<Preference> updates) {
|
||||||
retainAllPreference(updates);
|
|
||||||
|
|
||||||
final PreferenceGroup vpnGroup = mPreferenceScreen;
|
|
||||||
updatePreferenceGroup(vpnGroup, updates);
|
|
||||||
|
|
||||||
// Show all new preferences on the screen
|
|
||||||
for (Preference pref : updates) {
|
|
||||||
vpnGroup.addPreference(pref);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting @UiThread
|
|
||||||
void setShownAdvancedPreferences(final Collection<Preference> updates) {
|
|
||||||
retainAllPreference(updates);
|
|
||||||
|
|
||||||
PreferenceGroup advancedVpnGroup = mPreferenceScreen.findPreference(ADVANCED_VPN_GROUP_KEY);
|
|
||||||
PreferenceGroup vpnGroup = mPreferenceScreen.findPreference(VPN_GROUP_KEY);
|
|
||||||
advancedVpnGroup.setTitle(
|
|
||||||
mFeatureProvider.getAdvancedVpnPreferenceGroupTitle(getContext()));
|
|
||||||
vpnGroup.setTitle(mFeatureProvider.getVpnPreferenceGroupTitle(getContext()));
|
|
||||||
updatePreferenceGroup(advancedVpnGroup, updates);
|
|
||||||
updatePreferenceGroup(vpnGroup, updates);
|
|
||||||
|
|
||||||
// Show all new preferences on the screen
|
|
||||||
for (Preference pref : updates) {
|
|
||||||
String packageName = "";
|
|
||||||
if (pref instanceof LegacyVpnPreference) {
|
|
||||||
LegacyVpnPreference legacyPref = (LegacyVpnPreference) pref;
|
|
||||||
packageName = legacyPref.getPackageName();
|
|
||||||
} else if (pref instanceof AppPreference) {
|
|
||||||
AppPreference appPref = (AppPreference) pref;
|
|
||||||
packageName = appPref.getPackageName();
|
|
||||||
}
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(LOG_TAG, "setShownAdvancedPreferences() package name : " + packageName);
|
|
||||||
}
|
|
||||||
if (TextUtils.equals(packageName, mFeatureProvider.getAdvancedVpnPackageName())) {
|
|
||||||
advancedVpnGroup.addPreference(pref);
|
|
||||||
} else {
|
|
||||||
vpnGroup.addPreference(pref);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
advancedVpnGroup.setVisible(advancedVpnGroup.getPreferenceCount() > 0);
|
|
||||||
vpnGroup.setVisible(vpnGroup.getPreferenceCount() > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void retainAllPreference(Collection<Preference> updates) {
|
|
||||||
mLegacyVpnPreferences.values().retainAll(updates);
|
mLegacyVpnPreferences.values().retainAll(updates);
|
||||||
mAppPreferences.values().retainAll(updates);
|
mAppPreferences.values().retainAll(updates);
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePreferenceGroup(PreferenceGroup vpnGroup, Collection<Preference> updates) {
|
|
||||||
// Change {@param updates} in-place to only contain new preferences that were not already
|
// Change {@param updates} in-place to only contain new preferences that were not already
|
||||||
// added to the preference screen.
|
// added to the preference screen.
|
||||||
|
final PreferenceGroup vpnGroup = getPreferenceScreen();
|
||||||
for (int i = vpnGroup.getPreferenceCount() - 1; i >= 0; i--) {
|
for (int i = vpnGroup.getPreferenceCount() - 1; i >= 0; i--) {
|
||||||
Preference p = vpnGroup.getPreference(i);
|
Preference p = vpnGroup.getPreference(i);
|
||||||
if (updates.contains(p)) {
|
if (updates.contains(p)) {
|
||||||
@@ -427,6 +357,11 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
vpnGroup.removePreference(p);
|
vpnGroup.removePreference(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show any new preferences on the screen
|
||||||
|
for (Preference pref : updates) {
|
||||||
|
vpnGroup.addPreference(pref);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -448,16 +383,14 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
} else if (preference instanceof AppPreference) {
|
} else if (preference instanceof AppPreference) {
|
||||||
AppPreference pref = (AppPreference) preference;
|
AppPreference pref = (AppPreference) preference;
|
||||||
boolean connected = (pref.getState() == AppPreference.STATE_CONNECTED);
|
boolean connected = (pref.getState() == AppPreference.STATE_CONNECTED);
|
||||||
String vpnPackageName = pref.getPackageName();
|
|
||||||
|
|
||||||
if ((!connected) || (isAdvancedVpn(mFeatureProvider, vpnPackageName, getContext())
|
if (!connected) {
|
||||||
&& !mFeatureProvider.isDisconnectDialogEnabled())) {
|
|
||||||
try {
|
try {
|
||||||
UserHandle user = UserHandle.of(pref.getUserId());
|
UserHandle user = UserHandle.of(pref.getUserId());
|
||||||
Context userContext = getContext().createPackageContextAsUser(
|
Context userContext = getActivity().createPackageContextAsUser(
|
||||||
getContext().getPackageName(), 0 /* flags */, user);
|
getActivity().getPackageName(), 0 /* flags */, user);
|
||||||
PackageManager pm = userContext.getPackageManager();
|
PackageManager pm = userContext.getPackageManager();
|
||||||
Intent appIntent = pm.getLaunchIntentForPackage(vpnPackageName);
|
Intent appIntent = pm.getLaunchIntentForPackage(pref.getPackageName());
|
||||||
if (appIntent != null) {
|
if (appIntent != null) {
|
||||||
userContext.startActivityAsUser(appIntent, user);
|
userContext.startActivityAsUser(appIntent, user);
|
||||||
return true;
|
return true;
|
||||||
@@ -537,32 +470,9 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
pref.setOnPreferenceClickListener(this);
|
pref.setOnPreferenceClickListener(this);
|
||||||
mAppPreferences.put(app, pref);
|
mAppPreferences.put(app, pref);
|
||||||
}
|
}
|
||||||
enableAdvancedVpnGearIconIfNecessary(pref);
|
|
||||||
return pref;
|
return pref;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableAdvancedVpnGearIconIfNecessary(AppPreference pref) {
|
|
||||||
Context context = getContext();
|
|
||||||
if (!isAdvancedVpn(mFeatureProvider, pref.getPackageName(), context)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isEnabled = false;
|
|
||||||
AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
|
|
||||||
List<AppOpsManager.PackageOps> apps =
|
|
||||||
appOpsManager.getPackagesForOps(
|
|
||||||
new int[] {OP_ACTIVATE_VPN, OP_ACTIVATE_PLATFORM_VPN});
|
|
||||||
if (apps != null) {
|
|
||||||
for (AppOpsManager.PackageOps pkg : apps) {
|
|
||||||
if (isAdvancedVpn(mFeatureProvider, pkg.getPackageName(), context)) {
|
|
||||||
isEnabled = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pref.setOnGearClickListener(isEnabled ? mGearListener : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
private Map<String, LegacyVpnInfo> getConnectedLegacyVpns() {
|
private Map<String, LegacyVpnInfo> getConnectedLegacyVpns() {
|
||||||
mConnectedLegacyVpn = mVpnManager.getLegacyVpnInfo(UserHandle.myUserId());
|
mConnectedLegacyVpn = mVpnManager.getLegacyVpnInfo(UserHandle.myUserId());
|
||||||
@@ -598,15 +508,7 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<AppVpnInfo> getVpnApps(Context context, boolean includeProfiles,
|
static List<AppVpnInfo> getVpnApps(Context context, boolean includeProfiles) {
|
||||||
AdvancedVpnFeatureProvider featureProvider) {
|
|
||||||
return getVpnApps(context, includeProfiles, featureProvider,
|
|
||||||
context.getSystemService(AppOpsManager.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
static List<AppVpnInfo> getVpnApps(Context context, boolean includeProfiles,
|
|
||||||
AdvancedVpnFeatureProvider featureProvider, AppOpsManager aom) {
|
|
||||||
List<AppVpnInfo> result = Lists.newArrayList();
|
List<AppVpnInfo> result = Lists.newArrayList();
|
||||||
|
|
||||||
final Set<Integer> profileIds;
|
final Set<Integer> profileIds;
|
||||||
@@ -619,19 +521,8 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
profileIds = Collections.singleton(UserHandle.myUserId());
|
profileIds = Collections.singleton(UserHandle.myUserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (featureProvider.isAdvancedVpnSupported(context)) {
|
// Fetch VPN-enabled apps from AppOps.
|
||||||
PackageManager pm = context.getPackageManager();
|
AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
|
||||||
try {
|
|
||||||
ApplicationInfo appInfo =
|
|
||||||
pm.getApplicationInfo(
|
|
||||||
featureProvider.getAdvancedVpnPackageName(), /* flags= */ 0);
|
|
||||||
int userId = UserHandle.getUserId(appInfo.uid);
|
|
||||||
result.add(new AppVpnInfo(userId, featureProvider.getAdvancedVpnPackageName()));
|
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
|
||||||
Log.e(LOG_TAG, "Advanced VPN package name not found.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<AppOpsManager.PackageOps> apps =
|
List<AppOpsManager.PackageOps> apps =
|
||||||
aom.getPackagesForOps(new int[] {OP_ACTIVATE_VPN, OP_ACTIVATE_PLATFORM_VPN});
|
aom.getPackagesForOps(new int[] {OP_ACTIVATE_VPN, OP_ACTIVATE_PLATFORM_VPN});
|
||||||
if (apps != null) {
|
if (apps != null) {
|
||||||
@@ -641,9 +532,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
// Skip packages for users outside of our profile group.
|
// Skip packages for users outside of our profile group.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (isAdvancedVpn(featureProvider, pkg.getPackageName(), context)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Look for a MODE_ALLOWED permission to activate VPN.
|
// Look for a MODE_ALLOWED permission to activate VPN.
|
||||||
boolean allowed = false;
|
boolean allowed = false;
|
||||||
for (AppOpsManager.OpEntry op : pkg.getOps()) {
|
for (AppOpsManager.OpEntry op : pkg.getOps()) {
|
||||||
@@ -662,12 +550,6 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isAdvancedVpn(AdvancedVpnFeatureProvider featureProvider,
|
|
||||||
String packageName, Context context) {
|
|
||||||
return featureProvider.isAdvancedVpnSupported(context)
|
|
||||||
&& TextUtils.equals(packageName, featureProvider.getAdvancedVpnPackageName());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<VpnProfile> loadVpnProfiles() {
|
private static List<VpnProfile> loadVpnProfiles() {
|
||||||
final ArrayList<VpnProfile> result = Lists.newArrayList();
|
final ArrayList<VpnProfile> result = Lists.newArrayList();
|
||||||
|
|
||||||
@@ -680,10 +562,4 @@ public class VpnSettings extends RestrictedSettingsFragment implements
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
void init(PreferenceScreen preferenceScreen, AdvancedVpnFeatureProvider featureProvider) {
|
|
||||||
mPreferenceScreen = preferenceScreen;
|
|
||||||
mFeatureProvider = featureProvider;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -47,7 +47,6 @@ import com.android.settings.security.SecurityFeatureProvider;
|
|||||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||||
import com.android.settings.slices.SlicesFeatureProvider;
|
import com.android.settings.slices.SlicesFeatureProvider;
|
||||||
import com.android.settings.users.UserFeatureProvider;
|
import com.android.settings.users.UserFeatureProvider;
|
||||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
|
||||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
@@ -88,7 +87,6 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
||||||
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||||
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||||
public AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call this in {@code @Before} method of the test class to use fake factory.
|
* Call this in {@code @Before} method of the test class to use fake factory.
|
||||||
@@ -138,7 +136,6 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
||||||
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
||||||
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
||||||
mAdvancedVpnFeatureProvider = mock(AdvancedVpnFeatureProvider.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -275,9 +272,4 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
||||||
return mAccessibilityMetricsFeatureProvider;
|
return mAccessibilityMetricsFeatureProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
|
||||||
return mAdvancedVpnFeatureProvider;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -45,7 +45,6 @@ import com.android.settings.security.SecurityFeatureProvider;
|
|||||||
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||||
import com.android.settings.slices.SlicesFeatureProvider;
|
import com.android.settings.slices.SlicesFeatureProvider;
|
||||||
import com.android.settings.users.UserFeatureProvider;
|
import com.android.settings.users.UserFeatureProvider;
|
||||||
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
|
|
||||||
import com.android.settings.wifi.WifiTrackerLibProvider;
|
import com.android.settings.wifi.WifiTrackerLibProvider;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
@@ -83,7 +82,6 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
|
||||||
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
|
||||||
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
|
||||||
public AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call this in {@code @Before} method of the test class to use fake factory.
|
* Call this in {@code @Before} method of the test class to use fake factory.
|
||||||
@@ -124,7 +122,6 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
|
||||||
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
|
||||||
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
|
||||||
mAdvancedVpnFeatureProvider = mock(AdvancedVpnFeatureProvider.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -261,9 +258,4 @@ public class FakeFeatureFactory extends FeatureFactory {
|
|||||||
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
|
||||||
return mAccessibilityMetricsFeatureProvider;
|
return mAccessibilityMetricsFeatureProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
|
|
||||||
return mAdvancedVpnFeatureProvider;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,102 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2022 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.vpn2;
|
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.spy;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Looper;
|
|
||||||
|
|
||||||
import androidx.test.annotation.UiThreadTest;
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
|
||||||
|
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
|
||||||
import com.android.settingslib.RestrictedPreference;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.junit.MockitoJUnit;
|
|
||||||
import org.mockito.junit.MockitoRule;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
public class AppManagementFragmentTest {
|
|
||||||
private static final String FAKE_PACKAGE_NAME = "com.fake.package.name";
|
|
||||||
private static final String ADVANCED_VPN_GROUP_PACKAGE_NAME = "com.advanced.package.name";
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
|
||||||
|
|
||||||
private AppManagementFragment mFragment;
|
|
||||||
private Context mContext;
|
|
||||||
private FakeFeatureFactory mFakeFeatureFactory;
|
|
||||||
private RestrictedPreference mPreferenceForget;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
@UiThreadTest
|
|
||||||
public void setUp() {
|
|
||||||
if (Looper.myLooper() == null) {
|
|
||||||
Looper.prepare();
|
|
||||||
}
|
|
||||||
|
|
||||||
mFragment = spy(new AppManagementFragment());
|
|
||||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
|
||||||
mPreferenceForget = new RestrictedPreference(mContext);
|
|
||||||
|
|
||||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
|
||||||
mFragment.init(ADVANCED_VPN_GROUP_PACKAGE_NAME,
|
|
||||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName())
|
|
||||||
.thenReturn(ADVANCED_VPN_GROUP_PACKAGE_NAME);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any()))
|
|
||||||
.thenReturn(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateRestrictedViews_isAdvancedVpn_hidesForgetPreference() {
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
|
||||||
.thenReturn(false);
|
|
||||||
mFragment.updateRestrictedViews();
|
|
||||||
assertThat(mPreferenceForget.isVisible()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateRestrictedViews_isNotAdvancedVpn_showsForgetPreference() {
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
|
||||||
.thenReturn(false);
|
|
||||||
mFragment.init(FAKE_PACKAGE_NAME,
|
|
||||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
|
||||||
mFragment.updateRestrictedViews();
|
|
||||||
assertThat(mPreferenceForget.isVisible()).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateRestrictedViews_isAdvancedVpnRemovable_showsForgetPreference() {
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnRemovable())
|
|
||||||
.thenReturn(true);
|
|
||||||
mFragment.init(FAKE_PACKAGE_NAME,
|
|
||||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(), mPreferenceForget);
|
|
||||||
mFragment.updateRestrictedViews();
|
|
||||||
assertThat(mPreferenceForget.isVisible()).isTrue();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,285 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2022 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.vpn2;
|
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.doNothing;
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.spy;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.app.AppOpsManager;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.os.UserHandle;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.ArraySet;
|
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceCategory;
|
|
||||||
import androidx.preference.PreferenceGroup;
|
|
||||||
import androidx.preference.PreferenceManager;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
import androidx.test.annotation.UiThreadTest;
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
|
||||||
|
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.ArgumentCaptor;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.MockitoJUnit;
|
|
||||||
import org.mockito.junit.MockitoRule;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
public class VpnSettingsTest {
|
|
||||||
private static final int USER_ID_1 = UserHandle.USER_NULL;
|
|
||||||
private static final String VPN_GROUP_KEY = "vpn_group";
|
|
||||||
private static final String VPN_GROUP_TITLE = "vpn_group_title";
|
|
||||||
private static final String VPN_PACKAGE_NAME = "vpn.package.name";
|
|
||||||
private static final String VPN_LAUNCH_INTENT = "vpn.action";
|
|
||||||
private static final String ADVANCED_VPN_GROUP_KEY = "advanced_vpn_group";
|
|
||||||
private static final String ADVANCED_VPN_GROUP_TITLE = "advanced_vpn_group_title";
|
|
||||||
private static final String ADVANCED_VPN_PACKAGE_NAME = "advanced.vpn.package.name";
|
|
||||||
private static final String ADVANCED_VPN_LAUNCH_INTENT = "advanced.vpn.action";
|
|
||||||
|
|
||||||
private final Intent mVpnIntent = new Intent().setAction(VPN_LAUNCH_INTENT);
|
|
||||||
private final Intent mAdvancedVpnIntent = new Intent().setAction(ADVANCED_VPN_LAUNCH_INTENT);
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private AppOpsManager mAppOpsManager;
|
|
||||||
@Mock
|
|
||||||
private PackageManager mPackageManager;
|
|
||||||
|
|
||||||
private VpnSettings mVpnSettings;
|
|
||||||
private Context mContext;
|
|
||||||
private PreferenceManager mPreferenceManager;
|
|
||||||
private PreferenceScreen mPreferenceScreen;
|
|
||||||
private PreferenceGroup mAdvancedVpnGroup;
|
|
||||||
private PreferenceGroup mVpnGroup;
|
|
||||||
private FakeFeatureFactory mFakeFeatureFactory;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
@UiThreadTest
|
|
||||||
public void setUp() throws PackageManager.NameNotFoundException {
|
|
||||||
if (Looper.myLooper() == null) {
|
|
||||||
Looper.prepare();
|
|
||||||
}
|
|
||||||
|
|
||||||
mVpnSettings = spy(new VpnSettings());
|
|
||||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
|
||||||
mAdvancedVpnGroup = spy(new PreferenceCategory(mContext));
|
|
||||||
mVpnGroup = spy(new PreferenceCategory(mContext));
|
|
||||||
mAdvancedVpnGroup.setKey(ADVANCED_VPN_GROUP_KEY);
|
|
||||||
mVpnGroup.setKey(VPN_GROUP_KEY);
|
|
||||||
mPreferenceManager = new PreferenceManager(mContext);
|
|
||||||
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
|
|
||||||
mPreferenceScreen.addPreference(mAdvancedVpnGroup);
|
|
||||||
mPreferenceScreen.addPreference(mVpnGroup);
|
|
||||||
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
|
||||||
mVpnSettings.init(mPreferenceScreen, mFakeFeatureFactory.getAdvancedVpnFeatureProvider());
|
|
||||||
|
|
||||||
when(mVpnSettings.getContext()).thenReturn(mContext);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider
|
|
||||||
.getAdvancedVpnPreferenceGroupTitle(mContext)).thenReturn(ADVANCED_VPN_GROUP_TITLE);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getVpnPreferenceGroupTitle(mContext))
|
|
||||||
.thenReturn(VPN_GROUP_TITLE);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.getAdvancedVpnPackageName())
|
|
||||||
.thenReturn(ADVANCED_VPN_PACKAGE_NAME);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any()))
|
|
||||||
.thenReturn(true);
|
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
|
||||||
doReturn(mContext).when(mContext).createContextAsUser(any(), anyInt());
|
|
||||||
doReturn(mContext).when(mContext).createPackageContextAsUser(any(), anyInt(), any());
|
|
||||||
doReturn(mPreferenceManager).when(mVpnGroup).getPreferenceManager();
|
|
||||||
doReturn(mPreferenceManager).when(mAdvancedVpnGroup).getPreferenceManager();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void setShownAdvancedPreferences_hasGeneralVpn_returnsVpnCountAs1() {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref =
|
|
||||||
spy(new AppPreference(mContext, USER_ID_1, VPN_PACKAGE_NAME));
|
|
||||||
updates.add(pref);
|
|
||||||
|
|
||||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
|
||||||
|
|
||||||
assertThat(mVpnGroup.getPreferenceCount()).isEqualTo(1);
|
|
||||||
assertThat(mVpnGroup.isVisible()).isTrue();
|
|
||||||
assertThat(mAdvancedVpnGroup.isVisible()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void setShownAdvancedPreferences_hasAdvancedVpn_returnsAdvancedVpnCountAs1() {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref =
|
|
||||||
spy(new AppPreference(mContext, USER_ID_1, ADVANCED_VPN_PACKAGE_NAME));
|
|
||||||
updates.add(pref);
|
|
||||||
|
|
||||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
|
||||||
|
|
||||||
assertThat(mAdvancedVpnGroup.getPreferenceCount()).isEqualTo(1);
|
|
||||||
assertThat(mAdvancedVpnGroup.isVisible()).isTrue();
|
|
||||||
assertThat(mVpnGroup.isVisible()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void setShownAdvancedPreferences_noVpn_returnsEmpty() {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
|
|
||||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
|
||||||
|
|
||||||
assertThat(mAdvancedVpnGroup.getPreferenceCount()).isEqualTo(0);
|
|
||||||
assertThat(mVpnGroup.getPreferenceCount()).isEqualTo(0);
|
|
||||||
assertThat(mAdvancedVpnGroup.isVisible()).isFalse();
|
|
||||||
assertThat(mVpnGroup.isVisible()).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getVpnApps_isAdvancedVpn_returnsOne() throws Exception {
|
|
||||||
ApplicationInfo info = new ApplicationInfo();
|
|
||||||
info.uid = 1111;
|
|
||||||
when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
|
|
||||||
|
|
||||||
assertThat(VpnSettings.getVpnApps(mContext, /* includeProfiles= */ false,
|
|
||||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(),
|
|
||||||
mAppOpsManager).size()).isEqualTo(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getVpnApps_isNotAdvancedVpn_returnsEmpty() {
|
|
||||||
int uid = 1111;
|
|
||||||
List<AppOpsManager.OpEntry> opEntries = new ArrayList<>();
|
|
||||||
List<AppOpsManager.PackageOps> apps = new ArrayList<>();
|
|
||||||
AppOpsManager.PackageOps packageOps =
|
|
||||||
new AppOpsManager.PackageOps(VPN_PACKAGE_NAME, uid, opEntries);
|
|
||||||
apps.add(packageOps);
|
|
||||||
when(mAppOpsManager.getPackagesForOps((int[]) any())).thenReturn(apps);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isAdvancedVpnSupported(any()))
|
|
||||||
.thenReturn(false);
|
|
||||||
|
|
||||||
assertThat(VpnSettings.getVpnApps(mContext, /* includeProfiles= */ false,
|
|
||||||
mFakeFeatureFactory.getAdvancedVpnFeatureProvider(),
|
|
||||||
mAppOpsManager)).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void clickVpn_VpnConnected_doesNotStartVpnLaunchIntent()
|
|
||||||
throws PackageManager.NameNotFoundException {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref = spy(new AppPreference(mContext, USER_ID_1, VPN_PACKAGE_NAME));
|
|
||||||
pref.setState(AppPreference.STATE_CONNECTED);
|
|
||||||
updates.add(pref);
|
|
||||||
when(mContext.createPackageContextAsUser(any(), anyInt(), any())).thenReturn(mContext);
|
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
|
||||||
when(mPackageManager.getLaunchIntentForPackage(any())).thenReturn(mVpnIntent);
|
|
||||||
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
|
||||||
doNothing().when(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
mVpnSettings.setShownPreferences(updates);
|
|
||||||
|
|
||||||
mVpnSettings.onPreferenceClick(pref);
|
|
||||||
|
|
||||||
verify(mContext, never()).startActivityAsUser(any(), any());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void clickVpn_VpnDisconnected_startsVpnLaunchIntent()
|
|
||||||
throws PackageManager.NameNotFoundException {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref = spy(new AppPreference(mContext, USER_ID_1, VPN_PACKAGE_NAME));
|
|
||||||
pref.setState(AppPreference.STATE_DISCONNECTED);
|
|
||||||
updates.add(pref);
|
|
||||||
when(mContext.createPackageContextAsUser(any(), anyInt(), any())).thenReturn(mContext);
|
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
|
||||||
when(mPackageManager.getLaunchIntentForPackage(any())).thenReturn(mVpnIntent);
|
|
||||||
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
|
||||||
doNothing().when(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
mVpnSettings.setShownPreferences(updates);
|
|
||||||
|
|
||||||
mVpnSettings.onPreferenceClick(pref);
|
|
||||||
|
|
||||||
verify(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
assertThat(TextUtils.equals(captor.getValue().getAction(),
|
|
||||||
VPN_LAUNCH_INTENT)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void clickAdvancedVpn_VpnConnectedDisconnectDialogDisabled_startsAppLaunchIntent()
|
|
||||||
throws PackageManager.NameNotFoundException {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref =
|
|
||||||
spy(new AppPreference(mContext, USER_ID_1, ADVANCED_VPN_PACKAGE_NAME));
|
|
||||||
pref.setState(AppPreference.STATE_CONNECTED);
|
|
||||||
updates.add(pref);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isDisconnectDialogEnabled())
|
|
||||||
.thenReturn(false);
|
|
||||||
when(mContext.createPackageContextAsUser(any(), anyInt(), any())).thenReturn(mContext);
|
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
|
||||||
when(mPackageManager.getLaunchIntentForPackage(any())).thenReturn(mAdvancedVpnIntent);
|
|
||||||
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
|
||||||
doNothing().when(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
|
||||||
|
|
||||||
mVpnSettings.onPreferenceClick(pref);
|
|
||||||
|
|
||||||
verify(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
assertThat(TextUtils.equals(captor.getValue().getAction(),
|
|
||||||
ADVANCED_VPN_LAUNCH_INTENT)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void clickAdvancedVpn_VpnConnectedDisconnectDialogEnabled_doesNotStartAppLaunchIntent()
|
|
||||||
throws PackageManager.NameNotFoundException {
|
|
||||||
Set<Preference> updates = new ArraySet<>();
|
|
||||||
AppPreference pref =
|
|
||||||
spy(new AppPreference(mContext, USER_ID_1, ADVANCED_VPN_PACKAGE_NAME));
|
|
||||||
pref.setState(AppPreference.STATE_CONNECTED);
|
|
||||||
updates.add(pref);
|
|
||||||
when(mFakeFeatureFactory.mAdvancedVpnFeatureProvider.isDisconnectDialogEnabled())
|
|
||||||
.thenReturn(true);
|
|
||||||
when(mContext.createPackageContextAsUser(any(), anyInt(), any())).thenReturn(mContext);
|
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
|
||||||
when(mPackageManager.getLaunchIntentForPackage(any())).thenReturn(mAdvancedVpnIntent);
|
|
||||||
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
|
||||||
doNothing().when(mContext).startActivityAsUser(captor.capture(), any());
|
|
||||||
mVpnSettings.setShownAdvancedPreferences(updates);
|
|
||||||
|
|
||||||
mVpnSettings.onPreferenceClick(pref);
|
|
||||||
|
|
||||||
verify(mContext, never()).startActivityAsUser(any(), any());
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user