Implement rating flow
Этот коммит содержится в:
@@ -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<Void> flow = reviewManager.launchReviewFlow(activity, infoTask.getResult());
|
||||
flow.addOnCompleteListener(flowTask -> {
|
||||
preferences.edit().putBoolean(KEY_RATING_SHOWN, true).apply();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Ссылка в новой задаче
Block a user