From 47fbfbd71486097d713206a2e562da569ae9ca05 Mon Sep 17 00:00:00 2001 From: Frank PREEL Date: Sat, 26 Jan 2019 21:22:19 +0100 Subject: [PATCH 1/4] Add DSN setting screen --- res/xml/dns_settings.xml | 54 +++++ src/com/android/settings/dns/DNSSettings.java | 209 ++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100755 res/xml/dns_settings.xml create mode 100755 src/com/android/settings/dns/DNSSettings.java diff --git a/res/xml/dns_settings.xml b/res/xml/dns_settings.xml new file mode 100755 index 00000000000..24e17f8ea60 --- /dev/null +++ b/res/xml/dns_settings.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + diff --git a/src/com/android/settings/dns/DNSSettings.java b/src/com/android/settings/dns/DNSSettings.java new file mode 100755 index 00000000000..508a34633ab --- /dev/null +++ b/src/com/android/settings/dns/DNSSettings.java @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2019 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 android.content.Context; + +import android.os.UserHandle; + +//import android.content.pm.PackageManager; + +import android.provider.Settings; + +import com.android.settings.SettingsPreferenceFragment; +import android.view.View.OnClickListener; +import android.content.DialogInterface; +import com.android.settings.search.Indexable; +import com.android.internal.logging.nano.MetricsProto; + +import com.android.settings.widget.ToggleSwitch; + +import com.android.settings.R; + +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 android.support.v7.preference.Preference; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; + +import android.support.v7.preference.EditTextPreference; +import android.support.v7.preference.TwoStatePreference; + +import android.support.v14.preference.SwitchPreference; +import android.support.v7.preference.EditTextPreference; + +import android.util.Log; + + +import android.net.NetworkAgent; +import android.net.NetworkInfo; + +import android.net.ConnectivityManager; + + +import android.net.NetworkUtils; + +import java.net.Inet4Address; + +import android.net.wifi.WifiManager; +import android.telephony.TelephonyManager; +import java.lang.reflect.Method; + +import static android.provider.Settings.System.VIBRATE_WHEN_RINGING; + +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); + + //pm = getActivity().getPackageManager(); + + 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); + Log.v(TAG, "on create useNetworkDNS>>>"+useNwDNS); + ((TwoStatePreference) useNetworkDNS).setChecked(useNwDNS == 1); + if (useNwDNS == 1) { + boolean b = Settings.System.putInt(getContext().getContentResolver(), USE_NETWORK_DNS, 1); + Log.v(TAG, "Set value>>>"+b); + } + + String s = Settings.System.getString(getContext().getContentResolver(), OVERRIDE_DNS_IP_V4); + Log.v(TAG, "on create s>>>"+s); + overrideDNSIPV4.setSummary(checkNull(s)); + + overrideDNSIPV4.setText(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.setSummary(checkNull(val)); + boolean result = Settings.System.putString(getContext().getContentResolver(), OVERRIDE_DNS_IP_V4, val); + resartNetworks(); + return result; + } + overrideDNSIPV4.setSummary(checkNull(dnsNotSet)); + } + + //new NetworkInfo(ConnectivityManager.TYPE_BLUETOOTH, 0, NETWORK_TYPE, ""); + 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); + } + + //try{ + final TelephonyManager telMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); + //final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE); + boolean dataEnabled = telMgr.isDataEnabled(); + Log.v(TAG, "Data network state>"+dataEnabled); + if (dataEnabled){ + telMgr.setDataEnabled(false); + telMgr.setDataEnabled(true); + } + //setDataEnabled.setAccessible(true); + //setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled)); + //} + /*catch(Throwable t){ + Log.e(TAG, "setMobileConnectionEnabled", t); + }*/ + } + + @Override + public void onResume() { + super.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + +} -- GitLab From cfbd44a127f56946c0e0d1a8173f22e3a0448602 Mon Sep 17 00:00:00 2001 From: Frank PREEL Date: Sat, 26 Jan 2019 21:23:06 +0100 Subject: [PATCH 2/4] Add DSN setting screen --- res/values-fr/strings.xml | 26 ++++++++++++++++++++++++++ res/xml/network_and_internet.xml | 11 ++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) mode change 100644 => 100755 res/values-fr/strings.xml mode change 100644 => 100755 res/xml/network_and_internet.xml diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml old mode 100644 new mode 100755 index 1c433debaf0..8c97c894f43 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -1,6 +1,7 @@ "MicroG" + + "Settings DNS" + "DNS" + "DNS" + "Entrez l'IP du DNS" + "Utiliser le DNS du réseau" + "Permet d'utiliser le serveur DNS fourni par le réseau" + "Définissez le DNS à utiliser" + "9.9.9.9" + "Configuration du DNS" + + + + + + + + + + + + + + + diff --git a/res/xml/network_and_internet.xml b/res/xml/network_and_internet.xml old mode 100644 new mode 100755 index ee25bee5989..617cb81cd14 --- a/res/xml/network_and_internet.xml +++ b/res/xml/network_and_internet.xml @@ -1,5 +1,6 @@ MicroG + OpenKeychain + + "Settings DNS" + "DNS" + "DNS" + "Entrez l'IP du DNS" + "Utiliser le DNS du réseau" + "Permet d'utiliser le serveur DNS fourni par le réseau" + "Définissez le DNS à utiliser" + "9.9.9.9" + "Configuration du DNS" + -- GitLab