Skip to content

Commit 919feb4

Browse files
committed
Private DNS: Set mode in an async task
The Private DNS mode must not be set on the UI thread, as it could trigger a network host lookup (in case a hostname is specified). Perform it in a background task instead. Additionally, stop using reflection utils since TestDPC now builds against Q SDK. Bug: 120416121 Test: Manual, set mode from TestDPC Change-Id: I4e81541da8e15df7e966d8a25903d757d24caf8b
1 parent 9fcd16a commit 919feb4

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

app/src/main/java/com/afwsamples/testdpc/policy/networking/PrivateDnsModeFragment.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import com.afwsamples.testdpc.DeviceAdminReceiver;
3535
import com.afwsamples.testdpc.R;
36-
import com.afwsamples.testdpc.common.ReflectionUtil;
3736

3837
@TargetApi(29)
3938
public class PrivateDnsModeFragment extends Fragment implements View.OnClickListener,
@@ -122,10 +121,9 @@ private void updateSelectedMode(int checkedId) {
122121

123122
private int getPrivateDnsMode() {
124123
try {
125-
return (Integer) ReflectionUtil.invoke(
126-
mDpm, "getGlobalPrivateDnsMode",
124+
return mDpm.getGlobalPrivateDnsMode(
127125
DeviceAdminReceiver.getComponentName(getActivity()));
128-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
126+
} catch (SecurityException e) {
129127
Log.w(TAG, "Failure getting current mode", e);
130128
}
131129

@@ -134,10 +132,9 @@ private int getPrivateDnsMode() {
134132

135133
private String getPrivateDnsHost() {
136134
try {
137-
return (String) ReflectionUtil.invoke(
138-
mDpm, "getGlobalPrivateDnsHost",
135+
return mDpm.getGlobalPrivateDnsHost(
139136
DeviceAdminReceiver.getComponentName(getActivity()));
140-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
137+
} catch (SecurityException e) {
141138
Log.w(TAG, "Failure getting host", e);
142139
}
143140

@@ -146,23 +143,12 @@ private String getPrivateDnsHost() {
146143

147144
private void setPrivateDnsMode(int mode, String resolver) {
148145
Log.w(TAG, String.format("Setting mode %d host %s", mSelectedMode, resolver));
149-
try {
150-
ReflectionUtil.invoke(
151-
mDpm, "setGlobalPrivateDns",
152-
new Class[]{ComponentName.class, int.class, String.class},
153-
DeviceAdminReceiver.getComponentName(getActivity()),
154-
mode,
155-
resolver);
156-
Toast.makeText(getActivity(), R.string.setting_private_dns_succeess,
157-
Toast.LENGTH_LONG).show();
158-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
159-
Log.w(TAG, "Failed to invoke, cause", e);
160-
161-
// This is the real exception.
162-
Throwable causeOfCause = e.getCause().getCause();
163-
164-
Toast.makeText(getActivity(),
165-
"Failure: " + causeOfCause.getMessage(), Toast.LENGTH_LONG).show();
166-
}
146+
147+
final ComponentName component = DeviceAdminReceiver.getComponentName(getActivity());
148+
new SetPrivateDnsTask(
149+
mDpm, component, mode, resolver, (int msgId, Object... args) -> {
150+
Toast.makeText(getActivity(), getString(msgId, args),
151+
Toast.LENGTH_LONG).show();
152+
}).execute(new Void[0]);
167153
}
168154
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.afwsamples.testdpc.policy.networking;
18+
19+
import android.annotation.TargetApi;
20+
import android.app.admin.DevicePolicyManager;
21+
import android.content.ComponentName;
22+
import android.os.AsyncTask;
23+
import android.util.Log;
24+
25+
import com.afwsamples.testdpc.policy.keymanagement.ShowToastCallback;
26+
import com.afwsamples.testdpc.R;
27+
28+
@TargetApi(29)
29+
final class SetPrivateDnsTask extends AsyncTask<Void, Void, String> {
30+
public static final String TAG = "Networking";
31+
private final ShowToastCallback mCallback;
32+
private final DevicePolicyManager mDpm;
33+
private final ComponentName mComponent;
34+
private final int mMode;
35+
private final String mResolver;
36+
37+
public SetPrivateDnsTask(
38+
DevicePolicyManager dpm, ComponentName component, int mode, String resolver,
39+
ShowToastCallback callback) {
40+
mDpm = dpm;
41+
mComponent = component;
42+
mCallback = callback;
43+
mMode = mode;
44+
mResolver = resolver;
45+
}
46+
47+
@Override
48+
protected String doInBackground(Void... params) {
49+
try {
50+
mDpm.setGlobalPrivateDns(mComponent, mMode, mResolver);
51+
return null;
52+
} catch (SecurityException | IllegalArgumentException e) {
53+
Log.w(TAG, "Failed to invoke, cause", e);
54+
return e.getMessage();
55+
}
56+
}
57+
58+
@Override
59+
protected void onPostExecute(String error) {
60+
if (error == null) {
61+
mCallback.showToast(R.string.setting_private_dns_succeess);
62+
} else {
63+
mCallback.showToast(R.string.setting_private_dns_failure, error);
64+
}
65+
}
66+
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@
774774
<string name="private_dns_mode_unknown">Unknown</string>
775775
<string name="private_dns_mode_apply">Set</string>
776776
<string name="setting_private_dns_succeess">Success setting Private DNS</string>
777+
<string name="setting_private_dns_failure">FAILED: %s</string>
777778

778779
<!-- Strings for data usage -->
779780
<string name="data_usage">Data usage</string>

0 commit comments

Comments
 (0)