Skip to content

Commit 1206707

Browse files
author
android-build-team Robot
committed
Snap for 4625063 from 088138718d881eacfda75b2b1493a4651859140b to ub-testdpc-pic-release
Change-Id: I320cf05796d70ef61ce34c9d84fceaff068043e1
2 parents b1d601d + 69b3255 commit 1206707

14 files changed

+121
-307
lines changed

app/src/main/java/com/afwsamples/testdpc/DeviceAdminReceiver.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@
4141
import android.text.TextUtils;
4242
import android.util.Log;
4343
import android.widget.Toast;
44-
4544
import com.afwsamples.testdpc.common.NotificationUtil;
46-
import com.afwsamples.testdpc.common.ReflectionUtil;
4745
import com.afwsamples.testdpc.common.Util;
4846
import com.afwsamples.testdpc.policy.UserRestriction;
4947
import com.afwsamples.testdpc.provision.PostProvisioningTask;
50-
5148
import java.io.BufferedReader;
5249
import java.io.BufferedWriter;
5350
import java.io.File;
@@ -149,17 +146,10 @@ public void onNetworkLogsAvailable(Context context, Intent intent, long batchTok
149146
Toast.LENGTH_LONG)
150147
.show();
151148

152-
ArrayList<String> loggedEvents = new ArrayList<String>();
149+
ArrayList<String> loggedEvents = new ArrayList<>();
153150
if (BuildCompat.isAtLeastP()) {
154151
for (NetworkEvent event : events) {
155-
long id;
156-
try {
157-
id = (long) ReflectionUtil.invoke(event, "getId");
158-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
159-
Log.e(TAG, "Can't invoke getId()", e);
160-
id = 0;
161-
}
162-
loggedEvents.add("(id:" + id + ")" + event.toString());
152+
loggedEvents.add(event.toString());
163153
}
164154
} else {
165155
events.forEach(event -> loggedEvents.add(event.toString()));
@@ -562,7 +552,7 @@ private static void updatePasswordConstraintNotification(Context context) {
562552

563553
if (um.hasUserRestriction(UserRestriction.DISALLOW_UNIFIED_PASSWORD)
564554
&& Util.isManagedProfileOwner(context)
565-
&& Util.isUsingUnifiedPassword(context, getComponentName(context))) {
555+
&& isUsingUnifiedPassword(context)) {
566556
problems.add(context.getText(R.string.separate_challenge_required_title));
567557
}
568558

@@ -586,6 +576,15 @@ private static void updatePasswordConstraintNotification(Context context) {
586576
}
587577
}
588578

579+
@TargetApi(28)
580+
private static Boolean isUsingUnifiedPassword(Context context) {
581+
if (!BuildCompat.isAtLeastP()) {
582+
return false;
583+
}
584+
final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
585+
return dpm.isUsingUnifiedPassword(getComponentName(context));
586+
}
587+
589588
/**
590589
* Notify the admin receiver that something about the password has changed, e.g. quality
591590
* constraints or separate challenge requirements.

app/src/main/java/com/afwsamples/testdpc/SetupManagementFragment.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,27 +313,19 @@ private boolean maybeSpecifyNExtras(Intent intent) {
313313
return true;
314314
}
315315

316-
// TODO: replace with O SDK API
317-
private static final String EXTRA_PROVISIONING_DISCLAIMERS =
318-
"android.app.extra.PROVISIONING_DISCLAIMERS";
319-
private static final String EXTRA_PROVISIONING_DISCLAIMER_HEADER =
320-
"android.app.extra.PROVISIONING_DISCLAIMER_HEADER";
321-
private static final String EXTRA_PROVISIONING_DISCLAIMER_CONTENT =
322-
"android.app.extra.PROVISIONING_DISCLAIMER_CONTENT";
323-
324316
private void specifyDefaultDisclaimers(Intent intent) {
325317
if (BuildCompat.isAtLeastO()) {
326318
Bundle emmBundle = new Bundle();
327-
emmBundle.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER,
319+
emmBundle.putString(DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_HEADER,
328320
getString(R.string.default_disclaimer_emm_name));
329-
emmBundle.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT,
321+
emmBundle.putParcelable(DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_CONTENT,
330322
resourceToUri(getActivity(), R.raw.emm_disclaimer));
331323
Bundle companyBundle = new Bundle();
332-
companyBundle.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER,
324+
companyBundle.putString(DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_HEADER,
333325
getString(R.string.default_disclaimer_company_name));
334-
companyBundle.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT,
326+
companyBundle.putParcelable(DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_CONTENT,
335327
resourceToUri(getActivity(), R.raw.company_disclaimer));
336-
intent.putExtra(EXTRA_PROVISIONING_DISCLAIMERS,
328+
intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMERS,
337329
new Bundle[] { emmBundle, companyBundle });
338330
}
339331
}

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,4 @@ private static DevicePolicyManager getDevicePolicyManager(Context context) {
257257
return (DevicePolicyManager)context.getSystemService(Service.DEVICE_POLICY_SERVICE);
258258
}
259259

260-
// TODO: nuke this when available in the SDK.
261-
@TargetApi(VERSION_CODES.O)
262-
public static Boolean isUsingUnifiedPassword(Context context, ComponentName admin) {
263-
if (!BuildCompat.isAtLeastP()) {
264-
return false;
265-
}
266-
final DevicePolicyManager dpm = getDevicePolicyManager(context);
267-
268-
boolean unified = false;
269-
try {
270-
unified = (Boolean) ReflectionUtil.invoke(dpm, "isUsingUnifiedPassword", admin);
271-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
272-
Log.e(TAG, "Reflection call failed", e);
273-
}
274-
return unified;
275-
}
276260
}

app/src/main/java/com/afwsamples/testdpc/policy/MeteredDataRestrictionInfoAdapter.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.afwsamples.testdpc.policy;
1717

18+
import android.annotation.TargetApi;
1819
import android.content.ComponentName;
1920
import android.content.Context;
2021
import android.content.DialogInterface;
@@ -26,7 +27,6 @@
2627

2728
import com.afwsamples.testdpc.DeviceAdminReceiver;
2829
import com.afwsamples.testdpc.R;
29-
import com.afwsamples.testdpc.common.ReflectionUtil;
3030
import com.afwsamples.testdpc.common.ToggleComponentsArrayAdapter;
3131

3232
import java.util.ArrayList;
@@ -96,19 +96,15 @@ public void onClick(DialogInterface dialog, int which) {
9696
}
9797
}
9898

99+
@TargetApi(28)
99100
private void setMeteredDataRestrictedPkgs(List<String> pkgNames) {
100-
try {
101-
final List<String> excludedPkgs = (List<String>) ReflectionUtil.invoke(
102-
mDevicePolicyManager, "setMeteredDataDisabled",
103-
new Class[] {ComponentName.class, List.class},
104-
DeviceAdminReceiver.getComponentName(mContext), pkgNames);
105-
if (!excludedPkgs.isEmpty()) {
106-
Toast.makeText(mContext, mContext.getString(
107-
R.string.metered_data_restriction_failed_pkgs, excludedPkgs),
108-
Toast.LENGTH_LONG).show();
109-
}
110-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
111-
Log.e(TAG, "Can't invoke setMeteredDataDisabled", e);
101+
final List<String> excludedPkgs = mDevicePolicyManager.setMeteredDataDisabled(
102+
DeviceAdminReceiver.getComponentName(mContext), pkgNames);
103+
104+
if (!excludedPkgs.isEmpty()) {
105+
Toast.makeText(mContext, mContext.getString(
106+
R.string.metered_data_restriction_failed_pkgs, excludedPkgs),
107+
Toast.LENGTH_LONG).show();
112108
}
113109
}
114110
}

app/src/main/java/com/afwsamples/testdpc/policy/OverrideApnFragment.java

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import android.app.admin.DevicePolicyManager;
2424
import android.content.ComponentName;
2525
import android.content.Context;
26-
import android.os.Build;
2726
import android.os.Bundle;
2827
import android.support.v14.preference.SwitchPreference;
2928
import android.support.v7.preference.Preference;
@@ -36,8 +35,6 @@
3635
import com.afwsamples.testdpc.DeviceAdminReceiver;
3736
import com.afwsamples.testdpc.R;
3837
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
39-
import com.afwsamples.testdpc.common.ReflectionUtil;
40-
import java.lang.reflect.Method;
4138
import java.net.InetAddress;
4239
import java.net.MalformedURLException;
4340
import java.net.URL;
@@ -106,12 +103,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
106103
switch (key) {
107104
case ENABLE_OVERRIDE_APN_KEY:
108105
boolean enabled = (boolean) newValue;
109-
try {
110-
ReflectionUtil.invoke(mDevicePolicyManager, "setOverrideApnsEnabled",
111-
new Class[]{ComponentName.class, boolean.class}, mAdminComponentName, enabled);
112-
} catch (Exception e) {
113-
Log.e(LOG_TAG, "setOverrideApnsEnabled not implemented", e);
114-
}
106+
mDevicePolicyManager.setOverrideApnsEnabled(mAdminComponentName, enabled);
115107
reloadEnableOverrideApnUi();
116108
return true;
117109
}
@@ -201,15 +193,7 @@ void showInsertOverrideApnDialog() {
201193
networkbitmask,
202194
mvnoTypeEditText.getText().toString()
203195
);
204-
int insertedId = -1;
205-
try {
206-
insertedId = (int) ReflectionUtil.invoke(mDevicePolicyManager,
207-
"addOverrideApn",
208-
new Class[]{ComponentName.class, ApnSetting.class},
209-
mAdminComponentName, apn);
210-
} catch (Exception e) {
211-
Log.e(LOG_TAG, "addOverrideApn method not implemented", e);
212-
}
196+
int insertedId = mDevicePolicyManager.addOverrideApn(mAdminComponentName, apn);
213197
if (insertedId == -1) {
214198
showToast(R.string.insert_override_apn_error);
215199
} else {
@@ -221,18 +205,10 @@ void showInsertOverrideApnDialog() {
221205
}
222206

223207
private void onRemoveOverrideApn() {
224-
try {
225-
List<ApnSetting> apnSettings = (List<ApnSetting>)ReflectionUtil.invoke(mDevicePolicyManager,
226-
"getOverrideApns",
227-
new Class[]{ComponentName.class},
228-
mAdminComponentName);
229-
for(ApnSetting apn : apnSettings) {
230-
ReflectionUtil.invoke(mDevicePolicyManager, "removeOverrideApn",
231-
new Class[]{ComponentName.class, int.class},
232-
mAdminComponentName, apn.getId());
233-
}
234-
} catch (Exception e) {
235-
Log.e(LOG_TAG, "removeOverrideApn method not implemented", e);
208+
List<ApnSetting> apnSettings =
209+
mDevicePolicyManager.getOverrideApns(mAdminComponentName);
210+
for (ApnSetting apn : apnSettings) {
211+
mDevicePolicyManager.removeOverrideApn(mAdminComponentName, apn.getId());
236212
}
237213
}
238214

@@ -249,33 +225,25 @@ private ApnSetting makeApnSetting(String operatorNumeric, String entryName, Stri
249225
String user, String password, int authType, List<String> types, String protocol,
250226
String roamingProtocol, boolean carrierEnabled, int networkTypeBitmask,
251227
String mvnoType) {
252-
try {
253228
ApnSetting.Builder builder = new ApnSetting.Builder();
254-
Method setNetworkTypeBitmask = ApnSetting.Builder.class.getMethod(
255-
"setNetworkTypeBitmask", new Class[]{int.class});
256-
setNetworkTypeBitmask.invoke(builder, networkTypeBitmask);
257-
258-
return builder.setOperatorNumeric(operatorNumeric)
259-
.setEntryName(entryName)
260-
.setApnName(apnName)
261-
.setProxy(proxy)
262-
.setPort(port)
263-
.setMmsc(mmsc)
264-
.setMmsProxy(mmsProxy)
265-
.setMmsPort(mmsPort)
266-
.setUser(user)
267-
.setPassword(password)
268-
.setAuthType(authType)
269-
.setTypes(types)
270-
.setProtocol(protocol)
271-
.setRoamingProtocol(roamingProtocol)
272-
.setCarrierEnabled(carrierEnabled)
273-
.setMvnoType(mvnoType)
274-
.build();
275-
} catch (Exception e) {
276-
Log.e(LOG_TAG, "setNetworkTypeBitmask not implemented.", e);
277-
}
278-
return null;
229+
return builder.setOperatorNumeric(operatorNumeric)
230+
.setEntryName(entryName)
231+
.setApnName(apnName)
232+
.setProxy(proxy)
233+
.setPort(port)
234+
.setMmsc(mmsc)
235+
.setMmsProxy(mmsProxy)
236+
.setMmsPort(mmsPort)
237+
.setUser(user)
238+
.setPassword(password)
239+
.setAuthType(authType)
240+
.setTypes(types)
241+
.setProtocol(protocol)
242+
.setRoamingProtocol(roamingProtocol)
243+
.setCarrierEnabled(carrierEnabled)
244+
.setMvnoType(mvnoType)
245+
.setNetworkTypeBitmask(networkTypeBitmask)
246+
.build();
279247
}
280248

281249
private URL URLFromString(String url) {
@@ -314,15 +282,7 @@ private String[] parseTypes(String types) {
314282
}
315283

316284
private void reloadEnableOverrideApnUi() {
317-
boolean enabled = false;
318-
try {
319-
enabled = (boolean)ReflectionUtil.invoke(mDevicePolicyManager,
320-
"isOverrideApnEnabled",
321-
new Class[]{ComponentName.class}, mAdminComponentName);
322-
} catch (Exception e) {
323-
Log.e(LOG_TAG, "setOverrideApnEnabled not implemented", e);
324-
showToast("setOverrideApnEnabled not implemented");
325-
}
285+
boolean enabled = mDevicePolicyManager.isOverrideApnEnabled(mAdminComponentName);
326286
if (mEnableOverrideApnPreference.isEnabled()) {
327287
mEnableOverrideApnPreference.setChecked(enabled);
328288
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,11 @@ private void showManageLockTaskListPrompt(int dialogTitle,
13441344
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
13451345
final List<ResolveInfo> primaryUserAppList = mPackageManager
13461346
.queryIntentActivities(launcherIntent, 0);
1347+
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
1348+
homeIntent.addCategory(Intent.CATEGORY_HOME);
1349+
// Also show the default launcher in this list
1350+
final ResolveInfo defaultLauncher = mPackageManager.resolveActivity(homeIntent, 0);
1351+
primaryUserAppList.add(defaultLauncher);
13471352
if (primaryUserAppList.isEmpty()) {
13481353
showToast(R.string.no_primary_app_available);
13491354
} else {
@@ -2011,14 +2016,14 @@ private void loadSecurityPatch() {
20112016
securityPatchPreference.setSummary(display);
20122017
}
20132018

2014-
@TargetApi(Build.VERSION_CODES.O)
2019+
@TargetApi(28)
20152020
private void loadSeparateChallenge() {
20162021
final Preference separateChallengePreference = findPreference(SEPARATE_CHALLENGE_KEY);
20172022
if (!separateChallengePreference.isEnabled()) {
20182023
return;
20192024
}
20202025

2021-
final Boolean separate = !Util.isUsingUnifiedPassword(getActivity(), mAdminComponentName);
2026+
final Boolean separate = !mDevicePolicyManager.isUsingUnifiedPassword(mAdminComponentName);
20222027
separateChallengePreference.setSummary(String.format(
20232028
getString(R.string.separate_challenge_summary),
20242029
Boolean.toString(separate)));
@@ -2811,14 +2816,9 @@ private void showSetMeteredDataPrompt() {
28112816
.show();
28122817
}
28132818

2819+
@TargetApi(28)
28142820
private List<String> getMeteredDataRestrictedPkgs() {
2815-
try {
2816-
return (List<String>) ReflectionUtil.invoke(mDevicePolicyManager,
2817-
"getMeteredDataDisabled", mAdminComponentName);
2818-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
2819-
Log.e(TAG, "Can't invoke getMeteredDataDisabled", e);
2820-
return new ArrayList<>();
2821-
}
2821+
return mDevicePolicyManager.getMeteredDataDisabled(mAdminComponentName);
28222822
}
28232823

28242824
/**

app/src/main/java/com/afwsamples/testdpc/policy/SecurityLogsFragment.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import android.widget.ListView;
3232
import com.afwsamples.testdpc.DeviceAdminReceiver;
3333
import com.afwsamples.testdpc.R;
34-
import com.afwsamples.testdpc.common.ReflectionUtil;
3534
import java.util.ArrayList;
3635
import java.util.Date;
3736
import java.util.List;
@@ -108,14 +107,7 @@ private void processEvents(List<SecurityEvent> logs) {
108107
StringBuilder sb = new StringBuilder();
109108
sb.append(getStringEventTagFromId(event.getTag()));
110109
if (BuildCompat.isAtLeastP()) {
111-
long id;
112-
try {
113-
id = (long) ReflectionUtil.invoke(event, "getId");
114-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
115-
Log.e(TAG, "Can't invoke getId()", e);
116-
id = 0;
117-
}
118-
sb.append(" (id: " + id + ")");
110+
sb.append(" (id: " + getEventId(event) + ")");
119111
}
120112
sb.append(" (").append(new Date(TimeUnit.NANOSECONDS.toMillis(
121113
event.getTimeNanos()))).append("): ");
@@ -127,6 +119,11 @@ private void processEvents(List<SecurityEvent> logs) {
127119
}
128120
}
129121

122+
@TargetApi(28)
123+
private long getEventId(SecurityEvent event) {
124+
return event.getId();
125+
}
126+
130127
private String getStringEventTagFromId(int eventId) {
131128
final String eventTag;
132129
switch (eventId) {

0 commit comments

Comments
 (0)