I suggest to add an App that will start at every system start to switch to a secondary user. The method require a version of Android 10 minimum.
Let's suppose that the secondary user is not admin and it's the only secondary user.
Source code:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.switchuser">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.switchuser.BootReceiver"
tools:ignore="Instantiatable"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
<receiver
android:name=".AppAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_receiver" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
res/xml/device_admin_receiver.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
<disable-keyguard-features />
</uses-policies>
</device-admin>
MainActivity.java
package com.example.switchuser;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.UserHandle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final int RESULT_ENABLE = 11;
private ComponentName compName;
private DevicePolicyManager manager;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
compName = new ComponentName(this, AppAdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Explain why we need this permission");
startActivityForResult(intent, RESULT_ENABLE);
manager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
# Get list of secondary users
List<UserHandle> listUsers = manager.getSecondaryUsers(compName);
#Switch to the first secondary user in the list
manager.switchUser(compName, listUsers.get(0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AppAdminReceiver.java
package com.example.switchuser;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
public class AppAdminReceiver extends DeviceAdminReceiver {
private static final String TAG = "AppAdminReceiver";
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onProfileProvisioningComplete(Context context, Intent intent) {
// Enable the profile
DevicePolicyManager manager =
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName componentName = this.getComponentName(context);
manager.setProfileName(componentName, "SwitchUser");
manager.setLockTaskPackages(componentName, new String[]{"com.example.switchuser"});
}
/**
* @return A newly instantiated {@link ComponentName} for this
* DeviceAdminReceiver.
*/
public static ComponentName getComponentName(Context context) {
return new ComponentName(context.getApplicationContext(), AppAdminReceiver.class);
}
@Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "Device Admin : enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "Device Admin : disabled", Toast.LENGTH_SHORT).show();
}
}
BootReceiver.java
package com.example.switchuser;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.UserHandle;
import androidx.annotation.RequiresApi;
import java.util.List;
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "Boot Receiver";
private ComponentName compName;
private DevicePolicyManager manager;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if (intent.getAction().equalsIgnoreCase(
Intent.ACTION_BOOT_COMPLETED)) {
compName = new ComponentName(context, AppAdminReceiver.class);
manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
List<UserHandle> listUsers = manager.getSecondaryUsers(compName);
manager.switchUser(compName, listUsers.get(0));
}
}
}
}
To enable the application as Device Owner in order have permissions to change the user, we need to use ADB:
adb root
adb shell dpm set-device-owner com.example.switchuser/.AppAdminReceiver
User restriction
In order to harden the user you can:
- Dump
/data/system/users/10.xml
where 10 is the id of the user. The file looks like this:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<user id="10" serialNumber="10" flags="16" created="1598034338552" lastLoggedIn="1598035769294" lastLoggedInFingerprint="Android/sdk_phone_x86_64/generic_x86_64:10/QPP6.190730.005.B1/5775370:userdebug/test-keys" icon="/data/system/users/10/photo.png" profileBadge="0">
<name>New user</name>
<restrictions no_sms="true" no_outgoing_calls="true" />
</user>
- Add more restrictions. The list of restrictions can be founded on UserManager Documentation
- push the changes
adb push 10.xml /data/system/users/10.xml
Reboot
Once the device start the application switch the user to the secondary user.
Tests
The app was tested on AVD (Android Virtual Device)
Source code on github