Skip to content

Commit

Permalink
android widget: update widget every 31 minutes and when you touch it
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Jan 11, 2025
1 parent 94d7e12 commit f2148d9
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@

package org.toni.customfetch_android.widget

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
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

const val WIDGET_CLICK_ACTION = "org.toni.customfetch_android.WIDGET_CLICK"

/**
* Implementation of App Widget functionality.
* App Widget Configuration implemented in [customfetchConfigureActivity]
Expand All @@ -46,8 +50,9 @@ class customfetch : AppWidgetProvider() {
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds)
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}

override fun onDeleted(context: Context, appWidgetIds: IntArray) {
Expand All @@ -57,6 +62,22 @@ class customfetch : AppWidgetProvider() {
}
}

override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
Log.d("onReceiveTest", "intent.action = ${intent?.action}")
if (context != null && intent?.action == WIDGET_CLICK_ACTION) {
val appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID
)

if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
Log.d("onReceiveTest", "Widget clicked!")
updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId)
}
}
}

override fun onAppWidgetOptionsChanged(
context: Context,
appWidgetManager: AppWidgetManager,
Expand All @@ -65,15 +86,6 @@ class customfetch : AppWidgetProvider() {
) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}

override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created

}

override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
}

// https://stackoverflow.com/a/58501760
Expand Down Expand Up @@ -117,7 +129,6 @@ internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int

) {
val disableLineWrap = getDisableLineWrap(context, appWidgetId)
val bgColor = getBgColor(context, appWidgetId)
Expand Down Expand Up @@ -145,8 +156,22 @@ internal fun updateAppWidget(
textPaint
)

// needed for when touching the widget
val intent = Intent(context, customfetch::class.java).apply {
action = WIDGET_CLICK_ACTION
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
}

val pendingIntent = PendingIntent.getBroadcast(
context,
appWidgetId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.customfetch)
views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)
views.setTextViewText(R.id.customfetch_text, parsedContent)
views.setInt(R.id.widget_root, "setBackgroundColor", bgColor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class customfetchConfigureActivity : Activity() {
binding.argumentsConfigure.text.toString(),
binding.additionalTruncateWidth.text.toString(),
disableLineWrap,
bgColor)
bgColor
)

// It is the responsibility of the configuration activity to update the app widget
val appWidgetManager = AppWidgetManager.getInstance(context)
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/xml/customfetch_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
android:resizeMode="horizontal|vertical"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:updatePeriodMillis="10000"
android:updatePeriodMillis="1860000"
android:widgetCategory="home_screen" />
2 changes: 1 addition & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ layout = [
"${auto}Terminal: $<user.terminal>",
"${auto}Shell: $<user.shell>",
"${auto}Packages: $<os.pkgs>",)#"
#if !ANDROID_APP
#if !CF_ANDROID
R"#(
"${auto}Theme: $<theme-gtk-all.name>",
"${auto}Icons: $<theme-gtk-all.icons>",
Expand Down
2 changes: 1 addition & 1 deletion include/platform.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _PLATFORM_H
#define _PLATFORM_H

#if (defined(__ANDROID__) || defined(ANDROID_API))
#if (defined(__ANDROID__) || defined(ANDROID_API) || ANDROID_APP)
# define CF_ANDROID 1
#else
# define CF_ANDROID 0
Expand Down
7 changes: 7 additions & 0 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ using systemInfo_t =
// used in systemInfo_t most of the time
using variant = std::variant<std::string, size_t, double>;

#if !ANDROID_APP
#define CHECK_INIT(x) if (x)
#else
#define CHECK_INIT(x) if (true)
#endif

namespace Query
{

Expand Down Expand Up @@ -187,6 +193,7 @@ class CPU

// private:
double freq_max_cpuinfo = 0;
// only in Android
std::string modelname;
std::string vendor;
};
Expand Down
4 changes: 2 additions & 2 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args)

if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
// idk, trick the diviser
// idk, trick the divider
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount() * byte_unit, byte_unit);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.total_amount() * byte_unit, byte_unit);

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

if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
// idk, trick the diviser
// idk, trick the divider
byte_units.at(USED) = auto_devide_bytes(query_ram.swap_used_amount() * byte_unit, byte_unit);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.swap_total_amount() * byte_unit, byte_unit);

Expand Down
2 changes: 1 addition & 1 deletion src/query/android/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static System::System_t get_system_infos()

System::System()
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
if (uname(&m_uname_infos) != 0)
die("uname() failed: {}\nCould not get system infos", strerror(errno));
Expand Down
2 changes: 1 addition & 1 deletion src/query/android/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static std::string get_shell_name(const std::string_view shell_path)

User::User() noexcept
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
const uid_t uid = getuid();

Expand Down
2 changes: 1 addition & 1 deletion src/query/unix/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ static double get_cpu_temp()

CPU::CPU() noexcept
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
m_cpu_infos = get_cpu_infos();
m_bInit = true;
Expand Down
2 changes: 1 addition & 1 deletion src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Disk::Disk(const std::string& path, systemInfo_t& queried_paths)
FILE* mountsFile = setmntent("/proc/mounts", "r");
if (mountsFile == NULL)
{
perror(_("setmntent"));
perror("setmntent");
error(_("setmntent() failed. Could not get disk info"));
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/query/unix/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ static RAM::RAM_t get_amount() noexcept
return memory_infos;
}

std::string line;
static u_short iter_index = 0;
std::string line;
std::uint16_t iter_index = 0;
while (std::getline(file, line) && iter_index < 4)
{
if (hasStart(line, "MemAvailable:"))
Expand Down Expand Up @@ -107,7 +107,7 @@ static RAM::RAM_t get_amount() noexcept

RAM::RAM() noexcept
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
m_memory_infos = get_amount();
m_bInit = true;
Expand Down
6 changes: 3 additions & 3 deletions src/query/unix/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ static System::System_t get_system_infos_os_releases()
}

// get OS /etc/os-release infos
u_short iter_index = 0;
std::string line;
std::uint16_t iter_index = 0;
std::string line;
while (std::getline(os_release_file, line) && iter_index < 5)
{
if (hasStart(line, "PRETTY_NAME="))
Expand All @@ -166,7 +166,7 @@ static System::System_t get_system_infos_os_releases()

System::System()
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
if (uname(&m_uname_infos) != 0)
die(_("uname() failed: {}\nCould not get system infos"), strerror(errno));
Expand Down
2 changes: 1 addition & 1 deletion src/query/unix/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ static std::string get_term_version(std::string_view term_name)

User::User() noexcept
{
if (!m_bInit)
CHECK_INIT(!m_bInit)
{
const uid_t uid = getuid();

Expand Down

0 comments on commit f2148d9

Please sign in to comment.