Skip to content

Commit fcac667

Browse files
authored
Merge pull request #53 from LeanBitLab/jules-add-ram-usage-16726876618766929699
Add RAM Usage feature to the widget
2 parents 33c177c + 9104157 commit fcac667

16 files changed

Lines changed: 250 additions & 15 deletions

app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ class AwidgetProvider : AppWidgetProvider() {
269269
val showStorage = prefs.getBoolean("show_storage", false)
270270
val sizeStorage = prefs.getFloat("size_storage", 14f)
271271

272+
val showRam = prefs.getBoolean("show_ram", false)
273+
val sizeRam = prefs.getFloat("size_ram", 14f)
274+
272275
var showTasks = prefs.getBoolean("show_tasks", false)
273276
if (showTasks && androidx.core.content.ContextCompat.checkSelfPermission(context, PERMISSION_READ_TASKS_ORG) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
274277
showTasks = false
@@ -477,6 +480,7 @@ class AwidgetProvider : AppWidgetProvider() {
477480
}
478481
if (showData) updateDataUsage(context, tickViews, prefs)
479482
if (showStorage) updateStorageStats(context, tickViews, prefs)
483+
if (showRam) updateRamStats(context, tickViews, prefs)
480484
return tickViews
481485
} else if (mode == UpdateMode.CALENDAR_ONLY) {
482486
val calViews = RemoteViews(context.packageName, layoutId)
@@ -686,6 +690,14 @@ class AwidgetProvider : AppWidgetProvider() {
686690
updateStorageStats(context, views, prefs)
687691
}
688692

693+
// --- RAM ---
694+
views.setViewVisibility(R.id.text_ram, if (showRam) android.view.View.VISIBLE else android.view.View.GONE)
695+
if (showRam) {
696+
views.setTextViewTextSize(R.id.text_ram, android.util.TypedValue.COMPLEX_UNIT_SP, sizeRam)
697+
views.setTextColor(R.id.text_ram, secondaryColor)
698+
updateRamStats(context, views, prefs)
699+
}
700+
689701
// --- Step Counter ---
690702
views.setViewVisibility(R.id.text_steps, if (showSteps) android.view.View.VISIBLE else android.view.View.GONE)
691703
if (showSteps) {
@@ -751,6 +763,7 @@ class AwidgetProvider : AppWidgetProvider() {
751763
StackEntry(R.id.text_weather_condition, showWeather, sizeWeather, "show_weather_condition"),
752764
StackEntry(R.id.text_data_usage, showData, sizeData, "show_data_usage"),
753765
StackEntry(R.id.text_storage, showStorage, sizeStorage, "show_storage"),
766+
StackEntry(R.id.text_ram, showRam, sizeRam, "show_ram"),
754767
StackEntry(R.id.text_steps, showSteps, sizeSteps, "show_steps"),
755768
StackEntry(R.id.text_screen_time, showScreenTime, sizeScreenTime, "show_screen_time")
756769
)
@@ -810,6 +823,10 @@ class AwidgetProvider : AppWidgetProvider() {
810823
val storagePendingIntent = PendingIntent.getActivity(context, 3, storageIntent, PendingIntent.FLAG_IMMUTABLE)
811824
views.setOnClickPendingIntent(R.id.text_storage, storagePendingIntent)
812825

826+
// fallback to internal storage settings if memory card settings not available or device specific
827+
val ramPendingIntent = PendingIntent.getActivity(context, 10, Intent(android.provider.Settings.ACTION_INTERNAL_STORAGE_SETTINGS), PendingIntent.FLAG_IMMUTABLE)
828+
views.setOnClickPendingIntent(R.id.text_ram, ramPendingIntent)
829+
813830
val dataIntent = Intent(android.provider.Settings.ACTION_DATA_USAGE_SETTINGS)
814831
val dataPendingIntent = PendingIntent.getActivity(context, 4, dataIntent, PendingIntent.FLAG_IMMUTABLE)
815832
views.setOnClickPendingIntent(R.id.text_data_usage, dataPendingIntent)
@@ -1294,6 +1311,29 @@ class AwidgetProvider : AppWidgetProvider() {
12941311
}
12951312
}
12961313

1314+
private fun updateRamStats(context: Context, views: RemoteViews, prefs: android.content.SharedPreferences) {
1315+
try {
1316+
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
1317+
val memoryInfo = android.app.ActivityManager.MemoryInfo()
1318+
activityManager.getMemoryInfo(memoryInfo)
1319+
val freeBytes = memoryInfo.availMem
1320+
1321+
val gb = freeBytes / (1024f * 1024f * 1024f)
1322+
1323+
val gbStr = String.format("%.1f", gb)
1324+
val span = android.text.SpannableString("$gbStr GB")
1325+
span.setSpan(android.text.style.RelativeSizeSpan(0.5f), gbStr.length, gbStr.length + 3, android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) // GB
1326+
1327+
if (prefs.getBoolean("bold_ram", false)) {
1328+
span.setSpan(android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, span.length, android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
1329+
}
1330+
1331+
views.setTextViewText(R.id.text_ram, span)
1332+
} catch (e: Exception) {
1333+
views.setTextViewText(R.id.text_ram, "Err")
1334+
}
1335+
}
1336+
12971337
private fun loadStepCount(context: Context, views: RemoteViews, prefs: android.content.SharedPreferences) {
12981338
try {
12991339
val totalSteps = prefs.getFloat("last_total_steps", 0f)

app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ class MainActivity : AppCompatActivity() {
711711
ReorderItem("show_weather_condition", getString(R.string.section_weather_condition), prefs.getBoolean("show_weather_condition", false)),
712712
ReorderItem("show_data_usage", getString(R.string.section_data_usage), prefs.getBoolean("show_data_usage", false)),
713713
ReorderItem("show_storage", getString(R.string.section_storage), prefs.getBoolean("show_storage", false)),
714+
ReorderItem("show_ram", getString(R.string.section_ram), prefs.getBoolean("show_ram", false)),
714715
ReorderItem("show_steps", getString(R.string.section_steps), prefs.getBoolean("show_steps", false)),
715716
ReorderItem("show_screen_time", getString(R.string.section_screen_time), prefs.getBoolean("show_screen_time", false))
716717
)
@@ -853,6 +854,7 @@ class MainActivity : AppCompatActivity() {
853854
setupWeatherSection()
854855
setupDataUsageSection()
855856
setupStorageSection()
857+
setupRamSection()
856858
setupStepsSection()
857859
setupScreenTimeSection()
858860
setupKeepAliveSection()
@@ -1024,6 +1026,18 @@ class MainActivity : AppCompatActivity() {
10241026
checkAllPermissions()
10251027
}
10261028
}
1029+
private fun setupRamSection() {
1030+
// RAM
1031+
bindFoldedSection(
1032+
R.id.header_ram, R.drawable.ic_storage, getString(R.string.section_ram),
1033+
R.id.content_ram, R.id.row_ram_toggle,
1034+
"show_ram", false,
1035+
sizeRowId = R.id.row_ram_size, prefSizeKey = "size_ram", defSize = 14f, minSize = 10f, maxSize = 74f,
1036+
isContent = true
1037+
).also { it.tag = "ram" }
1038+
bindToggle(R.id.row_ram_bold, "Bold Text", "bold_ram", false)
1039+
}
1040+
10271041
private fun setupStorageSection() {
10281042
// Storage
10291043
bindFoldedSection(
@@ -1394,7 +1408,7 @@ class MainActivity : AppCompatActivity() {
13941408
"show_time" to true, "size_time" to 58f,
13951409
"show_date" to true, "size_date" to 16f,
13961410
"show_battery" to false, "show_temp" to false,
1397-
"show_storage" to false, "show_data_usage" to false,
1411+
"show_storage" to false, "show_ram" to false, "show_data_usage" to false,
13981412
"show_steps" to false, "show_screen_time" to false,
13991413
"show_next_alarm" to false, "show_world_clock" to false,
14001414
"show_events" to false, "show_tasks" to false,
@@ -1412,7 +1426,7 @@ class MainActivity : AppCompatActivity() {
14121426
"show_date" to true, "size_date" to 14f,
14131427
"show_battery" to true, "size_battery" to 28f, "bold_battery" to true,
14141428
"show_temp" to true, "size_temp" to 18f, "bold_temp" to true,
1415-
"show_storage" to false, "show_data_usage" to false,
1429+
"show_storage" to false, "show_ram" to false, "show_data_usage" to false,
14161430
"show_steps" to false, "show_screen_time" to false,
14171431
"show_next_alarm" to true, "size_next_alarm" to 12f,
14181432
"show_world_clock" to false,
@@ -1425,15 +1439,15 @@ class MainActivity : AppCompatActivity() {
14251439
"date_color_idx" to 2, "date_color_r" to 255, "date_color_g" to 0, "date_color_b" to 180,
14261440
"outline_color_idx" to 2, "outline_color_r" to 0, "outline_color_g" to 200, "outline_color_b" to 255,
14271441
"bg_color_idx" to 2, "bg_color_r" to 10, "bg_color_g" to 10, "bg_color_b" to 20,
1428-
"widget_right_column_order" to "show_battery,show_temp,show_weather_condition,show_data_usage,show_storage,show_steps,show_screen_time"
1442+
"widget_right_column_order" to "show_battery,show_temp,show_weather_condition,show_data_usage,show_storage,show_ram,show_steps,show_screen_time"
14291443
)),
14301444
// Cockpit: green on dark, monospace, info-heavy, terminal look
14311445
Preset("cockpit", "Cockpit", mapOf(
14321446
"show_time" to true, "size_time" to 42f,
14331447
"show_date" to true, "size_date" to 14f,
14341448
"show_battery" to true, "size_battery" to 18f, "bold_battery" to false,
14351449
"show_temp" to true, "size_temp" to 16f, "bold_temp" to false,
1436-
"show_storage" to true, "size_storage" to 14f, "bold_storage" to false,
1450+
"show_storage" to true, "size_storage" to 14f, "bold_storage" to false, "show_ram" to false, "size_ram" to 14f, "bold_ram" to false,
14371451
"show_data_usage" to true, "size_data" to 14f, "bold_data_usage" to false,
14381452
"show_steps" to false, "show_screen_time" to false,
14391453
"show_next_alarm" to true, "size_next_alarm" to 14f,
@@ -1447,14 +1461,14 @@ class MainActivity : AppCompatActivity() {
14471461
"date_color_idx" to 2, "date_color_r" to 0, "date_color_g" to 200, "date_color_b" to 80,
14481462
"outline_color_idx" to 2, "outline_color_r" to 0, "outline_color_g" to 120, "outline_color_b" to 40,
14491463
"bg_color_idx" to 2, "bg_color_r" to 5, "bg_color_g" to 15, "bg_color_b" to 5,
1450-
"widget_right_column_order" to "show_battery,show_storage,show_data_usage,show_temp,show_weather_condition,show_steps,show_screen_time"
1464+
"widget_right_column_order" to "show_battery,show_storage,show_ram,show_data_usage,show_temp,show_weather_condition,show_steps,show_screen_time"
14511465
)),
14521466
// Sunset: warm oranges/gold, serif font, elegant minimal
14531467
Preset("sunset", "Sunset", mapOf(
14541468
"show_time" to true, "size_time" to 54f,
14551469
"show_date" to true, "size_date" to 18f,
14561470
"show_battery" to true, "size_battery" to 24f, "bold_battery" to true,
1457-
"show_temp" to false, "show_storage" to false,
1471+
"show_temp" to false, "show_storage" to false, "show_ram" to false,
14581472
"show_data_usage" to false, "show_steps" to false,
14591473
"show_screen_time" to false,
14601474
"show_next_alarm" to true, "size_next_alarm" to 14f,
@@ -1467,14 +1481,14 @@ class MainActivity : AppCompatActivity() {
14671481
"text_color_secondary_idx" to 2, "text_color_secondary_r" to 230, "text_color_secondary_g" to 140, "text_color_secondary_b" to 60,
14681482
"date_color_idx" to 2, "date_color_r" to 255, "date_color_g" to 120, "date_color_b" to 50,
14691483
"bg_color_idx" to 2, "bg_color_r" to 30, "bg_color_g" to 15, "bg_color_b" to 8,
1470-
"widget_right_column_order" to "show_battery,show_temp,show_weather_condition,show_data_usage,show_storage,show_steps,show_screen_time"
1484+
"widget_right_column_order" to "show_battery,show_temp,show_weather_condition,show_data_usage,show_storage,show_ram,show_steps,show_screen_time"
14711485
)),
14721486
// Monochrome: white outline, all white text, medium font, classic layout
14731487
Preset("monochrome", "Monochrome", mapOf(
14741488
"show_time" to true, "size_time" to 48f,
14751489
"show_date" to true, "size_date" to 14f,
14761490
"show_battery" to true, "size_battery" to 22f, "bold_battery" to false,
1477-
"show_temp" to false, "show_storage" to true, "size_storage" to 14f,
1491+
"show_temp" to false, "show_storage" to true, "size_storage" to 14f, "show_ram" to false, "size_ram" to 14f,
14781492
"show_data_usage" to false, "show_steps" to false,
14791493
"show_screen_time" to false,
14801494
"show_next_alarm" to true, "size_next_alarm" to 14f,
@@ -1488,14 +1502,14 @@ class MainActivity : AppCompatActivity() {
14881502
"date_color_idx" to 2, "date_color_r" to 200, "date_color_g" to 200, "date_color_b" to 200,
14891503
"outline_color_idx" to 2, "outline_color_r" to 100, "outline_color_g" to 100, "outline_color_b" to 100,
14901504
"bg_color_idx" to 2, "bg_color_r" to 25, "bg_color_g" to 25, "bg_color_b" to 25,
1491-
"widget_right_column_order" to "show_battery,show_storage,show_temp,show_weather_condition,show_data_usage,show_steps,show_screen_time"
1505+
"widget_right_column_order" to "show_battery,show_storage,show_ram,show_temp,show_weather_condition,show_data_usage,show_steps,show_screen_time"
14921506
)),
14931507
// Snowfall: icy blues, light font, airy feel
14941508
Preset("snowfall", "Snowfall", mapOf(
14951509
"show_time" to true, "size_time" to 60f,
14961510
"show_date" to true, "size_date" to 16f,
14971511
"show_battery" to false, "show_temp" to true, "size_temp" to 20f, "bold_temp" to false,
1498-
"show_storage" to false, "show_data_usage" to false,
1512+
"show_storage" to false, "show_ram" to false, "show_data_usage" to false,
14991513
"show_steps" to false, "show_screen_time" to false,
15001514
"show_next_alarm" to false,
15011515
"show_world_clock" to false,
@@ -1507,7 +1521,7 @@ class MainActivity : AppCompatActivity() {
15071521
"text_color_secondary_idx" to 2, "text_color_secondary_r" to 130, "text_color_secondary_g" to 180, "text_color_secondary_b" to 230,
15081522
"date_color_idx" to 2, "date_color_r" to 100, "date_color_g" to 170, "date_color_b" to 255,
15091523
"bg_color_idx" to 2, "bg_color_r" to 10, "bg_color_g" to 20, "bg_color_b" to 40,
1510-
"widget_right_column_order" to "show_temp,show_battery,show_weather_condition,show_data_usage,show_storage,show_steps,show_screen_time"
1524+
"widget_right_column_order" to "show_temp,show_battery,show_weather_condition,show_data_usage,show_storage,show_ram,show_steps,show_screen_time"
15111525
))
15121526
)
15131527

@@ -1704,10 +1718,10 @@ class MainActivity : AppCompatActivity() {
17041718

17051719
// Subset Limit: Battery, Weather, Temp, Data, Storage (Max 5 allowed now to fit stack)
17061720
val subsetCount = contentSwitches.count {
1707-
it.isChecked && (it.tag == "battery" || it.tag == "weather_condition" || it.tag == "temp" || it.tag == "data" || it.tag == "storage")
1721+
it.isChecked && (it.tag == "battery" || it.tag == "weather_condition" || it.tag == "temp" || it.tag == "data" || it.tag == "storage" || it.tag == "ram")
17081722
}
17091723

1710-
if (subsetCount > 5) {
1724+
if (subsetCount > 6) {
17111725
com.google.android.material.snackbar.Snackbar.make(
17121726
findViewById(R.id.fab_update),
17131727
getString(R.string.error_max_subset_items),

app/src/main/res/layout/activity_main.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,53 @@
11191119
</LinearLayout>
11201120
</com.google.android.material.card.MaterialCardView>
11211121

1122+
<!-- FEATURE: RAM -->
1123+
<com.google.android.material.card.MaterialCardView
1124+
android:id="@+id/card_ram"
1125+
style="@style/Widget.Material3.CardView.Outlined"
1126+
android:layout_width="match_parent"
1127+
android:layout_height="wrap_content"
1128+
android:layout_marginBottom="8dp"
1129+
app:cardBackgroundColor="?attr/colorSurfaceContainerHigh">
1130+
1131+
<LinearLayout
1132+
android:layout_width="match_parent"
1133+
android:layout_height="wrap_content"
1134+
android:orientation="vertical">
1135+
1136+
<include layout="@layout/settings_folded_header"
1137+
android:id="@+id/header_ram"
1138+
android:layout_width="match_parent"
1139+
android:layout_height="wrap_content"/>
1140+
1141+
<LinearLayout
1142+
android:id="@+id/content_ram"
1143+
android:layout_width="match_parent"
1144+
android:layout_height="wrap_content"
1145+
android:orientation="vertical"
1146+
android:visibility="gone"
1147+
android:paddingStart="8dp"
1148+
android:paddingEnd="8dp"
1149+
android:paddingBottom="8dp">
1150+
1151+
<include layout="@layout/settings_toggle_row"
1152+
android:id="@+id/row_ram_toggle"
1153+
android:layout_width="match_parent"
1154+
android:layout_height="wrap_content"/>
1155+
1156+
<include layout="@layout/settings_slider_row"
1157+
android:id="@+id/row_ram_size"
1158+
android:layout_width="match_parent"
1159+
android:layout_height="wrap_content"/>
1160+
1161+
<include layout="@layout/settings_toggle_row"
1162+
android:id="@+id/row_ram_bold"
1163+
android:layout_width="match_parent"
1164+
android:layout_height="wrap_content"/>
1165+
</LinearLayout>
1166+
</LinearLayout>
1167+
</com.google.android.material.card.MaterialCardView>
1168+
11221169
<!-- FEATURE: Steps -->
11231170
<com.google.android.material.card.MaterialCardView
11241171
android:id="@+id/card_steps"

app/src/main/res/layout/widget_layout.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@
106106
android:layout_alignParentEnd="true"
107107
android:layout_alignParentTop="true"
108108
android:layout_marginTop="2dp"/>
109+
<TextView
110+
android:id="@+id/text_ram"
111+
android:layout_width="wrap_content"
112+
android:layout_height="wrap_content"
113+
android:text=""
114+
android:textColor="#CCFFFFFF"
115+
android:textSize="14sp"
116+
android:visibility="gone"
117+
android:layout_alignParentEnd="true"
118+
android:layout_alignParentTop="true"
119+
android:layout_marginTop="2dp"/>
109120

110121
<!-- Steps (Below Storage) -->
111122
<TextView

app/src/main/res/layout/widget_layout_black.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@
109109
android:layout_alignParentTop="true"
110110
android:layout_alignParentEnd="true"
111111
android:layout_marginTop="2dp"/>
112+
<TextView
113+
android:id="@+id/text_ram"
114+
android:layout_width="wrap_content"
115+
android:layout_height="wrap_content"
116+
android:text=""
117+
android:textColor="#CCFFFFFF"
118+
android:textSize="14sp"
119+
android:visibility="gone"
120+
android:layout_alignParentTop="true"
121+
android:layout_alignParentEnd="true"
122+
android:layout_marginTop="2dp"/>
112123

113124
<!-- Steps (Below Storage) -->
114125
<TextView

app/src/main/res/layout/widget_layout_condensed.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@
109109
android:layout_alignParentEnd="true"
110110
android:layout_alignParentTop="true"
111111
android:layout_marginTop="2dp"/>
112+
<TextView
113+
android:id="@+id/text_ram"
114+
android:layout_width="wrap_content"
115+
android:layout_height="wrap_content"
116+
android:text=""
117+
android:textColor="#CCFFFFFF"
118+
android:textSize="14sp"
119+
android:visibility="gone"
120+
android:layout_alignParentEnd="true"
121+
android:layout_alignParentTop="true"
122+
android:layout_marginTop="2dp"/>
112123

113124
<!-- Steps (Below Storage) -->
114125
<TextView

0 commit comments

Comments
 (0)