Skip to content

Commit

Permalink
Fix #769 - Add custom reminder notification names (#786)
Browse files Browse the repository at this point in the history
* Replace (redundant) app name in notification title with user-inputted notification name.
* Small style fixes
* Add bridgette to contributors list
  • Loading branch information
bridgette authored and barbeau committed Aug 4, 2017
1 parent 4834f3a commit 64c9606
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ We'd like to thank the following developers who have contributed to OneBusAway A
* Cagri Cetin
* Mike Karabushin
* Aziz Batihk
* Charles Bond
* Charles Bond
* Bridgette Eichelberger (bridgette@outlook.com)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.onebusaway.android.tripservice;

import org.onebusaway.android.R;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.ui.ArrivalsListActivity;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.ContentResolver;
Expand All @@ -25,10 +29,6 @@
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import org.onebusaway.android.R;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.ui.ArrivalsListActivity;

/**
* A task (thread) that is responsible for generating a Notification to remind the user of an
* arriving bus.
Expand Down Expand Up @@ -61,14 +61,18 @@ public final class NotifierTask implements Runnable {

private String mNotifyText;

private String mNotifyTitle;

public NotifierTask(Context context,
TaskContext taskContext,
Uri uri,
String notifyTitle,
String notifyText) {
mContext = context;
mTaskContext = taskContext;
mCR = mContext.getContentResolver();
mUri = uri;
mNotifyTitle = notifyTitle;
mNotifyText = notifyText;
}

Expand Down Expand Up @@ -111,7 +115,7 @@ private void notify(Cursor c) {
new ArrivalsListActivity.Builder(mContext, stopId).getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = createNotification(mNotifyText,
Notification notification = createNotification(mNotifyTitle, mNotifyText,
pendingContentIntent, pendingDeleteIntent);

mTaskContext.setNotification(id, notification);
Expand All @@ -132,13 +136,15 @@ private void notify(Cursor c) {
* Create a notification and populate it with our latest data. This method replaces
* an implementation using Notification.setLatestEventInfo((), which was deprecated (see #290).
*
* @param notifyTitle notification title
* @param notifyText notification text
* @param contentIntent intent to fire on click
* @param deleteIntent intent to remove/delete
*/
private Notification createNotification(String notifyText,
private Notification createNotification(String notifyTitle,
String notifyText,
PendingIntent contentIntent,
PendingIntent deleteIntent) {
final String title = mContext.getString(R.string.app_name);
return new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_stat_notification)
.setDefaults(Notification.DEFAULT_ALL)
Expand All @@ -147,7 +153,7 @@ private Notification createNotification(String notifyText,
//.setVibrate(VIBRATE_PATTERN)
.setContentIntent(contentIntent)
.setDeleteIntent(deleteIntent)
.setContentTitle(title)
.setContentTitle(notifyTitle)
.setContentText(notifyText)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*/
package org.onebusaway.android.tripservice;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import org.onebusaway.android.io.ObaApi;
import org.onebusaway.android.io.elements.ObaArrivalInfo;
import org.onebusaway.android.io.request.ObaArrivalInfoRequest;
Expand All @@ -30,6 +24,12 @@
import org.onebusaway.android.ui.ArrivalInfo;
import org.onebusaway.android.util.UIUtils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
* A task (thread) that is responsible for polling the server to determine if a Notification to
* remind the user of an arriving bus should be triggered.
Expand Down Expand Up @@ -137,11 +137,16 @@ private void poll1(Cursor c) {
// Bus is within the reminder interval (or it possibly has left!)
// Send off a notification.
//Log.d(TAG, "Notify for trip: " + alertUri);
TripService.notifyTrip(mContext, mUri, arrivalInfo.getNotifyText());
TripService.notifyTrip(mContext, mUri, getReminderName(tripId, stopId), arrivalInfo.getNotifyText());
}
}
}

private String getReminderName(String tripId, String stopId) {
final Uri uri = ObaContract.Trips.buildUri(tripId, stopId);
return UIUtils.stringForQuery(mContext, uri, ObaContract.Trips.NAME);
}

private long getReminderMin(String tripId, String stopId) {
final Uri uri = ObaContract.Trips.buildUri(tripId, stopId);
return (long) UIUtils.intForQuery(mContext, uri, ObaContract.Trips.REMINDER);
Expand All @@ -151,7 +156,7 @@ private long getReminderMin(String tripId, String stopId) {
* Checks arrivals from given ObaArrivalInfoResponse
*
* @param response arrival information
* @param tripId
* @param tripId trip id
* @return ArrivalInfo, or return null if the arrival can't be found.
*/
private ArrivalInfo checkArrivals(ObaArrivalInfoResponse response, String tripId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ private void schedule1(Cursor c, Time tNow, long now) {
}

private boolean scheduleAlert(Uri uri,
String tripId,
String stopId,
long triggerTime) {
String tripId,
String stopId,
long triggerTime) {

Time tmp = new Time();
tmp.set(triggerTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.onebusaway.android.tripservice;

import org.onebusaway.android.BuildConfig;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.util.UIUtils;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
Expand All @@ -30,10 +34,6 @@
import android.os.RemoteException;
import android.util.Log;

import org.onebusaway.android.BuildConfig;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.util.UIUtils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -84,6 +84,8 @@ public class TripService extends Service {

private static final String NOTIFY_TEXT = ".notifyText";

private static final String NOTIFY_TITLE = ".notifyTitle";

private ExecutorService mThreadPool;

private NotificationManager mNM;
Expand Down Expand Up @@ -181,9 +183,10 @@ private int handleCommand(Intent intent, int startId) {

} else if (ACTION_NOTIFY.equals(action)) {
// Create the notification
String notifyTitle = intent.getStringExtra(NOTIFY_TITLE);
String notifyText = intent.getStringExtra(NOTIFY_TEXT);

mThreadPool.submit(new NotifierTask(this, taskContext, uri, notifyText));
mThreadPool.submit(new NotifierTask(this, taskContext, uri, notifyTitle, notifyText));
return START_REDELIVER_INTENT;

} else if (ACTION_CANCEL.equals(action)) {
Expand All @@ -205,9 +208,9 @@ public IBinder onBind(Intent arg0) {
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code,
Parcel data,
Parcel reply,
int flags) throws RemoteException {
Parcel data,
Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
Expand All @@ -233,17 +236,18 @@ public static void pollTrip(Context context, Uri alertUri, long triggerTime) {
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, alarmIntent);
}

public static void notifyTrip(Context context, Uri alertUri, String notifyText) {
public static void notifyTrip(Context context, Uri alertUri, String notifyTitle, String notifyText) {
final Intent intent = new Intent(context, TripService.class);
intent.setAction(ACTION_NOTIFY);
intent.setData(alertUri);
intent.putExtra(NOTIFY_TEXT, notifyText);
intent.putExtra(NOTIFY_TITLE, notifyTitle);
context.startService(intent);
}

public static String getRouteShortName(Context context, String id) {
return UIUtils.stringForQuery(context, Uri.withAppendedPath(
ObaContract.Routes.CONTENT_URI, id),
ObaContract.Routes.CONTENT_URI, id),
ObaContract.Routes.SHORTNAME
);
}
Expand Down
3 changes: 2 additions & 1 deletion onebusaway-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@
"* Cagri Cetin\n"
"* Mike Karabushin\n"
"* Aziz Batihk\n"
"* Charles Bond\n\n"
"* Charles Bond\n"
"* Bridgette Eichelberger\n\n"

"Want to contribute to this app? Check it out on Github at
https://github.com/OneBusAway/onebusaway-android.\n\n"
Expand Down

0 comments on commit 64c9606

Please sign in to comment.