Skip to content

Commit 4834c15

Browse files
Fix closing MediaPlayer web app and custom web app
1 parent eb1425e commit 4834c15

File tree

2 files changed

+40
-77
lines changed

2 files changed

+40
-77
lines changed

src/com/connectsdk/sampler/fragments/MediaPlayerFragment.java

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class MediaPlayerFragment extends BaseFragment {
8282
public Button jumpButton;
8383
public CheckBox loopingButton;
8484

85-
public static LaunchSession launchSession;
85+
public LaunchSession launchSession;
8686

8787
public TextView positionTextView;
8888
public TextView durationTextView;
@@ -97,14 +97,14 @@ public class MediaPlayerFragment extends BaseFragment {
9797

9898
public boolean mSeeking;
9999
public Runnable mRefreshRunnable;
100-
public static final int REFRESH_INTERVAL_MS = (int) TimeUnit.SECONDS.toMillis(1);
100+
public final int REFRESH_INTERVAL_MS = (int) TimeUnit.SECONDS.toMillis(1);
101101
public Handler mHandler;
102102
public long totalTimeDuration;
103103
public boolean mIsGettingPlayPosition;
104104

105105

106-
static boolean isPlayingImage = false;
107-
static boolean isPlaying = false;
106+
boolean isPlayingImage = false;
107+
boolean isPlaying = false;
108108

109109
private MediaControl mMediaControl = null;
110110
private PlaylistControl mPlaylistControl = null;
@@ -317,21 +317,13 @@ private void playAudio() {
317317

318318
@Override
319319
public void onError(ServiceCommandError error) {
320-
Log.d("LG", "Error playing audio");
321-
if (launchSession != null) {
322-
launchSession.close(null);
323-
launchSession = null;
324-
testResponse = new TestResponseObject(false, error.getCode(), error.getMessage());
325-
stopUpdating();
326-
disableMedia();
327-
isPlaying = isPlayingImage = false;
328-
}
320+
Log.d("LG", "Error playing audio", error);
321+
stopMediaSession();
329322
}
330323

331324
@Override
332325
public void onSuccess(MediaLaunchObject object) {
333326
Log.d("LG", "Started playing audio");
334-
335327
launchSession = object.launchSession;
336328
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode, TestResponseObject.Play_Audio);
337329
mMediaControl = object.mediaControl;
@@ -340,8 +332,6 @@ public void onSuccess(MediaLaunchObject object) {
340332
stopUpdating();
341333
enableMedia();
342334
isPlaying = true;
343-
disconnectWebAppSession();
344-
345335
}
346336
});
347337
}
@@ -358,14 +348,8 @@ private void playM3U() {
358348

359349
@Override
360350
public void onError(ServiceCommandError error) {
361-
Log.d("LG", "Error playing audio");
362-
if (launchSession != null) {
363-
launchSession.close(null);
364-
launchSession = null;
365-
stopUpdating();
366-
disableMedia();
367-
isPlaying = isPlayingImage = false;
368-
}
351+
Log.d("LG", "Error playing audio", error);
352+
stopMediaSession();
369353
}
370354

371355
@Override
@@ -377,7 +361,6 @@ public void onSuccess(MediaLaunchObject object) {
377361
stopUpdating();
378362
enableMedia();
379363
isPlaying = true;
380-
disconnectWebAppSession();
381364
}
382365
});
383366
}
@@ -391,32 +374,24 @@ private void showImage() {
391374
String description = "Blender Open Movie Project";
392375
String icon = "http://ec2-54-201-108-205.us-west-2.compute.amazonaws.com/samples/media/photoIcon.jpg";
393376

394-
getMediaPlayer().displayImage(imagePath, mimeType, title, description, icon, new MediaPlayer.LaunchListener() {
377+
getMediaPlayer().displayImage(imagePath, mimeType, title, description, icon, new
378+
MediaPlayer.LaunchListener() {
379+
380+
@Override
381+
public void onError(ServiceCommandError error) {
382+
Log.e("Error", "Error displaying Image", error);
383+
stopMediaSession();
384+
}
395385

396386
@Override
397387
public void onSuccess(MediaLaunchObject object) {
398388
launchSession = object.launchSession;
399389
closeButton.setEnabled(true);
400-
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode, TestResponseObject.Display_image);
390+
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode,
391+
TestResponseObject.Display_image);
401392
closeButton.setOnClickListener(closeListener);
402393
stopUpdating();
403394
isPlayingImage = true;
404-
disconnectWebAppSession();
405-
406-
}
407-
408-
@Override
409-
public void onError(ServiceCommandError error) {
410-
Log.e("Error", "Error displaying Image");
411-
if (launchSession != null) {
412-
launchSession.close(null);
413-
launchSession = null;
414-
testResponse = new TestResponseObject(false, error.getCode(), error.getMessage());
415-
stopUpdating();
416-
disableMedia();
417-
isPlaying = isPlayingImage = false;
418-
419-
}
420395
}
421396
});
422397
}
@@ -441,31 +416,37 @@ private void playVideo() {
441416

442417
getMediaPlayer().playMedia(mediaInfo, shouldLoop, new MediaPlayer.LaunchListener() {
443418

419+
@Override
420+
public void onError(ServiceCommandError error) {
421+
Log.e("Error", "Error playing video", error);
422+
stopMediaSession();
423+
}
424+
444425
public void onSuccess(MediaLaunchObject object) {
445426
launchSession = object.launchSession;
446-
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode, TestResponseObject.Play_Video);
427+
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode,
428+
TestResponseObject.Play_Video);
447429
mMediaControl = object.mediaControl;
448430
mPlaylistControl = object.playlistControl;
449431
stopUpdating();
450432
enableMedia();
451433
isPlaying = true;
452-
disconnectWebAppSession();
453-
}
454-
455-
@Override
456-
public void onError(ServiceCommandError error) {
457-
if (launchSession != null) {
458-
launchSession.close(null);
459-
launchSession = null;
460-
testResponse = new TestResponseObject(false, error.getCode(), error.getMessage());
461-
stopUpdating();
462-
disableMedia();
463-
isPlaying = isPlayingImage = false;
464-
}
465434
}
466435
});
467436
}
468437

