mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Move Termux app specific logic out of NotificationUtils
This commit is contained in:
@@ -15,6 +15,7 @@ import com.termux.shared.notification.NotificationUtils;
|
|||||||
import com.termux.shared.file.FileUtils;
|
import com.termux.shared.file.FileUtils;
|
||||||
import com.termux.shared.models.ReportInfo;
|
import com.termux.shared.models.ReportInfo;
|
||||||
import com.termux.app.models.UserAction;
|
import com.termux.app.models.UserAction;
|
||||||
|
import com.termux.shared.notification.TermuxNotificationUtils;
|
||||||
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
|
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
|
||||||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
|
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
|
||||||
import com.termux.shared.data.DataUtils;
|
import com.termux.shared.data.DataUtils;
|
||||||
@@ -131,7 +132,7 @@ public class CrashUtils {
|
|||||||
if (builder == null) return;
|
if (builder == null) return;
|
||||||
|
|
||||||
// Send the notification
|
// Send the notification
|
||||||
int nextNotificationId = NotificationUtils.getNextNotificationId(context);
|
int nextNotificationId = TermuxNotificationUtils.getNextNotificationId(context);
|
||||||
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
|
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
|
||||||
if (notificationManager != null)
|
if (notificationManager != null)
|
||||||
notificationManager.notify(nextNotificationId, builder.build());
|
notificationManager.notify(nextNotificationId, builder.build());
|
||||||
|
@@ -16,6 +16,7 @@ import com.termux.shared.models.ResultData;
|
|||||||
import com.termux.shared.models.errors.Errno;
|
import com.termux.shared.models.errors.Errno;
|
||||||
import com.termux.shared.models.errors.Error;
|
import com.termux.shared.models.errors.Error;
|
||||||
import com.termux.shared.notification.NotificationUtils;
|
import com.termux.shared.notification.NotificationUtils;
|
||||||
|
import com.termux.shared.notification.TermuxNotificationUtils;
|
||||||
import com.termux.shared.shell.ResultSender;
|
import com.termux.shared.shell.ResultSender;
|
||||||
import com.termux.shared.shell.ShellUtils;
|
import com.termux.shared.shell.ShellUtils;
|
||||||
import com.termux.shared.termux.AndroidUtils;
|
import com.termux.shared.termux.AndroidUtils;
|
||||||
@@ -233,7 +234,7 @@ public class PluginUtils {
|
|||||||
if (builder == null) return;
|
if (builder == null) return;
|
||||||
|
|
||||||
// Send the notification
|
// Send the notification
|
||||||
int nextNotificationId = NotificationUtils.getNextNotificationId(context);
|
int nextNotificationId = TermuxNotificationUtils.getNextNotificationId(context);
|
||||||
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
|
NotificationManager notificationManager = NotificationUtils.getNotificationManager(context);
|
||||||
if (notificationManager != null)
|
if (notificationManager != null)
|
||||||
notificationManager.notify(nextNotificationId, builder.build());
|
notificationManager.notify(nextNotificationId, builder.build());
|
||||||
|
@@ -10,9 +10,6 @@ import android.os.Build;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.termux.shared.logger.Logger;
|
import com.termux.shared.logger.Logger;
|
||||||
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
|
|
||||||
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
|
|
||||||
import com.termux.shared.termux.TermuxConstants;
|
|
||||||
|
|
||||||
public class NotificationUtils {
|
public class NotificationUtils {
|
||||||
|
|
||||||
@@ -49,35 +46,6 @@ public class NotificationUtils {
|
|||||||
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to get the next unique notification id that isn't already being used by the app.
|
|
||||||
*
|
|
||||||
* Termux app and its plugin must use unique notification ids from the same pool due to usage of android:sharedUserId.
|
|
||||||
* https://commonsware.com/blog/2017/06/07/jobscheduler-job-ids-libraries.html
|
|
||||||
*
|
|
||||||
* @param context The {@link Context} for operations.
|
|
||||||
* @return Returns the notification id that should be safe to use.
|
|
||||||
*/
|
|
||||||
public synchronized static int getNextNotificationId(final Context context) {
|
|
||||||
if (context == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
|
||||||
|
|
||||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
|
||||||
if (preferences == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
|
||||||
|
|
||||||
int lastNotificationId = preferences.getLastNotificationId();
|
|
||||||
|
|
||||||
int nextNotificationId = lastNotificationId + 1;
|
|
||||||
while(nextNotificationId == TermuxConstants.TERMUX_APP_NOTIFICATION_ID || nextNotificationId == TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_ID) {
|
|
||||||
nextNotificationId++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nextNotificationId == Integer.MAX_VALUE || nextNotificationId < 0)
|
|
||||||
nextNotificationId = TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
|
||||||
|
|
||||||
preferences.setLastNotificationId(nextNotificationId);
|
|
||||||
return nextNotificationId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get {@link Notification.Builder}.
|
* Get {@link Notification.Builder}.
|
||||||
*
|
*
|
||||||
|
@@ -0,0 +1,38 @@
|
|||||||
|
package com.termux.shared.notification;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
|
||||||
|
import com.termux.shared.settings.preferences.TermuxPreferenceConstants;
|
||||||
|
import com.termux.shared.termux.TermuxConstants;
|
||||||
|
|
||||||
|
public class TermuxNotificationUtils {
|
||||||
|
/**
|
||||||
|
* Try to get the next unique notification id that isn't already being used by the app.
|
||||||
|
*
|
||||||
|
* Termux app and its plugin must use unique notification ids from the same pool due to usage of android:sharedUserId.
|
||||||
|
* https://commonsware.com/blog/2017/06/07/jobscheduler-job-ids-libraries.html
|
||||||
|
*
|
||||||
|
* @param context The {@link Context} for operations.
|
||||||
|
* @return Returns the notification id that should be safe to use.
|
||||||
|
*/
|
||||||
|
public synchronized static int getNextNotificationId(final Context context) {
|
||||||
|
if (context == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
||||||
|
|
||||||
|
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context);
|
||||||
|
if (preferences == null) return TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
||||||
|
|
||||||
|
int lastNotificationId = preferences.getLastNotificationId();
|
||||||
|
|
||||||
|
int nextNotificationId = lastNotificationId + 1;
|
||||||
|
while(nextNotificationId == TermuxConstants.TERMUX_APP_NOTIFICATION_ID || nextNotificationId == TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_ID) {
|
||||||
|
nextNotificationId++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextNotificationId == Integer.MAX_VALUE || nextNotificationId < 0)
|
||||||
|
nextNotificationId = TermuxPreferenceConstants.TERMUX_APP.DEFAULT_VALUE_KEY_LAST_NOTIFICATION_ID;
|
||||||
|
|
||||||
|
preferences.setLastNotificationId(nextNotificationId);
|
||||||
|
return nextNotificationId;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user