C# equivalent of this Java code in Xamarin

Hi all,

I'm trying to implement the Java code from this blog-post: http://www-jo.se/f.pfleger/android-package-size

PackageManager pm = getPackageManager();

Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);

getPackageSizeInfo.invoke(pm, "com.android.mms", new IPackageStatsObserver.Stub() {

    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
        throws RemoteException {

        Log.i(TAG, "codeSize: " + pStats.codeSize);
    }
});

I've succesfully gotten the method using Class.GetMethod, Class.FromType and Class.ForName methods.

var getPackageSizeInfo = context.PackageManager.Class.GetMethod("getPackageSizeInfo",
    Java.Lang.Class.FromType(typeof (Java.Lang.String)),
    Java.Lang.Class.ForName("android.content.pm.IPackageStatsObserver"));

var ipso = new MyPackageStatsObserver();

var info = getPackageSizeInfo.Invoke(context.PackageManager, new Java.Lang.String(p.PackageName), ipso);

However I'm stuck at the IPackageStatsObserver.Stub part. This is a static abstract class which is part of the IPackageStatsObserver Java interface. How can I create an implementation of this class? I've tried something along the lines of:

[Register("android/content/pm/IPackageStatsObserver", DoNotGenerateAcw = true)]
public interface IPackageStatsObserver : IJavaObject
{
}

[Register("android/content/pm/IPackageStatsObserver$Stub", DoNotGenerateAcw = true)]
public abstract class PackageStatsObserverStub : Object, IPackageStatsObserver
{
}

[Register("MyPackageStatsObserver")]
public class MyPackageStatsObserver : PackageStatsObserverStub
{
    [Register("onGetStatsCompleted", "(Landroid/content/pm/PackageStats;Ljava/lang/String;)V", "")]
    public void OnGetStatsCompleted(PackageStats pStats, bool succeeded)
    {
    }
}

But this produces the following compile error: error: package android.content.pm.IPackageStatsObserver does not exist

The generated Java-code seems legit:

public class MyPackageStatsObserver
    extends android.content.pm.IPackageStatsObserver.Stub
    implements
        mono.android.IGCUserPeer
{
    ..snip..
}

Posts

  • DevillersDevillers USMember

    Managed to get a working solution with generated code from a Android Interface Definition Language based file of IPackageStatsObserver wrapped with a IInvocationHandler-based proxy.

Sign In or Register to comment.