diff options
author | mert <mert.tumer@collabora.com> | 2020-03-13 21:21:14 +0300 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2020-03-18 00:36:05 +0100 |
commit | 76f4c6de3be7c8e70c29afcadd9a1b10ab6a34bd (patch) | |
tree | d27ab6e1c988bf3b4163711995cfd1384e2cd2b4 | |
parent | 876854d3ebb859d9bce870c9a80dd2c83b20e57d (diff) |
android: added a rating dialog
Change-Id: If1fed5bff1f7b607027d01a69d09de997fae8473
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/90479
Tested-by: Jan Holesovsky <kendy@collabora.com>
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
-rw-r--r-- | android/lib/build.gradle | 2 | ||||
-rw-r--r-- | android/lib/libSettings.gradle.in | 1 | ||||
-rw-r--r-- | android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java | 57 | ||||
-rw-r--r-- | android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java | 86 | ||||
-rw-r--r-- | android/lib/src/main/res/layout/rate_app_layout.xml | 28 | ||||
-rw-r--r-- | android/lib/src/main/res/values/strings.xml | 3 | ||||
-rw-r--r-- | configure.ac | 10 |
7 files changed, 186 insertions, 1 deletions
diff --git a/android/lib/build.gradle b/android/lib/build.gradle index 8af6e8a2a..ceac6b811 100644 --- a/android/lib/build.gradle +++ b/android/lib/build.gradle @@ -25,6 +25,7 @@ android { buildTypes { debug { buildConfigField "String", "GIT_COMMIT", "\"${liboGitFullCommit}\"" + buildConfigField "boolean", "GOOGLE_PLAY_ENABLED", "${liboGooglePlay}" ndk { abiFilters = [] abiFilters.addAll("${liboAndroidAbi}".split(' ').collect{it as String}) @@ -33,6 +34,7 @@ android { } release { buildConfigField "String", "GIT_COMMIT", "\"${liboGitFullCommit}\"" + buildConfigField "boolean", "GOOGLE_PLAY_ENABLED", "${liboGooglePlay}" ndk { abiFilters = [] abiFilters.addAll("${liboAndroidAbi}".split(' ').collect{it as String}) diff --git a/android/lib/libSettings.gradle.in b/android/lib/libSettings.gradle.in index d492e0389..3b6208fb7 100644 --- a/android/lib/libSettings.gradle.in +++ b/android/lib/libSettings.gradle.in @@ -10,4 +10,5 @@ ext { liboGitFullCommit = '@LOOLWSD_VERSION_HASH@' liboApplicationId = '@ANDROID_PACKAGE_NAME@' liboAndroidAbi = '@ANDROID_ABI@' + liboGooglePlay = '@APP_GOOGLE_PLAY@' } 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 0459b8db2..de3f5da3b 100644 --- a/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java +++ b/android/lib/src/main/java/org/libreoffice/androidlib/LOActivity.java @@ -10,11 +10,13 @@ package org.libreoffice.androidlib; import android.Manifest; +import android.app.AlertDialog; import android.content.ClipData; import android.content.ClipDescription; import android.content.ClipboardManager; import android.content.ActivityNotFoundException; import android.content.ContentResolver; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; @@ -43,6 +45,7 @@ import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; +import android.widget.RatingBar; import android.widget.TextView; import android.widget.Toast; @@ -65,7 +68,6 @@ import java.util.Map; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -101,6 +103,7 @@ public class LOActivity extends AppCompatActivity { private WebView mWebView; private SharedPreferences sPrefs; private Handler mMainHandler = null; + private RateAppController rateAppController; private boolean isDocEditable = false; private boolean isDocDebuggable = BuildConfig.DEBUG; @@ -263,6 +266,10 @@ public class LOActivity extends AppCompatActivity { setContentView(R.layout.lolib_activity_main); mProgressDialog = new ProgressDialog(this); + if (BuildConfig.GOOGLE_PLAY_ENABLED) + this.rateAppController = new RateAppController(this); + else + this.rateAppController = null; init(); } @@ -403,6 +410,25 @@ 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) { @@ -750,6 +776,10 @@ public class LOActivity extends AppCompatActivity { System.loadLibrary("androidapp"); } + public SharedPreferences getPrefs() { + return sPrefs; + } + /** * Initialize the LOOLWSD to load 'loadFileURL'. */ @@ -825,6 +855,31 @@ 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(); + } } } }); diff --git a/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java b/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java new file mode 100644 index 000000000..5b819ff33 --- /dev/null +++ b/android/lib/src/main/java/org/libreoffice/androidlib/RateAppController.java @@ -0,0 +1,86 @@ +/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.libreoffice.androidlib; + +import java.text.SimpleDateFormat; +import java.util.Date; + +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; + + RateAppController(LOActivity activity) { + this.mActivity = activity; + + 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); + } + } + + /** + * 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; + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); + Date today = new Date(); + if (!dateFormat.format(today).equals(dateFormat.format(this.lastDate))) { + if (this.counter == 4) + ret = true; + + updateCounter(); + } + return ret; + } + + private void updateCounter() { + this.counter = ++this.counter % 5; + mActivity.getPrefs().edit().putInt(RateAppController.RATE_ASK_COUNTER_KEY, this.counter).apply(); + updateDate(); + } + + private void updateDate() { + Date date = new Date(); + this.lastDate = date; + mActivity.getPrefs().edit().putLong(RateAppController.RATE_COUNTER_LAST_UPDATE_KEY, this.lastDate.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(); + } +} diff --git a/android/lib/src/main/res/layout/rate_app_layout.xml b/android/lib/src/main/res/layout/rate_app_layout.xml new file mode 100644 index 000000000..21e28be9e --- /dev/null +++ b/android/lib/src/main/res/layout/rate_app_layout.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" android:layout_width="match_parent" + android:layout_height="wrap_content"> + + + <TextView + android:id="@+id/textView2" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/rate_our_app_text" + android:padding="10dp" + android:textColor="@color/text_color_secondary"/> + + <RatingBar + android:id="@+id/ratingBar" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:indeterminate="false" + android:isIndicator="false" + android:max="5" + android:numStars="5" + android:rating="0" + android:stepSize="1" + android:layout_gravity="center" + android:layout_margin="12dp" + /> +</LinearLayout>
\ No newline at end of file diff --git a/android/lib/src/main/res/values/strings.xml b/android/lib/src/main/res/values/strings.xml index 6f0c87d5b..674c3b62e 100644 --- a/android/lib/src/main/res/values/strings.xml +++ b/android/lib/src/main/res/values/strings.xml @@ -6,6 +6,9 @@ <string name="cannot_open_file_chooser">Cannot open file chooser</string> <string name="saving">Saving...</string> <string name="preparing_for_the_first_start_after_an_update">Preparing for the first start after an update.</string> + <string name="rate_now">Rate now</string> + <string name="later">Later</string> + <string name="rate_our_app_text">Thank you for using our app! If you enjoy using it, please give us a 5-stars review in Google Play.</string> <!-- Loading SlideShow Dialog Strings --> <string name="loading">Loading...</string> diff --git a/configure.ac b/configure.ac index 3c34c153d..efd54b872 100644 --- a/configure.ac +++ b/configure.ac @@ -133,6 +133,10 @@ AC_ARG_ENABLE([androidapp], to work similarly to the iOS app, from the JavaScript and the pseudo WebSocket message plumbing point of view.])) +AC_ARG_ENABLE([android-google-play], + AS_HELP_STRING([--enable-android-google-play], + [When enabled, the app encoruages the user periodically to rate the app on Google Play Store.])) + AC_ARG_WITH(android-package-name, AS_HELP_STRING([--with-android-package-name="org.libreoffice.androidapp"], [Set Android package name of the build.]), @@ -958,6 +962,12 @@ AC_SUBST(APP_BRANDING_DIR) AC_SUBST(APP_IC_LAUNCHER) AC_SUBST(APP_HAS_BRANDING) +APP_GOOGLE_PLAY="false" +if test "$enable_android_google_play" = "yes"; then + APP_GOOGLE_PLAY="true" +fi +AC_SUBST(APP_GOOGLE_PLAY) + AS_IF([test "$ENABLE_IOSAPP" = "true"], [ if test `uname -s` = "Darwin"; then |