Add Cloudflare DNS as a private DNS provider
* Handle this entirely in Settings, no need to touch anything else at all Change-Id: I7e8a320d47e7c5ddbcb9acfaf23032ae92d5d70d
This commit is contained in:
committed by
Michael Bestas
parent
a53f729545
commit
8e37b18872
@@ -37,6 +37,10 @@
|
||||
android:id="@+id/private_dns_mode_off"
|
||||
layout="@layout/preference_widget_dialog_radiobutton"/>
|
||||
|
||||
<include
|
||||
android:id="@+id/private_dns_mode_cloudflare"
|
||||
layout="@layout/preference_widget_dialog_radiobutton"/>
|
||||
|
||||
<include
|
||||
android:id="@+id/private_dns_mode_opportunistic"
|
||||
layout="@layout/preference_widget_dialog_radiobutton"/>
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||
<!-- Private DNS -->
|
||||
<string name="private_dns_mode_cloudflare" translatable="false">Cloudflare DNS</string>
|
||||
<!-- Alternative: 1dot1dot1dot1.cloudflare-dns.com -->
|
||||
<string name="private_dns_hostname_cloudflare" translatable="false">one.one.one.one</string>
|
||||
|
||||
<!-- Advanced keyboard settings -->
|
||||
<string name="keyboard_extras_title">Extras</string>
|
||||
<string name="advanced_keyboard_settings_title">Advanced settings</string>
|
||||
|
||||
@@ -71,9 +71,13 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
// DNS_MODE -> RadioButton id
|
||||
private static final Map<Integer, Integer> PRIVATE_DNS_MAP;
|
||||
|
||||
// Only used in Settings, update on additions to ConnectivitySettingsUtils
|
||||
private static final int PRIVATE_DNS_MODE_CLOUDFLARE = 4;
|
||||
|
||||
static {
|
||||
PRIVATE_DNS_MAP = new HashMap<>();
|
||||
PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_OFF, R.id.private_dns_mode_off);
|
||||
PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_CLOUDFLARE, R.id.private_dns_mode_cloudflare);
|
||||
PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_OPPORTUNISTIC, R.id.private_dns_mode_opportunistic);
|
||||
PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_PROVIDER_HOSTNAME, R.id.private_dns_mode_provider);
|
||||
}
|
||||
@@ -136,6 +140,15 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
protected void onBindDialogView(View view) {
|
||||
final Context context = getContext();
|
||||
mMode = ConnectivitySettingsManager.getPrivateDnsMode(context);
|
||||
if (mMode == PRIVATE_DNS_MODE_PROVIDER_HOSTNAME) {
|
||||
final String privateDnsHostname =
|
||||
ConnectivitySettingsManager.getPrivateDnsHostname(context);
|
||||
final String cloudflareHostname =
|
||||
context.getString(R.string.private_dns_hostname_cloudflare);
|
||||
if (privateDnsHostname.equals(cloudflareHostname)) {
|
||||
mMode = PRIVATE_DNS_MODE_CLOUDFLARE;
|
||||
}
|
||||
}
|
||||
mRadioGroup = view.findViewById(R.id.private_dns_radio_group);
|
||||
mRadioGroup.check(PRIVATE_DNS_MAP.getOrDefault(mMode, R.id.private_dns_mode_opportunistic));
|
||||
mRadioGroup.setOnCheckedChangeListener(this);
|
||||
@@ -143,6 +156,9 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
// Initial radio button text
|
||||
final RadioButton offRadioButton = view.findViewById(R.id.private_dns_mode_off);
|
||||
offRadioButton.setText(com.android.settingslib.R.string.private_dns_mode_off);
|
||||
final RadioButton cloudflareRadioButton =
|
||||
view.findViewById(R.id.private_dns_mode_cloudflare);
|
||||
cloudflareRadioButton.setText(R.string.private_dns_mode_cloudflare);
|
||||
final RadioButton opportunisticRadioButton =
|
||||
view.findViewById(R.id.private_dns_mode_opportunistic);
|
||||
opportunisticRadioButton.setText(
|
||||
@@ -178,6 +194,8 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
||||
if (checkedId == R.id.private_dns_mode_off) {
|
||||
mMode = PRIVATE_DNS_MODE_OFF;
|
||||
} else if (checkedId == R.id.private_dns_mode_cloudflare) {
|
||||
mMode = PRIVATE_DNS_MODE_CLOUDFLARE;
|
||||
} else if (checkedId == R.id.private_dns_mode_opportunistic) {
|
||||
mMode = PRIVATE_DNS_MODE_OPPORTUNISTIC;
|
||||
} else if (checkedId == R.id.private_dns_mode_provider) {
|
||||
@@ -249,6 +267,7 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
@VisibleForTesting
|
||||
void doSaveButton() {
|
||||
Context context = getContext();
|
||||
int modeToSet = mMode;
|
||||
if (mMode == PRIVATE_DNS_MODE_PROVIDER_HOSTNAME) {
|
||||
if (mHostnameLayout == null || mHostnameText == null) {
|
||||
Log.e(TAG, "Can't find hostname resources!");
|
||||
@@ -267,12 +286,17 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
|
||||
|
||||
ConnectivitySettingsManager.setPrivateDnsHostname(context,
|
||||
mHostnameText.getText().toString());
|
||||
} else if (mMode == PRIVATE_DNS_MODE_CLOUDFLARE) {
|
||||
final String cloudflareHostname =
|
||||
context.getString(R.string.private_dns_hostname_cloudflare);
|
||||
ConnectivitySettingsManager.setPrivateDnsHostname(context, cloudflareHostname);
|
||||
modeToSet = PRIVATE_DNS_MODE_PROVIDER_HOSTNAME;
|
||||
}
|
||||
|
||||
ConnectivitySettingsManager.setPrivateDnsMode(context, mMode);
|
||||
ConnectivitySettingsManager.setPrivateDnsMode(context, modeToSet);
|
||||
|
||||
FeatureFactory.getFeatureFactory().getMetricsFeatureProvider()
|
||||
.action(context, SettingsEnums.ACTION_PRIVATE_DNS_MODE, mMode);
|
||||
.action(context, SettingsEnums.ACTION_PRIVATE_DNS_MODE, modeToSet);
|
||||
getDialog().dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,9 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
|
||||
Settings.Global.getUriFor(PRIVATE_DNS_SPECIFIER),
|
||||
};
|
||||
|
||||
// Only used in Settings, update on additions to ConnectivitySettingsUtils
|
||||
private static final int PRIVATE_DNS_MODE_CLOUDFLARE = 4;
|
||||
|
||||
private final Handler mHandler;
|
||||
private final ContentObserver mSettingsObserver;
|
||||
private final ConnectivityManager mConnectivityManager;
|
||||
@@ -129,15 +132,24 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
|
||||
switch (mode) {
|
||||
case PRIVATE_DNS_MODE_OFF:
|
||||
return res.getString(com.android.settingslib.R.string.private_dns_mode_off);
|
||||
case PRIVATE_DNS_MODE_CLOUDFLARE:
|
||||
case PRIVATE_DNS_MODE_OPPORTUNISTIC:
|
||||
return dnsesResolved ? res.getString(R.string.private_dns_mode_on)
|
||||
: res.getString(
|
||||
com.android.settingslib.R.string.private_dns_mode_opportunistic);
|
||||
case PRIVATE_DNS_MODE_PROVIDER_HOSTNAME:
|
||||
return dnsesResolved
|
||||
? ConnectivitySettingsManager.getPrivateDnsHostname(mContext)
|
||||
: res.getString(
|
||||
com.android.settingslib.R.string.private_dns_mode_provider_failure);
|
||||
if (!dnsesResolved) {
|
||||
return res.getString(
|
||||
com.android.settingslib.R.string.private_dns_mode_provider_failure);
|
||||
}
|
||||
final String privateDnsHostname =
|
||||
ConnectivitySettingsManager.getPrivateDnsHostname(mContext);
|
||||
final String cloudflareHostname =
|
||||
res.getString(R.string.private_dns_hostname_cloudflare);
|
||||
if (privateDnsHostname.equals(cloudflareHostname)) {
|
||||
return res.getString(R.string.private_dns_mode_cloudflare);
|
||||
}
|
||||
return privateDnsHostname;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user