This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #13507 - Extracts review prompt behavior into ReviewPromptController
- Loading branch information
Showing
6 changed files
with
101 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/src/main/java/org/mozilla/fenix/components/ReviewPromptController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* 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.mozilla.fenix.components | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import androidx.annotation.VisibleForTesting | ||
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 org.mozilla.fenix.utils.Settings | ||
|
||
/** | ||
* Interface that describes the settings needed to track the Review Prompt. | ||
*/ | ||
interface ReviewSettings { | ||
var numberOfAppLaunches: Int | ||
val isDefaultBrowser: Boolean | ||
var lastReviewPromptTimeInMillis: Long | ||
} | ||
|
||
/** | ||
* Wraps `Settings` to conform to `ReviewSettings`. | ||
*/ | ||
class FenixReviewSettings( | ||
val settings: Settings | ||
): ReviewSettings { | ||
override var numberOfAppLaunches: Int | ||
get() = settings.numberOfAppLaunches | ||
set(value) { settings.numberOfAppLaunches = value } | ||
override val isDefaultBrowser: Boolean | ||
get() = settings.isDefaultBrowser() | ||
override var lastReviewPromptTimeInMillis: Long | ||
get() = settings.lastReviewPromptTimeInMillis | ||
set(value) { settings.lastReviewPromptTimeInMillis = value } | ||
} | ||
|
||
/** | ||
* Controls the Review Prompt behavior. | ||
*/ | ||
class ReviewPromptController( | ||
private val context: Context, | ||
private val reviewSettings: ReviewSettings, | ||
private val timeNowInMillis: () -> Long = { System.currentTimeMillis() } | ||
) { | ||
suspend fun promptReview(activity: Activity) { | ||
if (shouldShowPrompt()) { | ||
val manager = ReviewManagerFactory.create(context) | ||
val reviewInfo = manager.requestReview() | ||
manager.launchReview(activity, reviewInfo) | ||
|
||
reviewSettings.lastReviewPromptTimeInMillis = timeNowInMillis() | ||
} | ||
} | ||
|
||
fun trackApplicationLaunch() { | ||
reviewSettings.numberOfAppLaunches = reviewSettings.numberOfAppLaunches + 1 | ||
} | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
fun shouldShowPrompt(): Boolean { | ||
if (!reviewSettings.isDefaultBrowser) { return false } | ||
|
||
val hasOpenedFiveTimes = reviewSettings.numberOfAppLaunches >= 5 | ||
val apprxFourMonthsAgo = timeNowInMillis() - (APPRX_MONTH_IN_MILLIS * 4) | ||
val hasNotBeenPromptedLastFourMonths = reviewSettings.lastReviewPromptTimeInMillis <= apprxFourMonthsAgo | ||
|
||
return hasOpenedFiveTimes && hasNotBeenPromptedLastFourMonths | ||
} | ||
|
||
companion object { | ||
private const val APPRX_MONTH_IN_MILLIS: Long = 1000L * 60L * 60L * 24L * 30L | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters