diff --git a/android/app/src/main/java/org/toni/customfetch_android/widget/customfetch.kt b/android/app/src/main/java/org/toni/customfetch_android/widget/customfetch.kt index 7c86c56..a073c3b 100644 --- a/android/app/src/main/java/org/toni/customfetch_android/widget/customfetch.kt +++ b/android/app/src/main/java/org/toni/customfetch_android/widget/customfetch.kt @@ -3,11 +3,18 @@ package org.toni.customfetch_android.widget import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context +import android.os.Bundle +import android.text.SpannableStringBuilder import android.text.Spanned +import android.text.TextPaint +import android.text.TextUtils.TruncateAt +import android.text.TextUtils.ellipsize +import android.util.TypedValue import android.widget.RemoteViews import androidx.core.text.HtmlCompat import org.toni.customfetch_android.R + /** * Implementation of App Widget functionality. * App Widget Configuration implemented in [customfetchConfigureActivity] @@ -31,6 +38,43 @@ class customfetch : AppWidgetProvider() { } } + override fun onAppWidgetOptionsChanged( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetId: Int, + newOptions: Bundle + ) { + // Get the new widget size + val minWidthDp = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) + val maxWidthDp = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) + val minWidthPx = convertDpToPx(context, minWidthDp) + val maxWidthPx = convertDpToPx(context, maxWidthDp) + + val parsedContent = SpannableStringBuilder() + val arguments = loadTitlePref(context, appWidgetId) + val htmlContent = customfetchConfigureActivity().mainAndroid("customfetch $arguments") + + val eachLine = htmlContent!!.split("
").map { it.trim() } + val paint = TextPaint() + for (line in eachLine) { + var parsedLine = HtmlCompat.fromHtml(line, HtmlCompat.FROM_HTML_MODE_LEGACY) + parsedLine = ellipsize(parsedLine, paint, minWidthDp * 0.8f, TruncateAt.END) as Spanned + parsedContent.appendLine(parsedLine) + } + + val views = RemoteViews(context.packageName, R.layout.customfetch) + views.setTextViewText(R.id.customfetch_text, parsedContent) + appWidgetManager.updateAppWidget(appWidgetId, views) + } + + private fun convertDpToPx(context: Context, dp: Int): Int { + return TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + dp.toFloat(), + context.resources.displayMetrics + ).toInt() + } + override fun onEnabled(context: Context) { // Enter relevant functionality for when the first widget is created } @@ -46,14 +90,9 @@ internal fun updateAppWidget( appWidgetId: Int, initConfigureActivity: Boolean = false ) { - val arguments = loadTitlePref(context, appWidgetId) - val htmlContent = - if (initConfigureActivity) customfetchConfigureActivity().mainAndroid("customfetch $arguments") - else "useless text" - // Construct the RemoteViews object val views = RemoteViews(context.packageName, R.layout.customfetch) - views.setTextViewText(R.id.customfetch_text, htmlContent?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) }) + views.setTextViewText(R.id.customfetch_text, "Loading...") // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views) diff --git a/android/app/src/main/res/layout/customfetch.xml b/android/app/src/main/res/layout/customfetch.xml index 3552eee..d123d47 100644 --- a/android/app/src/main/res/layout/customfetch.xml +++ b/android/app/src/main/res/layout/customfetch.xml @@ -10,12 +10,10 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" - android:scrollHorizontally="true" - android:scrollbars="horizontal" android:focusable="true" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" - android:textSize="7sp" + android:textSize="8sp" android:fontFamily="monospace" tools:ignore="SmallSp" /> diff --git a/android/app/src/main/res/values-v31/styles.xml b/android/app/src/main/res/values-v31/styles.xml index d607618..8d4e14b 100644 --- a/android/app/src/main/res/values-v31/styles.xml +++ b/android/app/src/main/res/values-v31/styles.xml @@ -13,4 +13,4 @@ ?android:attr/textColorPrimary true - \ No newline at end of file + diff --git a/android/app/src/main/res/xml/customfetch_info.xml b/android/app/src/main/res/xml/customfetch_info.xml index 2cba38b..b16f181 100644 --- a/android/app/src/main/res/xml/customfetch_info.xml +++ b/android/app/src/main/res/xml/customfetch_info.xml @@ -4,8 +4,8 @@ android:description="@string/app_widget_description" android:initialKeyguardLayout="@layout/customfetch" android:initialLayout="@layout/customfetch" - android:minWidth="110dp" - android:minHeight="110dp" + android:minWidth="60dp" + android:minHeight="20dp" android:previewImage="@drawable/example_appwidget_preview" android:previewLayout="@layout/customfetch" android:resizeMode="horizontal|vertical"