65
65
import androidx .appcompat .view .menu .ActionMenuItemView ;
66
66
import androidx .appcompat .widget .ActionMenuView ;
67
67
import androidx .appcompat .widget .Toolbar ;
68
+ import androidx .core .app .NotificationManagerCompat ;
68
69
import androidx .core .content .ContextCompat ;
69
70
import androidx .core .content .FileProvider ;
70
71
import androidx .drawerlayout .widget .DrawerLayout ;
@@ -158,9 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
158
159
setUpNavigationDrawer (savedInstanceState );
159
160
160
161
loadDefaultFragmentView ();
161
- startAndBindService ();
162
- registerEventBus ();
163
- registerConscryptProvider ();
162
+
164
163
165
164
if (!Systems .hasUserGrantedAllNecessaryPermissions (this )){
166
165
LOG .debug ("Permission check - missing permissions" );
@@ -170,6 +169,10 @@ protected void onCreate(Bundle savedInstanceState) {
170
169
else {
171
170
LOG .debug ("Permission check - OK" );
172
171
172
+ startAndBindService ();
173
+ registerEventBus ();
174
+ registerConscryptProvider ();
175
+
173
176
if (preferenceHelper .shouldStartLoggingOnAppLaunch ()){
174
177
LOG .debug ("Start logging on app launch" );
175
178
EventBus .getDefault ().postSticky (new CommandEvents .RequestStartStop (true ));
@@ -269,6 +272,9 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
269
272
permissions .add (Manifest .permission .ACCESS_FINE_LOCATION );
270
273
permissions .add (Manifest .permission .READ_EXTERNAL_STORAGE );
271
274
permissions .add (Manifest .permission .WRITE_EXTERNAL_STORAGE );
275
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
276
+ permissions .add (Manifest .permission .POST_NOTIFICATIONS );
277
+ }
272
278
273
279
if (Build .VERSION .SDK_INT == Build .VERSION_CODES .Q ) {
274
280
// Only on Android 10 (Q), the permission dialog can include an 'Allow all the time'
@@ -1501,6 +1507,16 @@ private void startAndBindService() {
1501
1507
* Stops the service if it isn't logging. Also unbinds.
1502
1508
*/
1503
1509
private void stopAndUnbindServiceIfRequired () {
1510
+ if (!NotificationManagerCompat .from (this ).areNotificationsEnabled ()) {
1511
+ // Alright. Why is this needed.
1512
+ // If the notification permission has been revoked or not granted for whatever reason.
1513
+ // When the application opens, the service starts, then stops right away.
1514
+ // Android requires a notification to be shown for a foreground service within 5 seconds.
1515
+ // So the application crashes and comes back repeatedly. Very weird.
1516
+ // The answer - if notifications are disabled, don't unbind the service. It will stop on its own.
1517
+ // Might be related: https://stackoverflow.com/questions/73067939/start-foreground-service-after-notification-permission-was-disabled-causes-crash
1518
+ return ;
1519
+ }
1504
1520
if (session .isBoundToService ()) {
1505
1521
1506
1522
try {
@@ -1514,6 +1530,10 @@ private void stopAndUnbindServiceIfRequired() {
1514
1530
if (!session .isStarted ()) {
1515
1531
LOG .debug ("Stopping the service" );
1516
1532
try {
1533
+ // Stop service crashes if the intent is null. lol
1534
+ if (serviceIntent == null ){
1535
+ serviceIntent = new Intent (this , GpsLoggingService .class );
1536
+ }
1517
1537
stopService (serviceIntent );
1518
1538
} catch (Exception e ) {
1519
1539
LOG .error ("Could not stop the service" , e );
0 commit comments