Connect VPN warning help icon with the network & internet page.

New VpnInfoPreference created to control the presence or absence
of the warning icon based on the vpn type.

The VpnPreferenceController type casts the preference based on
whether the provider model is enabled. It also detects legacy vpns
by using the LegacyVpnProfileStore, and comparing VPN profile
usernames with the active VPN config's user.

Screenshot: https://screenshot.googleplex.com/AfGrH8wRusTvbf4
Test: atest -c SettingsUnitTests
Bug: Bug: 176821216
Change-Id: I1aa93be5b90b404d9d9cb9bf47ea76fdc9aad401
Merged-In: I1aa93be5b90b404d9d9cb9bf47ea76fdc9aad401
(cherry picked from commit 7a68eb3b5e)
This commit is contained in:
Jeremy Goldman
2021-03-15 15:24:15 +08:00
parent bd9a65cca6
commit 76de845f3d
3 changed files with 123 additions and 1 deletions

View File

@@ -98,7 +98,7 @@
android:order="10" android:order="10"
android:fragment="com.android.settings.datausage.DataSaverSummary"/> android:fragment="com.android.settings.datausage.DataSaverSummary"/>
<com.android.settingslib.RestrictedPreference <com.android.settings.vpn2.VpnInfoPreference
android:fragment="com.android.settings.vpn2.VpnSettings" android:fragment="com.android.settings.vpn2.VpnSettings"
android:key="vpn_settings" android:key="vpn_settings"
android:title="@string/vpn_settings_title" android:title="@string/vpn_settings_title"

View File

@@ -27,6 +27,8 @@ import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.SettingsSlicesContract; import android.provider.SettingsSlicesContract;
import android.security.Credentials;
import android.security.LegacyVpnProfileStore;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
@@ -36,8 +38,11 @@ import androidx.preference.PreferenceScreen;
import com.android.internal.net.LegacyVpnInfo; import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig; import com.android.internal.net.VpnConfig;
import com.android.internal.net.VpnProfile;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.vpn2.VpnInfoPreference;
import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -147,6 +152,10 @@ public class VpnPreferenceController extends AbstractPreferenceController
} else { } else {
summary = getNameForVpnConfig(vpn, UserHandle.of(uid)); summary = getNameForVpnConfig(vpn, UserHandle.of(uid));
} }
// Optionally add warning icon if an insecure VPN is present.
if (Utils.isProviderModelEnabled(mContext) && mPreference instanceof VpnInfoPreference) {
((VpnInfoPreference) mPreference).setInsecureVpn(hasInsecureVpn());
}
ThreadUtils.postOnMainThread(() -> mPreference.setSummary(summary)); ThreadUtils.postOnMainThread(() -> mPreference.setSummary(summary));
} }
@@ -167,6 +176,20 @@ public class VpnPreferenceController extends AbstractPreferenceController
} }
} }
@VisibleForTesting
protected boolean hasInsecureVpn() {
for (String key : LegacyVpnProfileStore.list(Credentials.VPN)) {
final VpnProfile profile = VpnProfile.decode(key,
LegacyVpnProfileStore.get(Credentials.VPN + key));
// Return whether any profile is an insecure type.
if (VpnProfile.isLegacyType(profile.type)) {
return true;
}
}
// We did not find any insecure VPNs.
return false;
}
// Copied from SystemUI::SecurityControllerImpl // Copied from SystemUI::SecurityControllerImpl
private final ConnectivityManager.NetworkCallback private final ConnectivityManager.NetworkCallback
mNetworkCallback = new ConnectivityManager.NetworkCallback() { mNetworkCallback = new ConnectivityManager.NetworkCallback() {

View File

@@ -0,0 +1,99 @@
/*
* 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.vpn2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.RestrictedPreference;
/**
* A preference with an Info icon on the side
*/
public class VpnInfoPreference extends RestrictedPreference implements View.OnClickListener {
private boolean mIsInsecureVpn = false;
private String mHelpUrl;
public VpnInfoPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mHelpUrl = context.getString(R.string.help_url_insecure_vpn);
}
@Override
protected int getSecondTargetResId() {
// Note: in the future, we will probably want to provide a configuration option
// for this info to not be the warning color.
return R.layout.preference_widget_warning;
}
@Override
protected boolean shouldHideSecondTarget() {
return false;
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final View icon = holder.findViewById(R.id.warning_button);
if (mIsInsecureVpn && !TextUtils.isEmpty(mHelpUrl)) {
icon.setVisibility(View.VISIBLE);
icon.setOnClickListener(this);
icon.setEnabled(true);
} else {
icon.setVisibility(View.GONE);
icon.setOnClickListener(this);
icon.setEnabled(false);
}
// Hide the divider from view
final View divider = holder.findViewById(R.id.two_target_divider);
divider.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.warning_button) {
final Intent intent = HelpUtils.getHelpIntent(
getContext(), mHelpUrl, this.getClass().getName());
if (intent != null) {
((Activity) getContext()).startActivityForResult(intent, 0);
}
}
}
/**
* Sets whether this preference corresponds to an insecure VPN. This will also affect whether
* the warning icon appears to the user.
*/
public void setInsecureVpn(boolean isInsecureVpn) {
if (mIsInsecureVpn != isInsecureVpn) {
mIsInsecureVpn = isInsecureVpn;
notifyChanged();
}
}
}