Advertisement
Guest User

AppNag

a guest
Jun 7th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import android.app.Dialog;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.net.Uri;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11.  
  12. public class AppNag {
  13.      public final static String APP_TITLE = "Pencil Touch";
  14.         public final static String APP_PNAME = "com.sprucewerk.penciltouch";
  15.        
  16.         private final static int DAYS_UNTIL_PROMPT = 1;
  17.         private final static int LAUNCHES_UNTIL_PROMPT = 3;
  18.        
  19.         public static void app_launched(Context mContext) {
  20.             SharedPreferences prefs = mContext.getSharedPreferences("appnag", 0);
  21.            
  22.             SharedPreferences.Editor editor = prefs.edit();
  23.            
  24.             // Increment launch counter
  25.             long launch_count = prefs.getLong("launch_count", 0) + 1;
  26.             editor.putLong("launch_count", launch_count);
  27.  
  28.             // Get date of first launch
  29.             Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
  30.             if (date_firstLaunch == 0) {
  31.                 date_firstLaunch = System.currentTimeMillis();
  32.                 editor.putLong("date_firstlaunch", date_firstLaunch);
  33.             }
  34.            
  35.             // Wait at least n days before opening
  36.             if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
  37.                 if (System.currentTimeMillis() >= date_firstLaunch +
  38.                         (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
  39.                     showRateDialog(mContext, editor);
  40.                 }
  41.             }
  42.            
  43.             editor.commit();
  44.         }  
  45.        
  46.         public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
  47.             final Dialog dialog = new Dialog(mContext);
  48.             dialog.setTitle("Get " + APP_TITLE +" Pro. Now!");
  49.  
  50.             LinearLayout ll = new LinearLayout(mContext);
  51.             ll.setOrientation(LinearLayout.VERTICAL);
  52.            
  53.             TextView tv = new TextView(mContext);
  54.             tv.setText("If you enjoy using " + APP_TITLE +
  55.                     ", get the Pro version. Get more effects, more fun, more support, more updates...!");
  56.             tv.setWidth(240);
  57.             tv.setPadding(4, 0, 4, 10);
  58.             ll.addView(tv);
  59.            
  60.             Button b1 = new Button(mContext);
  61.             b1.setText("Get " + APP_TITLE+" Pro");
  62.             b1.setOnClickListener(new OnClickListener() {
  63.                 public void onClick(View v) {
  64.                     mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
  65.                     dialog.dismiss();
  66.                 }
  67.             });        
  68.             ll.addView(b1);
  69.  
  70.             Button b2 = new Button(mContext);
  71.             b2.setText("Next Time");
  72.             b2.setOnClickListener(new OnClickListener() {
  73.                 public void onClick(View v) {
  74.                     dialog.dismiss();
  75.                 }
  76.             });
  77.             ll.addView(b2);
  78.  
  79.             dialog.setContentView(ll);        
  80.             dialog.show();        
  81.         }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement