0

I have been trying to add licensing support to my app and I am not getting any errors, however the logcat says it is checking the license but it doesn't do anything in the app. When the app opens up, the first thing it does is check the license. The last logcat entry I get is, "Received Response" and "Clearing timeout". None of the actual handling I have set up to do certain things if the response is NOT_LICENSED or LICENSED happens, which I have set up to log. It is probably some glaring issue that I haven't seen as I am a newer developer. Yes I have the library imported correctly.

public class MainActivity extends ListActivity {

public static final String PREFS_NAME = "MyPrefsFile";
private ArrayAdapter<String> adapter;
private ListView list;  
private Context c = this;
boolean home;
boolean school;

boolean licensed = false;
boolean didCheck = false;
boolean checkingLicense = false;

SharedPreferences userIsLicensed=null;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);    

    userIsLicensed = getSharedPreferences(PREFS_NAME, 0);    
    licensed = userIsLicensed.getBoolean("licensed", false);

    Toast.makeText(this, "Checking application license...", Toast.LENGTH_SHORT).show();
    // Check the license
    checkLicense();

//License Checking Part

    String BASE64_PUBLIC_KEY = "KEY";

    LicenseCheckerCallback mLicenseCheckerCallback;
    LicenseChecker mChecker;

    Handler mHandler;

    // REPLACE WITH YOUR OWN SALT , THIS IS FROM EXAMPLE
    private final byte[] SALT = new byte[]{BYTES};

    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);
        doCheck();
    }

    protected class MyLicenseCheckerCallback implements LicenseCheckerCallback {

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

            Log.i("License","Accepted!");
            // Should allow user access.
            displayResult(getString(R.string.allow));
            licensed = true;
            checkingLicense = false;
            didCheck = true;

            SharedPreferences.Editor editor = userIsLicensed.edit();
            editor.putBoolean("licensed", true);
            editor.commit();

            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);

        }

        @SuppressWarnings("deprecation")
        public void dontAllow() {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            Log.i("License","Denied!");
            displayResult(getString(R.string.dont_allow));
            licensed = false;
            // 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.
            checkingLicense = false;
            didCheck = true;

            SharedPreferences.Editor editor = userIsLicensed.edit();
            editor.putBoolean("licensed", false);
            editor.commit();

            showDialog(0);
        }

        @SuppressWarnings("deprecation")
        public void applicationError(ApplicationErrorReport 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);
        }

        @Override
        public void allow(int reason) {
            // TODO Auto-generated method stub

        }

        @Override
        public void dontAllow(int reason) {
            // TODO Auto-generated method stub

        }

        @Override
        public void applicationError(int errorCode) {
            // TODO Auto-generated method stub

        }
    }

    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();

    }
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Code_Insanity
  • 2,648
  • 3
  • 17
  • 21

1 Answers1

0

Well I finally figured it out, I had to override allow() and dontAllow() to get it to work!

Code_Insanity
  • 2,648
  • 3
  • 17
  • 21