Skip to content

Commit 75e17d4

Browse files
author
android-build-team Robot
committed
Snap for 4651315 from 0d7ba3210a631ded014690415455686cb5664f61 to ub-testdpc-pic-release
Change-Id: I17ea60cbc74e1fef99533c2631706ba3548776a9
2 parents 1206707 + 3eed62f commit 75e17d4

File tree

8 files changed

+89
-109
lines changed

8 files changed

+89
-109
lines changed

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,24 +286,36 @@ public void onBugreportFailed(Context context, Intent intent, int failureCode) {
286286
@TargetApi(Build.VERSION_CODES.O)
287287
@Override
288288
public void onUserAdded(Context context, Intent intent, UserHandle newUser) {
289-
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
290-
String message = context.getString(R.string.on_user_added_message,
291-
userManager.getSerialNumberForUser(newUser));
292-
Log.i(TAG, message);
293-
NotificationUtil.showNotification(context, R.string.on_user_added_title,
294-
message,
295-
NotificationUtil.USER_ADDED_NOTIFICATION_ID);
289+
handleUserAction(context, newUser, R.string.on_user_added_title,
290+
R.string.on_user_added_message, NotificationUtil.USER_ADDED_NOTIFICATION_ID);
296291
}
297292

298293
@TargetApi(Build.VERSION_CODES.O)
299294
@Override
300295
public void onUserRemoved(Context context, Intent intent, UserHandle removedUser) {
301-
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
302-
String message = context.getString(R.string.on_user_removed_message,
303-
userManager.getSerialNumberForUser(removedUser));
304-
Log.i(TAG, message);
305-
NotificationUtil.showNotification(context, R.string.on_user_removed_title, message,
306-
NotificationUtil.USER_REMOVED_NOTIFICATION_ID);
296+
handleUserAction(context, removedUser, R.string.on_user_removed_title,
297+
R.string.on_user_removed_message, NotificationUtil.USER_REMOVED_NOTIFICATION_ID);
298+
}
299+
300+
@TargetApi(Build.VERSION_CODES.P)
301+
@Override
302+
public void onUserStarted(Context context, Intent intent, UserHandle startedUser) {
303+
handleUserAction(context, startedUser, R.string.on_user_started_title,
304+
R.string.on_user_started_message, NotificationUtil.USER_STARTED_NOTIFICATION_ID);
305+
}
306+
307+
@TargetApi(Build.VERSION_CODES.P)
308+
@Override
309+
public void onUserStopped(Context context, Intent intent, UserHandle stoppedUser) {
310+
handleUserAction(context, stoppedUser, R.string.on_user_stopped_title,
311+
R.string.on_user_stopped_message, NotificationUtil.USER_STOPPED_NOTIFICATION_ID);
312+
}
313+
314+
@TargetApi(Build.VERSION_CODES.P)
315+
@Override
316+
public void onUserSwitched(Context context, Intent intent, UserHandle switchedUser) {
317+
handleUserAction(context, switchedUser, R.string.on_user_switched_title,
318+
R.string.on_user_switched_message, NotificationUtil.USER_SWITCHED_NOTIFICATION_ID);
307319
}
308320

309321
@TargetApi(Build.VERSION_CODES.M)
@@ -636,4 +648,13 @@ public void onTransferAffiliatedProfileOwnershipComplete(Context context, UserHa
636648
context.getString(R.string.transfer_ownership_affiliated_complete_message, user),
637649
NotificationUtil.TRANSFER_AFFILIATED_PROFILE_OWNERSHIP_COMPLETE_ID);
638650
}
651+
652+
private void handleUserAction(Context context, UserHandle userHandle, int titleResId,
653+
int messageResId, int notificationId) {
654+
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
655+
String message = context.getString(messageResId,
656+
userManager.getSerialNumberForUser(userHandle));
657+
Log.i(TAG, message);
658+
NotificationUtil.showNotification(context, titleResId, message, notificationId);
659+
}
639660
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public class NotificationUtil {
1919
public static final int PASSWORD_EXPIRATION_NOTIFICATION_ID = 2;
2020
public static final int USER_ADDED_NOTIFICATION_ID = 3;
2121
public static final int USER_REMOVED_NOTIFICATION_ID = 4;
22-
public static final int PROFILE_OWNER_CHANGED_ID = 5;
23-
public static final int DEVICE_OWNER_CHANGED_ID = 6;
24-
public static final int TRANSFER_OWNERSHIP_COMPLETE_ID = 7;
25-
public static final int TRANSFER_AFFILIATED_PROFILE_OWNERSHIP_COMPLETE_ID = 8;
22+
public static final int USER_STARTED_NOTIFICATION_ID = 5;
23+
public static final int USER_STOPPED_NOTIFICATION_ID = 6;
24+
public static final int USER_SWITCHED_NOTIFICATION_ID = 7;
25+
public static final int PROFILE_OWNER_CHANGED_ID = 8;
26+
public static final int DEVICE_OWNER_CHANGED_ID = 9;
27+
public static final int TRANSFER_OWNERSHIP_COMPLETE_ID = 10;
28+
public static final int TRANSFER_AFFILIATED_PROFILE_OWNERSHIP_COMPLETE_ID = 11;
2629

2730
public static void showNotification(
2831
Context context, @StringRes int titleId, String msg, int notificationId) {

app/src/main/java/com/afwsamples/testdpc/common/preference/DpcPreferenceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ public void disableIfConstraintsNotMet() {
220220
*/
221221
private CharSequence findConstraintViolation() {
222222
if (getDeviceSdkInt() < mMinSdkVersion) {
223-
// FIXME: Remove this special checking once O is out.
224-
if (mMinSdkVersion >= 26) {
223+
// FIXME: Remove this special checking once P is out.
224+
if (mMinSdkVersion > 27) {
225225
return mContext.getString(R.string.requires_preview_release);
226226
}
227227
return mContext.getString(R.string.requires_android_api_level, mMinSdkVersion);

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

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
9191
import com.afwsamples.testdpc.common.CertificateUtil;
9292
import com.afwsamples.testdpc.common.MediaDisplayFragment;
93-
import com.afwsamples.testdpc.common.ReflectionUtil;
9493
import com.afwsamples.testdpc.common.UserArrayAdapter;
9594
import com.afwsamples.testdpc.common.Util;
9695
import com.afwsamples.testdpc.common.preference.DpcPreference;
@@ -149,7 +148,6 @@
149148
import java.util.List;
150149
import java.util.Set;
151150
import java.util.TimeZone;
152-
import java.util.concurrent.Executor;
153151
import java.util.stream.Collectors;
154152

155153
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
@@ -1854,16 +1852,7 @@ private void showSwitchUserPrompt() {
18541852
@TargetApi(28)
18551853
private void showStartUserInBackgroundPrompt() {
18561854
showChooseUserPrompt(R.string.start_user_in_background, userHandle -> {
1857-
int status = USER_OPERATION_ERROR_UNKNOWN;
1858-
try {
1859-
status = (int) ReflectionUtil.invoke(
1860-
mDevicePolicyManager,
1861-
"startUserInBackground",
1862-
mAdminComponentName,
1863-
userHandle);
1864-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1865-
Log.e(TAG,"Can't invoke startUserInBackground", e);
1866-
}
1855+
int status = mDevicePolicyManager.startUserInBackground(mAdminComponentName, userHandle);
18671856
showToast(status == USER_OPERATION_SUCCESS
18681857
? R.string.user_started_in_background
18691858
: R.string.failed_to_start_user_in_background);
@@ -1877,16 +1866,7 @@ private void showStartUserInBackgroundPrompt() {
18771866
@TargetApi(28)
18781867
private void showStopUserPrompt() {
18791868
showChooseUserPrompt(R.string.stop_user, userHandle -> {
1880-
int status = USER_OPERATION_ERROR_UNKNOWN;
1881-
try {
1882-
status = (int) ReflectionUtil.invoke(
1883-
mDevicePolicyManager,
1884-
"stopUser",
1885-
mAdminComponentName,
1886-
userHandle);
1887-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1888-
Log.e(TAG,"Can't invoke stopUser", e);
1889-
}
1869+
int status = mDevicePolicyManager.stopUser(mAdminComponentName, userHandle);
18901870
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_stopped
18911871
: R.string.failed_to_stop_user);
18921872
});
@@ -1927,15 +1907,7 @@ private void showChooseUserPrompt(int titleResId, UserCallback callback) {
19271907
*/
19281908
@TargetApi(28)
19291909
private void logoutUser() {
1930-
int status = USER_OPERATION_ERROR_UNKNOWN;
1931-
try {
1932-
status = (int) ReflectionUtil.invoke(
1933-
mDevicePolicyManager,
1934-
"logoutUser",
1935-
mAdminComponentName);
1936-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1937-
Log.e(TAG,"Can't invoke logoutUser", e);
1938-
}
1910+
int status = mDevicePolicyManager.logoutUser(mAdminComponentName);
19391911
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_logouted
19401912
: R.string.failed_to_logout_user);
19411913
}
@@ -2849,22 +2821,14 @@ private void showClearAppDataPrompt() {
28492821

28502822
@TargetApi(28)
28512823
private void clearApplicationUserData(String packageName) {
2852-
try {
2853-
ReflectionUtil.invoke(
2854-
mDevicePolicyManager, "clearApplicationUserData",
2855-
new Class[]{ComponentName.class, String.class, Executor.class,
2856-
DevicePolicyManager.OnClearApplicationUserDataListener.class},
2857-
mAdminComponentName,
2858-
packageName,
2859-
new MainThreadExecutor(),
2860-
(DevicePolicyManager.OnClearApplicationUserDataListener)
2861-
(__, succeed) -> showToast(
2862-
succeed ? R.string.clear_app_data_success
2863-
: R.string.clear_app_data_failure,
2864-
packageName));
2865-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
2866-
Log.e(LOG_TAG, "cannot invoke clearApplicationUserData", e);
2867-
}
2824+
mDevicePolicyManager.clearApplicationUserData(
2825+
mAdminComponentName,
2826+
packageName,
2827+
new MainThreadExecutor(),
2828+
(__, succeed) -> showToast(succeed ?
2829+
R.string.clear_app_data_success
2830+
: R.string.clear_app_data_failure,
2831+
packageName));
28682832
}
28692833

28702834
@TargetApi(Build.VERSION_CODES.N)

app/src/main/java/com/afwsamples/testdpc/policy/keyguard/PasswordBlacklistFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.util.Date;
4242
import java.util.List;
4343

44-
@TargetApi(Build.VERSION_CODES.N)
44+
@TargetApi(28)
4545
public class PasswordBlacklistFragment extends BaseManageComponentFragment<Void>
4646
implements EditDeleteArrayAdapter.OnEditButtonClickListener<String> {
4747

app/src/main/java/com/afwsamples/testdpc/policy/keyguard/PasswordConstraintsFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.content.Intent;
2525
import android.net.Uri;
2626
import android.os.Bundle;
27+
import android.support.v4.os.BuildCompat;
2728
import android.support.v7.preference.EditTextPreference;
2829
import android.support.v7.preference.ListPreference;
2930
import android.support.v7.preference.Preference;
@@ -211,7 +212,9 @@ public void onResume() {
211212

212213
// Settings that may have been changed by other users need updating.
213214
updateExpirationTimes();
214-
refreshBlacklistPreferences();
215+
if (BuildCompat.isAtLeastP()) {
216+
refreshBlacklistPreferences();
217+
}
215218
}
216219

217220
@Override

app/src/main/java/com/afwsamples/testdpc/policy/systemupdatepolicy/SystemUpdatePolicyFragment.java

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import android.widget.ListView;
3939
import android.widget.RadioGroup;
4040
import android.widget.Toast;
41+
4142
import com.afwsamples.testdpc.DeviceAdminReceiver;
4243
import com.afwsamples.testdpc.R;
43-
import com.afwsamples.testdpc.common.ReflectionUtil;
44-
import java.lang.reflect.InvocationTargetException;
44+
4545
import java.time.LocalDate;
4646
import java.time.format.DateTimeFormatter;
4747
import java.util.ArrayList;
@@ -251,7 +251,7 @@ private void promptToSetFreezePeriod(FreezePeriodPickResult callback, final Loca
251251
});
252252
}
253253

254-
@TargetApi(Build.VERSION_CODES.P)
254+
@TargetApi(28)
255255
private boolean setSystemUpdatePolicy() {
256256
SystemUpdatePolicy newPolicy;
257257
switch (mSystemUpdatePolicySelection.getCheckedRadioButtonId()) {
@@ -269,33 +269,21 @@ private boolean setSystemUpdatePolicy() {
269269
default:
270270
newPolicy = null;
271271
}
272-
try {
273-
if (BuildCompat.isAtLeastP() && newPolicy != null && mFreezePeriods.size() != 0) {
274-
List<Pair<Integer, Integer>> periods = new ArrayList<>(mFreezePeriods.size());
275-
for (Period p : mFreezePeriods) {
276-
periods.add(p.toIntegers());
277-
}
278-
ReflectionUtil.invoke(newPolicy, "setFreezePeriods",
279-
new Class[]{List.class}, periods);
272+
if (BuildCompat.isAtLeastP() && newPolicy != null && mFreezePeriods.size() != 0) {
273+
List<Pair<Integer, Integer>> periods = new ArrayList<>(mFreezePeriods.size());
274+
for (Period p : mFreezePeriods) {
275+
periods.add(p.toIntegers());
280276
}
281-
mDpm.setSystemUpdatePolicy(DeviceAdminReceiver.getComponentName(getActivity()),
282-
newPolicy);
283-
Toast.makeText(getContext(), "Policy set successfully", Toast.LENGTH_SHORT).show();
284-
return true;
285-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
286-
Throwable cause = e.getCause();
287-
if (cause instanceof InvocationTargetException) {
288-
if (cause.getCause().getClass().getSimpleName().equals(
289-
"ValidationFailedException")) {
290-
cause = cause.getCause();
291-
}
277+
try {
278+
newPolicy.setFreezePeriods(periods);
279+
mDpm.setSystemUpdatePolicy(DeviceAdminReceiver.getComponentName(getActivity()),
280+
newPolicy);
281+
Toast.makeText(getContext(), "Policy set successfully", Toast.LENGTH_LONG).show();
282+
return true;
283+
} catch (IllegalArgumentException e) {
284+
Toast.makeText(getContext(), "Failed to set system update policy: " + e.getMessage(),
285+
Toast.LENGTH_LONG).show();
292286
}
293-
cause.printStackTrace();
294-
Toast.makeText(getContext(), "Fail to set policy: " + cause.getMessage(),
295-
Toast.LENGTH_LONG).show();
296-
} catch (IllegalArgumentException e) {
297-
Toast.makeText(getContext(), "setSystemUpdatePolicy fails: " + e.getMessage(),
298-
Toast.LENGTH_LONG).show();
299287
}
300288
return false;
301289
}
@@ -309,7 +297,7 @@ private void updateMaintenanceWindowDisplay() {
309297
mSetMaintenanceWindowEnd.setText(formatMinutes(mMaintenanceEnd));
310298
}
311299

312-
@TargetApi(Build.VERSION_CODES.P)
300+
@TargetApi(28)
313301
private void reloadSystemUpdatePolicy() {
314302
SystemUpdatePolicy policy = mDpm.getSystemUpdatePolicy();
315303
String policyDescription = "Unknown";
@@ -344,18 +332,13 @@ private void reloadSystemUpdatePolicy() {
344332
break;
345333
}
346334
if (BuildCompat.isAtLeastP()) {
347-
try {
348-
List<Pair<Integer, Integer>> freezePeriods = (List<Pair<Integer, Integer>>)
349-
ReflectionUtil.invoke(policy, "getFreezePeriods");
350-
mFreezePeriods.clear();
351-
for (Pair<Integer, Integer> period : freezePeriods) {
352-
Period p = new Period(period.first, period.second);
353-
mFreezePeriods.add(p);
354-
}
355-
mFreezePeriodAdapter.notifyDataSetChanged();
356-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
357-
e.printStackTrace();
335+
List<Pair<Integer, Integer>> freezePeriods = policy.getFreezePeriods();
336+
mFreezePeriods.clear();
337+
for (Pair<Integer, Integer> period : freezePeriods) {
338+
Period p = new Period(period.first, period.second);
339+
mFreezePeriods.add(p);
358340
}
341+
mFreezePeriodAdapter.notifyDataSetChanged();
359342
mFreezePeriodPanel.setVisibility(View.VISIBLE);
360343
}
361344
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<!-- Reasons for disabling preferences -->
9696
<string name="requires_android_api_level">Requires API level
9797
<xliff:g id="api level">%d</xliff:g></string>
98-
<string name="requires_preview_release">Requires the Android O preview release</string>
98+
<string name="requires_preview_release">Requires the Android P preview release</string>
9999
<string name="requires">Requires\u0020</string>
100100
<string name="requires_delimiter">,\u0020</string>
101101
<string name="requires_or">\u0020or\u0020</string>
@@ -645,8 +645,14 @@
645645
<string name="ephemeral_user">Ephemeral user</string>
646646
<string name="on_user_added_title">User Created</string>
647647
<string name="on_user_removed_title">User Removed</string>
648+
<string name="on_user_started_title">User Started</string>
649+
<string name="on_user_stopped_title">User Stopped</string>
650+
<string name="on_user_switched_title">User Switched</string>
648651
<string name="on_user_added_message">User created with serial number %1$d.</string>
649652
<string name="on_user_removed_message">User with serial number %1$d removed.</string>
653+
<string name="on_user_started_message">User with serial number %1$d started.</string>
654+
<string name="on_user_stopped_message">User with serial number %1$d stopped.</string>
655+
<string name="on_user_switched_message">User with serial number %1$d switched.</string>
650656
<string name="no_secondary_users_available">No secondary users available.</string>
651657

652658
<!-- Affiliation ids -->

0 commit comments

Comments
 (0)