Skip to content

Commit f2148d9

Browse files
committed
android widget: update widget every 31 minutes and when you touch it
1 parent 94d7e12 commit f2148d9

File tree

14 files changed

+61
-28
lines changed

14 files changed

+61
-28
lines changed

android/app/src/main/java/org/toni/customfetch_android/widget/customfetch.kt

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@
2525

2626
package org.toni.customfetch_android.widget
2727

28+
import android.app.PendingIntent
2829
import android.appwidget.AppWidgetManager
2930
import android.appwidget.AppWidgetProvider
3031
import android.content.Context
32+
import android.content.Intent
3133
import android.content.res.Configuration.ORIENTATION_PORTRAIT
3234
import android.os.Bundle
3335
import android.text.TextPaint
3436
import android.util.Log
3537
import android.widget.RemoteViews
3638
import org.toni.customfetch_android.R
3739

40+
const val WIDGET_CLICK_ACTION = "org.toni.customfetch_android.WIDGET_CLICK"
41+
3842
/**
3943
* Implementation of App Widget functionality.
4044
* App Widget Configuration implemented in [customfetchConfigureActivity]
@@ -46,8 +50,9 @@ class customfetch : AppWidgetProvider() {
4650
appWidgetIds: IntArray
4751
) {
4852
// There may be multiple widgets active, so update all of them
49-
for (appWidgetId in appWidgetIds)
53+
for (appWidgetId in appWidgetIds) {
5054
updateAppWidget(context, appWidgetManager, appWidgetId)
55+
}
5156
}
5257

5358
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
@@ -57,6 +62,22 @@ class customfetch : AppWidgetProvider() {
5762
}
5863
}
5964

65+
override fun onReceive(context: Context?, intent: Intent?) {
66+
super.onReceive(context, intent)
67+
Log.d("onReceiveTest", "intent.action = ${intent?.action}")
68+
if (context != null && intent?.action == WIDGET_CLICK_ACTION) {
69+
val appWidgetId = intent.getIntExtra(
70+
AppWidgetManager.EXTRA_APPWIDGET_ID,
71+
AppWidgetManager.INVALID_APPWIDGET_ID
72+
)
73+
74+
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
75+
Log.d("onReceiveTest", "Widget clicked!")
76+
updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId)
77+
}
78+
}
79+
}
80+
6081
override fun onAppWidgetOptionsChanged(
6182
context: Context,
6283
appWidgetManager: AppWidgetManager,
@@ -65,15 +86,6 @@ class customfetch : AppWidgetProvider() {
6586
) {
6687
updateAppWidget(context, appWidgetManager, appWidgetId)
6788
}
68-
69-
override fun onEnabled(context: Context) {
70-
// Enter relevant functionality for when the first widget is created
71-
72-
}
73-
74-
override fun onDisabled(context: Context) {
75-
// Enter relevant functionality for when the last widget is disabled
76-
}
7789
}
7890

7991
// https://stackoverflow.com/a/58501760
@@ -117,7 +129,6 @@ internal fun updateAppWidget(
117129
context: Context,
118130
appWidgetManager: AppWidgetManager,
119131
appWidgetId: Int
120-
121132
) {
122133
val disableLineWrap = getDisableLineWrap(context, appWidgetId)
123134
val bgColor = getBgColor(context, appWidgetId)
@@ -145,8 +156,22 @@ internal fun updateAppWidget(
145156
textPaint
146157
)
147158

159+
// needed for when touching the widget
160+
val intent = Intent(context, customfetch::class.java).apply {
161+
action = WIDGET_CLICK_ACTION
162+
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
163+
}
164+
165+
val pendingIntent = PendingIntent.getBroadcast(
166+
context,
167+
appWidgetId,
168+
intent,
169+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
170+
)
171+
148172
// Construct the RemoteViews object
149173
val views = RemoteViews(context.packageName, R.layout.customfetch)
174+
views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)
150175
views.setTextViewText(R.id.customfetch_text, parsedContent)
151176
views.setInt(R.id.widget_root, "setBackgroundColor", bgColor);
152177

android/app/src/main/java/org/toni/customfetch_android/widget/customfetchConfigureActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class customfetchConfigureActivity : Activity() {
7878
binding.argumentsConfigure.text.toString(),
7979
binding.additionalTruncateWidth.text.toString(),
8080
disableLineWrap,
81-
bgColor)
81+
bgColor
82+
)
8283

8384
// It is the responsibility of the configuration activity to update the app widget
8485
val appWidgetManager = AppWidgetManager.getInstance(context)

android/app/src/main/res/xml/customfetch_info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
android:resizeMode="horizontal|vertical"
1111
android:targetCellWidth="1"
1212
android:targetCellHeight="1"
13-
android:updatePeriodMillis="10000"
13+
android:updatePeriodMillis="1860000"
1414
android:widgetCategory="home_screen" />

include/config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ layout = [
255255
"${auto}Terminal: $<user.terminal>",
256256
"${auto}Shell: $<user.shell>",
257257
"${auto}Packages: $<os.pkgs>",)#"
258-
#if !ANDROID_APP
258+
#if !CF_ANDROID
259259
R"#(
260260
"${auto}Theme: $<theme-gtk-all.name>",
261261
"${auto}Icons: $<theme-gtk-all.icons>",

include/platform.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _PLATFORM_H
22
#define _PLATFORM_H
33

4-
#if (defined(__ANDROID__) || defined(ANDROID_API))
4+
#if (defined(__ANDROID__) || defined(ANDROID_API) || ANDROID_APP)
55
# define CF_ANDROID 1
66
#else
77
# define CF_ANDROID 0

include/query.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ using systemInfo_t =
5151
// used in systemInfo_t most of the time
5252
using variant = std::variant<std::string, size_t, double>;
5353

54+
#if !ANDROID_APP
55+
#define CHECK_INIT(x) if (x)
56+
#else
57+
#define CHECK_INIT(x) if (true)
58+
#endif
59+
5460
namespace Query
5561
{
5662

@@ -187,6 +193,7 @@ class CPU
187193

188194
// private:
189195
double freq_max_cpuinfo = 0;
196+
// only in Android
190197
std::string modelname;
191198
std::string vendor;
192199
};

src/parse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args)
17171717

17181718
if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
17191719
{
1720-
// idk, trick the diviser
1720+
// idk, trick the divider
17211721
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount() * byte_unit, byte_unit);
17221722
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.total_amount() * byte_unit, byte_unit);
17231723

@@ -1748,7 +1748,7 @@ void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args)
17481748

17491749
if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
17501750
{
1751-
// idk, trick the diviser
1751+
// idk, trick the divider
17521752
byte_units.at(USED) = auto_devide_bytes(query_ram.swap_used_amount() * byte_unit, byte_unit);
17531753
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.swap_total_amount() * byte_unit, byte_unit);
17541754

src/query/android/system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static System::System_t get_system_infos()
7373

7474
System::System()
7575
{
76-
if (!m_bInit)
76+
CHECK_INIT(!m_bInit)
7777
{
7878
if (uname(&m_uname_infos) != 0)
7979
die("uname() failed: {}\nCould not get system infos", strerror(errno));

src/query/android/user.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static std::string get_shell_name(const std::string_view shell_path)
5858

5959
User::User() noexcept
6060
{
61-
if (!m_bInit)
61+
CHECK_INIT(!m_bInit)
6262
{
6363
const uid_t uid = getuid();
6464

src/query/unix/cpu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ static double get_cpu_temp()
429429

430430
CPU::CPU() noexcept
431431
{
432-
if (!m_bInit)
432+
CHECK_INIT(!m_bInit)
433433
{
434434
m_cpu_infos = get_cpu_infos();
435435
m_bInit = true;

0 commit comments

Comments
 (0)