From 914a157afc1f904817e8082be188a75bc82ebdc3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 8 Oct 2020 22:32:43 -0700 Subject: [PATCH] Implement rating flow --- app/build.gradle | 3 +- .../traccar/client/GoogleMainApplication.java | 39 +++++++++++++++ .../org/traccar/client/ServiceReceiver.java | 49 +++++++++++++++++++ .../org/traccar/client/MainApplication.java | 5 ++ .../java/org/traccar/client/MainFragment.java | 1 + .../org/traccar/client/TrackingService.java | 7 ++- build.gradle | 2 +- 7 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 app/src/google/java/org/traccar/client/ServiceReceiver.java diff --git a/app/build.gradle b/app/build.gradle index 147c2f4b..e0aa376c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,7 +46,7 @@ android { } dependencies { - implementation 'com.google.android.material:material:1.3.0-alpha02' + implementation 'com.google.android.material:material:1.3.0-alpha03' implementation 'androidx.multidex:multidex:2.0.1' implementation 'androidx.preference:preference:1.1.1' testImplementation 'junit:junit:4.13' @@ -55,6 +55,7 @@ dependencies { googleImplementation 'com.google.firebase:firebase-crashlytics-gradle:2.3.0' googleImplementation 'com.google.firebase:firebase-analytics:17.5.0' googleImplementation 'com.google.android.gms:play-services-location:17.1.0' + googleImplementation 'com.google.android.play:core:1.8.2' } if (getGradle().getStartParameter().getTaskRequests().toString().contains('Google')) { diff --git a/app/src/google/java/org/traccar/client/GoogleMainApplication.java b/app/src/google/java/org/traccar/client/GoogleMainApplication.java index fe59fa85..058919ba 100644 --- a/app/src/google/java/org/traccar/client/GoogleMainApplication.java +++ b/app/src/google/java/org/traccar/client/GoogleMainApplication.java @@ -15,16 +15,55 @@ */ package org.traccar.client; +import android.app.Activity; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.os.Build; + +import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; +import androidx.preference.PreferenceManager; + +import com.google.android.play.core.review.ReviewManager; +import com.google.android.play.core.review.ReviewManagerFactory; +import com.google.android.play.core.tasks.Task; import com.google.firebase.analytics.FirebaseAnalytics; public class GoogleMainApplication extends MainApplication { + private static final String KEY_RATING_SHOWN = "ratingShown"; + private static final long RATING_THRESHOLD = -24 * 60 * 60 * 1000L; + private FirebaseAnalytics firebaseAnalytics; @Override public void onCreate() { super.onCreate(); firebaseAnalytics = FirebaseAnalytics.getInstance(this); + + IntentFilter filter = new IntentFilter(); + filter.addAction(TrackingService.ACTION_STARTED); + filter.addAction(TrackingService.ACTION_STOPPED); + registerReceiver(new ServiceReceiver(), filter); + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) + @Override + public void handleRatingFlow(@NonNull Activity activity) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + boolean ratingShown = preferences.getBoolean(KEY_RATING_SHOWN, false); + long totalDuration = preferences.getLong(ServiceReceiver.KEY_DURATION, 0); + if (!ratingShown && totalDuration > RATING_THRESHOLD) { + ReviewManager reviewManager = ReviewManagerFactory.create(activity); + reviewManager.requestReviewFlow().addOnCompleteListener(infoTask -> { + if (infoTask.isSuccessful()) { + Task flow = reviewManager.launchReviewFlow(activity, infoTask.getResult()); + flow.addOnCompleteListener(flowTask -> { + preferences.edit().putBoolean(KEY_RATING_SHOWN, true).apply(); + }); + } + }); + } } } diff --git a/app/src/google/java/org/traccar/client/ServiceReceiver.java b/app/src/google/java/org/traccar/client/ServiceReceiver.java new file mode 100644 index 00000000..b37e1d43 --- /dev/null +++ b/app/src/google/java/org/traccar/client/ServiceReceiver.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.client; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; + +import androidx.preference.PreferenceManager; + +public class ServiceReceiver extends BroadcastReceiver { + + public static final String KEY_DURATION = "serviceTime"; + + private static long startTime = 0; + + @Override + public void onReceive(Context context, Intent intent) { + if (TrackingService.ACTION_STARTED.equals(intent.getAction())) { + startTime = System.currentTimeMillis(); + } else { + if (startTime > 0) { + updateTime(context, System.currentTimeMillis() - startTime); + startTime = 0; + } + } + } + + private void updateTime(Context context, long duration) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + long totalDuration = preferences.getLong(KEY_DURATION, 0); + preferences.edit().putLong(KEY_DURATION, totalDuration + duration).apply(); + } + +} diff --git a/app/src/main/java/org/traccar/client/MainApplication.java b/app/src/main/java/org/traccar/client/MainApplication.java index a864aba6..43416c81 100644 --- a/app/src/main/java/org/traccar/client/MainApplication.java +++ b/app/src/main/java/org/traccar/client/MainApplication.java @@ -16,6 +16,7 @@ package org.traccar.client; import android.annotation.TargetApi; +import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -26,6 +27,7 @@ import android.os.Build; import android.preference.PreferenceManager; +import androidx.annotation.NonNull; import androidx.multidex.MultiDexApplication; public class MainApplication extends MultiDexApplication { @@ -71,4 +73,7 @@ private void migrateLegacyPreferences(SharedPreferences preferences) { } } + public void handleRatingFlow(@NonNull Activity activity) { + } + } diff --git a/app/src/main/java/org/traccar/client/MainFragment.java b/app/src/main/java/org/traccar/client/MainFragment.java index 6b7bf0d5..3248060b 100644 --- a/app/src/main/java/org/traccar/client/MainFragment.java +++ b/app/src/main/java/org/traccar/client/MainFragment.java @@ -218,6 +218,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin } else { stopTrackingService(); } + ((MainApplication) getActivity().getApplication()).handleRatingFlow(getActivity()); } else if (key.equals(KEY_DEVICE)) { findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null)); } diff --git a/app/src/main/java/org/traccar/client/TrackingService.java b/app/src/main/java/org/traccar/client/TrackingService.java index 83b3cffe..0cc84468 100644 --- a/app/src/main/java/org/traccar/client/TrackingService.java +++ b/app/src/main/java/org/traccar/client/TrackingService.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,9 @@ public class TrackingService extends Service { + public static final String ACTION_STARTED = "org.traccar.action.SERVICE_STARTED"; + public static final String ACTION_STOPPED = "org.traccar.action.SERVICE_STOPPED"; + private static final String TAG = TrackingService.class.getSimpleName(); private static final int NOTIFICATION_ID = 1; @@ -82,6 +85,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { @Override public void onCreate() { Log.i(TAG, "service create"); + sendBroadcast(new Intent(ACTION_STARTED)); StatusActivity.addMessage(getString(R.string.status_service_create)); startForeground(NOTIFICATION_ID, createNotification(this)); @@ -120,6 +124,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { @Override public void onDestroy() { Log.i(TAG, "service destroy"); + sendBroadcast(new Intent(ACTION_STOPPED)); StatusActivity.addMessage(getString(R.string.status_service_destroy)); stopForeground(true); diff --git a/build.gradle b/build.gradle index 10e3e1e0..d66a6989 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:4.0.2' classpath 'com.google.gms:google-services:4.3.3' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0' }