Bug 1853113 - Monitor Search Widget presence based on activation

Previously, the way we registered if the user has installed the
search widget was keeping a count that increments every time
the widget was added and decrements everytime it was removed. The
main problem here was the wrong usage of onEnabled and onDeleted.
This patch aims to move towards a boolean based preference and use
the provided onDisabled method together with onEnabled. The logic
here is as follows: When the first instance of the widget has been
added, onEnabled will be called and the preference set to true and
no matter how many widgets are present on the homescreen, it will
stay true. When the last widget is removed, onDisabled will
be called and the pref will be set to false.
fenix/119.0
DreVla 8 months ago committed by mergify[bot]
parent a81168ad37
commit 02589af6fe

@ -1211,19 +1211,21 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = true,
)
fun addSearchWidgetInstalled(count: Int) {
/**
* Used in [SearchWidgetProvider] to update when the search widget
* exists on home screen or if it has been removed completely.
*/
fun setSearchWidgetInstalled(installed: Boolean) {
val key = appContext.getPreferenceKey(R.string.pref_key_search_widget_installed)
val newValue = preferences.getInt(key, 0) + count
preferences.edit()
.putInt(key, newValue)
.putBoolean(key, installed)
.apply()
}
val searchWidgetInstalled: Boolean
get() = 0 < preferences.getInt(
appContext.getPreferenceKey(R.string.pref_key_search_widget_installed),
0,
)
val searchWidgetInstalled by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_search_widget_installed),
default = false,
)
fun incrementNumTimesPrivateModeOpened() = numTimesPrivateModeOpened.increment()

@ -21,6 +21,7 @@ import androidx.annotation.Dimension.Companion.DP
import androidx.annotation.VisibleForTesting
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.toBitmap
import org.mozilla.fenix.GleanMetrics.Metrics
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.R
@ -37,11 +38,13 @@ class SearchWidgetProvider : AppWidgetProvider() {
// The existing name replicates the name and package we used in Fennec.
override fun onEnabled(context: Context) {
context.settings().addSearchWidgetInstalled(1)
context.settings().setSearchWidgetInstalled(true)
Metrics.searchWidgetInstalled.set(true)
}
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
context.settings().addSearchWidgetInstalled(-appWidgetIds.size)
override fun onDisabled(context: Context) {
context.settings().setSearchWidgetInstalled(false)
Metrics.searchWidgetInstalled.set(false)
}
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {

Loading…
Cancel
Save