Search code examples
androidandroid-activityservicenotificationsusb-otg

Android: OTG Storage notification conflicts with radio c


I am working on an ongoing service which listens for a USB interface connection to a radio. When such a connection is found, this service updates its notification to reflect said connectivity; when a connection is lost (such as through an unplug event, or the radio being turned off), this service will update its notification to reflect said dis-connectivity. This service also writes to a database when it detects a change, so other applications can utilize that information.

This service works properly on USB ports which aren't configured for OTG storage. However, when a USB port with OTG storage enabled is used, I run into some issues where the service notification doesn't update properly, even though the database is being correctly written to. I believe this is because the radio I am connecting to also functions as a OTG storage device, and so once a connection with said radio is made, the OTG storage notification occurs, and I may be losing my notification context. Further, if disconnect from the radio before the OTG storage is able to properly mount, the service notification and database correctly update.

For example, if I am currently connected to a radio and OTG storage has mounted, if I disconnect said radio, the OTG storage will unmount but my service notification will not reflect that change, even though I can see that in the database the change occured properly.

I am running this on Android 4.0.4, and so I thought this issue was merely because my code was deprecated:

import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;

Notification notification = new Notification(icon, contentTitle, time);
notification.setLatestEventInfo(this, contentTitle, contentText,
            contentIntent);

notification.flags = Notification.FLAG_FOREGROUND_SERVICE
            | Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

notificationManager.notify(NOTIFICATION_ID, notification);

But I still have issues when updating to Notification.Builder:

Notification.Builder notiBuilder = 
            new Notification.Builder(this);

notiBuilder.setSmallIcon(icon);
notiBuilder.setContentTitle(contentTitle);
notiBuilder.setContentText(contentText);
notiBuilder.setContentIntent(contentIntent);
notiBuilder.setPriority(2);

notificationManager.notify(NOTIFICATION_ID, notiBuilder.getNotification());

So I'm a little confused about what the issue might be.

Please let me know if you have any ideas or suggestions, or if there's any code I should have included to be more helpful.


Solution

  • Update: Rather than being an issue with the actual notification, the issue turned out to be a race condition between the OTG storage and my service listening for the same unplug event. In order to workaround this issue, I made my service listen for the change in the OTG storage state. This seems to have resolved the issue, although is admittedly more of a workaround than anything.