438+
439+
private void stopMediaSession() {
440+
// don't call launchSession.close() here, currently it can close
441+
// a different web app in WebOS
442+
if (launchSession != null) {
443+
launchSession = null;
444+
stopUpdating();
445+
disableMedia();
446+
isPlaying = isPlayingImage = false;
447+
}
448+
}
449+
469450
@Override
470451
public void disableButtons() {
471452
mSeekBar.setEnabled(false);
@@ -854,14 +835,6 @@ private String formatTime(long millisec) {
854835
return time;
855836
}
856837

857-
private void disconnectWebAppSession() {
858-
if (WebAppFragment.mWebAppSession != null) {
859-
WebAppFragment.mWebAppSession.setWebAppSessionListener(null);
860-
WebAppFragment.mWebAppSession.close(null);
861-
WebAppFragment.isLaunched = false;
862-
}
863-
}
864-
865838
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
866839
ImageView bmImage;
867840

src/com/connectsdk/sampler/fragments/WebAppFragment.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class WebAppFragment extends BaseFragment {
5757
TextView responseMessageTextView;
5858
LaunchSession runningAppSession;
5959

60-
static WebAppSession mWebAppSession;
60+
WebAppSession mWebAppSession;
6161
ServiceSubscription<WebAppPinStatusListener> isWebAppPinnedSubscription;
6262
String webAppId = null;
6363

@@ -183,14 +183,13 @@ public void onSuccess(WebAppSession webAppSession) {
183183
testResponse = new TestResponseObject(true, TestResponseObject.SuccessCode, TestResponseObject.Launched_WebAPP);
184184
webAppSession.setWebAppSessionListener(webAppListener);
185185
isLaunched = true;
186-
disconnectMediaPlayerSession();
186+
187187
if (getTv().hasAnyCapability(WebAppLauncher.Message_Send, WebAppLauncher.Message_Receive, WebAppLauncher.Message_Receive_JSON, WebAppLauncher.Message_Send_JSON))
188188
webAppSession.connect(connectionListener);
189189
else
190190
connectionListener.onSuccess(webAppSession.launchSession);
191191

192192
mWebAppSession = webAppSession;
193-
194193
}
195194
});
196195
}
@@ -225,8 +224,6 @@ public void onSuccess(WebAppSession webAppSession) {
225224
if (getTv().hasCapabilities(WebAppLauncher.Message_Send_JSON)) sendJSONButton.setEnabled(true);
226225
if (getTv().hasCapabilities(WebAppLauncher.Close)) closeWebAppButton.setEnabled(true);
227226
isLaunched = true;
228-
disconnectMediaPlayerSession();
229-
230227
}
231228
});
232229
}
@@ -529,11 +526,4 @@ public void setRunningAppInfo(LaunchSession session) {
529526
runningAppSession = session;
530527
}
531528

532-
private void disconnectMediaPlayerSession() {
533-
if (MediaPlayerFragment.launchSession != null) {
534-
MediaPlayerFragment.launchSession.close(null);
535-
MediaPlayerFragment.launchSession = null;
536-
MediaPlayerFragment.isPlaying = MediaPlayerFragment.isPlayingImage = false;
537-
}
538-
}
539529
}

0 commit comments

Comments
 (0)