Skip to content

Commit

Permalink
Fix MediaPlaybackService exception issue.
Browse files Browse the repository at this point in the history
b/194427284

Change-Id: I7b405366675c06d5c35a6817e0cba68d60783196
(cherry picked from commit 8f5cfd959c2eaba3546708c8425efa1c14bbbf56)
  • Loading branch information
Cobalt Team committed Jul 23, 2021
1 parent faa8e07 commit dc9aafb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class MediaPlaybackService extends Service {
private static final int NOTIFICATION_ID = 193266736; // CL number for uniqueness.
private static final String NOTIFICATION_CHANNEL_ID = "dev.cobalt.coat media playback service";
private static final String NOTIFICATION_CHANNEL_NAME = "Media playback service";
private Context context;

@Override
public void onCreate() {
Expand All @@ -46,7 +45,6 @@ public void onCreate() {
return;
}
getStarboardBridge().onServiceStart(this);
context = getApplicationContext();
}

@Override
Expand All @@ -70,7 +68,6 @@ public void onDestroy() {
return;
}
getStarboardBridge().onServiceDestroy(this);
context = null;
super.onDestroy();
Log.i(TAG, "Destroying the Media playback service.");
}
Expand All @@ -92,7 +89,7 @@ public void stopService() {
private void hideNotification() {
Log.i(TAG, "Hiding notification after stopped the service");
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}

Expand All @@ -105,7 +102,7 @@ private void createChannel() {
@RequiresApi(26)
private void createChannelInternalV26() {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel =
new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
Expand All @@ -115,7 +112,7 @@ private void createChannelInternalV26() {
try {
notificationManager.createNotificationChannel(channel);
} catch (IllegalArgumentException e) {

// intentional empty.
}
}

Expand All @@ -128,13 +125,13 @@ public void deleteChannel() {
@RequiresApi(26)
private void deleteChannelInternalV26() {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
}

Notification buildNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setSmallIcon(android.R.drawable.stat_sys_warning)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ protected void onServiceDestroy(Service service) {
protected void startMediaPlaybackService() {
Service service = serviceHolder.get();
if (service == null) {
if (appContext == null) {
Log.w(TAG, "Activiy already destoryed.");
return;
}
Log.i(TAG, "Cold start - Instantiating a MediaPlaybackService.");
Intent intent = new Intent(appContext, MediaPlaybackService.class);
appContext.startService(intent);
Expand Down

0 comments on commit dc9aafb

Please sign in to comment.