Skip to content

Commit 9947f2a

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Support onUserStarted / onUserStopped / onUserSwitched in TestDPC" into ub-testdpc-pic
2 parents 3e571ea + 568385e commit 9947f2a

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
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/res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)