Skip to content

Commit

Permalink
android app: make truncate text kinda more optimal
Browse files Browse the repository at this point in the history
also some misc changes + save disableWrapLines settings (if it works)
  • Loading branch information
Toni500github committed Dec 13, 2024
1 parent 09fcd4b commit dbd5fc8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</h1>

<p align="center">
A system information fetch tool (or neofetch like program), which its focus point is the perfomance and <b>customizability</b>
A system information fetch tool (or neofetch like program), which its focus point is the performance and <b>customizability</b>
</p>
<p align="center">
<img src="https://img.shields.io/github/languages/top/Toni500github/customfetch?logo=cplusplusbuilder&label=" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class SettingsActivity : AppCompatActivity() {
if (!Environment.isExternalStorageManager()) {
AlertDialog.Builder(this)
.setTitle("Grant external storage management permission")
.setMessage("Customfetch needs permissions to manage external storage for writing config files\n"+
.setMessage("Customfetch needs permissions to manage external storage for writing config files.\n"+
"By default we going to read/write the following directories:\n"+
"/storage/emulated/0/.config/\n"+
"/storage/emulated/0/.config/customfetch/")
// The dialog is automatically dismissed when a dialog button is clicked.

.setPositiveButton("Grant permission"
) { _, _ ->
val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.res.Configuration.ORIENTATION_PORTRAIT
import android.os.Bundle
import android.text.TextPaint
import android.util.Log
import android.widget.RemoteViews
import org.toni.customfetch_android.R
Expand Down Expand Up @@ -38,17 +39,7 @@ class customfetch : AppWidgetProvider() {
appWidgetId: Int,
newOptions: Bundle
) {
// Get the new widget size
val widgetSize = WidgetSizeProvider(context)
val width = (widgetSize.getWidgetsSize(appWidgetId).first * 0.237f) // getWidgetSize(minWidthDp, maxWidthDp, context)
Log.d("widthTesting", "width = $width")
Log.d("wrappingTest", "disableLineWrap = $disableLineWrap")

val parsedContent = customfetchRender.getParsedContent(context, appWidgetId, width, disableLineWrap)

val views = RemoteViews(context.packageName, R.layout.customfetch)
views.setTextViewText(R.id.customfetch_text, parsedContent)
appWidgetManager.updateAppWidget(appWidgetId, views)
updateAppWidget(context, appWidgetManager, appWidgetId)
}

override fun onEnabled(context: Context) {
Expand Down Expand Up @@ -102,17 +93,29 @@ internal fun updateAppWidget(
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetSize = WidgetSizeProvider(context)
val width = (widgetSize.getWidgetsSize(appWidgetId).first * 0.237f)
// create a TextPaint to be used to measure text size
val textPaint = TextPaint()
val textSizeSp = 14f
val textSizePx = textSizeSp * context.resources.displayMetrics.scaledDensity
textPaint.textSize = textSizePx

val width = WidgetSizeProvider(context).getWidgetsSize(appWidgetId).first.toFloat()
Log.d("widthTesting", "textSizePx = $textSizePx")
Log.d("widthTesting", "width = $width")
Log.d("wrappingTest", "disableLineWrap = $disableLineWrap")

val parsedContent = customfetchRender.getParsedContent(context, appWidgetId, width, disableLineWrap)
val parsedContent = customfetchRender.getParsedContent(
context,
appWidgetId,
width,
disableLineWrap,
textPaint
)

// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.customfetch)
views.setTextViewText(R.id.customfetch_text, parsedContent)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ var disableLineWrap = false
*/
class customfetchConfigureActivity : Activity() {
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private lateinit var appWidgetText: EditText
private lateinit var argumentsConfig: EditText
private lateinit var argsHelp: TextView
private lateinit var showModulesList: CheckBox
private lateinit var disableWrapLines: CheckBox
private var onClickListener = View.OnClickListener {
val context = this@customfetchConfigureActivity

// When the button is clicked, store the string locally
val widgetText = appWidgetText.text.toString()
val widgetText = argumentsConfig.text.toString()
saveTitlePref(context, appWidgetId, widgetText)

// It is the responsibility of the configuration activity to update the app widget
Expand Down Expand Up @@ -63,7 +63,7 @@ class customfetchConfigureActivity : Activity() {
if (!Files.exists(Path(filesDir.absolutePath + "ascii")))
copyToAssetFolder(assets, filesDir.absolutePath, "ascii")

appWidgetText = binding.appwidgetText
argumentsConfig = binding.argumentsConfigure
argsHelp = binding.argsHelp
showModulesList = binding.showModulesList
disableWrapLines = binding.disableWrapLines
Expand All @@ -83,7 +83,7 @@ class customfetchConfigureActivity : Activity() {
return
}

appWidgetText.setText(loadTitlePref(this@customfetchConfigureActivity, appWidgetId))
argumentsConfig.setText(loadTitlePref(this@customfetchConfigureActivity, appWidgetId))
argsHelp.text = customfetchRender.mainAndroid("customfetch --help")

showModulesList.setOnCheckedChangeListener { _, isChecked ->
Expand All @@ -99,7 +99,7 @@ class customfetchConfigureActivity : Activity() {
}

class CustomfetchMainRender {
fun getParsedContent(context: Context, appWidgetId: Int, width: Float, disableLineWrap: Boolean, otherArguments: String = ""): SpannableStringBuilder {
fun getParsedContent(context: Context, appWidgetId: Int, width: Float, disableLineWrap: Boolean, paint: TextPaint, otherArguments: String = ""): SpannableStringBuilder {
val parsedContent = SpannableStringBuilder()
val arguments = otherArguments.ifEmpty {
loadTitlePref(context, appWidgetId)
Expand All @@ -109,7 +109,6 @@ class CustomfetchMainRender {

if (disableLineWrap) {
val eachLine = htmlContent!!.split("<br>").map { it.trim() }
val paint = TextPaint()//.apply { textSize = 7f }
for (line in eachLine) {
var parsedLine = HtmlCompat.fromHtml(line, HtmlCompat.FROM_HTML_MODE_COMPACT)
parsedLine =
Expand All @@ -118,10 +117,7 @@ class CustomfetchMainRender {
}
} else {
parsedContent.append(htmlContent?.let {
HtmlCompat.fromHtml(
it,
HtmlCompat.FROM_HTML_MODE_COMPACT
)
HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_COMPACT)
})
}

Expand All @@ -144,6 +140,7 @@ private const val PREF_PREFIX_KEY = "appwidget_"
internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String) {
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text)
prefs.putBoolean(PREF_PREFIX_KEY + "bool_" + appWidgetId, disableLineWrap)
prefs.apply()
}

Expand All @@ -152,6 +149,7 @@ internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String) {
internal fun loadTitlePref(context: Context, appWidgetId: Int): String {
val prefs = context.getSharedPreferences(PREFS_NAME, 0)
val titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null)
disableLineWrap = prefs.getBoolean(PREF_PREFIX_KEY + "bool_" + appWidgetId, false)
return titleValue ?: "-D ${context.filesDir.absolutePath} -a small"
}

Expand Down
18 changes: 9 additions & 9 deletions android/app/src/main/res/layout/customfetch_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:labelFor="@id/appwidget_text"
android:labelFor="@id/arguments_configure"
android:text="@string/configure" />

<EditText
android:id="@+id/appwidget_text"
android:id="@+id/arguments_configure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="EXAMPLE"
android:inputType="text" />

<CheckBox
android:id="@+id/disable_wrap_lines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@+id/arguments_configure"
android:text="@string/disable_wrap_lines" />

<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/add_widget" />

<CheckBox
android:id="@+id/disable_wrap_lines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@+id/add_button"
android:text="@string/disable_wrap_lines" />

<CheckBox
android:id="@+id/show_modules_list"
android:layout_width="wrap_content"
Expand Down
4 changes: 2 additions & 2 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="appwidget_text">EXAMPLE</string>
<string name="configure">Configure (insert command line options)</string>
<string name="add_widget">Add widget</string>
<string name="app_widget_description">This is an app widget description</string>
<string name="app_widget_description">Neofetch like program, which its focus point is the performance and customizability</string>
<string name="modules_list">Display modules list</string>
<string name="disable_wrap_lines">Truncate text (may be unstable)</string>
<string name="disable_wrap_lines">Truncate text (may be unstable, use it only on phones)</string>
</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="60dp"
android:minHeight="20dp"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/example_appwidget_preview"
android:previewLayout="@layout/customfetch"
android:resizeMode="horizontal|vertical"
Expand Down

0 comments on commit dbd5fc8

Please sign in to comment.