Add remote megaphone snooze capabilities.

This commit is contained in:
Cody Henthorne
2022-10-27 16:43:23 -04:00
parent 2ea5c7e3bc
commit c357c35303
11 changed files with 542 additions and 13 deletions

View File

@@ -307,7 +307,7 @@ public final class Megaphones {
}
private static @NonNull Megaphone buildRemoteMegaphone(@NonNull Context context) {
RemoteMegaphoneRecord record = RemoteMegaphoneRepository.getRemoteMegaphoneToShow();
RemoteMegaphoneRecord record = RemoteMegaphoneRepository.getRemoteMegaphoneToShow(System.currentTimeMillis());
if (record != null) {
Megaphone.Builder builder = new Megaphone.Builder(Event.REMOTE_MEGAPHONE, Megaphone.Style.BASIC)

View File

@@ -4,7 +4,10 @@ import android.app.Application
import android.content.Context
import androidx.annotation.AnyThread
import androidx.annotation.WorkerThread
import org.json.JSONArray
import org.json.JSONException
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.badges.models.Badge
import org.thoughtcrime.securesms.components.settings.app.AppSettingsActivity
import org.thoughtcrime.securesms.database.RemoteMegaphoneDatabase
@@ -19,16 +22,23 @@ import org.thoughtcrime.securesms.util.LocaleFeatureFlags
import org.thoughtcrime.securesms.util.PlayServicesUtil
import org.thoughtcrime.securesms.util.VersionTracker
import java.util.Objects
import kotlin.math.min
import kotlin.time.Duration.Companion.days
/**
* Access point for interacting with Remote Megaphones.
*/
object RemoteMegaphoneRepository {
private val TAG = Log.tag(RemoteMegaphoneRepository::class.java)
private val db: RemoteMegaphoneDatabase = SignalDatabase.remoteMegaphones
private val context: Application = ApplicationDependencies.getApplication()
private val snooze: Action = Action { _, controller, _ -> controller.onMegaphoneSnooze(Megaphones.Event.REMOTE_MEGAPHONE) }
private val snooze: Action = Action { _, controller, remote ->
controller.onMegaphoneSnooze(Megaphones.Event.REMOTE_MEGAPHONE)
db.snooze(remote)
}
private val finish: Action = Action { context, controller, remote ->
if (remote.imageUri != null) {
@@ -40,7 +50,7 @@ object RemoteMegaphoneRepository {
private val donate: Action = Action { context, controller, remote ->
controller.onMegaphoneNavigationRequested(AppSettingsActivity.manageSubscriptions(context))
finish.run(context, controller, remote)
snooze.run(context, controller, remote)
}
private val actions = mapOf(
@@ -65,12 +75,13 @@ object RemoteMegaphoneRepository {
@WorkerThread
@JvmStatic
fun getRemoteMegaphoneToShow(): RemoteMegaphoneRecord? {
return db.getPotentialMegaphonesAndClearOld()
fun getRemoteMegaphoneToShow(now: Long = System.currentTimeMillis()): RemoteMegaphoneRecord? {
return db.getPotentialMegaphonesAndClearOld(now)
.asSequence()
.filter { it.imageUrl == null || it.imageUri != null }
.filter { it.countries == null || LocaleFeatureFlags.shouldShowReleaseNote(it.uuid, it.countries) }
.filter { it.conditionalId == null || checkCondition(it.conditionalId) }
.filter { it.snoozedAt == 0L || checkSnooze(it, now) }
.firstOrNull()
}
@@ -95,6 +106,17 @@ object RemoteMegaphoneRepository {
}
}
private fun checkSnooze(record: RemoteMegaphoneRecord, now: Long): Boolean {
if (record.seenCount == 0) {
return true
}
val gaps: JSONArray? = record.getDataForAction(ActionId.SNOOZE)?.getJSONArray("snoozeDurationDays")?.takeIf { it.length() > 0 }
val gapDays: Int? = gaps?.getIntOrNull(record.seenCount - 1)
return gapDays == null || (record.snoozedAt + gapDays.days.inWholeMilliseconds <= now)
}
private fun shouldShowDonateMegaphone(): Boolean {
return VersionTracker.getDaysSinceFirstInstalled(context) >= 7 &&
PlayServicesUtil.getPlayServicesStatus(context) == PlayServicesUtil.PlayServicesStatus.SUCCESS &&
@@ -108,4 +130,16 @@ object RemoteMegaphoneRepository {
fun interface Action {
fun run(context: Context, controller: MegaphoneActionController, remoteMegaphone: RemoteMegaphoneRecord)
}
/**
* Gets the int at the specified index, or last index of array if larger then array length, or null if unable to parse json
*/
private fun JSONArray.getIntOrNull(index: Int): Int? {
return try {
getInt(min(index, length() - 1))
} catch (e: JSONException) {
Log.w(TAG, "Unable to parse", e)
null
}
}
}