Skip to content

Commit 1c7f916

Browse files
Move Termux app specific logic out of NotificationUtils
1 parent 553913c commit 1c7f916

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

app/src/main/java/com/termux/app/utils/CrashUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.termux.shared.file.FileUtils;
1616
import com.termux.shared.models.ReportInfo;
1717
import com.termux.app.models.UserAction;
18+
import com.termux.shared.notification.TermuxNotificationUtils;
1819
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
1920
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
2021
import com.termux.shared.data.DataUtils;
@@ -131,7 +132,7 @@ public static void sendCrashReportNotification(final Context context, String log
131132
if (builder == null) return;
132133

133134
// Send the notification
134-
int nextNotificationId = NotificationUtils.getNextNotificationId(context);
135+
int nextNotificationId = TermuxNotificationUtils.getNextNotificationId(context);
135136
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
136137
if (notificationManager != null)
137138
notificationManager.notify(nextNotificationId, builder.build());

app/src/main/java/com/termux/app/utils/PluginUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.termux.shared.models.errors.Errno;
1717
import com.termux.shared.models.errors.Error;
1818
import com.termux.shared.notification.NotificationUtils;
19+
import com.termux.shared.notification.TermuxNotificationUtils;
1920
import com.termux.shared.shell.ResultSender;
2021
import com.termux.shared.shell.ShellUtils;
2122
import com.termux.shared.termux.AndroidUtils;
@@ -233,7 +234,7 @@ public static void sendPluginCommandErrorNotification(Context context, String lo
233234
if (builder == null) return;
234235

235236
// Send the notification
236-
int nextNotificationId = NotificationUtils.getNextNotificationId(context);
237+
int nextNotificationId = TermuxNotificationUtils.getNextNotificationId(context);
237238
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
238239
if (notificationManager != null)
239240
notificationManager.notify(nextNotificationId, builder.build());

termux-shared/src/main/java/com/termux/shared/notification/NotificationUtils.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import androidx.annotation.Nullable;
1111

1212
import com.termux.shared.logger.Logger;
13-
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
14-
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
15-
import com.termux.shared.termux.TermuxConstants;
1613

1714
public class NotificationUtils {
1815

@@ -49,35 +46,6 @@ public static NotificationManager getNotificationManager(final Context context)
4946
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
5047
}
5148

52-
/**
53-
* Try to get the next unique notification id that isn't already being used by the app.
54-
*
55-
* Termux app and its plugin must use unique notification ids from the same pool due to usage of android:sharedUserId.
56-
* https://commonsware.com/blog/2017/06/07/jobscheduler-job-ids-libraries.html
57-
*
58-
* @param context The {@link Context} for operations.
59-
* @return Returns the notification id that should be safe to use.
60-
*/
61-
public synchronized static int getNextNotificationId(final Context context) {
62-
if (context == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
63-
64-
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
65-
if (preferences == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
66-
67-
int lastNotificationId = preferences.getLastNotificationId();
68-
69-
int nextNotificationId = lastNotificationId + 1;
70-
while(nextNotificationId == TermuxConstants.TERMUX_APP_NOTIFICATION_ID || nextNotificationId == TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_ID) {
71-
nextNotificationId++;
72-
}
73-
74-
if (nextNotificationId == Integer.MAX_VALUE || nextNotificationId < 0)
75-
nextNotificationId = TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
76-
77-
preferences.setLastNotificationId(nextNotificationId);
78-
return nextNotificationId;
79-
}
80-
8149
/**
8250
* Get {@link Notification.Builder}.
8351
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.termux.shared.notification;
2+
3+
import android.content.Context;
4+
5+
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
6+
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
7+
import com.termux.shared.termux.TermuxConstants;
8+
9+
public class TermuxNotificationUtils {
10+
/**
11+
* Try to get the next unique notification id that isn't already being used by the app.
12+
*
13+
* Termux app and its plugin must use unique notification ids from the same pool due to usage of android:sharedUserId.
14+
* https://commonsware.com/blog/2017/06/07/jobscheduler-job-ids-libraries.html
15+
*
16+
* @param context The {@link Context} for operations.
17+
* @return Returns the notification id that should be safe to use.
18+
*/
19+
public synchronized static int getNextNotificationId(final Context context) {
20+
if (context == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
21+
22+
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
23+
if (preferences == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
24+
25+
int lastNotificationId = preferences.getLastNotificationId();
26+
27+
int nextNotificationId = lastNotificationId + 1;
28+
while(nextNotificationId == TermuxConstants.TERMUX_APP_NOTIFICATION_ID || nextNotificationId == TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_ID) {
29+
nextNotificationId++;
30+
}
31+
32+
if (nextNotificationId == Integer.MAX_VALUE || nextNotificationId < 0)
33+
nextNotificationId = TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
34+
35+
preferences.setLastNotificationId(nextNotificationId);
36+
return nextNotificationId;
37+
}
38+
}

0 commit comments

Comments
 (0)