From 1ac8cf262e7e3bf61e22ff79df7893daeeb665de Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Tue, 1 Dec 2026 21:41:30 +0100 Subject: [PATCH 01/11] Give every free user a 7-day Pro trial Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FW6WZYhfv7RYfBa5VzPAGW --- CHANGELOG.md | 4 + .../opendocument/droid/nonfree/AdManager.kt | 70 +++++++- .../droid/background/BillingPreferences.kt | 25 +++ .../droid/nonfree/BillingManager.kt | 102 +++++++++++- .../opendocument/droid/nonfree/ProTrial.kt | 50 ++++++ .../droid/ui/activity/DocumentFragment.kt | 23 ++- .../droid/ui/activity/LandingFragment.kt | 7 +- .../droid/ui/activity/MainActivity.kt | 156 +++++++++++++++++- .../droid/ui/widget/DocumentActions.kt | 22 ++- .../droid/ui/widget/EditingTools.kt | 63 +++++-- .../droid/ui/widget/LandingAdapter.kt | 8 +- .../droid/ui/widget/LandingItem.kt | 12 +- app/src/main/res/drawable/bg_pro_dot.xml | 6 + app/src/main/res/layout/dialog_pass.xml | 46 +++++- app/src/main/res/layout/item_editing_tool.xml | 85 ++++++---- app/src/main/res/values/strings.xml | 34 ++++ .../opendocument/droid/nonfree/AdManager.kt | 2 + .../droid/nonfree/ProTrialTest.kt | 75 +++++++++ 18 files changed, 711 insertions(+), 79 deletions(-) create mode 100644 app/src/main/java/app/opendocument/droid/nonfree/ProTrial.kt create mode 100644 app/src/main/res/drawable/bg_pro_dot.xml create mode 100644 app/src/test/java/app/opendocument/droid/nonfree/ProTrialTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 91bfe4a8435a..9e03fb04d06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ release, a pull request cuts that heading to the version. The release run makes section the body of the GitHub release, and refuses a version without one. `scripts/store-copy.py` writes Play's "What's new" from the same section, under 500 characters per locale. +## Unreleased + +- The free app starts with 7 days of Pro: every editing tool and no ads. + ## 4.26.0 - Recent documents show an icon for their file type. diff --git a/app/src/ads/java/app/opendocument/droid/nonfree/AdManager.kt b/app/src/ads/java/app/opendocument/droid/nonfree/AdManager.kt index 59e0e1f058d9..e0b6e49b3c71 100644 --- a/app/src/ads/java/app/opendocument/droid/nonfree/AdManager.kt +++ b/app/src/ads/java/app/opendocument/droid/nonfree/AdManager.kt @@ -30,6 +30,11 @@ class AdManager { /** Whether [initialize] ran far enough for the fields below to exist. */ private var initialized = false + /** + * Whether the ads sdk was started. Not before the first consent update: the trial asks none. + */ + private var sdkStarted = false + private lateinit var activity: Activity private lateinit var crashManager: CrashManager private lateinit var analyticsManager: AnalyticsManager @@ -55,6 +60,23 @@ class AdManager { private val retries = Handler(Looper.getMainLooper()) + /** Ends the house ad's turn in a slot the banner fills, see [onDocumentOpened]. */ + private val houseTurns = Handler(Looper.getMainLooper()) + + private val houseTurnEnd = Runnable { + val adView = this.adView + + if (!enabled || adView == null || isActivityGone()) { + return@Runnable + } + + show(adView) + + if (!paused) { + adView.loadAd(AdRequest.Builder().build()) + } + } + /** * How long the next retry waits: doubling from [FIRST_RETRY_DELAY_MS] up to the unit's rate. */ @@ -92,6 +114,13 @@ class AdManager { AppPreferences.of(activity).edit().putInt(PREF_HOUSE_AD_INDEX, value).apply() } + /** Fresh documents opened with the banner up, on disk like [houseAdIndex]. */ + private var documentsOnAds: Int + get() = AppPreferences.of(activity).getInt(PREF_DOCUMENTS_ON_ADS, 0) + set(value) { + AppPreferences.of(activity).edit().putInt(PREF_DOCUMENTS_ON_ADS, value).apply() + } + fun initialize( activity: Activity, analyticsManager: AnalyticsManager, @@ -107,6 +136,15 @@ class AdManager { return } + initialized = true + } + + private fun startSdk() { + if (sdkStarted) { + return + } + sdkStarted = true + try { MobileAds.initialize(activity) } catch (e: Throwable) { @@ -119,8 +157,6 @@ class AdManager { val configuration = RequestConfiguration.Builder().setTestDeviceIds(listOf(TEST_DEVICE_ID)).build() MobileAds.setRequestConfiguration(configuration) - - initialized = true } fun setEnabled(enabled: Boolean) { @@ -141,6 +177,29 @@ class AdManager { onPurchaseRequested = listener } + /** + * Counts a fresh document opened on ads. Every [HOUSE_AD_EVERY]th one starts with the house ad + * in the banner's place, for as long as one banner stands. + */ + fun onDocumentOpened() { + if (!enabled || consentInformation == null || isActivityGone()) { + return + } + + val opened = documentsOnAds + 1 + documentsOnAds = opened + + // an empty slot shows the house ad already + if (opened % HOUSE_AD_EVERY != 0 || !hasAd || adView == null) { + return + } + + showHouseAd() + + houseTurns.removeCallbacks(houseTurnEnd) + houseTurns.postDelayed(houseTurnEnd, RETRY_DELAY_MS) + } + /** Swaps the slot between the two views already in it, or hides both while neither has run. */ private fun show(view: View?) { if (!enabled) { @@ -186,6 +245,8 @@ class AdManager { return } + startSdk() + val params = ConsentRequestParameters.Builder().setTagForUnderAgeOfConsent(false).build() val consentInformation = UserMessagingPlatform.getConsentInformation(activity) @@ -535,6 +596,7 @@ class AdManager { fun destroyAds() { retries.removeCallbacksAndMessages(null) + houseTurns.removeCallbacksAndMessages(null) destroyAdView() @@ -563,6 +625,10 @@ class AdManager { const val TEST_DEVICE_ID = "46C05048B04145D0724C1ADA7FC17619" const val PREF_HOUSE_AD_INDEX = "house_ad_index" + const val PREF_DOCUMENTS_ON_ADS = "house_ad_documents" + + /** The house ad takes the first banner of every fifth document. */ + const val HOUSE_AD_EVERY = 5 // 10s, 20s, 40s, then the 60s the unit refreshes at. Not shorter: the sdk throttles a // burst of failed requests itself. diff --git a/app/src/main/java/app/opendocument/droid/background/BillingPreferences.kt b/app/src/main/java/app/opendocument/droid/background/BillingPreferences.kt index 9b10ef512cb8..9923da8cb090 100644 --- a/app/src/main/java/app/opendocument/droid/background/BillingPreferences.kt +++ b/app/src/main/java/app/opendocument/droid/background/BillingPreferences.kt @@ -33,9 +33,34 @@ class BillingPreferences(context: Context) { sharedPreferences.edit { putBoolean(PASS_KEY, pass) } } + /** When the pro trial started, or null before it has. */ + fun trialStartedAt(): Long? = + sharedPreferences.getLong(TRIAL_STARTED_AT_KEY, -1L).takeIf { it >= 0 } + + fun setTrialStartedAt(startedAt: Long) { + sharedPreferences.edit { putLong(TRIAL_STARTED_AT_KEY, startedAt) } + } + + /** Whether the trial needs nothing more: its end screen was answered, or pro was held. */ + fun isTrialSettled(): Boolean = sharedPreferences.getBoolean(TRIAL_SETTLED_KEY, false) + + fun setTrialSettled() { + sharedPreferences.edit { putBoolean(TRIAL_SETTLED_KEY, true) } + } + + /** Whether the screen that starts the trial was closed. */ + fun isTrialIntroShown(): Boolean = sharedPreferences.getBoolean(TRIAL_INTRO_KEY, false) + + fun setTrialIntroShown() { + sharedPreferences.edit { putBoolean(TRIAL_INTRO_KEY, true) } + } + private companion object { const val PURCHASE_KEY = "purchaseAcknowledged" const val LIFETIME_PRO_KEY = "lifetimePro" const val PASS_KEY = "pass" + const val TRIAL_STARTED_AT_KEY = "trialStartedAt" + const val TRIAL_SETTLED_KEY = "trialSettled" + const val TRIAL_INTRO_KEY = "trialIntroShown" } } diff --git a/app/src/main/java/app/opendocument/droid/nonfree/BillingManager.kt b/app/src/main/java/app/opendocument/droid/nonfree/BillingManager.kt index 93e8dd1003ff..8de30858ef68 100644 --- a/app/src/main/java/app/opendocument/droid/nonfree/BillingManager.kt +++ b/app/src/main/java/app/opendocument/droid/nonfree/BillingManager.kt @@ -17,6 +17,15 @@ class BillingManager { /** Whether removeAds ran in this session. The ads stay away until the next launch. */ private var adsRemoved = false + private val clock: () -> Long = System::currentTimeMillis + private val trial = ProTrial(clock) + + /** Whether this build runs the trial at all. Off under instrumented tests. */ + private var trialOffered = true + + /** Read at launch and kept for the session, like an ended pass. */ + private var trialPhase = ProTrial.Phase.NONE + /** * Fires when play reports what the preferences did not know: a purchase, a restore, or an ended * pass. [unlocked] says whether the pro features have just been unlocked, and [purchased] @@ -37,18 +46,23 @@ class BillingManager { preferences.setPurchased(true) } + if (enabled && trialOffered) { + trialPhase = startTrial(preferences) + } + // also without play: the last answer stands until play gives a new one - Features.unlocked = isPro() + Features.unlocked = isPro() || trialPhase == ProTrial.Phase.RUNNING if (!enabled) { adManager.showGoogleAds() return } - if (isAdFree()) { - removeAds(adManager) - } else { - adManager.showGoogleAds() + when { + isAdFree() -> removeAds(adManager) + // no banner and no consent form until the user picks ads on the end screen + holdsAds() -> Unit + else -> adManager.showGoogleAds() } // asked at every launch, not only until something turns up: the flags live only on this @@ -62,6 +76,23 @@ class BillingManager { playBilling?.queryOwned() } + /** Starts the trial at the first eligible launch, and reads where it stands. */ + private fun startTrial(preferences: BillingPreferences): ProTrial.Phase { + if (isPro()) { + preferences.setTrialSettled() + } + + if ( + preferences.trialStartedAt() == null && + !preferences.isTrialSettled() && + ProTrial.isEligible(Features.withAds, isPro()) + ) { + preferences.setTrialStartedAt(clock()) + } + + return trial.phase(preferences.trialStartedAt(), preferences.isTrialSettled()) + } + private fun apply(entitlement: Entitlement, purchased: Boolean) { val preferences = billingPreferences ?: return val adManager = adManager ?: return @@ -78,6 +109,11 @@ class BillingManager { } preferences.setPass(entitlement.pass) + // pro answers the end screen too, and a pass that lapses later brings today's ads back + if (isPro()) { + preferences.setTrialSettled() + } + if (!wasAdFree && isAdFree()) { removeAds(adManager) } @@ -99,6 +135,11 @@ class BillingManager { adManager.removeAds() + // the trial asks google for nothing, the consent update included + if (holdsAds()) { + return + } + // no banner to gate, but withdrawal has to survive the purchase, and only an // update tells us whether the sdk wants a way back out adManager.updateConsentInfo() @@ -116,7 +157,52 @@ class BillingManager { } /** Whether ads are on screen, or about to be: not after [isAdFree] was true in this session. */ - fun showsAds(): Boolean = !isAdFree() && !adsRemoved + fun showsAds(): Boolean = !isAdFree() && !adsRemoved && !holdsAds() + + /** The trial, or its end screen unanswered: no banner and no call to google. */ + private fun holdsAds(): Boolean = trialPhase != ProTrial.Phase.NONE + + /** The trial's days left while it runs, rounded up; null otherwise or with pro. */ + fun trialDaysLeft(): Int? { + if (trialPhase != ProTrial.Phase.RUNNING || isPro()) { + return null + } + + val startedAt = billingPreferences?.trialStartedAt() ?: return null + + return trial.daysLeft(startedAt) + } + + /** Whether the start screen is due: the trial runs and the screen was never closed. */ + fun isTrialIntroDue(): Boolean = + trialDaysLeft() != null && billingPreferences?.isTrialIntroShown() == false + + fun setTrialIntroShown() { + billingPreferences?.setTrialIntroShown() + } + + /** Whether the end screen is due: the trial is over and the user has not chosen yet. */ + fun isTrialEndDue(): Boolean = trialPhase == ProTrial.Phase.ENDED && !isPro() + + /** + * The end screen's free answer: ads from now on. The consent form opens only where no answer is + * on file, and the banner follows. + */ + fun chooseAds() { + settleTrial() + + val adManager = adManager ?: return + if (!isAdFree()) { + adManager.showGoogleAds() + } + } + + /** The end screen is answered, and the app is what the entitlement says from now on. */ + fun settleTrial() { + billingPreferences?.setTrialSettled() + + trialPhase = ProTrial.Phase.NONE + } /** The pro features bought in lite: the pass, or an old purchase for life. */ fun isPro(): Boolean { @@ -148,4 +234,8 @@ class BillingManager { fun setEnabled(enabled: Boolean) { this.enabled = enabled } + + fun setTrialOffered(offered: Boolean) { + trialOffered = offered + } } diff --git a/app/src/main/java/app/opendocument/droid/nonfree/ProTrial.kt b/app/src/main/java/app/opendocument/droid/nonfree/ProTrial.kt new file mode 100644 index 000000000000..3a46a734ee95 --- /dev/null +++ b/app/src/main/java/app/opendocument/droid/nonfree/ProTrial.kt @@ -0,0 +1,50 @@ +package app.opendocument.droid.nonfree + +import kotlin.math.ceil + +/** + * Seven days of pro for every free install, once, from the launch that starts them. Only the time + * math: [BillingManager] stores the start and the answer to the end screen, and reads the phase at + * launch, so the trial ends at the next launch rather than under an open document. + */ +class ProTrial(private val clock: () -> Long) { + + enum class Phase { + /** Not started, or answered: the app is what the entitlement says. */ + NONE, + + /** Pro tools on, no ads, no consent form. */ + RUNNING, + + /** Over, and the end screen not answered yet: Pro tools locked, still no ads. */ + ENDED, + } + + /** [startedAt] is when the trial started, or null before it has. */ + fun phase(startedAt: Long?, settled: Boolean): Phase = + when { + startedAt == null || settled -> Phase.NONE + clock() - startedAt < LENGTH_MILLIS -> Phase.RUNNING + else -> Phase.ENDED + } + + /** The days left, rounded up: 7 at the start and 1 during the last day, and after it. */ + fun daysLeft(startedAt: Long): Int { + val left = startedAt + LENGTH_MILLIS - clock() + + return ceil(left.toDouble() / DAY_MILLIS).toInt().coerceIn(1, DAYS) + } + + companion object { + + const val DAYS = 7 + const val DAY_MILLIS = 24 * 60 * 60 * 1000L + const val LENGTH_MILLIS = DAYS * DAY_MILLIS + + /** + * Who gets the trial: a build that sells the pass, to someone who does not hold pro. An old + * ad removal without pro counts as free here. Every country. + */ + fun isEligible(sellsPass: Boolean, holdsPro: Boolean): Boolean = sellsPass && !holdsPro + } +} diff --git a/app/src/main/java/app/opendocument/droid/ui/activity/DocumentFragment.kt b/app/src/main/java/app/opendocument/droid/ui/activity/DocumentFragment.kt index b52bc99d3e64..2445c27f1429 100644 --- a/app/src/main/java/app/opendocument/droid/ui/activity/DocumentFragment.kt +++ b/app/src/main/java/app/opendocument/droid/ui/activity/DocumentFragment.kt @@ -473,15 +473,18 @@ class DocumentFragment : Fragment(), DocumentLoader.Listener { * there is no strip over it at all - undo, redo and save are the bar's. */ private fun showEditingTools(document: LoadedDocument, editing: Boolean) { + val trialDays = (activity as? MainActivity)?.trialDaysLeft() + when { !editing || !document.editing.isEditable -> editingTools.hide() document.editing == EditingKind.DOCUMENT || document.editing == EditingKind.SHEET -> editingTools.showFormatting( sheet = document.editing == EditingKind.SHEET, locked = !Features.advancedEditing, + trialDays = trialDays, ) document.editing == EditingKind.ANNOTATION -> - editingTools.showMarking(locked = !Features.advancedEditing) + editingTools.showMarking(locked = !Features.advancedEditing, trialDays = trialDays) else -> editingTools.hide() } } @@ -565,6 +568,14 @@ class DocumentFragment : Fragment(), DocumentLoader.Listener { } override fun onLocked() { + // the trial's badge, which leads to the purchase sheet like the other counters + val mainActivity = requireActivity() as MainActivity + if (mainActivity.trialDaysLeft() != null) { + mainActivity.offerPass(null) + + return + } + // bought during this edit: the tools unlock once the page is rendered again if (Features.advancedEditing) { showUnlockedEditing() @@ -839,14 +850,20 @@ class DocumentFragment : Fragment(), DocumentLoader.Listener { R.drawable.ic_menu_book, ) - // last, so the furthest from the thumb: lite's pass and foss's sponsors. pro has paid + // last, so the furthest from the thumb: lite's pass and foss's sponsors. pro has paid. + // during the trial it counts the days left + val mainActivity = requireActivity() as MainActivity + val trialDays = mainActivity.trialDaysLeft() val support = - if (!(requireActivity() as MainActivity).offersSupport()) null + if (!mainActivity.offersSupport()) null else DocumentActions.Action( DocumentActions.ACTION_SUPPORT, R.string.menu_support, R.drawable.ic_favorite, + trialDays?.let { + resources.getQuantityString(R.plurals.trial_counter, it, it) + }, ) // the order they unfold in, most wanted first - and what a reader reaches for mid-document diff --git a/app/src/main/java/app/opendocument/droid/ui/activity/LandingFragment.kt b/app/src/main/java/app/opendocument/droid/ui/activity/LandingFragment.kt index bf5df7a498ef..cac3080da93a 100644 --- a/app/src/main/java/app/opendocument/droid/ui/activity/LandingFragment.kt +++ b/app/src/main/java/app/opendocument/droid/ui/activity/LandingFragment.kt @@ -132,12 +132,17 @@ class LandingFragment : Fragment(), LandingAdapter.Listener { // lite's pass, or foss's sponsors, where it is seen without unfolding anything. asked here // rather than carried in the state: what billing knows lives on the activity, not in // anything the ViewModel reads, and the activity refreshes this once play has answered + // the same row counts the trial's days while it runs if (mainActivity.offersSupport()) { + val trialDays = mainActivity.trialDaysLeft() items.add( LandingItem.Action( LandingItem.ACTION_SUPPORT, - R.string.menu_support, + if (trialDays != null) R.string.trial_row else R.string.menu_support, R.drawable.ic_favorite, + trialDays?.let { + resources.getQuantityString(R.plurals.trial_days_left, it, it) + }, ) ) } diff --git a/app/src/main/java/app/opendocument/droid/ui/activity/MainActivity.kt b/app/src/main/java/app/opendocument/droid/ui/activity/MainActivity.kt index 84eea3d417c1..13e6f2c87b90 100644 --- a/app/src/main/java/app/opendocument/droid/ui/activity/MainActivity.kt +++ b/app/src/main/java/app/opendocument/droid/ui/activity/MainActivity.kt @@ -169,6 +169,12 @@ class MainActivity : AppCompatActivity() { // fetched while an externally opened document is read: back must not wait on the network private var preparedReview: InAppReview.Prepared? = null + /** The trial's start or end screen, once per launch: "Pay once" does not bring it back. */ + private var trialDialog: AlertDialog? = null + + // set by "Support us with ads", and thanked for once the consent flow has settled + private var thankForAds = false + /** * Loads and saves the open document. Scoped to the activity, so it survives a configuration * change and [DocumentFragment] finds it already there rather than waiting for a binding. @@ -296,6 +302,10 @@ class MainActivity : AppCompatActivity() { analyticsManager.setCurrentScreen(this, "screen_main") } + + if (this.documentFragment == null && loadOnStart == null) { + showTrialScreenIfDue() + } } /** @@ -440,12 +450,27 @@ class MainActivity : AppCompatActivity() { adManager.setAdContainer(adContainer) // the landing screen is drawn long before the consent update comes back, and the privacy // options row only exists once it has - adManager.setConsentListener { landingFragment?.refresh() } + adManager.setConsentListener { + landingFragment?.refresh() + + if (thankForAds) { + thankForAds = false + + SnackbarHelper.show( + this, + R.string.trial_ads_thanks, + null, + isIndefinite = false, + isError = false, + ) + } + } adManager.setPurchaseListener { offerPass(null) } adManager.initialize(this, analyticsManager, crashManager) billingManager = BillingManager() billingManager.setEnabled(adsAvailable) + billingManager.setTrialOffered(!IS_TESTING) // the support row goes once play reports a purchase, and the open document is rendered // again with the editing scope the pass brings billingManager.setEntitlementListener { unlocked, purchased -> @@ -772,6 +797,99 @@ class MainActivity : AppCompatActivity() { else -> true } + /** The pro trial's days left while it runs, rounded up; null otherwise. */ + fun trialDaysLeft(): Int? = + if (::billingManager.isInitialized) billingManager.trialDaysLeft() else null + + /** + * The trial's screens, on the landing screen only: the start once, and the end at every launch + * until one of its answers is picked. Neither opens over a document. + */ + private fun showTrialScreenIfDue() { + if (trialDialog != null || isFinishing) { + return + } + + when { + billingManager.isTrialIntroDue() -> showTrialIntro() + billingManager.isTrialEndDue() -> showTrialEnd() + } + } + + private fun showTrialIntro() { + analyticsManager.report("trial_started") + + val message = + listOf( + R.string.trial_start_body, + R.string.trial_start_after, + R.string.trial_privacy, + ) + .joinToString("\n\n") { getString(it) } + + trialDialog = + AlertDialog.Builder(this) + .setTitle(R.string.trial_start_title) + .setMessage(message) + .setPositiveButton(R.string.trial_got_it, null) + // back and a tap outside close it too; a rotation does not, so it comes back + .setOnDismissListener { billingManager.setTrialIntroShown() } + .show() + } + + /** + * The purchase sheet once the trial is over, with "Support us with ads" in place of "Not now". + * It is not cancelable, and returns at the next launch until an answer is picked. An old ad + * removal has no ads to choose, and closes it instead. + */ + private fun showTrialEnd() { + analyticsManager.report("trial_end_shown") + + val adFree = billingManager.isAdFree() + + val view = layoutInflater.inflate(R.layout.dialog_pass, null) + val lead = view.findViewById(R.id.pass_lead) + lead.setText(R.string.trial_end_body) + lead.visibility = View.VISIBLE + view.findViewById(R.id.pass_why).visibility = View.GONE + view.findViewById(R.id.pass_privacy).visibility = View.VISIBLE + + val builder = + AlertDialog.Builder(this) + .setTitle(R.string.trial_end_title) + .setView(view) + .setCancelable(false) + if (adFree) { + builder.setNegativeButton(R.string.trial_end_close) { _, _ -> + analyticsManager.report("trial_end_closed") + + billingManager.settleTrial() + landingFragment?.refresh() + } + } + + val dialog = builder.show() + trialDialog = dialog + + bindPassButtons(view, dialog, null) + + if (!adFree) { + val adsButton = view.findViewById(R.id.pass_ads) + adsButton.visibility = View.VISIBLE + view.findViewById(R.id.pass_ads_note).visibility = View.VISIBLE + + adsButton.setOnClickListener { + analyticsManager.report("trial_chose_ads") + + dialog.dismiss() + + thankForAds = true + billingManager.chooseAds() + landingFragment?.refresh() + } + } + } + /** The pass in lite, the sponsors anywhere else. */ fun offerSupport() { if (Features.withAds) { @@ -821,21 +939,46 @@ class MainActivity : AppCompatActivity() { val view = layoutInflater.inflate(R.layout.dialog_pass, null) val featureView = view.findViewById(R.id.pass_feature) - val buyButton = view.findViewById