Android Open Source - sensorreadout App Rater






From Project

Back to project page sensorreadout.

License

The source code is released under:

Apache License

If you think the Android project sensorreadout 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

package de.onyxbits.sensorreadout;
/*from w  w  w. j  a  v  a2 s. c  o m*/
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

/**
 * Ask the user about reviewing the app on Google Play after a reasonable
 * amount of time and launches.
 */
class AppRater implements DialogInterface.OnClickListener {

  private static final int DAYS_UNTIL_PROMPT = 7;
  private static final int LAUNCHES_UNTIL_PROMPT = 14;
  private static final String PREFSFILE = "apprater";

  private Context context;

  private AppRater(Context context) {
    this.context = context;
  }

  /**
   * Should be called on every <code>Activity.onCreate()</code>. Checks if the
   * trial period is over and if so brings up a notification dialog.
   * 
   * @param ctx
   *          The application context.
   */
  public static void appLaunched(Context ctx) {
    try {
      // Don't even bother if not installed from Google Play
      PackageManager pm = ctx.getPackageManager();
      String pn = ctx.getPackageName();
      String inst = pm.getInstallerPackageName(pn);
      if (inst == null || !(inst.startsWith("com.google")
          || inst.startsWith("com.android"))) {
        return;
      }
    }
    catch (Exception exp) {
      Log.w("AppRater",exp);
      // Just bailout. Don't even try recovery.
      return;
    }

    SharedPreferences prefs = ctx.getSharedPreferences(PREFSFILE, 0);
    if (prefs.getBoolean("dontshowagain", false)) {
      return;
    }

    SharedPreferences.Editor editor = prefs.edit();

    // Increment launch counter
    long launch_count = prefs.getLong("launch_count", 0) + 1;
    editor.putLong("launch_count", launch_count);

    // Get date of first launch
    Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
    if (date_firstLaunch == 0) {
      date_firstLaunch = System.currentTimeMillis();
      editor.putLong("date_firstlaunch", date_firstLaunch);
    }
    editor.commit();

    // Wait at least n days before opening
    if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
      if (System.currentTimeMillis() >= date_firstLaunch
          + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
        showRateDialog(ctx);
      }
    }
  }

  /**
   * Just bring up the rating dialog without checking whether or not the trial
   * period has ended
   * 
   * @param ctx
   *          application context
   */
  public static void showRateDialog(Context ctx) {
    AppRater rater = new AppRater(ctx);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle(R.string.apprater_title);
    builder.setMessage(R.string.apprater_message);
    builder.setIcon(R.drawable.ic_rate);
    builder.setPositiveButton(R.string.apprater_positive, rater);
    builder.setNegativeButton(R.string.apprater_negative, rater);
    builder.setNeutralButton(R.string.apprater_neutral, rater);
    builder.create().show();
  }

  @Override
  public void onClick(DialogInterface di, int id) {
    SharedPreferences.Editor editor = context
        .getSharedPreferences(PREFSFILE, 0).edit();
    switch (id) {
      case DialogInterface.BUTTON_POSITIVE: {
        try {
          context.startActivity(new Intent(Intent.ACTION_VIEW, Uri
              .parse("market://details?id=" + context.getPackageName())));
        }
        catch (ActivityNotFoundException exp) {

        }
        // Fall through intended!
      }
      case DialogInterface.BUTTON_NEGATIVE: {
        editor.putBoolean("dontshowagain", true);
        break;
      }
      case DialogInterface.BUTTON_NEUTRAL:
      default: {
        // Did the user select "Later" because s/he does not have network access
        // right now or because s/he wants to extend the trial period? Let's
        // assume the later and reset the launch counter, as it is the less
        // pesky option.
        editor.putLong("launch_count", 0);
      }
    }
    editor.commit();
    di.dismiss();
  }
}




Java Source Code List

de.onyxbits.sensorreadout.AppRater.java
de.onyxbits.sensorreadout.ExportTask.java
de.onyxbits.sensorreadout.OverviewActivity.java
de.onyxbits.sensorreadout.ReadoutActivity.java
de.onyxbits.sensorreadout.SensorAdapter.java
de.onyxbits.sensorreadout.Ticker.java
org.achartengine.ChartFactory.java
org.achartengine.GraphicalActivity.java
org.achartengine.GraphicalView.java
org.achartengine.ITouchHandler.java
org.achartengine.TouchHandlerOld.java
org.achartengine.TouchHandler.java
org.achartengine.chart.AbstractChart.java
org.achartengine.chart.BarChart.java
org.achartengine.chart.BubbleChart.java
org.achartengine.chart.ClickableArea.java
org.achartengine.chart.CombinedXYChart.java
org.achartengine.chart.CubicLineChart.java
org.achartengine.chart.DialChart.java
org.achartengine.chart.DoughnutChart.java
org.achartengine.chart.LineChart.java
org.achartengine.chart.PieChart.java
org.achartengine.chart.PieMapper.java
org.achartengine.chart.PieSegment.java
org.achartengine.chart.PointStyle.java
org.achartengine.chart.RangeBarChart.java
org.achartengine.chart.RangeStackedBarChart.java
org.achartengine.chart.RoundChart.java
org.achartengine.chart.ScatterChart.java
org.achartengine.chart.TimeChart.java
org.achartengine.chart.XYChart.java
org.achartengine.model.CategorySeries.java
org.achartengine.model.MultipleCategorySeries.java
org.achartengine.model.Point.java
org.achartengine.model.RangeCategorySeries.java
org.achartengine.model.SeriesSelection.java
org.achartengine.model.TimeSeries.java
org.achartengine.model.XYMultipleSeriesDataset.java
org.achartengine.model.XYSeries.java
org.achartengine.model.XYValueSeries.java
org.achartengine.renderer.BasicStroke.java
org.achartengine.renderer.DefaultRenderer.java
org.achartengine.renderer.DialRenderer.java
org.achartengine.renderer.SimpleSeriesRenderer.java
org.achartengine.renderer.XYMultipleSeriesRenderer.java
org.achartengine.renderer.XYSeriesRenderer.java
org.achartengine.tools.AbstractTool.java
org.achartengine.tools.FitZoom.java
org.achartengine.tools.PanListener.java
org.achartengine.tools.Pan.java
org.achartengine.tools.ZoomEvent.java
org.achartengine.tools.ZoomListener.java
org.achartengine.tools.Zoom.java
org.achartengine.util.IndexXYMap.java
org.achartengine.util.MathHelper.java
org.achartengine.util.XYEntry.java