Skip to content

Commit

Permalink
Fix Android O+ crashing if something isn't played within 5 seconds of…
Browse files Browse the repository at this point in the history
… startup
  • Loading branch information
daneren2005 committed Sep 8, 2018
1 parent 0e97692 commit f96b947
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
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 {
Expand All @@ -292,6 +292,10 @@ public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
shufflePlayBuffer = new ShufflePlayBuffer(this);
artistRadioBuffer = new ArtistRadioBuffer(this);
lifecycleSupport.onCreate();

if(Build.VERSION.SDK_INT >= 26) {
Notifications.shutGoogleUpNotification(this);
}
}

@Override
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/github/daneren2005/dsub/util/Notifications.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -447,6 +448,23 @@ private static NotificationChannel getDownloadingNotificationChannel(Context con
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);
}
Expand Down

0 comments on commit f96b947

Please sign in to comment.