0

Implementing application licensing is quite straight forward but it looks as though the example code is tailored for a full fledged android application and not a widget.

Like, in application, we mostly add the licensing code either in Splash screen or in Main activity and if user is not allowed then we just finish the activity and not allowed to use the application.

Similarly, in widget, can anyone throw some clue on where to add licensing code and what action to take if user is not allowed?

It will be great if someone can point to example.

Thank you in advance.

Veer
  • 2,071
  • 19
  • 24

2 Answers2

0

Add configuration activity to your widget and implement your licensing logic there. If you don't get a positive result, don't let them add the widget. You could do periodic license checks in your widget update service.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
0

Thanks Nikolay for pointing to Configuration Activity. That's what actually where the implementation of licensing service will go. Following is configuration activity with licensing service implementation:

package com.mobisys.android.contactwidget;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.appwidget.AppWidgetManager;
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;

public class ConfigActivity extends Activity {
    private LicenseChecker mChecker;
    private LicenseCheckerCallback mLicenseCheckerCallback;
    public static final String BASE64_PUBLIC_KEY = "PUBLIC KEY OF YOUR ACCOUNT";
    public static final byte[] SALT = new byte[] {
    -120, 30, 50, -20, 33, -100, 32, -90, -98, 104, 12,
    110, 78, -34, 105, 21, 62, 35, -12, 97
    };

    private AESObfuscator mObsfuscator;
    private String android_id;
    ProgressDialog pg;
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

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

        setResult(RESULT_CANCELED);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
             mAppWidgetId = extras.getInt(
                     AppWidgetManager.EXTRA_APPWIDGET_ID,
                     AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
             finish();
        }

        pg=ProgressDialog.show(this, "Verification", "Verifying purchase");
        pg.show();
        android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
        mObsfuscator = new AESObfuscator(SALT, getPackageName(), android_id);
        ServerManagedPolicy serverPolicy = new ServerManagedPolicy(this,mObsfuscator);

        mLicenseCheckerCallback = new MyLicenseCheckerCallback();
        mChecker = new LicenseChecker(
            this, serverPolicy,
            BASE64_PUBLIC_KEY  // Your public licensing key.
            );
        mChecker.checkAccess(mLicenseCheckerCallback);
    }

    private void onAllow(){
         Intent resultValue = new Intent();
         resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
         setResult(RESULT_OK, resultValue);
         finish();
    }

    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
        @Override
        public void allow(int reason) {
            if(pg!=null&&pg.isShowing())
                pg.dismiss();
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    onAllow();
                }

            });
        }

        @Override
        public void dontAllow(int reason) {
            if(pg!=null&&pg.isShowing())
                pg.dismiss();

            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }

            showDialog(0);
        }

        @Override
        public void applicationError(int errorCode) {
            Toast.makeText(ConfigActivity.this, "Application Error:"+errorCode, Toast.LENGTH_SHORT).show();
            if(pg!=null&&pg.isShowing())
                pg.dismiss();
            if (isFinishing()) return;

            finish();
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // We have only one dialog.
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
        .setTitle("Application Not Licensed")
        .setCancelable(false)
        .setMessage("This application is not licensed. Please purchase it from Google play")
        .setPositiveButton("Buy App", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,int which) {
                Intent marketIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("market://details?id=" + getPackageName()));
                    startActivity(marketIntent);
                    finish();
                }})
                .setNegativeButton("Exit",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                }
            }
        );

        return builder.create();
    }

}
Veer
  • 2,071
  • 19
  • 24