-
Notifications
You must be signed in to change notification settings - Fork 22
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
Notification handling and work manager tweaks #177
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rtibbles some comments. I think I want to clean up a few things but could be done in followup. Fine with either!
@@ -24,7 +24,7 @@ | |||
<application android:label="@string/app_name" | |||
android:icon="@mipmap/icon" | |||
android:allowBackup="true" | |||
|
|||
android:name=".App" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rtibbles This is for defining the main Application class. I wasn't sure if this would cause any problems with python4android, but it doesn't seem like it.
|
||
boolean longRunning = getInputData().getBoolean(ARGUMENT_LONG_RUNNING, false); | ||
public boolean isLongRunning() { | ||
return getTags().contains(TAG_LONG_RUNNING); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rtibbles I made this a tag instead because I think there might be a way to listen on the bind/unbind of these task workers to the service process, perhaps to handle notifications or some other service process behavior, and tags seem a little more straightforward to check against.
@Override | ||
public void run() { | ||
Log.d(TAG, "Running with python worker argument: " + serviceArg); | ||
Log.d(TAG, id + " Running with python worker argument: " + arg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I want to better format these log messages using the request ID. I was reading on Java's String interpolation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the logging in general leaves a lot to be desired, but you've made it no worse!
} else { | ||
completer.set(Result.failure()); | ||
Log.w(TAG, id + " Exception in remote python work, scheduling retry", e); | ||
future.set(Result.retry()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rtibbles should I just get rid of this retry behavior since we're not even sure it works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might make things simpler - and I had kind of assumed that WorkManager should be handling this with its retry behaviour also?
|
||
public static boolean isWorkerContext() { | ||
return PythonWorker.mWorker != null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was at one point going to use these externally but didn't
R.drawable.baseline_notifications_paused_24, | ||
context.getString(R.string.notification_service_channel_action), | ||
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) | ||
).build()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds that Manage
action button to the sticky service notification
return; | ||
} | ||
ID_CHANNEL_DEFAULT = context.getString(R.string.notification_default_channel_id); | ||
ID_CHANNEL_SERVICE = context.getString(R.string.notification_service_channel_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rtibbles I saw some code examples where these were just static variables. Maybe we don't need them in that string configs and these references can be simplified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was also a little confused why the examples I was seeing drew from the resource manifest - I guess the main thing is to have them be referenceable in other files.
RemoteWorkManager workManager = RemoteWorkManager.getInstance(context); | ||
WorkQuery workQuery = WorkQuery.Builder | ||
.fromTags(tags) | ||
.build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RemoteWorkManager
only has getWorkInfos
so I had to use the WorkQuery
here
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.93 6,11v5l-2,2v1h16v-1l-2,-2zM14.5,9.8l-2.8,3.4h2.8L14.5,15h-5v-1.8l2.8,-3.4L9.5,9.8L9.5,8h5v1.8z"/> | ||
</vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bell icon with snooze 'z'. It doesn't seem to show anywhere 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, it's meant to show up in the notification intent launch though, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and the API requires it but I never saw it show
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No real concerns, thanks for the walk through!
@Override | ||
public void run() { | ||
Log.d(TAG, "Running with python worker argument: " + serviceArg); | ||
Log.d(TAG, id + " Running with python worker argument: " + arg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the logging in general leaves a lot to be desired, but you've made it no worse!
} else { | ||
completer.set(Result.failure()); | ||
Log.w(TAG, id + " Exception in remote python work, scheduling retry", e); | ||
future.set(Result.retry()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might make things simpler - and I had kind of assumed that WorkManager should be handling this with its retry behaviour also?
if (mWorker != null) { | ||
// We could also update progress on the worker here, if we need info about it on | ||
// the Android side | ||
// @see setProgressAsync |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - I guess the only thing would be if we wanted to handle the progress updating of the notification via Java rather than doing send notification here.
PythonUtil.loadLibraries( | ||
new File(context.getApplicationInfo().nativeLibraryDir) | ||
); | ||
// Initialize the work manager | ||
WorkManager.getInstance(getApplicationContext()); | ||
super.onCreate(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, to be honest, I really have no idea - if it works, it works!
|
||
// defaults for service notification channel | ||
if (channelId.equals(NotificationRef.ID_CHANNEL_SERVICE)) { | ||
setOngoing(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, looking at the docs for it, it does seem like it makes the notification more rather than less prominent: https://developer.android.com/reference/android/app/Notification.Builder#setOngoing(boolean)
return; | ||
} | ||
ID_CHANNEL_DEFAULT = context.getString(R.string.notification_default_channel_id); | ||
ID_CHANNEL_SERVICE = context.getString(R.string.notification_service_channel_id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was also a little confused why the examples I was seeing drew from the resource manifest - I guess the main thing is to have them be referenceable in other files.
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.93 6,11v5l-2,2v1h16v-1l-2,-2zM14.5,9.8l-2.8,3.4h2.8L14.5,15h-5v-1.8l2.8,-3.4L9.5,9.8L9.5,8h5v1.8z"/> | ||
</vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, it's meant to show up in the notification intent launch though, right?
Summary
App
class to define configuration forWorkManager
, including a fixed thread pool executor, and moves notification channel creation to itWorkManager
so we only initialize it in our foreground service processRemoteWorkManager
when queuing / checking work requests (ref)Notifier
interface,NotificationRef
andNotificationBuilder
classes for utility in handling notificationsWorker
which opens a background thread for processing automatically (ref)