android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java        |  
 46 ---
 android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java |  
134 ++++++----
 2 files changed, 87 insertions(+), 93 deletions(-)

New commits:
commit eab62f8ab7ecf455cec286bf52f049a3951448ab
Author:     Jan Holesovsky <ke...@collabora.com>
AuthorDate: Tue Mar 17 23:05:39 2020 +0100
Commit:     Jan Holesovsky <ke...@collabora.com>
CommitDate: Wed Mar 18 00:36:38 2020 +0100

    android: Move the code to RateAppController & simplify a bit.
    
    Change-Id: I03eb0dbce22b83bac2c3cfc06db1bff2faaac076
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/90677
    Tested-by: Jan Holesovsky <ke...@collabora.com>
    Reviewed-by: Jan Holesovsky <ke...@collabora.com>

diff --git 
a/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java 
b/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java
index de3f5da3b..4070f955f 100644
--- a/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java
+++ b/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java
@@ -410,25 +410,6 @@ public class LOActivity extends AppCompatActivity {
         }
     }
 
-    /** opens up the app page on Google Play */
-    private void openInGooglePlay() {
-        String marketUri = String.format("market://details?id=%1$s", 
getPackageName());
-        String webUri = 
String.format("https://play.google.com/store/apps/details?id=%1$s";, 
getPackageName());
-
-        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketUri));
-        if (getPackageManager().queryIntentActivities(intent, 0).size() <= 0) {
-            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUri));
-            if (getPackageManager().queryIntentActivities(intent, 0).size() <= 
0) {
-                intent = null;
-            }
-        }
-
-        if (intent != null) {
-            rateAppController.updateStatus();
-            startActivity(intent);
-        }
-    }
-
     @Override
     protected void onNewIntent(Intent intent) {
 
@@ -855,31 +836,8 @@ public class LOActivity extends AppCompatActivity {
                     }
                     else if (message.startsWith("'statusindicatorfinish:") || 
message.startsWith("'error:")) {
                         mProgressDialog.dismiss();
-                        if (BuildConfig.GOOGLE_PLAY_ENABLED && 
rateAppController != null && rateAppController.shouldAsk()) {
-                            android.app.AlertDialog.Builder builder = new 
android.app.AlertDialog.Builder(LOActivity.this);
-                            final View rateAppLayout = 
getLayoutInflater().inflate(R.layout.rate_app_layout, null);
-                            builder.setView(rateAppLayout);
-                            RatingBar ratingBar = 
rateAppLayout.findViewById(R.id.ratingBar);
-
-                            
builder.setPositiveButton(getString(R.string.rate_now), new 
DialogInterface.OnClickListener() {
-                                @Override
-                                public void onClick(DialogInterface dialog, 
int which) {
-                                    // start google play activity for rating
-                                    openInGooglePlay();
-                                }
-                            });
-                            
builder.setNegativeButton(getString(R.string.later), null);
-                            final AlertDialog alertDialog = builder.create();
-                            ratingBar.setOnRatingBarChangeListener(new 
RatingBar.OnRatingBarChangeListener() {
-                                @Override
-                                public void onRatingChanged(RatingBar 
ratingBar1, float v, boolean b) {
-                                    // start google play activity for rating
-                                    openInGooglePlay();
-                                    alertDialog.dismiss();
-                                }
-                            });
-                            alertDialog.show();
-                        }
+                        if (BuildConfig.GOOGLE_PLAY_ENABLED && 
rateAppController != null)
+                            rateAppController.askUserForRating();
                     }
                 }
             });
diff --git 
a/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java 
b/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java
index 5b819ff33..57a7717f0 100644
--- 
a/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java
+++ 
b/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java
@@ -9,78 +9,114 @@
 
 package org.libreoffice.androidlib;
 
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.net.Uri;
+import android.view.View;
+import android.widget.RatingBar;
+
+import androidx.appcompat.app.AlertDialog;
+
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 
+/** Class to take care of reminding user that it is a good idea to rate the 
app. */
 public class RateAppController {
     private static String RATE_ASK_COUNTER_KEY = "RATE_ASK_COUNTER";
     private static String RATE_COUNTER_LAST_UPDATE_KEY = 
"RATE_COUNTER_LAST_UPDATE_DATE";
-    /** 1=POSTPONED, 2=RATED */
-    private static String RATE_ASK_STATUS_KEY = "RATE_ASK_STATUS";
-    LOActivity mActivity;
-    private int counter;
-    private Date lastDate;
-    private int status;
+    private static String RATE_ALREADY_RATED_KEY = "RATE_ALREADY_RATED";
+
+    private LOActivity mActivity;
 
     RateAppController(LOActivity activity) {
         this.mActivity = activity;
+    }
+
+    /** Opens up the app page in Google Play. */
+    private void openInGooglePlay() {
+        String marketUri = String.format("market://details?id=%1$s", 
mActivity.getPackageName());
+        String webUri = 
String.format("https://play.google.com/store/apps/details?id=%1$s";, 
mActivity.getPackageName());
+
+        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketUri));
+        if (mActivity.getPackageManager().queryIntentActivities(intent, 
0).size() <= 0) {
+            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUri));
+            if (mActivity.getPackageManager().queryIntentActivities(intent, 
0).size() <= 0) {
+                intent = null;
+            }
+        }
 
