Search code examples
androidandroid-5.0-lollipopdevice-policy-managerdevice-owner

Setup a new user after it's created


From my device owner application, I'd like to create a new user and switch directly to it. For now, I can only create a new user, switch to it but:

  • it brings me to the keyguard screen, that I need to manually unlock.
  • then, tells me to setup the newly created user - with firstname, lastname, WIFI settings, and 3 Google usage statistics/reporting options.

I'd like to know if there's a way to programmatically setup the new user and switch directly to it's "session". I'd like to programmatically avoid the "unlock" page et pre-setup the newly created user with name, WIFI settings, but also available apps and security settings.

here's what I do so far :

// init block (in onCreate...)
DevicePolicyManager mDPM  = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mDeviceAdminRcvr = new ComponentName(this, DeviceAdminRcvr.class);    

// in my button "create a new user"
ComponentName profileOwnerComponent = new ComponentName(this, ProfileAdminRcvr.class);
Bundle adminExtras = new Bundle();

UserHandle userHandle = mDPM.createAndInitializeUser(mDeviceAdminRcvr, name, ownerName, profileOwnerComponent, adminExtras);

// TODO : place here missing instructions to provision the user...
mDPM.switchUser(mDeviceAdminRcvr, userHandle);

I couldn't find any documentation on the official Google page about device owner apps or profile apps. Could anyone help me or point me to useful links ?


Solution

  • As far as I've seen, there is no way to programmatically unlock the screen lock. Even the Smart lock functionnality added in Lollipop will just disable the Key Guard, which means that the "PIN" or "Pattern" will transform into a "Swipe Lock" when a trusted agent unlocks the device. Even in this case, you'll need to manually swipe the screen to unlock the device.

    Concerning the second point, it's possible to avoid the "Setup Wizard" proposed the first time you unlock a newly created user. Here's how to do it :

    • in your ProfileAdminRcvr.java, you'll need to hide the system application called com.google.android.setupwizard. You could do this in the onEnabled() method of your DeviceAdminReceiver's implementation (the one you set for your profile when creating the user).
    • To complete this, you can disable the "first use hint", by setting the Settings.Secure.SKIP_FIRST_USE_HINTS property.

    Here's the code to do it :

    public class ProfileOwnerRcvr extends DeviceAdminReceiver {
    
      private DevicePolicyManager mDPM;
      private ComponentName mProfileAdminRcvr;
    
      @Override
      public void onEnabled(Context context, Intent intent) {
    
        mDPM.setProfileName(mProfileAdminRcvr, "My new user");
        // ... setup other things by yourself...        
    
        mDPM.setApplicationHidden( mProfileAdminRcvr, "com.google.android.setupwizard", true);
        mDPM.setSecureSetting(mProfileAdminRcvr, Settings.Secure.SKIP_FIRST_USE_HINTS, "1");
    
    }