Skip to content

Commit

Permalink
Improve deprecation of old registerAppWithDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Dec 27, 2023
1 parent 570b482 commit 05bc2d2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const val PREF_MASTER = "unifiedpush.connector"
const val PREF_MASTER_TOKEN = "unifiedpush.connector"
const val PREF_MASTER_INSTANCE = "unifiedpush.instances"
const val PREF_MASTER_DISTRIBUTOR = "unifiedpush.distributor"
const val PREF_MASTER_DISTRIBUTOR_ACK = "unifiedpush.distributor_ack"
const val PREF_MASTER_NO_DISTRIB_DIALOG_ACK = "unifiedpush.no_distrib_dialog"

const val ACTION_NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class MessagingReceiver : BroadcastReceiver() {
when (intent.action) {
ACTION_NEW_ENDPOINT -> {
val endpoint = intent.getStringExtra(EXTRA_ENDPOINT) ?: return
store.distributorAck = true
onNewEndpoint(context, endpoint, instance)
}
// keep REFUSED for old distributors supporting AND_1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,23 @@ internal class Store(context: Context) {

internal fun removeDistributor() {
synchronized(distributorLock) {
preferences.edit().remove(PREF_MASTER_DISTRIBUTOR).commit()
preferences.edit().apply {
remove(PREF_MASTER_DISTRIBUTOR)
remove(PREF_MASTER_DISTRIBUTOR_ACK)
}.commit()
}
}

internal var distributorAck: Boolean
get() = synchronized(distributorLock) {
preferences.getBoolean(PREF_MASTER_DISTRIBUTOR_ACK, false)
}
@SuppressLint("ApplySharedPref")
set(value) = synchronized(distributorLock) {
preferences.edit().putBoolean(PREF_MASTER_DISTRIBUTOR_ACK, value).commit()
}


internal fun saveNoDistributorAck() {
preferences.edit().putBoolean(PREF_MASTER_NO_DISTRIB_DIALOG_ACK, true).apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object UnifiedPush {
val store = Store(context)
val token = store.getTokenOrNew(instance)

val distributor = store.tryGetDistributor() ?: return
val distributor = getSavedDistributor(context) ?: return

val broadcastIntent = Intent()
broadcastIntent.`package` = distributor
Expand All @@ -41,9 +41,10 @@ object UnifiedPush {

@JvmStatic
@Deprecated(
"Replace with registerAppWithDialog(" +
"Context, String, RegistrationDialogContent, ArrayList<String>, String" +
")"
"Replace with registerAppWithDialog",
replaceWith = ReplaceWith("registerAppWithDialog(" +
"context, instance, RegistrationDialogContent().apply { noDistributorDialog.message = dialogMessage }, features, messageForDistributor" +
")")
)
fun registerAppWithDialog(
context: Context,
Expand All @@ -70,7 +71,7 @@ object UnifiedPush {
features: ArrayList<String> = DEFAULT_FEATURES,
messageForDistributor: String = ""
) {
if (getDistributor(context).isNotEmpty()) {
getAckDistributor(context)?.let {
registerApp(context, instance)
return
}
Expand Down Expand Up @@ -147,7 +148,11 @@ object UnifiedPush {
@JvmStatic
fun unregisterApp(context: Context, instance: String = INSTANCE_DEFAULT) {
val store = Store(context)
val distributor = store.tryGetDistributor() ?: return
val distributor = getSavedDistributor(context) ?: run {
store.removeInstances()
store.removeDistributor()
return
}
val token = store.tryGetToken(instance) ?: return
val broadcastIntent = Intent()
broadcastIntent.`package` = distributor
Expand Down Expand Up @@ -209,15 +214,40 @@ object UnifiedPush {
}

@JvmStatic
fun getDistributor(context: Context): String {
@Deprecated(
"Replace with getSavedDistributor or getAckDistributor",
replaceWith = ReplaceWith("getAckDistributor(context)")
)
fun getDistributor(context: Context): String = getAckDistributor(context) ?: ""

/**
* This function returns the distributor registered by the user,
* but the distributor may not have sent a new endpoint yet
*/
@JvmStatic
fun getSavedDistributor(context: Context): String? = getDistributor(context, false)

/**
* This function returns the distributor registered by the user,
* and the distributor has already sent a new endpoint
*/
@JvmStatic
fun getAckDistributor(context: Context): String? = getDistributor(context, true)

@JvmStatic
private fun getDistributor(context: Context, ack: Boolean): String? {
val store = Store(context)
store.tryGetDistributor()?.let { distributor ->
if (ack && !store.distributorAck) {
return null
}
return store.tryGetDistributor()?.let { distributor ->
if (distributor in getDistributors(context)) {
Log.d(LOG_TAG, "Found saved distributor.")
return distributor
distributor
} else {
null
}
}
return ""
}

@JvmStatic
Expand Down

0 comments on commit 05bc2d2

Please sign in to comment.