Created
January 21, 2016 23:21
-
-
Save SharpCoder/c1aa025bcc52a103d753 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.debuggle.kale; | |
| import java.util.Date; | |
| import android.app.AlertDialog; | |
| import android.content.Context; | |
| import android.content.DialogInterface; | |
| import android.content.DialogInterface.OnClickListener; | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| import com.debuggle.kalefree.R; | |
| public class TrialHelper { | |
| private static int getDemoDaysLeftOrStartDemo(Context ctx) { | |
| // Default value if anything fails. | |
| int daysleft = 21; | |
| // Get the first install date/time. | |
| try { | |
| long installed = ctx.getPackageManager().getPackageInfo("com.debuggle.kalefree", 0).firstInstallTime; | |
| // Get the days left. | |
| Date start = new Date(installed); | |
| Date end = new Date(); | |
| // Get the difference. | |
| long difference = end.getTime() - start.getTime(); | |
| // Determine how many days there are by dividing the difference by the total milliseconds in a day. | |
| daysleft = 21 - (int)( difference / (long)86400000 ); | |
| } catch ( Exception error ) { } | |
| // Return the value. | |
| return daysleft; | |
| } | |
| public static int GetDemoDaysLeft(Context ctx) { | |
| return getDemoDaysLeftOrStartDemo(ctx); | |
| } | |
| public static void ShowDemoWarning(Context ctx, final Runnable onsuccess, final Runnable onfail) { | |
| final Context context = ctx; | |
| final int demoDays = getDemoDaysLeftOrStartDemo(ctx); | |
| // Check for silly cases. | |
| if ( demoDays > 22 ) { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(context); | |
| builder | |
| .setTitle(context.getText(R.string.msg_trial_title)) | |
| .setMessage(context.getText(R.string.msg_trial_failure)) | |
| .setCancelable(false) | |
| .setNegativeButton(context.getText(R.string.action_cancel), new OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| if ( onfail != null ) onfail.run(); | |
| } | |
| }); | |
| builder.create().show(); | |
| } else if ( demoDays < 20 ) { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(context); | |
| builder | |
| .setTitle(context.getText(R.string.msg_trial_title)) | |
| .setMessage(context.getText(R.string.msg_trial_msg).toString().replace("{0}", "" + Math.max(demoDays, 0))) | |
| .setPositiveButton(context.getText(R.string.txtUpgrade), new OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| Intent intent = new Intent(Intent.ACTION_VIEW); | |
| intent.setData(Uri.parse("market://details?id=com.debuggle.kale")); | |
| context.startActivity(intent); | |
| // This fixes a bug where backspacing from the play store makes the | |
| // app look broken. | |
| if ( onfail != null ) { | |
| onfail.run(); | |
| } | |
| } | |
| }) | |
| .setNegativeButton(context.getText(R.string.action_cancel), new OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialog, int which) { | |
| if ( demoDays >= 0 && onsuccess != null) { | |
| onsuccess.run(); | |
| } else if ( demoDays < 0 && onfail != null) { | |
| onfail.run(); | |
| } | |
| } | |
| }) | |
| .setCancelable(false); | |
| builder.create().show(); | |
| } else { | |
| // Don't bug the user for a few days. | |
| if ( onsuccess != null ) { | |
| onsuccess.run(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment