Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Target SDK to 34 and fix foreground services compatibility with Android 14 #1232

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions onebusaway-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ repositories {
}

android {
compileSdk 33
compileSdk 34

defaultConfig {
minSdkVersion 21
targetSdkVersion 33
targetSdkVersion 34
versionCode 143
versionName "2.14.0"

Expand Down
3 changes: 3 additions & 0 deletions onebusaway-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand All @@ -37,6 +38,7 @@
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="${applicationId}.permission.TRIP_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!-- ACTIVITY_RECOGNITION API 28 and lower -->
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<!-- ACTIVITY_RECOGNITION API 29 and higher -->
Expand Down Expand Up @@ -294,6 +296,7 @@

<service
android:name=".tripservice.TripService"
android:foregroundServiceType="specialUse"
android:permission="${applicationId}.permission.TRIP_SERVICE"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.location.Location;
import android.os.Build;
import android.os.IBinder;
Expand Down Expand Up @@ -155,7 +156,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
mNavProvider.navigate(path);
}
Notification notification = mNavProvider.getForegroundStartingNotification();
startForeground(NavigationServiceProvider.NOTIFICATION_ID, notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(NavigationServiceProvider.NOTIFICATION_ID, notification,ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
}else{
startForeground(NavigationServiceProvider.NOTIFICATION_ID, notification);
}
return START_STICKY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
Expand Down Expand Up @@ -170,7 +171,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
.setContentText(foregroundNotifyText)
.setContentIntent(pendingIntent).build();

startForeground(FOREGROUND_NOTIFICATION_ID, notification);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(FOREGROUND_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
}else{
startForeground(FOREGROUND_NOTIFICATION_ID, notification);
}
}
return handleCommand(intent, startId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.onebusaway.android.ui;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
Expand Down Expand Up @@ -53,6 +54,7 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
Expand Down Expand Up @@ -96,6 +98,8 @@
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

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

public class TripDetailsListFragment extends ListFragment {

public static final String TAG = "TripDetailsListFragment";
Expand Down Expand Up @@ -554,6 +558,12 @@ public void onClick(DialogInterface dialog, int which) {

ObaAnalytics.reportUiEvent(mFirebaseAnalytics, getString(R.string.analytics_label_destination_reminder), getString(R.string.analytics_label_destination_reminder_variant_started));

// Request the user to grant the POST_NOTIFICATIONS permission.
ActivityCompat.requestPermissions(getActivity(),
new String[] {Manifest.permission.POST_NOTIFICATIONS},
NOTIFICATION_PERMISSION_REQUEST);
dialog.dismiss();

startNavigationService(setUpNavigationService(position));
Toast.makeText(Application.get(),
Application.get().getString(R.string.destination_reminder_title),
Expand Down Expand Up @@ -1192,7 +1202,11 @@ private void registerReceiver() {
// filter specifies which event Receiver should listen to
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SERVICE_DESTROYED);
getActivity().registerReceiver(new TripEndReceiver(), filter);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE){
getActivity().registerReceiver(new TripEndReceiver(), filter, Context.RECEIVER_NOT_EXPORTED);
}else{
getActivity().registerReceiver(new TripEndReceiver(), filter);
}
}

/**
Expand Down
Loading