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

Fixes issue #1076 : Replaced IntentService with JobIntentService in RealtimeService #1294

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 4 additions & 8 deletions onebusaway-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
<!-- Permission request for activity recognition and background location is disabled because the related feature has been turned off.
See issue #1240 for more details: https://github.com/OneBusAway/onebusaway-android/issues/1240-->
<!-- ACTIVITY_RECOGNITION API 28 and lower -->
<!-- <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />-->
<!-- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />-->
<!-- <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />-->
<!-- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />-->
<!-- ACTIVITY_RECOGNITION API 29 and higher -->
<!-- <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />-->
<!-- <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />-->
<!--To enable checkBatteryOptimizations feature, also uncomment checkBatteryOptimizations() in
the HomeActivity's onCreate() method-->
<!--<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>-->
Expand All @@ -62,7 +62,7 @@
android:requestLegacyExternalStorage="true">

<meta-data android:name="com.onesignal.NotificationServiceExtension"
android:value="org.onebusaway.android.tripservice.ArrivalsRemindersServiceExtension" />
android:value="org.onebusaway.android.tripservice.ArrivalsRemindersServiceExtension" />

<meta-data
android:name="android.app.default_searchable"
Expand Down Expand Up @@ -304,10 +304,6 @@
android:name="org.onebusaway.android.provider.ObaProvider"
android:authorities="${databaseAuthority}"/>

<service
android:name=".directions.realtime.RealtimeService">
</service>

<service
android:name=".nav.NavigationService"
android:foregroundServiceType="location" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import android.app.Activity;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -27,7 +26,13 @@
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import androidx.work.WorkManager;
import androidx.work.OneTimeWorkRequest;

import org.onebusaway.android.R;
import org.onebusaway.android.app.Application;
Expand All @@ -43,22 +48,23 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* This service is started after a trip is planned by the user so they can be notified if the
* trip results for their request change in the near future. For example, if a user plans a trip,
* and then the top result for that trip gets delayed by 20 minutes, the user will be notified
* that new trip results are available.
*/
public class RealtimeService extends IntentService {
public class RealtimeService extends Worker {

private static final String TAG = "RealtimeService";

private static final String ITINERARY_DESC = ".ItineraryDesc";
private static final String ITINERARY_END_DATE = ".ItineraryEndDate";

public RealtimeService() {
super("RealtimeService");
public RealtimeService(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

/**
Expand All @@ -75,26 +81,33 @@ public static void start(Activity source, Bundle bundle) {
}

bundle.putSerializable(OTPConstants.NOTIFICATION_TARGET, source.getClass());
Intent intent = new Intent(OTPConstants.INTENT_START_CHECKS);
intent.putExtras(bundle);
source.sendBroadcast(intent);
Data inputData = new Data.Builder()
.putAll(toMap(bundle))
.build();
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RealtimeService.class)
.setInputData(inputData)
.build();
WorkManager.getInstance(source).enqueue(workRequest);
}

@NonNull
@Override
public void onHandleIntent(Intent intent) {
Bundle bundle = intent.getExtras();
public Result doWork() {
Data inputData = getInputData();
Bundle bundle = toBundle(inputData);

if (intent.getAction().equals(OTPConstants.INTENT_START_CHECKS)) {
String action = inputData.getString("action");
if (OTPConstants.INTENT_START_CHECKS.equals(action)) {
disableListenForTripUpdates();
if (!rescheduleRealtimeUpdates(bundle)) {
Itinerary itinerary = getItinerary(bundle);
startRealtimeUpdates(bundle, itinerary);
}
} else if (intent.getAction().equals(OTPConstants.INTENT_CHECK_TRIP_TIME)) {
} else if (OTPConstants.INTENT_CHECK_TRIP_TIME.equals(action)) {
checkForItineraryChange(bundle);
}

RealtimeWakefulReceiver.completeWakefulIntent(intent);
return Result.success();
}

// Depending on preferences / whether there is realtime info, start updates.
Expand Down Expand Up @@ -143,18 +156,15 @@ private boolean rescheduleRealtimeUpdates(Bundle bundle) {

if (reschedule) {
Log.d(TAG, "Start service at " + queryStart);
Intent future = new Intent(OTPConstants.INTENT_START_CHECKS);
future.putExtras(bundle);

int flags;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
flags = PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_MUTABLE;
} else {
flags = PendingIntent.FLAG_CANCEL_CURRENT;
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, future, flags);
getAlarmManager().set(AlarmManager.RTC_WAKEUP, queryStart.getTime(), pendingIntent);
Data inputData = new Data.Builder()
.putAll(toMap(bundle))
.putString("action", OTPConstants.INTENT_START_CHECKS)
.build();
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(RealtimeService.class)
.setInitialDelay(queryStart.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.setInputData(inputData)
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(workRequest);
}

return reschedule;
Expand Down Expand Up @@ -240,8 +250,8 @@ private void showNotification(ItineraryDescription description, int title, int m
Class<? extends Activity> notificationTarget,
Bundle params, List<Itinerary> itineraries) {

String titleText = getResources().getString(title);
String messageText = getResources().getString(message);
String titleText = getApplicationContext().getResources().getString(title);
String messageText = getApplicationContext().getResources().getString(message);

Intent openIntent = new Intent(getApplicationContext(), notificationTarget);
openIntent.putExtras(params);
Expand Down Expand Up @@ -358,4 +368,69 @@ private Bundle getSimplifiedBundle(Bundle params) {
return extras;
}

}
private static Data toMap(Bundle bundle) {
Data.Builder builder = new Data.Builder();
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
if (value instanceof String) {
builder.putString(key, (String) value);
} else if (value instanceof Integer) {
builder.putInt(key, (Integer) value);
} else if (value instanceof Long) {
builder.putLong(key, (Long) value);
} else if (value instanceof Boolean) {
builder.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
builder.putFloat(key, (Float) value);
} else if (value instanceof Double) {
builder.putDouble(key, (Double) value);
} else if (value instanceof String[]) {
builder.putStringArray(key, (String[]) value);
} else if (value instanceof int[]) {
builder.putIntArray(key, (int[]) value);
} else if (value instanceof long[]) {
builder.putLongArray(key, (long[]) value);
} else if (value instanceof boolean[]) {
builder.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof float[]) {
builder.putFloatArray(key, (float[]) value);
} else if (value instanceof double[]) {
builder.putDoubleArray(key, (double[]) value);
}
}
return builder.build();
}

private static Bundle toBundle(Data data) {
Bundle bundle = new Bundle();
for (String key : data.getKeyValueMap().keySet()) {
Object value = data.getKeyValueMap().get(key);
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (Integer) value);
} else if (value instanceof Long) {
bundle.putLong(key, (Long) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
bundle.putFloat(key, (Float) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (Double) value);
} else if (value instanceof String[]) {
bundle.putStringArray(key, (String[]) value);
} else if (value instanceof int[]) {
bundle.putIntArray(key, (int[]) value);
} else if (value instanceof long[]) {
bundle.putLongArray(key, (long[]) value);
} else if (value instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof float[]) {
bundle.putFloatArray(key, (float[]) value);
} else if (value instanceof double[]) {
bundle.putDoubleArray(key, (double[]) value);
}
}
return bundle;
}
}