Skip to content

Commit

Permalink
android app: truncate text if too large (HELL YEAH)
Browse files Browse the repository at this point in the history
ok so we finally found how to truncate text based on the widget width. Now it works out-of-box with android_small.txt, but with others doesn't really well, but it's a thing now. merging soon to main
  • Loading branch information
Toni500github committed Dec 8, 2024
1 parent 7ca590d commit a9fd254
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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("<br>").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
}
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions android/app/src/main/res/layout/customfetch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/values-v31/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:clipToOutline">true</item>
</style>
</resources>
</resources>
4 changes: 2 additions & 2 deletions android/app/src/main/res/xml/customfetch_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a9fd254

Please sign in to comment.