From f96b9476bf4dd7e14b38f64b4288e62771d60b4e Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 7 Sep 2018 17:02:51 -0700 Subject: Fix Android O+ crashing if something isn't played within 5 seconds of startup --- .../daneren2005/dsub/service/DownloadService.java | 6 +++++- .../github/daneren2005/dsub/util/Notifications.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'app/src/main') diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index 0c794c36..6c3cfbd7 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -274,7 +274,7 @@ public class DownloadService extends Service { wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName()); wakeLock.setReferenceCounted(false); - WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "downloadServiceLock"); try { @@ -292,6 +292,10 @@ public class DownloadService extends Service { shufflePlayBuffer = new ShufflePlayBuffer(this); artistRadioBuffer = new ArtistRadioBuffer(this); lifecycleSupport.onCreate(); + + if(Build.VERSION.SDK_INT >= 26) { + Notifications.shutGoogleUpNotification(this); + } } @Override diff --git a/app/src/main/java/github/daneren2005/dsub/util/Notifications.java b/app/src/main/java/github/daneren2005/dsub/util/Notifications.java index f0bd6766..59341ebf 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Notifications.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Notifications.java @@ -51,6 +51,7 @@ public final class Notifications { // Notification IDs. public static final int NOTIFICATION_ID_PLAYING = 100; public static final int NOTIFICATION_ID_DOWNLOADING = 102; + public static final int NOTIFICATION_ID_SHUT_GOOGLE_UP = 103; public static final String NOTIFICATION_SYNC_GROUP = "github.daneren2005.dsub.sync"; private static boolean playShowing = false; @@ -447,6 +448,23 @@ public final class Notifications { return downloadingChannel; } + @TargetApi(Build.VERSION_CODES.O) + public static void shutGoogleUpNotification(final DownloadService downloadService) { + // On Android O+, service crashes if startForeground isn't called within 5 seconds of starting + getDownloadingNotificationChannel(downloadService); + + NotificationCompat.Builder builder; + builder = new NotificationCompat.Builder(downloadService) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setContentTitle(downloadService.getResources().getString(R.string.download_downloading_title, 0)) + .setContentText(downloadService.getResources().getString(R.string.download_downloading_summary, "Temp")) + .setChannelId("downloading-channel"); + + final Notification notification = builder.build(); + downloadService.startForeground(NOTIFICATION_ID_SHUT_GOOGLE_UP, notification); + downloadService.stopForeground(true); + } + public static void showSyncNotification(final Context context, int stringId, String extra) { showSyncNotification(context, stringId, extra, null); } -- cgit v1.2.3