-        if (mActivity.getPrefs().getInt(RateAppController.RATE_ASK_STATUS_KEY, 
-1) == -1) {
-            /** first time init */
-            Date date = new Date();
-            
mActivity.getPrefs().edit().putLong(RateAppController.RATE_COUNTER_LAST_UPDATE_KEY,
  date.getTime()).apply();
-            /** the status starts from 1 to postpone asking on the first start 
**/
-            
mActivity.getPrefs().edit().putInt(RateAppController.RATE_ASK_STATUS_KEY, 
1).apply();
-            
mActivity.getPrefs().edit().putInt(RateAppController.RATE_ASK_COUNTER_KEY, 
0).apply();
-            this.counter = 0;
-            this.lastDate = date;
-            this.status = 1;
-        } else {
-            this.status = 
mActivity.getPrefs().getInt(RateAppController.RATE_ASK_STATUS_KEY, 0);
-            this.counter = 
mActivity.getPrefs().getInt(RateAppController.RATE_ASK_COUNTER_KEY, 0);
-            long dateTime = 
mActivity.getPrefs().getLong(RateAppController.RATE_COUNTER_LAST_UPDATE_KEY,  
0);
-            this.lastDate = new Date(dateTime);
+        if (intent != null) {
+            mActivity.getPrefs().edit().putBoolean(RATE_ALREADY_RATED_KEY, 
true).apply();
+            mActivity.startActivity(intent);
         }
     }
 
+    /** Ask the user for rating from time to time (unless they've already 
rated, or it is not time yet. */
+    public void askUserForRating() {
+        if (!shouldAsk())
+            return;
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
+        final View rateAppLayout = 
mActivity.getLayoutInflater().inflate(R.layout.rate_app_layout, null);
+        builder.setView(rateAppLayout);
+
+        builder.setPositiveButton(R.string.rate_now, new 
DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                // start google play activity for rating
+                openInGooglePlay();
+            }
+        });
+        builder.setNegativeButton(R.string.later, null);
+
+        final AlertDialog alertDialog = builder.create();
+
+        RatingBar ratingBar = rateAppLayout.findViewById(R.id.ratingBar);
+        ratingBar.setOnRatingBarChangeListener(new 
RatingBar.OnRatingBarChangeListener() {
+            @Override
+            public void onRatingChanged(RatingBar ratingBar1, float v, boolean 
b) {
+                // start google play activity for rating
+                openInGooglePlay();
+                alertDialog.dismiss();
+            }
+        });
+        alertDialog.show();
+    }
+
     /**
      * The counter is incremented once in each day when a document is opened 
successfully
      * If the counter is 4 (meaning it's the 5th day, starting from 0), return 
true unless the user is already rated
      * When the dialog is dismissed, ask again in another 5 days
      */
-    public boolean shouldAsk() {
-        boolean ret = false;
-        /** if the status is 2, user is already rated (hopefully) so we don't 
have to do anything else */
-        if (this.status == 2)
-            return ret;
+    private boolean shouldAsk() {
+        // don't ask if the user has already rated
+        if (mActivity.getPrefs().getBoolean(RATE_ALREADY_RATED_KEY, false))
+            return false;
 
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
-        Date today = new Date();
-        if 
(!dateFormat.format(today).equals(dateFormat.format(this.lastDate))) {
-            if (this.counter == 4)
-                    ret = true;
+        final int COUNT_BETWEEN_RATINGS = 5;
 
-            updateCounter();
-        }
-        return ret;
-    }
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", 
Locale.US);
+        Date today = new Date();
+        long lastDate = 
mActivity.getPrefs().getLong(RateAppController.RATE_COUNTER_LAST_UPDATE_KEY, 0);
 
-    private void updateCounter() {
-        this.counter = ++this.counter % 5;
-        
mActivity.getPrefs().edit().putInt(RateAppController.RATE_ASK_COUNTER_KEY, 
this.counter).apply();
-        updateDate();
-    }
+        // don't ask if we have already asked and/or increased the countar 
today
+        if (dateFormat.format(today).equals(dateFormat.format(lastDate)))
+            return false;
 
-    private void updateDate() {
-        Date date = new Date();
-        this.lastDate = date;
-        
mActivity.getPrefs().edit().putLong(RateAppController.RATE_COUNTER_LAST_UPDATE_KEY,
 this.lastDate.getTime()).apply();
+        boolean ret = false;
+        int counter = mActivity.getPrefs().getInt(RATE_ASK_COUNTER_KEY, 0);
+        if (counter == COUNT_BETWEEN_RATINGS - 1)
+            ret = true;
 
-    }
+        // update the counter and date
+        mActivity.getPrefs().edit()
+            .putInt(RATE_ASK_COUNTER_KEY, (counter + 1) % 
COUNT_BETWEEN_RATINGS)
+            .putLong(RATE_COUNTER_LAST_UPDATE_KEY, today.getTime())
+            .apply();
 
-    /** This is called when the user clicked on rate now and this will make it 
never ask again */
-    public void updateStatus() {
-        this.status = 2;
-        
mActivity.getPrefs().edit().putInt(RateAppController.RATE_ASK_STATUS_KEY, 
this.status).apply();
+        return ret;
     }
 }
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to