Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b8cae18d authored by frank PREEL's avatar frank PREEL Committed by Aayush Gupta
Browse files

Settings: Add support for DNS over TLS [1/2]



Change-Id: Ie47ab95381db1f3a4d893b5adbb378a05b4c5bbc
Signed-off-by: Aayush Gupta's avatarAayush Gupta <theimpulson@e.email>
parent 2802d23a
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -12209,4 +12209,15 @@
    <!-- /e/ specific changes -->
    <string name="micro_g">MicroG</string>
    <string name="open_keychain">OpenKeychain</string>
    <!-- /e/ -->
    <string name="e_dns_settings_title">"DNS Settings"</string>
    <string name="e_dns_mode">"DNS"</string>
    <string name="e_dns_name">"DNS"</string>
    <string name="e_dns_title">"Enter DNS IP"</string>
    <string name="e_use_dhcp_dns">"Use network DNS"</string>
    <string name="e_use_dhcp_dns_summary">"Allow to use DNS from network provider"</string>
    <string name="e_dns_name_summary">"Set DNS to use"</string>
    <string name="e_dns_not_set">"9.9.9.9"</string>
    <string name="e_dns_summary">"DNS configuration"</string>
</resources>
+43 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019-2021 E FOUNDATION

     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/com.android.settings"
        android:title="@string/e_dns_settings_title">

    <SwitchPreference
        android:key="toggle_e_dns"
        android:title="@string/e_use_dhcp_dns"
        android:summary="@string/e_use_dhcp_dns_summary"
        android:defaultValue="true"
        android:icon="@drawable/ic_wifi_tethering"
        android:disableDependentsState="true"
	android:order="5"/>

    <EditTextPreference
	android:key="e_dns_value"
	android:icon="@drawable/ic_settings_accounts"        
	android:title="@string/e_dns_name"
	android:summary="@string/e_dns_name_summary"
	android:dependency="toggle_e_dns"
	android:dialogTitle="@string/e_dns_title"
	android:singleLine="true"
	android:inputType="phone"
	android:persistent="false"
	android:defaultValue="9.9.9.9"	
	android:order="10"/>

</PreferenceScreen>
+9 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 The Android Open Source Project
     Copyright (C) 2019-2021 E FOUNDATION

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -79,6 +80,14 @@
        settings:userRestriction="no_config_tethering"
        settings:useAdminDisabledSummary="true" />
	
    <com.android.settingslib.RestrictedPreference
        android:fragment="com.android.settings.dns.DNSSettings"
        android:key="e_dns_settings"
        android:title="@string/e_dns_mode"
        android:icon="@drawable/ic_settings_24dp"
        android:order="0"
        android:summary="@string/e_dns_summary"/>

    <com.android.settings.widget.MasterSwitchPreference
        android:fragment="com.android.settings.AllInOneTetherSettings"
        android:key="all_tether_settings"
+172 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019-2021 E FOUNDATION
 *
 * 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.dns;

import static android.provider.Settings.System.VIBRATE_WHEN_RINGING;

import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import android.view.View.OnClickListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.EditTextPreference;
import androidx.preference.TwoStatePreference;
import androidx.preference.SwitchPreference;
import android.util.Log;
import android.net.NetworkAgent;
import android.net.NetworkInfo;
import android.net.ConnectivityManager;
import android.net.NetworkUtils;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import com.android.settings.SettingsPreferenceFragment;
import com.android.settingslib.search.Indexable;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.widget.ToggleSwitch;
import com.android.settings.R;

import java.net.Inet4Address;
import java.lang.reflect.Method;


public class DNSSettings extends SettingsPreferenceFragment
        implements OnPreferenceChangeListener, Indexable {

    private static final String TAG = "DNSSettings";

    private static final String KEY_E_TOGGLE_DNS = "toggle_e_dns";
    private final static String KEY_E_DNS_VALUE = "e_dns_value";

    private SwitchPreference useNetworkDNS;
    private EditTextPreference	overrideDNSIPV4;

    private static final String USE_NETWORK_DNS = "USE_NETWORK_DNS";
    private static final String OVERRIDE_DNS_IP_V4 = "OVERRIDE_DNS_IP_V4"; // IPV4 DNS TO USE.

    private String dnsNotSet; // Default value

    @Override
    public int getMetricsCategory() {
        return MetricsProto.MetricsEvent.SETTINGS_NETWORK_CATEGORY;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

	dnsNotSet = getResources().getString(R.string.e_dns_not_set);

	addPreferencesFromResource(R.xml.dns_settings);
	useNetworkDNS = (SwitchPreference) findPreference(KEY_E_TOGGLE_DNS);
	overrideDNSIPV4 = (EditTextPreference) findPreference(KEY_E_DNS_VALUE);

	for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
		getPreferenceScreen().getPreference(i).setOnPreferenceChangeListener(this);
	}

	int useNwDNS = Settings.System.getInt(getContext().getContentResolver(), USE_NETWORK_DNS, 1);
	((TwoStatePreference) useNetworkDNS).setChecked(useNwDNS == 1);

	String s = Settings.System.getString(getContext().getContentResolver(), OVERRIDE_DNS_IP_V4);
	overrideDNSIPV4.setText(checkNull(s));
    }

    private final String checkNull(String value) {
        if (value == null || value.length() == 0) {
            return dnsNotSet;
        } else {
            return value;
        }
    }

    private final Inet4Address getIPv4Address(String text) {
        try {
            return (Inet4Address) NetworkUtils.numericToInetAddress(text);
        } catch (IllegalArgumentException | ClassCastException e) {
            return null;
        }
    }

    public boolean onPreferenceChange(Preference preference, Object newValue) {
	final String key = preference.getKey();

	if (KEY_E_TOGGLE_DNS.equals(key)) {
		final boolean val = (Boolean) newValue;
		boolean result = Settings.System.putInt(getContext().getContentResolver(), USE_NETWORK_DNS, val ? 1 : 0);
		resartNetworks();
		return result;

	}

	if (KEY_E_DNS_VALUE.equals(key)) {
		final String val = (String) newValue;
		final Inet4Address ipAddress = getIPv4Address(val);
		if (ipAddress != null) {
			overrideDNSIPV4.setText(checkNull(val));
			boolean result = Settings.System.putString(getContext().getContentResolver(), OVERRIDE_DNS_IP_V4, val);
			resartNetworks();
			return result;
		}
		overrideDNSIPV4.setText(checkNull(dnsNotSet));
	}

	return false;
    }

    private final void resartNetworks(){
	WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
	boolean wifiEnabled = wifiManager.isWifiEnabled();
	Log.v(TAG, "Wi-Fi state>"+wifiEnabled);
	if (wifiEnabled) {
		wifiManager.setWifiEnabled(false);
		wifiManager.setWifiEnabled(true);
	}

	final TelephonyManager telMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
		boolean dataEnabled = telMgr.isDataEnabled();
		Log.v(TAG, "Data network state>"+dataEnabled);
	if (dataEnabled){
		telMgr.setDataEnabled(false);
		telMgr.setDataEnabled(true);
	}
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}