Market License - Easy Implementation to Protect Your Apps

Quickly share your Android Code Snippets here...

Market License - Easy Implementation to Protect Your Apps

Postby neubanks89 » Fri Aug 13, 2010 7:49 pm

I just finished license protecting all my apps using Google relatively new license service. I've developed an easy way to manage this in all your applications without having to mess with any of your apps actual source code.

Before you start, Make you have included the license library in your project as explained here:
http://developer.android.com/guide/publishing/licensing.html

1. Make a new Activity in your project called LicenseCheck.java

2. Paste the following in that activity:

Code: Select all
/**
* @author Nick Eubanks
*
* Copyright (C) 2010 Android Infinity (http://www.androidinfinity.com)
*
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Toast;

import com.android.vending.licensing.AESObfuscator;
import com.android.vending.licensing.LicenseChecker;
import com.android.vending.licensing.LicenseCheckerCallback;
import com.android.vending.licensing.ServerManagedPolicy;

/**
* NOTES ON USING THIS LICENSE FILE IN YOUR APPLICATION:
* 1. Define the package
* of you application above
* 2. Be sure your public key is set properly  @BASE64_PUBLIC_KEY
* 3. Change your SALT using random digits
* 4. Under AllowAccess, Add your previously used MainActivity
* 5. Add this activity to
* your manifest and set intent filters to MAIN and LAUNCHER
* 6. Remove Intent Filters from previous main activity
*/
public class LicenseCheck extends Activity {
   private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
      @Override
      public void allow() {
         if (isFinishing()) {
            // Don't update UI if Activity is finishing.
            return;
         }
         // Should allow user access.
         startMainActivity();

      }

      @Override
      public void applicationError(ApplicationErrorCode errorCode) {
         if (isFinishing()) {
            // Don't update UI if Activity is finishing.
            return;
         }
         // 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.
         toast("Error: " + errorCode.name());
         startMainActivity();

      }

      @Override
      public void dontAllow() {
         if (isFinishing()) {
            // Don't update UI if Activity is finishing.
            return;
         }

         // Should not allow access. In most cases, the app should assume
         // the user has access unless it encounters this. If it does,
         // the app should inform the user of their unlicensed ways
         // and then either shut down the app or limit the user to a
         // restricted set of features.
         // In this example, we show a dialog that takes the user to Market.
         showDialog(0);
      }
   }
   private static final String BASE64_PUBLIC_KEY = "PLACE YOUR BASE KEY FROM GOOGLE HERE";

   private static final byte[] SALT = new byte[] { INPUT 20 RANDOM INTEGERS HERE };
   private LicenseChecker mChecker;

   // A handler on the UI thread.

   private LicenseCheckerCallback mLicenseCheckerCallback;

   private void doCheck() {

      mChecker.checkAccess(mLicenseCheckerCallback);
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Try to use more data here. ANDROID_ID is a single point of attack.
      String deviceId = Secure.getString(getContentResolver(),
            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);
      doCheck();

   }

   @Override
   protected Dialog onCreateDialog(int id) {
      // We have only one dialog.
      return new AlertDialog.Builder(this)
            .setTitle("Application Not Licensed")
            .setCancelable(false)
            .setMessage(
                  "This application is not licensed. Please purchase it from Android Market")
            .setPositiveButton("Buy App",
                  new DialogInterface.OnClickListener() {
                     @Override
                     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("Exit",
                  new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog,
                           int which) {
                        finish();
                     }
                  }).create();
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      mChecker.onDestroy();
   }

   private void startMainActivity() {
      startActivity(new Intent(this, MainActivity.class));  //REPLACE MainActivity.class WITH YOUR APPS ORIGINAL LAUNCH ACTIVITY
      finish();
   }

   public void toast(String string) {
      Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
   }

}


3. Change the Base Key to the one google provided, Place 20 random integers in the SALT, Change MainActivity.class to the Main Activity of your application.

4. Update your Manifest File with the new activity
Code: Select all
<!-- Old Launch Activity Here -->
<activity android:label="@string/app_name" android:name=".MainActivity" />
<!-- New License Launch Activity with all intent filters from your previous main activity -->
<!-- Translucent.NoTitleBar is so that this activity is never shown to the user -->      
<activity android:label="@string/app_name" android:name=".LicenseCheck"
         android:theme="@android:style/Theme.Translucent.NoTitleBar">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>


5. Add Permission In the manifest Tag but not in the application tag
Code: Select all
    </application>
   <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
</manifest>




You're all done! Make sure you test it out before publishing.
neubanks89
Freshman
Freshman
 
Posts: 4
Joined: Sun Jan 24, 2010 3:06 am

Top

Re: Market License - Easy Implementation to Protect Your App

Postby sblantipodi » Fri Aug 13, 2010 8:13 pm

you cutted and pasted the google code,
google code is worse than the one written by a kid,
they aren't able to write good documentation also for their own product.

Those lines:
Code: Select all
Intent marketIntent = new Intent(
                              Intent.ACTION_VIEW,
                              Uri.parse("http://market.android.com/details?id=" + getPackageName()));


needs to be replaced with this:
Code: Select all
Intent marketIntent = new Intent(
                              Intent.ACTION_VIEW,
                              Uri.parse("market://details?id=" + getPackageName()));


The google lines asks the users how to manage the URI, it let open the URI using a browser (as a first choice) and with the market app.
If the user choose the first choice, he got a 404 error, really confusing and it really have no sense to use http:// instead of market://...

Using market:// is really more efficient and it don't create confusion on users, no confusion no selling loss.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Re: Market License - Easy Implementation to Protect Your App

Postby neubanks89 » Fri Aug 13, 2010 8:24 pm

I've never had a problem using Google's own implementation. I've also never had it open the browser, on several test devices with no "default" options set. This is also ONLY seen when a user doesn't have a license, so it's really only confusion pirates have to deal with. I'd stick with Google's code.
neubanks89
Freshman
Freshman
 
Posts: 4
Joined: Sun Jan 24, 2010 3:06 am

Re: Market License - Easy Implementation to Protect Your App

Postby sblantipodi » Fri Aug 13, 2010 8:36 pm

neubanks89 wrote:I've never had a problem using Google's own implementation. I've also never had it open the browser, on several test devices with no "default" options set. This is also ONLY seen when a user doesn't have a license, so it's really only confusion pirates have to deal with. I'd stick with Google's code.


Probably you don't understood the code you pasted:
the code I'm referring is refered to:
"This application is not licensed. Please purchase it from Android Market"

The buy button to be clear.

The buy button with your code is wrong since let the user open the URI with the browser getting the 404 error,
if you never experienced this error means that you haven't tryed your software on a different phone from your personal one.

I'm sorry for my rude speaking, I have no problem with you, I'm also puntalizing that google code is crappy, uncomplete, full of bugs and self incompatible.
So since you cutted and pasted it, I'm suggesting other people to use a working way and not the stupid one suggested by the google kids.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Re: Market License - Easy Implementation to Protect Your App

Postby fausteric » Sat Sep 25, 2010 12:44 pm

I did exactly what you did but I get an error on;

@Override
public void allow() {

Messaghe of eclipse: Remove @Override annotation.

I did include the Licensing Verification Library in the project.

Did I forget somthing?
fausteric
Freshman
Freshman
 
Posts: 2
Joined: Sat Sep 25, 2010 12:33 pm

Re: Market License - Easy Implementation to Protect Your App

Postby angelica » Wed Aug 21, 2013 7:35 am

Will it be effective in apps that are being promoted through direct mail?
"play games at iphysicsgames.com
angelica
Freshman
Freshman
 
Posts: 9
Joined: Sat Oct 20, 2012 3:36 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 5 guests