-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Termux app specific logic out of NotificationUtils
- Loading branch information
1 parent
553913c
commit 1c7f916
Showing
4 changed files
with
42 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
termux-shared/src/main/java/com/termux/shared/notification/TermuxNotificationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |