Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin | Android | COSU Error

I am attempting to create a COSU device. It will be hardware sold by my company that runs a single application developed by us. I have looked through many tutorials in an attempt to set up a proof of concept for this.

I currently have the following.

In my AndroidManaifest.xml

Then later...

<application android:allowBackup="true" android:label="@string/app_name" android:keepScreenOn="true" >
    <receiver   android:name="DeviceAdmin"
                android:label="@string/sample_device_admin"
                android:description="@string/sample_device_admin_description"
                android:permission="android.permission.BIND_DEVICE_ADMIN">
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
        <device-admin>
            <uses-policies>
                <limit-password />
                <watch-login />
                <reset-password />
                <force-lock />
                <wipe-data />
            </uses-policies>
        </device-admin>
    </receiver>
    <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</application>

In 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 />
    </uses-policies>
</device-admin>

DeviceAdmin.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.App.Admin;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace CB.App
{
    public class DeviceAdmin : DeviceAdminReceiver
    {

    }
}

And finally the OnCreate of MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;

        // Setup the collection and register implementations
        IServiceCollection coll = new ServiceCollection();
        RegisterDependencies(coll);

        // Build the global services
        ServiceRegistrationHelper.RegisterCoreServices(coll);

        // Register the provide with the GlobalServices
        GlobalServices.RegisterServiceProvider(coll.BuildServiceProvider(new ServiceProviderOptions()));

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager)this.GetSystemService(Activity.DevicePolicyService);
        // get this app package name
        ComponentName mDPM = new ComponentName(this, typeof(DeviceAdmin).Name);

        if( myDevicePolicyManager.IsDeviceOwnerApp(this.PackageName))
        {
            //myDevicePolicyManager.ClearDeviceOwnerApp(this.PackageName);

            String[] packages = { this.PackageName };
            myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);
            StartLockTask();
            SetUpClock();
            DisplayHome();
        }
        else
        {
            Toast.MakeText(this.ApplicationContext, "Not Owner", ToastLength.Long).Show();
        }
    }

In the ADB Shell I ran the command dpm set-device-owner com.companyname.productname/.DeviceAdmin and received Success: Device owner set to package componentinfo{com.companyname.productname/com.companyname.productname.DeviceAdmin} Active admin set to component {com.companyname.productname/com.companyname.productename.DeviceAdmin}

It builds and deploys but when it gets to the line

myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);

It throws the error Java.Lang.SecurityException: No active admin ComponentInfo(com.lathem.cumberland/DeviceAdmin

What am I missing?

like image 864
Link Avatar asked Aug 13 '18 17:08

Link


People also ask

Why isn't my Xamarin app compiling?

The build log will contain the output from the native compiler, explaining why the code isn't compiling. This is always a bug in Xamarin.iOS; please file a new issue on github with your project or a test case.

What does this error indicate in Xamarin iOS?

This error indicates a bug in Xamarin.iOS. Please file a new issue on github. MT4125: The registrar found an invalid type '*' in signature for method '*': The interface must have a Protocol attribute specifying its wrapper type. MT4126: Cannot register two managed protocols ('*' and '*') with the same native name ('*').

Is it possible to develop Xamarin on Linux?

Update: Xamarin on Linux was too much of a moving target. These instructions are obsolete. Thanks to the awesome guys at JetBrains and their Project Rider IDE, it’s now possible ( unofficially) to develop Xamarin.Android apps on Linux. This is a basic project created with Project Rider and running on the emulator:

Which API level is used at compile time by Xamarin?

This API level is used at compile time by Xamarin.Android. Minimum Android Version – Specifies the oldest Android version that you want your app to support. This API level is used at run time by Android. Target Android Version – Specifies the version of Android that your app is intended to run on. This API level is used at run time by Android.


1 Answers

Remove the <device-admin> section from your receiver, this should be referenced via a separate xml resource, either via hard-coding the meta-data section in the receiver's manifest section or via class attributes:

[BroadcastReceiver(
    Name = "com.sushihangover.cosu.DeviceAdminReceiver", 
    Label = "StackOverflow",
    Permission = "android.permission.BIND_DEVICE_ADMIN",
    Exported = true
)]
[MetaData("android.app.device_admin", Resource = "@xml/device_admin_receiver")]
[IntentFilter(new[] { 
    "android.intent.action.DEVICE_ADMIN_ENABLE", 
    "android.intent.action.PROFILE_PROVISIONING_COMPLETE", 
    "android.intent.action.BOOT_COMPLETED" 
})]
public class MyDeviceAdminReceiver : DeviceAdminReceiver {}

Use the Java class name, not the C# name:

ComponentName mDPM = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdminReceiver)));

Note: Based upon my use of the Java name for my DeviceAdminReceiver subclass, the dpm set-device-owner would be:

adb shell dpm set-device-owner com.sushihangover.cosu/com.sushihangover.cosu.DeviceAdminReceiver
like image 65
SushiHangover Avatar answered Oct 21 '22 20:10

SushiHangover