I am suggesting two solutions. Both of them require root access, and both were tested on a OnePlus 6 running Android 10.
Solution #1
Have the following script (let's call it switch-user.sh) run during boot but after the user has entered device decryption credentials (PIN/password)
#!/system/bin/sh
while true; do
flag=/system/bin/getprop vold.fbe.decrypted
if [ $flag ]; then
sleep 1;
/system/bin/am switch-user USER_ID # replace USER_ID term with restricted user's id
exit;
else
sleep 1;
fi
done
What this script does is to continuously check (with a delay of 1 second) whether the user has decrypted the device or not. If yes, switch to the restricted user after one second, or continue to recheck. Since my restricted user's User Id is 10
, I used 10 as USER_ID
in it. Change it to whatever User Id your restricted user has. See my answer here if you need help in finding out the User Id.
Note: In the above script I am checking for the value of the property vold.fbe.decrypted
which works for my OnePlus 6 running Android 10. Your device may or may not have the same property. You can hunt for the relevant property by looking into the output of the command adb shell 'getprop | grep vold'
. Alternatively, see if sys.user.0.ce_available
property is available in your Android version.
Next thing to do is to put that script into a location in your Android from where the system would execute it on its own during boot. Since I use Magisk, I placed that switch-user.sh script into the directory /data/adb/service.d
(see Magisk's Boot Scripts section for further information on it) and added executable permission to it using the command:
adb shell su -c 'chmod +x /data/adb/service.d/switch-user.sh'
If you do not use Magisk, you can take the help from the answers at How can I run a script on boot?
Restart the device to see if your script works. In my case, whenever I enter my PIN to decrypt the device, than within two-three seconds the system automatically switches to my restricted user.
Solution #2
If you don't want the hassle of injecting a script into boot process you can setup an automation app which would switch the Android to your restricted user once it receives BOOT_COMPLETED broadcast, or in other words, do something during startup.
I can suggest Tasker and MacroDroid as I have tried them and they worked on Genymotion with Android 6.0 and on a OnePlus 6 with Android 10. You can try any app which can run a command/script on startup.
Tasker
For Tasker, the profile and the task should be this:
Profile: Event → Device Boot
Task: Code → Run Shell:
- Command:
su 2000 -c am switch-user USER_ID
- tick Use Root
- leave the rest of the settings untouched
Make sure to replace USER_ID
there with the actual user id of the restricted user.
MacroDroid
For MacroDroid, the Macro should be this:
Trigger: Device Events → Device Boot
Action: Applications → Shell Script:
- tick Rooted
- Enter the following command in "Enter script" field:
su 2000 -c am switch-user USER_ID
- leave the rest of the settings untouched
Make sure to replace USER_ID
there with the actual user id of the restricted user. See my answer here if you need help in finding out the User Id.
Note: because many apps would be competing to run their XYZ things during startup, you might notice a delay of a couple of seconds before user switching takes place.