QFieldAppRaterActivity.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package ch.opengis.vrahelper;
  2. import android.os.Bundle;
  3. import android.net.Uri;
  4. import android.app.Dialog;
  5. import android.app.AlertDialog;
  6. import android.app.Activity;
  7. import android.app.AlertDialog.Builder;
  8. import android.content.DialogInterface;
  9. import android.content.SharedPreferences;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.widget.LinearLayout;
  13. import android.widget.TextView;
  14. import android.widget.ImageView;
  15. import android.widget.Button;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.LayoutInflater;
  19. import android.util.Log;
  20. import android.graphics.drawable.ColorDrawable;
  21. public class QFieldAppRaterActivity extends Activity{
  22. private final static String APP_PNAME = "ch.opengis.vrahelper"; // Package Name
  23. private final static int DAYS_UNTIL_PROMPT = 30; // Min number of days
  24. private final static int LAUNCHES_UNTIL_PROMPT = 10; // Min number of launches
  25. private static final String TAG = "QField AppRater Activity";
  26. private SharedPreferences sharedPreferences;
  27. private SharedPreferences.Editor editor;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState){
  30. Log.d(TAG, "onCreate()");
  31. super.onCreate(savedInstanceState);
  32. sharedPreferences = getSharedPreferences("AppRater", Context.MODE_PRIVATE);
  33. if (sharedPreferences.getBoolean("DontShowAgain", false)) {
  34. Log.d(TAG, "Dont show again");
  35. finish();
  36. return;
  37. }
  38. editor = sharedPreferences.edit();
  39. // Increment launch counter
  40. long launchCount = sharedPreferences.getLong("LaunchCount", 0) + 1;
  41. Log.d(TAG, "Launch count: " + (launchCount - 1));
  42. editor.putLong("LaunchCount", launchCount);
  43. editor.commit();
  44. // Get date of first launch
  45. Long firstLaunch = sharedPreferences.getLong("FirstLaunch", 0);
  46. Log.d(TAG, "First launch: " + firstLaunch);
  47. if (firstLaunch == 0) {
  48. firstLaunch = System.currentTimeMillis();
  49. editor.putLong("FirstLaunch", firstLaunch);
  50. editor.commit();
  51. }
  52. // Wait at least n days before opening
  53. if (launchCount >= LAUNCHES_UNTIL_PROMPT) {
  54. if (
  55. false // never show the original rate dialog, we already have in QField UI
  56. && System.currentTimeMillis() >= firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)
  57. ) {
  58. showRateDialog();
  59. }else{
  60. finish();
  61. }
  62. }else{
  63. finish();
  64. }
  65. return;
  66. }
  67. public void showRateDialog() {
  68. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  69. Log.d(TAG, "showRateDialog()");
  70. LayoutInflater inflater = getLayoutInflater();
  71. // Inflate and set the layout for the dialog
  72. // Pass null as the parent view because its going in the dialog layout
  73. View view = inflater.inflate(R.layout.rate_dialog, null);
  74. builder.setView(view);
  75. builder.setTitle(getString(R.string.rate_title));
  76. builder.setMessage(getString(R.string.rate_message));
  77. // Set 5 stars image
  78. ImageView imgView = (ImageView) view.findViewById(R.id.image);
  79. imgView.setImageResource(R.drawable.five_stars);
  80. imgView.setOnClickListener(new View.OnClickListener(){
  81. public void onClick(View v) {
  82. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
  83. if (editor != null) {
  84. editor.putBoolean("DontShowAgain", true);
  85. editor.commit();
  86. }
  87. finish();
  88. }
  89. });
  90. builder.setPositiveButton(getString(R.string.rate_now), new DialogInterface.OnClickListener() {
  91. public void onClick(DialogInterface dialog, int id) {
  92. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
  93. if (editor != null) {
  94. editor.putBoolean("DontShowAgain", true);
  95. editor.commit();
  96. }
  97. finish();
  98. }
  99. });
  100. builder.setNeutralButton(getString(R.string.remind_later), new DialogInterface.OnClickListener() {
  101. public void onClick(DialogInterface dialog, int id) {
  102. finish();
  103. }
  104. });
  105. builder.setNegativeButton(getString(R.string.no_thanks), new DialogInterface.OnClickListener() {
  106. public void onClick(DialogInterface dialog, int id) {
  107. if (editor != null) {
  108. editor.putBoolean("DontShowAgain", true);
  109. editor.commit();
  110. }
  111. finish();
  112. }
  113. });
  114. AlertDialog dialog = builder.create();
  115. dialog.show();
  116. }
  117. }