Skip to content

Commit 2cc5d97

Browse files
committed
Stabilize the app when permissions have been revoked. Moved the start service to only after the permission check is complete.
In the stop service, only stop the service if notifications are enabled, due to a weird scenario where the service might start and immediately end due to lack of notifications, resulting in an app crash. How did we end up like this. Issue #1038 Issue #1153
1 parent 5716759 commit 2cc5d97

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

gpslogger/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<!-- allow this to run as a foreground service -->
2828
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2929

30+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
31+
3032

3133
<application
3234
android:allowBackup="true"

gpslogger/src/main/java/com/mendhak/gpslogger/GpsMainActivity.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import androidx.appcompat.view.menu.ActionMenuItemView;
6666
import androidx.appcompat.widget.ActionMenuView;
6767
import androidx.appcompat.widget.Toolbar;
68+
import androidx.core.app.NotificationManagerCompat;
6869
import androidx.core.content.ContextCompat;
6970
import androidx.core.content.FileProvider;
7071
import androidx.drawerlayout.widget.DrawerLayout;
@@ -158,9 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
158159
setUpNavigationDrawer(savedInstanceState);
159160

160161
loadDefaultFragmentView();
161-
startAndBindService();
162-
registerEventBus();
163-
registerConscryptProvider();
162+
164163

165164
if(!Systems.hasUserGrantedAllNecessaryPermissions(this)){
166165
LOG.debug("Permission check - missing permissions");
@@ -170,6 +169,10 @@ protected void onCreate(Bundle savedInstanceState) {
170169
else {
171170
LOG.debug("Permission check - OK");
172171

172+
startAndBindService();
173+
registerEventBus();
174+
registerConscryptProvider();
175+
173176
if(preferenceHelper.shouldStartLoggingOnAppLaunch()){
174177
LOG.debug("Start logging on app launch");
175178
EventBus.getDefault().postSticky(new CommandEvents.RequestStartStop(true));
@@ -269,6 +272,9 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
269272
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
270273
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
271274
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+
}
272278

273279
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
274280
// Only on Android 10 (Q), the permission dialog can include an 'Allow all the time'
@@ -1501,6 +1507,16 @@ private void startAndBindService() {
15011507
* Stops the service if it isn't logging. Also unbinds.
15021508
*/
15031509
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+
}
15041520
if (session.isBoundToService()) {
15051521

15061522
try {
@@ -1514,6 +1530,10 @@ private void stopAndUnbindServiceIfRequired() {
15141530
if (!session.isStarted()) {
15151531
LOG.debug("Stopping the service");
15161532
try {
1533+
// Stop service crashes if the intent is null. lol
1534+
if(serviceIntent == null){
1535+
serviceIntent = new Intent(this, GpsLoggingService.class);
1536+
}
15171537
stopService(serviceIntent);
15181538
} catch (Exception e) {
15191539
LOG.error("Could not stop the service", e);

0 commit comments

Comments
 (0)