Skip to content

Commit

Permalink
Ask for alarm and notification permissions to fix #1106
Browse files Browse the repository at this point in the history
  • Loading branch information
millanp authored and aaronbrethorst committed Feb 20, 2024
1 parent 6c33bcc commit fb90dca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions onebusaway-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<!--<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>-->
<!-- For reminders - SCHEDULE_EXACT_ALARM is required when targeting Android 12 or higher-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<!-- For notifications, POST_NOTIFICATIONS is required when targeting Android 12 or higher-->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:allowBackup="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.onebusaway.android.ui;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
Expand All @@ -26,6 +27,7 @@
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.format.DateUtils;
Expand All @@ -52,13 +54,16 @@
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;

import static org.onebusaway.android.util.PermissionUtils.NOTIFICATION_PERMISSION_REQUEST;

public class TripInfoActivity extends AppCompatActivity {

private static final String TAG = "TripInfoActivity";
Expand Down Expand Up @@ -418,11 +423,16 @@ public void saveTrip() {
// Repeats
//

// On Android Tiramisu and up, we must request
// permission to schedule exact alarms.
if (!TripService.canScheduleExactAlarms(getActivity())) {
showRequestAlarmsPermissionDialog(getActivity());
return;
// Make sure that the user has granted permission for notifications
// and exact alarms.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!TripService.canScheduleExactAlarms(getActivity())) {
showRequestAlarmsPermissionDialog(getActivity());
return;
}
ActivityCompat.requestPermissions(this.getActivity(),
new String[] {Manifest.permission.POST_NOTIFICATIONS},
NOTIFICATION_PERMISSION_REQUEST);
}

View view = getView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class PermissionUtils {
public static final int SAVE_BACKUP_PERMISSION_REQUEST = 2;
public static final int RESTORE_BACKUP_PERMISSION_REQUEST = 3;
public static final int BACKGROUND_LOCATION_PERMISSION_REQUEST = 4;
public static final int NOTIFICATION_PERMISSION_REQUEST = 5;

public static final String[] LOCATION_PERMISSIONS = {
Manifest.permission.ACCESS_FINE_LOCATION,
Expand Down

0 comments on commit fb90dca

Please sign in to comment.