From f3478b2e2406627fd616a4ca1a6f6c5b49e867a6 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Thu, 7 May 2020 23:26:09 +0300 Subject: [PATCH 1/4] Replace the CountDownTimer with a Handler --- .../com/kaltura/tvplayer/KalturaPlayer.java | 186 +++++++++--------- 1 file changed, 88 insertions(+), 98 deletions(-) diff --git a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java index e1071f79..02b0919c 100644 --- a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java +++ b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java @@ -2,9 +2,10 @@ import android.content.Context; import android.net.Uri; -import android.os.CountDownTimer; import android.os.Handler; +import android.os.HandlerThread; import android.os.Looper; +import android.os.SystemClock; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; @@ -68,8 +69,8 @@ public abstract class KalturaPlayer { private static final PKLog log = PKLog.get("KalturaPlayer"); public static final String DEFAULT_OVP_SERVER_URL = "https://cdnapisec.kaltura.com/"; - public static final int COUNT_DOWN_TOTAL = 5000; - public static final int COUNT_DOWN_INTERVAL = 100; + private static final int RETRIEVE_WAIT_TIMEOUT = 5000; + private static final int RETRIEVE_WAIT_INTERVAL = 100; public static final String OKHTTP = "okhttp"; static boolean playerConfigRetrieved; @@ -113,6 +114,16 @@ public enum Type { private PlayerInitOptions initOptions; private PlaylistController playlistController; + private HandlerThread retrieveWaitThread; + private Handler retrieveWaitHandler; + { + retrieveWaitThread = new HandlerThread("RetrieveWaitThread"); + retrieveWaitThread.start(); // quit() when done waiting + retrieveWaitHandler = new Handler(retrieveWaitThread.getLooper()); + + // retrieveWaitThread and retrieveWaitHandler are set to null when they are no longer needed. + } + KalturaPlayer(Context context, Type tvPlayerType, PlayerInitOptions initOptions) { this.context = context; @@ -691,16 +702,8 @@ public void loadPlaylistById(@NonNull OVPPlaylistIdOptions playlistOptions, @Non if (!isValidOVPPlayer()) return; - new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) { - @Override - public void onTick(long millisUntilFinished) { - if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - cancel(); - log.d("OVP loadPlaylist by id Done"); - if (playerConfigRetrieved) { - initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId)); - } - populatePartnersValues(); + runAfterRetrieve( + () -> { final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { if (error != null) { @@ -715,15 +718,9 @@ public void onTick(long millisUntilFinished) { } playlistController.playItem(playlistOptions.startIndex, autoPlay); })); - } - } - - @Override - public void onFinish() { - log.e("OVP loadPlaylist by id KalturaPlayerNotInitializedError"); - controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError); - } - }.start(); + }, + () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) + ); } @@ -739,16 +736,8 @@ public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull f return; } - new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) { - @Override - public void onTick(long millisUntilFinished) { - if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - cancel(); - log.d("OVP loadPlaylist Done"); - if (playerConfigRetrieved) { - initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId)); - } - populatePartnersValues(); + runAfterRetrieve( + () -> { final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { if (error != null) { @@ -763,15 +752,9 @@ public void onTick(long millisUntilFinished) { } playlistController.playItem(playlistOptions.startIndex, autoPlay); })); - } - } - - @Override - public void onFinish() { - log.e("OVP loadPlaylist KalturaPlaylistInitializedError"); - controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError); - } - }.start(); + }, + () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) + ); } public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) { @@ -785,16 +768,10 @@ public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull f } return; } - new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) { - @Override - public void onTick(long millisUntilFinished) { - if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - cancel(); - log.d("OTT loadPlaylist Done"); - if (playerConfigRetrieved) { - initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ott, initOptions.partnerId)); - } - populatePartnersValues(); + + + runAfterRetrieve( + () -> { final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { if (error != null) { @@ -809,15 +786,9 @@ public void onTick(long millisUntilFinished) { } playlistController.playItem(playlistOptions.startIndex, autoPlay); })); - } - } - - @Override - public void onFinish() { - log.e("OTT loadPlaylist KalturaPlayerNotInitializedError"); - controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError); - } - }.start(); + }, + () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) + ); } public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) { @@ -865,27 +836,12 @@ public void loadMedia(@NonNull OTTMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); - new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) { - @Override - public void onTick(long millisUntilFinished) { - if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - cancel(); - log.d("OTT loadMedia Done"); - if (playerConfigRetrieved) { - initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ott, initOptions.partnerId)); - } - populatePartnersValues(); + runAfterRetrieve( + () -> { final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId()); provider.load(response -> mediaLoadCompleted(response, listener)); - } - } - - @Override - public void onFinish() { - log.e("KalturaPlayerNotInitializedError"); - listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError); - } - }.start(); + }, + () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError)); } public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEntryLoadListener listener) { @@ -895,27 +851,13 @@ public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); - new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) { - @Override - public void onTick(long millisUntilFinished) { - if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - cancel(); - log.d("OVP loadMedia Done"); - if (playerConfigRetrieved) { - initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(Type.ovp, initOptions.partnerId)); - } - populatePartnersValues(); + runAfterRetrieve( + () -> { final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId()); provider.load(response -> mediaLoadCompleted(response, listener)); - } - } - - @Override - public void onFinish() { - log.e("KalturaPlayerNotInitializedError"); - listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError); - } - }.start(); + }, + () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError) + ); } private boolean isValidOVPPlayer() { @@ -1021,6 +963,54 @@ private PhoenixAnalyticsConfig getPhoenixAnalyticsConfig() { return new PhoenixAnalyticsConfig(getPartnerId(), getServerUrl(), getKS(), Consts.DEFAULT_ANALYTICS_TIMER_INTERVAL_HIGH_SEC); } + private boolean runIfReady(Runnable onReady) { + if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { + log.d("READY!"); + if (playerConfigRetrieved) { + initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(getTvPlayerType(), initOptions.partnerId)); + } + populatePartnersValues(); + onReady.run(); + return true; + } + return false; + } + + private void runAfterRetrieve(Runnable onReady, Runnable onTimeout) { + + if (runIfReady(onReady)) { + return; + } + + final long startTime = SystemClock.elapsedRealtime(); + + retrieveWaitHandler.post(new Runnable() { + + @Override + public void run() { + if (runIfReady(onReady)) { + retrieveWaitThread.quit(); + retrieveWaitThread = null; + retrieveWaitHandler = null; + + } else { + log.d("Not ready yet " + (SystemClock.elapsedRealtime() - startTime)); + + if (SystemClock.elapsedRealtime() - startTime < RETRIEVE_WAIT_TIMEOUT) { + // Try again in 100ms + retrieveWaitHandler.postDelayed(this, RETRIEVE_WAIT_INTERVAL); + } else { + log.e("Timed out waiting for retrieve"); + onTimeout.run(); + retrieveWaitThread.quit(); + retrieveWaitThread = null; + retrieveWaitHandler = null; + } + } + } + }); + } + public interface OnEntryLoadListener { void onEntryLoadComplete(PKMediaEntry entry, ErrorElement error); } From 331d8b30ca57f7e28badc91d31b401509a0c4060 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Thu, 7 May 2020 23:44:02 +0300 Subject: [PATCH 2/4] Loading playlist: de-duplicate code --- .../com/kaltura/tvplayer/KalturaPlayer.java | 71 ++++++------------- 1 file changed, 21 insertions(+), 50 deletions(-) diff --git a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java index 02b0919c..6312216b 100644 --- a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java +++ b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java @@ -56,6 +56,7 @@ import com.kaltura.tvplayer.playlist.PlaylistController; import com.kaltura.tvplayer.playlist.PlaylistEvent; +import com.kaltura.tvplayer.playlist.ProviderPlaylistOptions; import com.kaltura.tvplayer.utils.ConfigResolver; import com.kaltura.tvplayer.utils.NetworkUtils; @@ -703,27 +704,11 @@ public void loadPlaylistById(@NonNull OVPPlaylistIdOptions playlistOptions, @Non return; runAfterRetrieve( - () -> { - final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); - provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { - if (error != null) { - return; - } - PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OVP_ID); - playlistController.setPlaylistOptions(playlistOptions); - controllerListener.onPlaylistControllerComplete(playlistController, null); - setPlaylistController(playlistController); - if (messageBus != null) { - messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); - } - playlistController.playItem(playlistOptions.startIndex, autoPlay); - })); - }, + () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_ID), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); } - public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) { if (!isValidOVPPlayer()) @@ -737,22 +722,7 @@ public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull f } runAfterRetrieve( - () -> { - final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); - provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { - if (error != null) { - return; - } - PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OVP_LIST); - playlistController.setPlaylistOptions(playlistOptions); - controllerListener.onPlaylistControllerComplete(playlistController, null); - setPlaylistController(playlistController); - if (messageBus != null) { - messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); - } - playlistController.playItem(playlistOptions.startIndex, autoPlay); - })); - }, + () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_LIST), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); } @@ -769,28 +739,29 @@ public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull f return; } - runAfterRetrieve( - () -> { - final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); - provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { - if (error != null) { - return; - } - PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, PKPlaylistType.OTT_LIST); - playlistController.setPlaylistOptions(playlistOptions); - controllerListener.onPlaylistControllerComplete(playlistController, null); - setPlaylistController(playlistController); - if (messageBus != null) { - messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); - } - playlistController.playItem(playlistOptions.startIndex, autoPlay); - })); - }, + () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OTT_LIST), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); } + private void loadPlaylistHelper(@NonNull ProviderPlaylistOptions playlistOptions, @NonNull OnPlaylistControllerListener controllerListener, PKPlaylistType playlistType) { + final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); + provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { + if (error != null) { + return; + } + PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, playlistType); + playlistController.setPlaylistOptions(playlistOptions); + controllerListener.onPlaylistControllerComplete(playlistController, null); + setPlaylistController(playlistController); + if (messageBus != null) { + messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); + } + playlistController.playItem(playlistOptions.startIndex, autoPlay); + })); + } + public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) { if (!isValidBasicPlayer()) From 3b13a868204f5f8765df0d3f59c1e765ba70b678 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Fri, 8 May 2020 00:34:14 +0300 Subject: [PATCH 3/4] Extract method loadMediaHelper --- .../com/kaltura/tvplayer/KalturaPlayer.java | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java index 6312216b..5ff3a168 100644 --- a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java +++ b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java @@ -745,23 +745,6 @@ public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull f ); } - private void loadPlaylistHelper(@NonNull ProviderPlaylistOptions playlistOptions, @NonNull OnPlaylistControllerListener controllerListener, PKPlaylistType playlistType) { - final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); - provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { - if (error != null) { - return; - } - PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, playlistType); - playlistController.setPlaylistOptions(playlistOptions); - controllerListener.onPlaylistControllerComplete(playlistController, null); - setPlaylistController(playlistController); - if (messageBus != null) { - messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); - } - playlistController.playItem(playlistOptions.startIndex, autoPlay); - })); - } - public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull final OnPlaylistControllerListener controllerListener) { if (!isValidBasicPlayer()) @@ -800,6 +783,23 @@ public void loadPlaylist(@NonNull BasicPlaylistOptions playlistOptions, @NonNull playlistController.playItem(playlistOptions.startIndex, autoPlay); } + private void loadPlaylistHelper(@NonNull ProviderPlaylistOptions playlistOptions, @NonNull OnPlaylistControllerListener controllerListener, PKPlaylistType playlistType) { + final PlaylistProvider provider = playlistOptions.buildPlaylistProvider(getServerUrl(), getPartnerId(), playlistOptions.ks); + provider.load(response -> playlistLoadCompleted(response, (playlist, error) -> { + if (error != null) { + return; + } + PlaylistController playlistController = new PKPlaylistController(KalturaPlayer.this, playlist, playlistType); + playlistController.setPlaylistOptions(playlistOptions); + controllerListener.onPlaylistControllerComplete(playlistController, null); + setPlaylistController(playlistController); + if (messageBus != null) { + messageBus.post(new PlaylistEvent.PlaylistStarted(playlist)); + } + playlistController.playItem(playlistOptions.startIndex, autoPlay); + })); + } + public void loadMedia(@NonNull OTTMediaOptions mediaOptions, @NonNull final OnEntryLoadListener listener) { if (!isValidOTTPlayer()) @@ -808,10 +808,7 @@ public void loadMedia(@NonNull OTTMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); runAfterRetrieve( - () -> { - final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId()); - provider.load(response -> mediaLoadCompleted(response, listener)); - }, + () -> loadMediaHelper(mediaOptions, listener), () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError)); } @@ -823,14 +820,16 @@ public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); runAfterRetrieve( - () -> { - final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId()); - provider.load(response -> mediaLoadCompleted(response, listener)); - }, + () -> loadMediaHelper(mediaOptions, listener), () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError) ); } + private void loadMediaHelper(@NonNull MediaOptions mediaOptions, @NonNull OnEntryLoadListener listener) { + final MediaEntryProvider provider = mediaOptions.buildMediaProvider(getServerUrl(), getPartnerId()); + provider.load(response -> mediaLoadCompleted(response, listener)); + } + private boolean isValidOVPPlayer() { if (Type.basic.equals(tvPlayerType)) { return false; From c7dd0bdfd866aa32cd9711b1e6d2303715dd8101 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Wed, 29 Jul 2020 22:47:49 +0300 Subject: [PATCH 4/4] WIP --- tvplayer/build.gradle | 1 + .../com/kaltura/tvplayer/KalturaPlayer.java | 40 +++++++++++-------- .../com/kaltura/tvplayer/KnownPlugin.java | 4 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/tvplayer/build.gradle b/tvplayer/build.gradle index d8ab978b..bec93533 100644 --- a/tvplayer/build.gradle +++ b/tvplayer/build.gradle @@ -57,4 +57,5 @@ dependencies { repositories { google() jcenter() + maven { url 'https://jitpack.io' } } diff --git a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java index 5ff3a168..86b7cb96 100644 --- a/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java +++ b/tvplayer/src/main/java/com/kaltura/tvplayer/KalturaPlayer.java @@ -2,6 +2,7 @@ import android.content.Context; import android.net.Uri; +import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; @@ -703,7 +704,7 @@ public void loadPlaylistById(@NonNull OVPPlaylistIdOptions playlistOptions, @Non if (!isValidOVPPlayer()) return; - runAfterRetrieve( + runAfterLoadConfig( () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_ID), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); @@ -721,7 +722,7 @@ public void loadPlaylist(@NonNull OVPPlaylistOptions playlistOptions, @NonNull f return; } - runAfterRetrieve( + runAfterLoadConfig( () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OVP_LIST), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); @@ -739,7 +740,7 @@ public void loadPlaylist(@NonNull OTTPlaylistOptions playlistOptions, @NonNull f return; } - runAfterRetrieve( + runAfterLoadConfig( () -> loadPlaylistHelper(playlistOptions, controllerListener, PKPlaylistType.OTT_LIST), () -> controllerListener.onPlaylistControllerComplete(null, KalturaPlaylistInitializedError) ); @@ -807,7 +808,8 @@ public void loadMedia(@NonNull OTTMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); - runAfterRetrieve( + runAfterLoadConfig( + "OTT loadMedia", () -> loadMediaHelper(mediaOptions, listener), () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError)); } @@ -819,7 +821,7 @@ public void loadMedia(@NonNull OVPMediaOptions mediaOptions, @NonNull final OnEn prepareLoadMedia(mediaOptions); - runAfterRetrieve( + runAfterLoadConfig( () -> loadMediaHelper(mediaOptions, listener), () -> listener.onEntryLoadComplete(null, KalturaPlayerNotInitializedError) ); @@ -933,22 +935,22 @@ private PhoenixAnalyticsConfig getPhoenixAnalyticsConfig() { return new PhoenixAnalyticsConfig(getPartnerId(), getServerUrl(), getKS(), Consts.DEFAULT_ANALYTICS_TIMER_INTERVAL_HIGH_SEC); } - private boolean runIfReady(Runnable onReady) { + private boolean runIfReady(String logActionName, Runnable onReady) { if (playerConfigRetrieved || (initOptions != null && initOptions.tvPlayerParams != null)) { - log.d("READY!"); if (playerConfigRetrieved) { initOptions.setTVPlayerParams(PlayerConfigManager.retrieve(getTvPlayerType(), initOptions.partnerId)); } populatePartnersValues(); onReady.run(); + log.d(logActionName + ": success after player config was ready"); return true; } return false; } - private void runAfterRetrieve(Runnable onReady, Runnable onTimeout) { + private void runAfterLoadConfig(String logActionName, Runnable onReady, Runnable onTimeout) { - if (runIfReady(onReady)) { + if (runIfReady(logActionName, onReady)) { return; } @@ -958,10 +960,8 @@ private void runAfterRetrieve(Runnable onReady, Runnable onTimeout) { @Override public void run() { - if (runIfReady(onReady)) { - retrieveWaitThread.quit(); - retrieveWaitThread = null; - retrieveWaitHandler = null; + if (runIfReady(logActionName, onReady)) { + quitConfigWaitThread(); } else { log.d("Not ready yet " + (SystemClock.elapsedRealtime() - startTime)); @@ -972,15 +972,23 @@ public void run() { } else { log.e("Timed out waiting for retrieve"); onTimeout.run(); - retrieveWaitThread.quit(); - retrieveWaitThread = null; - retrieveWaitHandler = null; + quitConfigWaitThread(); } } } }); } + private void quitConfigWaitThread() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + retrieveWaitThread.quitSafely(); + } else { + retrieveWaitThread.quit(); // Less safe, but we have a very small percentage of + } + retrieveWaitThread = null; + retrieveWaitHandler = null; + } + public interface OnEntryLoadListener { void onEntryLoadComplete(PKMediaEntry entry, ErrorElement error); } diff --git a/tvplayer/src/main/java/com/kaltura/tvplayer/KnownPlugin.java b/tvplayer/src/main/java/com/kaltura/tvplayer/KnownPlugin.java index 0feb3b0b..52c0f55c 100644 --- a/tvplayer/src/main/java/com/kaltura/tvplayer/KnownPlugin.java +++ b/tvplayer/src/main/java/com/kaltura/tvplayer/KnownPlugin.java @@ -32,6 +32,8 @@ enum KnownPlugin { public final String className; public final PKPlugin.Factory factory; + private boolean registered; + KnownPlugin(String className, PKPlugin.Factory factory) { this.className = className; this.factory = factory; @@ -41,7 +43,7 @@ enum KnownPlugin { this(className, null); } - protected static void registerPluginByName(Context context, String pluginClassName) { + void registerPluginByName(Context context, String pluginClassName) { try { Class pluginClass = Class.forName(pluginClassName); final Field factoryField = pluginClass.getField("factory");