mirror of https://github.com/tasks/tasks
Use In-App Review API
parent
ce191d3325
commit
2aee91a04b
@ -0,0 +1,8 @@
|
||||
package org.tasks.play
|
||||
|
||||
import android.app.Activity
|
||||
import javax.inject.Inject
|
||||
|
||||
class PlayServices @Inject constructor() {
|
||||
fun requestReview(@Suppress("UNUSED_PARAMETER") activity: Activity) {}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.tasks.gtasks
|
||||
|
||||
import android.content.Context
|
||||
import com.google.android.gms.common.ConnectionResult
|
||||
import com.google.android.gms.common.GoogleApiAvailability.getInstance
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
|
||||
object PlayServices {
|
||||
fun isAvailable(context: Context) =
|
||||
getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS
|
||||
}
|
||||
@ -1,31 +1,36 @@
|
||||
package org.tasks.injection
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Lazy
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import org.tasks.gtasks.PlayServices
|
||||
import org.tasks.location.*
|
||||
import org.tasks.location.Geocoder
|
||||
import org.tasks.location.GeocoderMapbox
|
||||
import org.tasks.location.GoogleMapFragment
|
||||
import org.tasks.location.LocationService
|
||||
import org.tasks.location.LocationServiceAndroid
|
||||
import org.tasks.location.LocationServiceGooglePlay
|
||||
import org.tasks.location.MapFragment
|
||||
import org.tasks.location.OsmMapFragment
|
||||
import org.tasks.play.PlayServices
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class FlavorModule {
|
||||
@Provides
|
||||
fun getLocationService(
|
||||
@ApplicationContext context: Context,
|
||||
google: Lazy<LocationServiceGooglePlay>,
|
||||
android: Lazy<LocationServiceAndroid>
|
||||
): LocationService = if (PlayServices.isAvailable(context)) google.get() else android.get()
|
||||
android: Lazy<LocationServiceAndroid>,
|
||||
playServices: PlayServices,
|
||||
): LocationService = if (playServices.isAvailable()) google.get() else android.get()
|
||||
|
||||
@Provides
|
||||
fun getMapFragment(
|
||||
@ApplicationContext context: Context,
|
||||
osm: Lazy<OsmMapFragment>,
|
||||
google: Lazy<GoogleMapFragment>,
|
||||
): MapFragment = if (PlayServices.isAvailable(context)) google.get() else osm.get()
|
||||
playServices: PlayServices,
|
||||
): MapFragment = if (playServices.isAvailable()) google.get() else osm.get()
|
||||
|
||||
@Provides
|
||||
fun getGeocoder(mapbox: GeocoderMapbox): Geocoder = mapbox
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
package org.tasks.play
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import com.google.android.gms.common.ConnectionResult
|
||||
import com.google.android.gms.common.GoogleApiAvailability.getInstance
|
||||
import com.google.android.play.core.ktx.launchReview
|
||||
import com.google.android.play.core.ktx.requestReview
|
||||
import com.google.android.play.core.review.ReviewManagerFactory
|
||||
import com.todoroo.andlib.utility.DateUtilities.now
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import org.tasks.preferences.Preferences
|
||||
import org.tasks.time.DateTimeUtils.printTimestamp
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
class PlayServices @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val preferences: Preferences,
|
||||
) {
|
||||
fun isAvailable() =
|
||||
getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS
|
||||
|
||||
suspend fun requestReview(activity: Activity) {
|
||||
val now = now()
|
||||
val installCutoff = preferences.installDate + INSTALL_COOLDOWN
|
||||
val reviewCutoff = preferences.lastReviewRequest + REVIEW_COOLDOWN
|
||||
if (installCutoff > now || reviewCutoff > now) {
|
||||
Timber.d("wait for review request: install=${printTimestamp(installCutoff)} review=${printTimestamp(reviewCutoff)}")
|
||||
return
|
||||
}
|
||||
try {
|
||||
with(ReviewManagerFactory.create(context)) {
|
||||
val request = requestReview()
|
||||
launchReview(activity, request)
|
||||
preferences.lastReviewRequest = now
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val INSTALL_COOLDOWN = TimeUnit.DAYS.toMillis(14)
|
||||
private val REVIEW_COOLDOWN = TimeUnit.DAYS.toMillis(30)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue