Skip to content

Commit d480a8a

Browse files
Added: Add SystemEventReceiver that receives the ACTION_BOOT_COMPLETED and ACTION_MY_PACKAGE_REPLACED intents to refresh widgets
This should help fix issues on any potential devices that fail to send the `AppWidgetManager.ACTION_APPWIDGET_UPDATE` intent that is normally sent by Android to the app for it to refresh its widgets, but do send other intents on boot or app updates and don't kill the app after app updates.
1 parent aed127d commit d480a8a

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464
android:taskAffinity=".TermuxLaunchShortcutActivity"
6565
android:theme="@android:style/Theme.NoDisplay" />
6666

67+
<receiver
68+
android:name=".receivers.SystemEventReceiver"
69+
android:exported="false">
70+
<intent-filter>
71+
<action android:name="android.intent.action.BOOT_COMPLETED" />
72+
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
73+
<action android:name="android.intent.action.MY_PACKAGE_UNSUSPENDED" />
74+
</intent-filter>
75+
</receiver>
76+
6777
<service
6878
android:name=".TermuxWidgetService"
6979
android:exported="false"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.termux.widget.receivers;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.SystemClock;
7+
8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
11+
import com.termux.shared.data.IntentUtils;
12+
import com.termux.shared.logger.Logger;
13+
import com.termux.widget.TermuxWidgetProvider;
14+
15+
public class SystemEventReceiver extends BroadcastReceiver {
16+
17+
public static final String LOG_TAG = "SystemEventReceiver";
18+
19+
@Override
20+
public void onReceive(@NonNull Context context, @Nullable Intent intent) {
21+
if (intent == null) return;
22+
23+
String action = intent.getAction();
24+
long upTime = SystemClock.uptimeMillis();
25+
26+
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
27+
Logger.logInfo(LOG_TAG, "Received intent at " + upTime + "ms since boot:\n" + IntentUtils.getIntentString(intent));
28+
} else {
29+
Logger.logDebug(LOG_TAG, "Received intent:\n" + IntentUtils.getIntentString(intent));
30+
}
31+
32+
if (action == null) return;
33+
34+
switch (action) {
35+
case Intent.ACTION_BOOT_COMPLETED:
36+
case Intent.ACTION_MY_PACKAGE_REPLACED:
37+
case Intent.ACTION_MY_PACKAGE_UNSUSPENDED:
38+
sendIntentToRefreshAllWidgets(context);
39+
break;
40+
default:
41+
Logger.logError(LOG_TAG, "Invalid action \"" + action + "\" passed to " + LOG_TAG);
42+
}
43+
}
44+
45+
46+
public synchronized void sendIntentToRefreshAllWidgets(@NonNull Context context) {
47+
TermuxWidgetProvider.sendIntentToRefreshAllWidgets(context, LOG_TAG);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)