Settings: Allow disabling private DNS for VPN [2/2]

Allows using the VPN's DNS instead of the set private DNS
Automatically restores the previous set mode

Change-Id: Ib7d91bf151ed593648357c1d7e4013d309e2d927
This commit is contained in:
Ido Ben-Hur
2023-12-21 16:04:34 +02:00
committed by Joey
parent 0846def059
commit 6b5f34599b
5 changed files with 131 additions and 7 deletions

View File

@@ -258,4 +258,8 @@
<!-- Back gesture animation -->
<string name="back_gesture_arrow_title">Back gesture animation</string>
<string name="back_gesture_arrow_summary">Show an animated arrow for back gesture</string>
<!-- Enforce DNS for VPN -->
<string name="vpn_enforce_dns_title">Disable for VPN</string>
<string name="vpn_enforce_dns_summary">Disable the private DNS when connected to a VPN\nWill restore upon disconnecting</string>
</resources>

View File

@@ -94,15 +94,12 @@
settings:userRestriction="no_config_vpn"
settings:useAdminDisabledSummary="true" />
<com.android.settings.network.PrivateDnsModeDialogPreference
<Preference
android:fragment="com.android.settings.network.PrivateDnsSettings"
android:key="private_dns_settings"
android:title="@string/select_private_dns_configuration_title"
android:icon="@drawable/ic_settings_private_dns"
android:order="20"
android:dialogTitle="@string/select_private_dns_configuration_dialog_title"
android:dialogLayout="@layout/private_dns_mode_dialog"
android:positiveButtonText="@string/save"
android:negativeButtonText="@android:string/cancel" />
android:order="20" />
<Preference
android:fragment="com.android.settings.network.AdaptiveConnectivitySettings"

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2023 Yet Another AOSP 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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/select_private_dns_configuration_title">
<com.android.settings.network.PrivateDnsModeDialogPreference
android:key="private_dns_settings"
android:title="@string/select_private_dns_configuration_dialog_title"
android:order="10"
android:dialogTitle="@string/select_private_dns_configuration_dialog_title"
android:dialogLayout="@layout/private_dns_mode_dialog"
android:positiveButtonText="@string/save"
android:negativeButtonText="@android:string/cancel" />
<org.evolution.settings.preferences.SecureSettingSwitchPreference
android:key="vpn_enforce_dns"
android:title="@string/vpn_enforce_dns_title"
android:order="20"
android:summary="@string/vpn_enforce_dns_summary"
android:defaultValue="false" />
</PreferenceScreen>

View File

@@ -58,6 +58,7 @@ import java.util.List;
public class PrivateDnsPreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
private static final String KEY_PRIVATE_DNS_SETTINGS = "private_dns_settings";
private static final String KEY_ENFORCE_VPN_SETTINGS = "vpn_enforce_dns";
private static final Uri[] SETTINGS_URIS = new Uri[]{
Settings.Global.getUriFor(PRIVATE_DNS_MODE),
@@ -82,6 +83,7 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
private final ConnectivityManager mConnectivityManager;
private LinkProperties mLatestLinkProperties;
private Preference mPreference;
private Preference mEnforcePreference;
public PrivateDnsPreferenceController(Context context) {
super(context, KEY_PRIVATE_DNS_SETTINGS);
@@ -110,6 +112,7 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
mEnforcePreference = screen.findPreference(KEY_ENFORCE_VPN_SETTINGS);
}
@Override
@@ -201,7 +204,11 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
@Override
public void updateState(Preference preference) {
super.updateState(preference);
preference.setEnabled(!isManagedByAdmin());
final boolean isManaged = isManagedByAdmin();
preference.setEnabled(!isManaged);
if (mEnforcePreference == null) return;
final int mode = ConnectivitySettingsManager.getPrivateDnsMode(mContext);
mEnforcePreference.setEnabled(!isManaged && mode != PRIVATE_DNS_MODE_OFF);
}
private boolean isManagedByAdmin() {

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2023 Yet Another AOSP 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.network;
import android.app.settings.SettingsEnums;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/**
* Adaptive connectivity is a feature which automatically manages network connections.
*/
@SearchIndexable
public class PrivateDnsSettings extends DashboardFragment {
private static final String TAG = "PrivateDnsSettings";
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_NETWORK_CATEGORY;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.private_dns_settings;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle());
}
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context, Lifecycle lifecycle) {
final PrivateDnsPreferenceController privateDnsPreferenceController =
new PrivateDnsPreferenceController(context);
if (lifecycle != null) {
lifecycle.addObserver(privateDnsPreferenceController);
}
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(privateDnsPreferenceController);
return controllers;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.private_dns_settings) {
@Override
public List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, null);
}
};
}