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
|
package org.tasks.injection
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import dagger.Lazy
|
import dagger.Lazy
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import org.tasks.gtasks.PlayServices
|
import org.tasks.location.Geocoder
|
||||||
import org.tasks.location.*
|
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
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
class FlavorModule {
|
class FlavorModule {
|
||||||
@Provides
|
@Provides
|
||||||
fun getLocationService(
|
fun getLocationService(
|
||||||
@ApplicationContext context: Context,
|
google: Lazy<LocationServiceGooglePlay>,
|
||||||
google: Lazy<LocationServiceGooglePlay>,
|
android: Lazy<LocationServiceAndroid>,
|
||||||
android: Lazy<LocationServiceAndroid>
|
playServices: PlayServices,
|
||||||
): LocationService = if (PlayServices.isAvailable(context)) google.get() else android.get()
|
): LocationService = if (playServices.isAvailable()) google.get() else android.get()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
fun getMapFragment(
|
fun getMapFragment(
|
||||||
@ApplicationContext context: Context,
|
osm: Lazy<OsmMapFragment>,
|
||||||
osm: Lazy<OsmMapFragment>,
|
google: Lazy<GoogleMapFragment>,
|
||||||
google: Lazy<GoogleMapFragment>,
|
playServices: PlayServices,
|
||||||
): MapFragment = if (PlayServices.isAvailable(context)) google.get() else osm.get()
|
): MapFragment = if (playServices.isAvailable()) google.get() else osm.get()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
fun getGeocoder(mapbox: GeocoderMapbox): Geocoder = mapbox
|
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