If you think the Android project Tacere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (c) 2014 Jonathan Nelson/*www.java2s.com*/
* Released under the BSD license. For details see the COPYING file.
*/package org.ciasaboark.tacere.billing;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.view.ContextThemeWrapper;
import org.ciasaboark.tacere.BuildConfig;
import org.ciasaboark.tacere.R;
import org.ciasaboark.tacere.activity.ProUpgradeActivity;
publicclass Authenticator {
privatestaticfinal String TAG = "Authenticator";
privatestaticfinal String PREFERENCES_NAME = "licensing";
privatestaticfinal String KEY_PRO_VERSION = "pro";
privatefinal Context context;
private SharedPreferences prefs;
public Authenticator(Context ctx) {
if (ctx == null) {
thrownew IllegalArgumentException("Context can not be null");
}
this.context = ctx;
this.prefs = ctx.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_PRIVATE);
}
publicvoid deAuthenticate() {
prefs.edit().putBoolean(KEY_PRO_VERSION, false).commit();
}
publicvoid setAuthenticateSuccess(boolean isAuthenticated) {
prefs.edit().putBoolean(KEY_PRO_VERSION, isAuthenticated).commit();
}
publicboolean isAuthenticated() {
boolean isDonationKeyInstalled = isDonationKeyInstalled();
boolean hasProVersionBeenPurchased = prefs.getBoolean(KEY_PRO_VERSION, false);
boolean isFDroidBuild = isFDroidBuild();
return isDonationKeyInstalled || hasProVersionBeenPurchased || isFDroidBuild;
}
privateboolean isDonationKeyInstalled() {
PackageManager manager = context.getPackageManager();
boolean keyIsInstalled = false;
if (BuildConfig.DEBUG) {
//if we are running in debug mode then don't bother checking the signatures, just go by
//whether the key is installed
try {
manager.getPackageInfo("org.ciasaboark.tacere.key", 0);
keyIsInstalled = true;
} catch (PackageManager.NameNotFoundException e) {
}
} else {
keyIsInstalled = manager.checkSignatures("org.ciasaboark.tacere",
"org.ciasaboark.tacere.key") == PackageManager.SIGNATURE_MATCH;
}
return keyIsInstalled;
}
privateboolean isFDroidBuild() {
boolean isFDroidBuild = false;
PackageManager manager = context.getPackageManager();
isFDroidBuild = manager.checkSignatures("org.ciasaboark.tacere", "org.fdroid.fdroid")
== PackageManager.SIGNATURE_MATCH;
return isFDroidBuild;
}
publicvoid showUpgradeDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(context, R.style.Dialog)
);
builder.setTitle("Upgrade to Pro");
builder.setMessage("This feature is available in the pro version of Tacere");
builder.setPositiveButton("Tell Me More", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Intent i = new Intent(context, ProUpgradeActivity.class);
context.startActivity(i);
}
});
builder.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
//nothing to do here
}
});
builder.show();
}
}