javaandroidreact-nativenative

Is there a way to get device owner access in a react native project?


I'm still quite new to programming and I need to make the app where I work act as a device owner so that it blocks other apps from being opened (the idea of ​​a device for an app), but with what I understand from documentation and AIs help (ChatGPT and ClaudeAI) I didn't have any success.

Maybe some useful information: react-native version: 0.64.4 SDK Version: 30

I'm really lost, any help would be greatly appreciated, thanks in advance.

So far what I have done is create an AdminReceiver.java class with this code:

    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
    }

I added the reference to this class in AndroidManifest.xml:

    <receiver
        android:name=".AdminReceiver"
        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>

So I created the file with the necessary (and some unnecessary) permissions inside the res/xml folder:

    <device-admin xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-policies>
            <disable-camera />
            <disable-applications />
            <force-lock />
            <limit-password />
            <watch-login />
            <reset-password />
            <wipe-data />
        </uses-policies>
    </device-admin>

And I added this to my MainApplication.java (but I believe it is being useless...):

  @Override
  public void onCreate() {
    super.onCreate();
    
    // Initialize SoLoader
    SoLoader.init(this, /* native exopackage */ false);
    
    // Dispatcher for Expo modules
    ApplicationLifecycleDispatcher.onApplicationCreate(this);

    // Detailed logging (optional)
    try {
      DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
      ComponentName adminComponent = new ComponentName(this, AdminReceiver.class);
      
      Log.d("MainApplication", "Is Device Owner: " + 
            dpm.isDeviceOwnerApp(getApplicationContext().getPackageName()));
      Log.d("MainApplication", "Is Admin Active: " + 
            dpm.isAdminActive(adminComponent));
    } catch (Exception e) {
      Log.e("MainApplication", "Error checking device admin status", e);
    }
  }

Response text when I tried to use the command

adb shell dpm set-device-owner com.myGroup.myPackage/.AdminReceiver:

Error: Unknow admin: ComponentInfo{com.myGroup.myPackage/com.myGroup.myPackage.AdminReceiver}


Solution

  • After some reasearchs i found the issue, it's basically because atfter android 9 it need to have this format in the adb command:

    adb shell dpm set-device-owner com.myGroup.myPackage/com.myGroup.myPackage.AdminReceiver

    and not

    adb shell dpm set-device-owner com.myGroup.myPackage/.AdminReceiver