Skip to content

Commit

Permalink
android widget: rename functions and variables + android app: add not…
Browse files Browse the repository at this point in the history
…ice to use the widget when opening the app, and change alert icon
  • Loading branch information
Toni500github committed Dec 21, 2024
1 parent 745b4a6 commit 37f292e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.toni.customfetch_android

import org.toni.customfetch_android.R
import android.app.AlertDialog
import android.content.Intent
import android.content.res.AssetManager
Expand Down Expand Up @@ -43,7 +44,7 @@ class SettingsActivity : AppCompatActivity() {
val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
startActivity(intent)
}
.setIcon(android.R.drawable.ic_dialog_alert)
.setIcon(R.drawable.icon_alert_yellow)

val view: View = layoutInflater.inflate(R.layout.grant_perm, null, false);
alert.setView(view)
Expand All @@ -53,6 +54,15 @@ class SettingsActivity : AppCompatActivity() {

if (!Files.exists(Path(filesDir.absolutePath + "ascii")))
copyToAssetFolder(assets, filesDir.absolutePath, "ascii")

AlertDialog.Builder(this)
.setTitle("Application is useless, use widget")
.setMessage("The application settings doesn't do anything, currently.\n"+
"The main purpose of customfetch is the widget, so might as well check that out :)")
.setPositiveButton("Ok"
) { _, _ -> }
.setIcon(android.R.drawable.ic_dialog_info)
.show()
}

class SettingsFragment : PreferenceFragmentCompat() {
Expand Down Expand Up @@ -85,7 +95,7 @@ private fun copyDirectory(

val destDir = File(destinationDir, sourceDir)
if (!destDir.exists() && !destDir.mkdirs())
throw IOException("Failed to create directory: " + destDir.absolutePath)
throw IOException("Failed to create directory: ${destDir.absolutePath}")

for (fileName in files) {
val assetPath = "$sourceDir/$fileName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class customfetch : AppWidgetProvider() {
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
// When the user deletes the widget, delete the preference associated with it.
for (appWidgetId in appWidgetIds) {
deleteTitlePref(context, appWidgetId)
deleteConfigPrefs(context, appWidgetId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class customfetchConfigureActivity : Activity() {
val context = this@customfetchConfigureActivity

// When the button is clicked, store the string locally
saveTitlePref(context, appWidgetId, argumentsConfig.text.toString(), additionalTruncateWidth.text.toString())
saveConfigPrefs(context, appWidgetId, argumentsConfig.text.toString(), additionalTruncateWidth.text.toString())

// It is the responsibility of the configuration activity to update the app widget
val appWidgetManager = AppWidgetManager.getInstance(context)
Expand Down Expand Up @@ -79,7 +79,7 @@ class customfetchConfigureActivity : Activity() {
return
}

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

Expand All @@ -99,7 +99,7 @@ class CustomfetchMainRender {
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)
loadArgsPref(context, appWidgetId)
}

val htmlContent = mainAndroid("customfetch $arguments")
Expand Down Expand Up @@ -134,33 +134,33 @@ private const val PREFS_NAME = "org.toni.customfetch_android.customfetch"
private const val PREF_PREFIX_KEY = "appwidget_"

// Write the prefix to the SharedPreferences object for this widget
internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String, truncateWidth: String) {
internal fun saveConfigPrefs(context: Context, appWidgetId: Int, args: String, truncateWidth: 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.putString(PREF_PREFIX_KEY + "truncate_" + appWidgetId, truncateWidth)
prefs.putString(PREF_PREFIX_KEY + appWidgetId + "_args", args)
prefs.putBoolean(PREF_PREFIX_KEY + appWidgetId + "_disableLineWrap", disableLineWrap)
prefs.putString(PREF_PREFIX_KEY + appWidgetId + "_truncateWidth", truncateWidth)
prefs.apply()
}

// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
internal fun loadTitlePref(context: Context, appWidgetId: Int): String {
internal fun loadArgsPref(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"
val args = prefs.getString(PREF_PREFIX_KEY + appWidgetId + "_args", null)
disableLineWrap = prefs.getBoolean(PREF_PREFIX_KEY + appWidgetId + "_disableLineWrap", false)
return args ?: "-D ${context.filesDir.absolutePath} -a small"
}

internal fun loadTruncateWidthPref(context: Context, appWidgetId: Int): String {
val prefs = context.getSharedPreferences(PREFS_NAME, 0)
val truncateWidthValue = prefs.getString(PREF_PREFIX_KEY + "truncate_" + appWidgetId, null)
val truncateWidthValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId + "_truncateWidth", null)
return truncateWidthValue ?: "0"
}

internal fun deleteTitlePref(context: Context, appWidgetId: Int) {
internal fun deleteConfigPrefs(context: Context, appWidgetId: Int) {
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
prefs.remove(PREF_PREFIX_KEY + appWidgetId)
prefs.remove(PREF_PREFIX_KEY + "bool_" + appWidgetId)
prefs.remove(PREF_PREFIX_KEY + "truncate_" + appWidgetId)
prefs.remove(PREF_PREFIX_KEY + appWidgetId + "_args")
prefs.remove(PREF_PREFIX_KEY + appWidgetId + "_disableLineWrap")
prefs.remove(PREF_PREFIX_KEY + appWidgetId + "_truncateWidth")
prefs.apply()
}
6 changes: 6 additions & 0 deletions android/app/src/main/res/drawable/icon_alert_yellow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- By OOjs UI Team and other contributors, MIT, https://commons.wikimedia.org/w/index.php?curid=78132716 -->
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="20" android:viewportWidth="20" android:width="24dp">

<path android:fillColor="#FFffcc33" android:pathData="M19.64,16.36L11.53,2.3A1.85,1.85 0,0 0,10 1.21,1.85 1.85,0 0,0 8.48,2.3L0.36,16.36C-0.48,17.81 0.21,19 1.88,19h16.24c1.67,0 2.36,-1.19 1.52,-2.64zM11,16L9,16v-2h2zM11,12L9,12L9,6h2z"/>

</vector>

0 comments on commit 37f292e

Please sign in to comment.