Market License Check


i have decided to help other develper by posting source code and examples on how to use it on my blog. this is an example of a simple license check activity.

package com.android.example.license check;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import com.android.vending.licensing.*;

public abstract class LicenseCheckActivity extends Activity {

static boolean licensed = true;
static boolean didCheck = false;
static boolean checkingLicense = false;
static final String BASE64_PUBLIC_KEY = ” ENTER YOU PUBLIC KEY HERE “;

LicenseCheckerCallback mLicenseCheckerCallback;
LicenseChecker mChecker;

Handler mHandler;

SharedPreferences prefs;

// REPLACE WITH YOUR OWN SALT , THIS IS FROM EXAMPLE
private static final byte[] SALT = new byte[]{
-45, 65, 30, -128, -25, -57, 74, -64, 56, 88, -85, -45, 77, -117, -25, -113, -12, 32, -64,
89
};

private void displayResult(final String result) {
mHandler.post(new Runnable() {
public void run() {

setProgressBarIndeterminateVisibility(false);

}
});
}

protected void doCheck() {

didCheck = false;
checkingLicense = true;
setProgressBarIndeterminateVisibility(true);

mChecker.checkAccess(mLicenseCheckerCallback);
}

protected void checkLicense()

Log.i(“LICENSE”, “checkLicense”);
mHandler = new Handler();

// Try to use more data here. ANDROID_ID is a single point of attack.
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

// Library calls this when it’s done.
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
// Construct the LicenseChecker with a policy.
mChecker = new LicenseChecker(
this, new ServerManagedPolicy(this,
new AESObfuscator(SALT, getPackageName(), deviceId)),
BASE64_PUBLIC_KEY);

//        mChecker = new LicenseChecker(
//                this, new StrictPolicy(),
//                BASE64_PUBLIC_KEY);

doCheck();
}

protected class MyLicenseCheckerCallback implements LicenseCheckerCallback {

public void allow() {
Log.i(“LICENSE”, “allow”);
if (isFinishing()) {
// Don’t update UI if Activity is finishing.
return;
}
// Should allow user access.
displayResult(getString(R.string.allow));
licensed = true;
checkingLicense = false;
didCheck = true;

}

public void dontAllow() {
Log.i(“LICENSE”, “dontAllow”);
if (isFinishing()) {
// Don’t update UI if Activity is finishing.
return;
}
displayResult(getString(R.string.dont_allow));
licensed = false;
checkingLicense = false;
didCheck = true;

showDialog(0);
}

public void applicationError(ApplicationErrorCode errorCode) {
Log.i(“LICENSE”, “error: ” + errorCode);
if (isFinishing()) {
// Don’t update UI if Activity is finishing.
return;
}
licensed = false;
// This is a polite way of saying the developer made a mistake
// while setting up or calling the license checker library.
// Please examine the error code and fix the error.
@SuppressWarnings(“unused”)
String result = String.format(getString(R.string.application_error), errorCode);
checkingLicense = false;
didCheck = true;

//displayResult(result);
showDialog(0);
}
}

protected Dialog onCreateDialog(int id) {
// We have only one dialog.
return new AlertDialog.Builder(this)
.setTitle(R.string.unlicensed_dialog_title)
.setMessage(R.string.unlicensed_dialog_body)
.setPositiveButton(R.string.buy_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
http://market.android.com/details?id=” + getPackageName()));
startActivity(marketIntent);
finish();
}
})
.setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})

.setCancelable(false)
.setOnKeyListener(new DialogInterface.OnKeyListener(){
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
Log.i(“License”, “Key Listener”);
finish();
return true;
}
})
.create();

}

@Override
protected void onDestroy() {
super.onDestroy();
if (mChecker != null) {
Log.i(“LIcense”, “distroy checker”);
mChecker.onDestroy();
}
}
}

The best way to use this is like so. If this license check activity is used offline it will not be able to check the license and assume the application isn’t licensed. Only wifi can be use because data connection can be extremely slow.

first off your activity need the extend the license check.

public class MainActivity extends LicenseCheckActivity{

this needs to be in the onCreate methor to check the license.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
checkLicense();
}

these values need to be added to the strings file.

<!– online license check –>
<string name=”check_license”>Check license</string>
<string name=”checking_license”>Checking license…</string>
<string name=”dont_allow”>Don\’t allow the user access</string>
<string name=”allow”>Allow the user access</string>
<string name=”application_error”>Application error: %1$s</string>
<!– Unlicensed dialog messages –>
<string name=”unlicensed_dialog_title”>Application not licensed</string>
<string name=”unlicensed_dialog_body”>This application is not licensed. Please purchase it from Android Market.</string>
<string name=”buy_button”>Buy app</string>
<string name=”quit_button”>Exit</string>

also the permissions that are needed in the manifest need to look like this.

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=”com.android.vending.CHECK_LICENSE” />

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: