1 /*
2  * Copyright (C) 2016 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.android.cts.deviceowner;
18 
19 import android.app.Service;
20 import android.app.admin.DeviceAdminReceiver;
21 import android.app.admin.DevicePolicyManager;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.ServiceConnection;
28 import android.content.pm.PackageManager;
29 import android.os.IBinder;
30 import android.os.PersistableBundle;
31 import android.os.RemoteException;
32 import android.os.UserHandle;
33 import android.os.UserManager;
34 import android.provider.Settings;
35 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
36 import android.util.Log;
37 
38 import java.lang.reflect.InvocationTargetException;
39 import java.lang.reflect.Method;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Set;
43 import java.util.concurrent.Semaphore;
44 import java.util.concurrent.LinkedBlockingQueue;
45 import java.util.concurrent.TimeUnit;
46 import java.util.function.Predicate;
47 import java.util.stream.Collectors;
48 
49 /**
50  * Test {@link DevicePolicyManager#createAndManageUser}.
51  */
52 public class CreateAndManageUserTest extends BaseDeviceOwnerTest {
53     private static final String TAG = "CreateAndManageUserTest";
54 
55     private static final int BROADCAST_TIMEOUT = 300_000;
56 
57     private static final String AFFILIATION_ID = "affiliation.id";
58     private static final String EXTRA_AFFILIATION_ID = "affiliationIdExtra";
59     private static final String EXTRA_METHOD_NAME = "methodName";
60     private static final long ON_ENABLED_TIMEOUT_SECONDS = 120;
61 
62     @Override
setUp()63     protected void setUp() throws Exception {
64         super.setUp();
65     }
66 
67     @Override
tearDown()68     protected void tearDown() throws Exception {
69         mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_ADD_USER);
70         mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
71         super.tearDown();
72     }
73 
testCreateAndManageUser()74     public void testCreateAndManageUser() {
75         String testUserName = "TestUser_" + System.currentTimeMillis();
76 
77         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
78                 getWho(),
79                 testUserName,
80                 getWho(),
81                 null,
82                 /* flags */ 0);
83         Log.d(TAG, "User create: " + userHandle);
84     }
85 
testCreateAndManageUser_LowStorage()86     public void testCreateAndManageUser_LowStorage() {
87         String testUserName = "TestUser_" + System.currentTimeMillis();
88 
89         try {
90             mDevicePolicyManager.createAndManageUser(
91                     getWho(),
92                     testUserName,
93                     getWho(),
94                     null,
95                 /* flags */ 0);
96             fail("createAndManageUser should throw UserOperationException");
97         } catch (UserManager.UserOperationException e) {
98             assertEquals(UserManager.USER_OPERATION_ERROR_LOW_STORAGE, e.getUserOperationResult());
99         }
100     }
101 
testCreateAndManageUser_MaxUsers()102     public void testCreateAndManageUser_MaxUsers() {
103         String testUserName = "TestUser_" + System.currentTimeMillis();
104 
105         try {
106             mDevicePolicyManager.createAndManageUser(
107                     getWho(),
108                     testUserName,
109                     getWho(),
110                     null,
111                 /* flags */ 0);
112             fail("createAndManageUser should throw UserOperationException");
113         } catch (UserManager.UserOperationException e) {
114             assertEquals(UserManager.USER_OPERATION_ERROR_MAX_USERS, e.getUserOperationResult());
115         }
116     }
117 
118     @SuppressWarnings("unused")
assertSkipSetupWizard(Context context, DevicePolicyManager devicePolicyManager, ComponentName componentName)119     private static void assertSkipSetupWizard(Context context,
120             DevicePolicyManager devicePolicyManager, ComponentName componentName) throws Exception {
121         assertEquals("user setup not completed", 1,
122                 Settings.Secure.getInt(context.getContentResolver(),
123                         Settings.Secure.USER_SETUP_COMPLETE));
124     }
125 
testCreateAndManageUser_SkipSetupWizard()126     public void testCreateAndManageUser_SkipSetupWizard() throws Exception {
127         runCrossUserVerification(DevicePolicyManager.SKIP_SETUP_WIZARD, "assertSkipSetupWizard");
128         PrimaryUserService.assertCrossUserCallArrived();
129     }
130 
testCreateAndManageUser_GetSecondaryUsers()131     public void testCreateAndManageUser_GetSecondaryUsers() {
132         String testUserName = "TestUser_" + System.currentTimeMillis();
133 
134         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
135                 getWho(),
136                 testUserName,
137                 getWho(),
138                 null,
139                 /* flags */ 0);
140         Log.d(TAG, "User create: " + userHandle);
141 
142         List<UserHandle> secondaryUsers = mDevicePolicyManager.getSecondaryUsers(getWho());
143         assertEquals(1, secondaryUsers.size());
144         assertEquals(userHandle, secondaryUsers.get(0));
145     }
146 
testCreateAndManageUser_SwitchUser()147     public void testCreateAndManageUser_SwitchUser() throws Exception {
148         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
149                 getContext());
150 
151         String testUserName = "TestUser_" + System.currentTimeMillis();
152 
153         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
154                 getWho(),
155                 testUserName,
156                 getWho(),
157                 null,
158                 /* flags */ 0);
159         Log.d(TAG, "User create: " + userHandle);
160 
161         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
162         localBroadcastManager.registerReceiver(broadcastReceiver,
163                 new IntentFilter(BasicAdminReceiver.ACTION_USER_SWITCHED));
164         try {
165             assertTrue(mDevicePolicyManager.switchUser(getWho(), userHandle));
166             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
167         } finally {
168             localBroadcastManager.unregisterReceiver(broadcastReceiver);
169         }
170     }
171 
testCreateAndManageUser_CannotStopCurrentUser()172     public void testCreateAndManageUser_CannotStopCurrentUser() throws Exception {
173         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
174                 getContext());
175 
176         String testUserName = "TestUser_" + System.currentTimeMillis();
177 
178         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
179                 getWho(),
180                 testUserName,
181                 getWho(),
182                 null,
183                 /* flags */ 0);
184         Log.d(TAG, "User create: " + userHandle);
185 
186         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
187         localBroadcastManager.registerReceiver(broadcastReceiver,
188                 new IntentFilter(BasicAdminReceiver.ACTION_USER_SWITCHED));
189         try {
190             assertTrue(mDevicePolicyManager.switchUser(getWho(), userHandle));
191             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
192             assertEquals(UserManager.USER_OPERATION_ERROR_CURRENT_USER,
193                     mDevicePolicyManager.stopUser(getWho(), userHandle));
194         } finally {
195             localBroadcastManager.unregisterReceiver(broadcastReceiver);
196         }
197     }
198 
testCreateAndManageUser_StartInBackground()199     public void testCreateAndManageUser_StartInBackground() throws Exception {
200         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
201                 getContext());
202 
203         String testUserName = "TestUser_" + System.currentTimeMillis();
204 
205         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
206                 getWho(),
207                 testUserName,
208                 getWho(),
209                 null,
210                 /* flags */ 0);
211         Log.d(TAG, "User create: " + userHandle);
212 
213         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
214         localBroadcastManager.registerReceiver(broadcastReceiver,
215                 new IntentFilter(BasicAdminReceiver.ACTION_USER_STARTED));
216 
217         try {
218             // Start user in background and wait for onUserStarted
219             assertEquals(UserManager.USER_OPERATION_SUCCESS,
220                     mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
221             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
222         } finally {
223             localBroadcastManager.unregisterReceiver(broadcastReceiver);
224         }
225     }
226 
testCreateAndManageUser_StartInBackground_MaxRunningUsers()227     public void testCreateAndManageUser_StartInBackground_MaxRunningUsers() {
228         String testUserName = "TestUser_" + System.currentTimeMillis();
229 
230         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
231                 getWho(),
232                 testUserName,
233                 getWho(),
234                 null,
235                 /* flags */ 0);
236         Log.d(TAG, "User create: " + userHandle);
237 
238         // Start user in background and should receive max running users error
239         assertEquals(UserManager.USER_OPERATION_ERROR_MAX_RUNNING_USERS,
240                 mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
241     }
242 
testCreateAndManageUser_StopUser()243     public void testCreateAndManageUser_StopUser() throws Exception {
244         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
245                 getContext());
246 
247         String testUserName = "TestUser_" + System.currentTimeMillis();
248 
249         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
250                 getWho(),
251                 testUserName,
252                 getWho(),
253                 null,
254                 /* flags */ 0);
255         Log.d(TAG, "User create: " + userHandle);
256         assertEquals(UserManager.USER_OPERATION_SUCCESS,
257                 mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
258 
259         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
260         localBroadcastManager.registerReceiver(broadcastReceiver,
261                 new IntentFilter(BasicAdminReceiver.ACTION_USER_STOPPED));
262 
263         try {
264             assertEquals(UserManager.USER_OPERATION_SUCCESS,
265                     mDevicePolicyManager.stopUser(getWho(), userHandle));
266             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
267         } finally {
268             localBroadcastManager.unregisterReceiver(broadcastReceiver);
269         }
270     }
271 
testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser()272     public void testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser() throws Exception {
273         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
274                 getContext());
275 
276         String testUserName = "TestUser_" + System.currentTimeMillis();
277 
278         // Set DISALLOW_REMOVE_USER restriction
279         mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
280 
281         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
282                 getWho(),
283                 testUserName,
284                 getWho(),
285                 null,
286                 DevicePolicyManager.MAKE_USER_EPHEMERAL);
287         Log.d(TAG, "User create: " + userHandle);
288         assertEquals(UserManager.USER_OPERATION_SUCCESS,
289                 mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
290 
291         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
292         localBroadcastManager.registerReceiver(broadcastReceiver,
293                 new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
294 
295         try {
296             assertEquals(UserManager.USER_OPERATION_SUCCESS,
297                     mDevicePolicyManager.stopUser(getWho(), userHandle));
298             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
299         } finally {
300             localBroadcastManager.unregisterReceiver(broadcastReceiver);
301             // Clear DISALLOW_REMOVE_USER restriction
302             mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
303         }
304     }
305 
306     @SuppressWarnings("unused")
logoutUser(Context context, DevicePolicyManager devicePolicyManager, ComponentName componentName)307     private static void logoutUser(Context context, DevicePolicyManager devicePolicyManager,
308             ComponentName componentName) {
309         assertEquals("cannot logout user", UserManager.USER_OPERATION_SUCCESS,
310                 devicePolicyManager.logoutUser(componentName));
311     }
312 
testCreateAndManageUser_LogoutUser()313     public void testCreateAndManageUser_LogoutUser() throws Exception {
314         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
315                 getContext());
316 
317         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
318         localBroadcastManager.registerReceiver(broadcastReceiver,
319                 new IntentFilter(BasicAdminReceiver.ACTION_USER_STOPPED));
320 
321         try {
322             UserHandle userHandle = runCrossUserVerification(
323                     /* createAndManageUserFlags */ 0, "logoutUser");
324             assertEquals(userHandle, broadcastReceiver.waitForBroadcastReceived());
325         } finally {
326             localBroadcastManager.unregisterReceiver(broadcastReceiver);
327         }
328     }
329 
330     @SuppressWarnings("unused")
assertAffiliatedUser(Context context, DevicePolicyManager devicePolicyManager, ComponentName componentName)331     private static void assertAffiliatedUser(Context context,
332             DevicePolicyManager devicePolicyManager, ComponentName componentName) {
333         assertTrue("not affiliated user", devicePolicyManager.isAffiliatedUser());
334     }
335 
testCreateAndManageUser_Affiliated()336     public void testCreateAndManageUser_Affiliated() throws Exception {
337         runCrossUserVerification(/* createAndManageUserFlags */ 0, "assertAffiliatedUser");
338         PrimaryUserService.assertCrossUserCallArrived();
339     }
340 
341     @SuppressWarnings("unused")
assertEphemeralUser(Context context, DevicePolicyManager devicePolicyManager, ComponentName componentName)342     private static void assertEphemeralUser(Context context,
343             DevicePolicyManager devicePolicyManager, ComponentName componentName) {
344         assertTrue("not ephemeral user", devicePolicyManager.isEphemeralUser(componentName));
345     }
346 
testCreateAndManageUser_Ephemeral()347     public void testCreateAndManageUser_Ephemeral() throws Exception {
348         runCrossUserVerification(DevicePolicyManager.MAKE_USER_EPHEMERAL, "assertEphemeralUser");
349         PrimaryUserService.assertCrossUserCallArrived();
350     }
351 
352     @SuppressWarnings("unused")
assertAllSystemAppsInstalled(Context context, DevicePolicyManager devicePolicyManager, ComponentName componentName)353     private static void assertAllSystemAppsInstalled(Context context,
354             DevicePolicyManager devicePolicyManager, ComponentName componentName) {
355         PackageManager packageManager = context.getPackageManager();
356         // First get a set of installed package names
357         Set<String> installedPackageNames = packageManager
358                 .getInstalledApplications(/* flags */ 0)
359                 .stream()
360                 .map(applicationInfo -> applicationInfo.packageName)
361                 .collect(Collectors.toSet());
362         // Then filter all package names by those that are not installed
363         Set<String> uninstalledPackageNames = packageManager
364                 .getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES)
365                 .stream()
366                 .map(applicationInfo -> applicationInfo.packageName)
367                 .filter(((Predicate<String>) installedPackageNames::contains).negate())
368                 .collect(Collectors.toSet());
369         // Assert that all apps are installed
370         assertTrue("system apps not installed: " + uninstalledPackageNames,
371                 uninstalledPackageNames.isEmpty());
372     }
373 
testCreateAndManageUser_LeaveAllSystemApps()374     public void testCreateAndManageUser_LeaveAllSystemApps() throws Exception {
375         runCrossUserVerification(
376                 DevicePolicyManager.LEAVE_ALL_SYSTEM_APPS_ENABLED, "assertAllSystemAppsInstalled");
377         PrimaryUserService.assertCrossUserCallArrived();
378     }
379 
runCrossUserVerification(int createAndManageUserFlags, String methodName)380     private UserHandle runCrossUserVerification(int createAndManageUserFlags, String methodName) {
381         String testUserName = "TestUser_" + System.currentTimeMillis();
382 
383         // Set affiliation id to allow communication.
384         mDevicePolicyManager.setAffiliationIds(getWho(), Collections.singleton(AFFILIATION_ID));
385 
386         // Pack the affiliation id in a bundle so the secondary user can get it.
387         PersistableBundle bundle = new PersistableBundle();
388         bundle.putString(EXTRA_AFFILIATION_ID, AFFILIATION_ID);
389         bundle.putString(EXTRA_METHOD_NAME, methodName);
390 
391         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(
392                 getWho(),
393                 testUserName,
394                 SecondaryUserAdminReceiver.getComponentName(getContext()),
395                 bundle,
396                 createAndManageUserFlags);
397         Log.d(TAG, "User create: " + userHandle);
398         assertEquals(UserManager.USER_OPERATION_SUCCESS,
399                 mDevicePolicyManager.startUserInBackground(getWho(), userHandle));
400 
401         return userHandle;
402     }
403 
404     // createAndManageUser should circumvent the DISALLOW_ADD_USER restriction
testCreateAndManageUser_AddRestrictionSet()405     public void testCreateAndManageUser_AddRestrictionSet() {
406         mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_ADD_USER);
407 
408         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User",
409                 getWho(), null, 0);
410         assertNotNull(userHandle);
411     }
412 
testCreateAndManageUser_RemoveRestrictionSet()413     public void testCreateAndManageUser_RemoveRestrictionSet() {
414         mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_REMOVE_USER);
415 
416         UserHandle userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User",
417                 getWho(), null, 0);
418         assertNotNull(userHandle);
419 
420         boolean removed = mDevicePolicyManager.removeUser(getWho(), userHandle);
421         // When the device owner itself has set the user restriction, it should still be allowed
422         // to remove a user.
423         assertTrue(removed);
424     }
425 
testUserAddedOrRemovedBroadcasts()426     public void testUserAddedOrRemovedBroadcasts() throws InterruptedException {
427         LocalBroadcastReceiver receiver = new LocalBroadcastReceiver();
428         LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(
429                 getContext());
430         localBroadcastManager.registerReceiver(receiver,
431                 new IntentFilter(BasicAdminReceiver.ACTION_USER_ADDED));
432         UserHandle userHandle;
433         try {
434             userHandle = mDevicePolicyManager.createAndManageUser(getWho(), "Test User", getWho(),
435                     null, 0);
436             assertNotNull(userHandle);
437             assertEquals(userHandle, receiver.waitForBroadcastReceived());
438         } finally {
439             localBroadcastManager.unregisterReceiver(receiver);
440         }
441         localBroadcastManager.registerReceiver(receiver,
442                 new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
443         try {
444             assertTrue(mDevicePolicyManager.removeUser(getWho(), userHandle));
445             assertEquals(userHandle, receiver.waitForBroadcastReceived());
446         } finally {
447             localBroadcastManager.unregisterReceiver(receiver);
448         }
449     }
450 
451     static class LocalBroadcastReceiver extends BroadcastReceiver {
452         private LinkedBlockingQueue<UserHandle> mQueue = new LinkedBlockingQueue<>(1);
453 
454         @Override
onReceive(Context context, Intent intent)455         public void onReceive(Context context, Intent intent) {
456             UserHandle userHandle = intent.getParcelableExtra(BasicAdminReceiver.EXTRA_USER_HANDLE);
457             Log.d(TAG, "broadcast receiver received " + intent + " with userHandle "
458                     + userHandle);
459             mQueue.offer(userHandle);
460 
461         }
462 
waitForBroadcastReceived()463         UserHandle waitForBroadcastReceived() throws InterruptedException {
464             return mQueue.poll(BROADCAST_TIMEOUT, TimeUnit.MILLISECONDS);
465         }
466     }
467 
468     public static final class PrimaryUserService extends Service {
469         private static final Semaphore sSemaphore = new Semaphore(0);
470         private static String sError = null;
471 
472         private final ICrossUserService.Stub mBinder = new ICrossUserService.Stub() {
473             public void onEnabledCalled(String error) {
474                 Log.d(TAG, "onEnabledCalled on primary user");
475                 sError = error;
476                 sSemaphore.release();
477             }
478         };
479 
480         @Override
onBind(Intent intent)481         public IBinder onBind(Intent intent) {
482             return mBinder;
483         }
484 
assertCrossUserCallArrived()485         static void assertCrossUserCallArrived() throws Exception {
486             assertTrue(sSemaphore.tryAcquire(ON_ENABLED_TIMEOUT_SECONDS, TimeUnit.SECONDS));
487             if (sError != null) {
488                 throw new Exception(sError);
489             }
490         }
491     }
492 
493     public static final class SecondaryUserAdminReceiver extends DeviceAdminReceiver {
494         @Override
onEnabled(Context context, Intent intent)495         public void onEnabled(Context context, Intent intent) {
496             Log.d(TAG, "onEnabled called");
497             DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
498             ComponentName who = getComponentName(context);
499 
500             // Set affiliation ids
501             dpm.setAffiliationIds(
502                     who, Collections.singleton(intent.getStringExtra(EXTRA_AFFILIATION_ID)));
503 
504             String error = null;
505             try {
506                 Method method = CreateAndManageUserTest.class.getDeclaredMethod(
507                         intent.getStringExtra(EXTRA_METHOD_NAME), Context.class,
508                         DevicePolicyManager.class, ComponentName.class);
509                 method.setAccessible(true);
510                 method.invoke(null, context, dpm, who);
511             } catch (NoSuchMethodException | IllegalAccessException e) {
512                 error = e.toString();
513             } catch (InvocationTargetException e) {
514                 error = e.getCause().toString();
515             }
516 
517             // Call all affiliated users
518             final List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(who);
519             assertEquals(1, targetUsers.size());
520             pingTargetUser(context, dpm, targetUsers.get(0), error);
521         }
522 
pingTargetUser(Context context, DevicePolicyManager dpm, UserHandle target, String error)523         private void pingTargetUser(Context context, DevicePolicyManager dpm, UserHandle target,
524                 String error) {
525             Log.d(TAG, "Pinging target: " + target);
526             final ServiceConnection serviceConnection = new ServiceConnection() {
527                 @Override
528                 public void onServiceConnected(ComponentName name, IBinder service) {
529                     Log.d(TAG, "onServiceConnected is called in " + Thread.currentThread().getName());
530                     ICrossUserService crossUserService = ICrossUserService
531                             .Stub.asInterface(service);
532                     try {
533                         crossUserService.onEnabledCalled(error);
534                     } catch (RemoteException re) {
535                         Log.e(TAG, "Error when calling primary user", re);
536                         // Do nothing, primary user will time out
537                     }
538                 }
539 
540                 @Override
541                 public void onServiceDisconnected(ComponentName name) {
542                     Log.d(TAG, "onServiceDisconnected is called");
543                 }
544             };
545             final Intent serviceIntent = new Intent(context, PrimaryUserService.class);
546             assertTrue(dpm.bindDeviceAdminServiceAsUser(
547                     getComponentName(context),
548                     serviceIntent,
549                     serviceConnection,
550                     Context.BIND_AUTO_CREATE,
551                     target));
552         }
553 
getComponentName(Context context)554         public static ComponentName getComponentName(Context context) {
555             return new ComponentName(context, SecondaryUserAdminReceiver.class);
556         }
557     }
558 }
559