[mono-android] Does anyone have a code example for using Google Market Licensing with MonoDroid?
Mark Eaton
mark.eaton.me at gmail.com
Thu Jan 19 21:58:01 UTC 2012
Well I have made some progress here. I can get a pointer to a valid market
license instance.
Surprising I am enjoying this.
Now I need to implement the call back based on a Java interface:
"com/android/vending/licensing/LicenseCheckerCallback"
Below is my code example. Basically I am trying to create a C# wrapper
interface. To do this I am decorating this wrapper with an attribute. Then I
want to use a C# class that will implement this C# interface.
But am I on the right track here?
Even if you may not know the solution for this particular example but have
knowledge on creating a C# wrapper for a Java interface could be helpful.
I want to learn this as:
1. I prefer not to to learn Java. Although I find reading Java code quite
simple
2. There could be other Java libraries I might want to implement one day and
this will be a great start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace MarketLicensing
{
public class LicenseHelper
{
// Generate 20 random bytes, and put them here.
private byte[] Salt = new byte[] { 46, 65, 30, 128, 103, 57, 74, 64,
51, 88, 95, 45, 77, 117, 36, 113, 11, 32, 64, 89 };
private Java.Lang.String _base64PublicKey = new
Java.Lang.String("MyPublicKey");
private IntPtr _licensingInstance;
private LicenseCheckerCallback _licenseCheckerCallback;
public LicenseHelper(moonbase3.MainActivity activity)
{
// Get Android Device Id
var deviceId = new Java.Lang.String(
Android.Provider.Settings.Secure.GetString(activity.ContentResolver,
Android.Provider.Settings.Secure.AndroidId)).Handle;
// Library calls this when it's done.
_licenseCheckerCallback = new LicenseCheckerCallback();
// Construct the LicenseChecker with a policy.
var licensing =
JNIEnv.FindClass("com/android/vending/licensing/LicenseChecker");
var serverPolicy =
JNIEnv.FindClass("com/android/vending/licensing/ServerManagedPolicy");
var aesObfuscator =
JNIEnv.FindClass("com/android/vending/licensing/AESObfuscator");
var saltPointer = JNIEnv.NewArray<byte>(Salt);
var aesObfuscatorInstance = JNIEnv.CreateInstance(
aesObfuscator,
"([BLjava/lang/String;Ljava/lang/String;)V",
new JValue[] { new JValue(saltPointer), new JValue(new
Java.Lang.String(activity.PackageName).Handle), new JValue(deviceId) });
var serverPolicyInstance = JNIEnv.CreateInstance(
serverPolicy,
"(Landroid/content/Context;Lcom/android/vending/licensing/Obfuscator;)V",
new JValue[] { new JValue(activity.Handle), new
JValue(aesObfuscatorInstance) }
);
_licensingInstance = JNIEnv.CreateInstance(
licensing,
"(Landroid/content/Context;Lcom/android/vending/licensing/Policy;Ljava/lang/String;)V",
new JValue[] { new JValue(activity.Handle), new
JValue(serverPolicyInstance), new JValue(_base64PublicKey) }
);
//doCheck();
}
// ABOVE CODE ALL WORKS PERFECTLY
// HOWEVER AM I IMPLEMENTING THE BELOW CODE CORRECTLY????
// OR AM I WAY OFF TRACK??
[Android.Runtime.Register("com/android/vending/licensing/LicenseCheckerCallback",
DoNotGenerateAcw = true)]
public interface ILicenseCheckerCallback : IJavaObject
{
[Register("allow", Signature = "()V", DoNotGenerateAcw = true)]
void Allow();
}
public class LicenseCheckerCallback : Java.Lang.Object,
ILicenseCheckerCallback
{
public void Allow()
{
}
}
}
}
--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Does-anyone-have-a-code-example-for-using-Google-Market-Licensing-with-MonoDroid-tp5144555p5159094.html
Sent from the Mono for Android mailing list archive at Nabble.com.
More information about the Monodroid
mailing list