Skip to content

Commit

Permalink
android widget: fix widget update at boot + dont run updateWidget() w…
Browse files Browse the repository at this point in the history
…hen configuring the widget (finally)
  • Loading branch information
Toni500github committed Feb 8, 2025
1 parent f219298 commit e17bed0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:theme="@style/Theme.MainActivity"
tools:targetApi="31">
<receiver
android:name=".widget.customfetch"
android:name=".widget.Customfetch"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Expand All @@ -28,7 +28,7 @@
</receiver>

<activity
android:name=".widget.customfetchConfigureActivity"
android:name=".widget.CustomfetchConfigureActivity"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ import android.text.TextPaint
import android.util.Log
import android.widget.RemoteViews
import org.toni.customfetch_android.R
import org.toni.customfetch_android.getAppSettingsPrefInt

const val WIDGET_CLICK_ACTION = "org.toni.customfetch_android.WIDGET_CLICK"

/**
* Implementation of App Widget functionality.
* App Widget Configuration implemented in [customfetchConfigureActivity]
* App Widget Configuration implemented in [CustomfetchConfigureActivity]
*/
class customfetch : AppWidgetProvider() {
class Customfetch : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
Expand Down Expand Up @@ -131,6 +130,10 @@ internal fun updateAppWidget(
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
// if we are configuring the widget, then don't update the non existent widget
if (!isWidgetConfigured(context, appWidgetId))
return

val truncateText = getTruncateText(context, appWidgetId)
val bgColor = getBgColor(context, appWidgetId)
val widgetTextColor = getWidgetTextColor(context, appWidgetId)
Expand Down Expand Up @@ -159,7 +162,7 @@ internal fun updateAppWidget(
)

// both needed for when touching the widget
val intent = Intent(context, customfetch::class.java).apply {
val intent = Intent(context, Customfetch::class.java).apply {
action = WIDGET_CLICK_ACTION
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,23 @@ import androidx.core.graphics.toColorInt
import androidx.core.text.HtmlCompat
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener
import org.toni.customfetch_android.R
import org.toni.customfetch_android.databinding.CustomfetchConfigureBinding
import org.toni.customfetch_android.databinding.ColorpickerviewLayoutBinding
import org.toni.customfetch_android.databinding.CustomfetchConfigureBinding
import org.toni.customfetch_android.getAppSettingsPrefBool
import org.toni.customfetch_android.getAppSettingsPrefInt
import org.toni.customfetch_android.getAppSettingsPrefString
import java.io.File
import java.nio.file.Files
import kotlin.io.path.Path


/**
* The configuration screen for the [customfetch] AppWidget.
* The configuration screen for the [Customfetch] AppWidget.
*/
class customfetchConfigureActivity : Activity() {
class CustomfetchConfigureActivity : Activity() {
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private lateinit var binding: CustomfetchConfigureBinding
private var onAddWidget = View.OnClickListener {
val context = this@customfetchConfigureActivity
val context = this@CustomfetchConfigureActivity

// When the button is clicked, store the string locally
saveConfigPrefs(
Expand Down Expand Up @@ -118,7 +117,7 @@ class customfetchConfigureActivity : Activity() {
return
}

binding.argumentsConfigure.setText(getArgsPref(this@customfetchConfigureActivity, appWidgetId))
binding.argumentsConfigure.setText(getArgsPref(this@CustomfetchConfigureActivity, appWidgetId))
binding.additionalTruncateWidth.setText(getAppSettingsPrefString(this, "additional_truncate"))
binding.truncateText.isChecked = getAppSettingsPrefBool(this, "always_truncate")
binding.argsHelp.text = customfetchRender.mainAndroid("customfetch --help", true)
Expand Down Expand Up @@ -173,6 +172,8 @@ class customfetchConfigureActivity : Activity() {
}
}
}

markWidgetConfigured(this, appWidgetId)
}

private fun setSystemBgColor() {
Expand Down Expand Up @@ -354,5 +355,15 @@ internal fun deleteConfigPrefs(context: Context, appWidgetId: Int) {
prefs.apply()
}

fun markWidgetConfigured(context: Context, appWidgetId: Int) {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit().putBoolean(PREF_PREFIX_KEY + appWidgetId + "_configuring", true).apply()
}

fun isWidgetConfigured(context: Context, appWidgetId: Int): Boolean {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
return prefs.getBoolean(PREF_PREFIX_KEY + appWidgetId + "_configuring", false)
}

internal fun isValidHex(color: String): Boolean =
color.matches("^#[0-9A-Fa-f]{8}$".toRegex())
2 changes: 1 addition & 1 deletion android/app/src/main/res/xml/customfetch_info.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="org.toni.customfetch_android.widget.customfetchConfigureActivity"
android:configure="org.toni.customfetch_android.widget.CustomfetchConfigureActivity"
android:description="@string/app_widget_description"
android:initialKeyguardLayout="@layout/customfetch"
android:initialLayout="@layout/customfetch"
Expand Down

0 comments on commit e17bed0

Please sign in to comment.