From ca556095c88da440e581489c921dcee6e77bbb21 Mon Sep 17 00:00:00 2001 From: Gilad Nadav Date: Tue, 4 Apr 2017 11:13:36 +0300 Subject: [PATCH] FEM1203 - TVPapi Analyitics - STOP wrong position (#195) * add reset API to player so app can reset player state before change media * add reset to player API so app that wants to change media can call reset before so player state will be in the initial state * codacy * change API to be called stop add stopped event to this operation * support STOPPED event from player instead of sending in onDestroy * save the position in each interval add null protection * add comment * send the STOPPED event after stop is called * fix codacy --- .../com/kaltura/playkit/PlayerLoader.java | 1 + .../plugins/PhoenixAnalyticsPlugin.java | 29 ++++++++++++++----- .../plugins/TVPAPIAnalyticsPlugin.java | 7 +++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/PlayerLoader.java b/playkit/src/main/java/com/kaltura/playkit/PlayerLoader.java index 5eba29fea..d897773e8 100644 --- a/playkit/src/main/java/com/kaltura/playkit/PlayerLoader.java +++ b/playkit/src/main/java/com/kaltura/playkit/PlayerLoader.java @@ -87,6 +87,7 @@ public void updatePluginConfig(@NonNull String pluginName, @Nullable Object plug @Override public void destroy() { + stop(); releasePlugins(); releasePlayer(); } diff --git a/playkit/src/main/java/com/kaltura/playkit/plugins/PhoenixAnalyticsPlugin.java b/playkit/src/main/java/com/kaltura/playkit/plugins/PhoenixAnalyticsPlugin.java index b02f7ea78..0f81a8833 100644 --- a/playkit/src/main/java/com/kaltura/playkit/plugins/PhoenixAnalyticsPlugin.java +++ b/playkit/src/main/java/com/kaltura/playkit/plugins/PhoenixAnalyticsPlugin.java @@ -21,6 +21,7 @@ import java.util.TimerTask; + /** * Created by zivilan on 02/11/2016. */ @@ -28,6 +29,7 @@ public class PhoenixAnalyticsPlugin extends PKPlugin { private static final PKLog log = PKLog.get("PhoenixAnalyticsPlugin"); private static final String TAG = "PhoenixAnalytics"; + private static final double MEDIA_ENDED_THRESHOLD = 0.98; public enum PhoenixActionType{ HIT, @@ -50,7 +52,9 @@ public enum PhoenixActionType{ public Player player; public RequestQueue requestsExecutor; private java.util.Timer timer = new java.util.Timer(); - public MessageBus messageBus; + private long lastKnownPlayerPosition = 0; + public MessageBus messageBus; // used also by TVPAI Analytics + public static final Factory factory = new Factory() { @Override @@ -65,7 +69,7 @@ public PKPlugin newInstance() { @Override public void warmUp(Context context) { - + } }; @@ -99,7 +103,6 @@ protected void onApplicationResumed() { @Override public void onDestroy() { log.d("onDestroy"); - sendAnalyticsEvent(PhoenixActionType.STOP); timer.cancel(); } @@ -110,7 +113,8 @@ protected void onLoad(Player player, Object config, final MessageBus messageBus, this.pluginConfig = (JsonObject) config; this.mContext = context; this.messageBus = messageBus; - messageBus.listen(mEventListener, PlayerEvent.Type.PLAY, PlayerEvent.Type.PAUSE, PlayerEvent.Type.ENDED, PlayerEvent.Type.ERROR, PlayerEvent.Type.LOADED_METADATA); + messageBus.listen(mEventListener, PlayerEvent.Type.PLAY, PlayerEvent.Type.PAUSE, PlayerEvent.Type.ENDED, PlayerEvent.Type.ERROR, PlayerEvent.Type.LOADED_METADATA, PlayerEvent.Type.STOPPED); + log.d("onLoad"); } @@ -118,8 +122,13 @@ protected void onLoad(Player player, Object config, final MessageBus messageBus, @Override public void onEvent(PKEvent event) { if (event instanceof PlayerEvent) { - log.d(((PlayerEvent) event).type.toString()); + log.d("Player Event = " + ((PlayerEvent) event).type.name() + " , lastKnownPlayerPosition = " + lastKnownPlayerPosition); switch (((PlayerEvent) event).type) { + case STOPPED: + sendAnalyticsEvent(PhoenixActionType.STOP); + timer.cancel(); + timer = new java.util.Timer(); + break; case ENDED: timer.cancel(); timer = new java.util.Timer(); @@ -168,7 +177,8 @@ private void startMediaHitInterval(){ @Override public void run() { sendAnalyticsEvent(PhoenixActionType.HIT); - if ((float) player.getCurrentPosition() / player.getDuration() > 0.98){ + lastKnownPlayerPosition = player.getCurrentPosition(); + if ((float) lastKnownPlayerPosition / player.getDuration() > MEDIA_ENDED_THRESHOLD){ sendAnalyticsEvent(PhoenixActionType.FINISH); } } @@ -184,10 +194,13 @@ protected void sendAnalyticsEvent(final PhoenixActionType eventType){ String baseUrl = pluginConfig.has("baseUrl")? pluginConfig.getAsJsonPrimitive("baseUrl").getAsString():"http://api-preprod.ott.kaltura.com/v4_1/api_v3/"; String ks = pluginConfig.has("ks")? pluginConfig.getAsJsonPrimitive("ks").getAsString():"djJ8MTk4fN86RC6KBjyHtmG9bIBounF1ewb1SMnFNtAvaxKIAfHUwW0rT4GAYQf8wwUKmmRAh7G0olZ7IyFS1FTpwskuqQPVQwrSiy_J21kLxIUl_V9J"; int partnerId = pluginConfig.has("partnerId")? pluginConfig.getAsJsonPrimitive("partnerId").getAsInt():198; + String action = eventType.name().toLowerCase(); // used only for copmare - + if (!"stop".equals(action)) { + lastKnownPlayerPosition = player.getCurrentPosition(); + } RequestBuilder requestBuilder = BookmarkService.actionAdd(baseUrl, partnerId, ks, - "media", mediaConfig.getMediaEntry().getId(), eventType.name(), player.getCurrentPosition(), /*mediaConfig.getMediaEntry().getFileId()*/ fileId); + "media", mediaConfig.getMediaEntry().getId(), eventType.name(), lastKnownPlayerPosition, /*mediaConfig.getMediaEntry().getFileId()*/ fileId); requestBuilder.completion(new OnRequestCompletion() { @Override diff --git a/playkit/src/main/java/com/kaltura/playkit/plugins/TVPAPIAnalyticsPlugin.java b/playkit/src/main/java/com/kaltura/playkit/plugins/TVPAPIAnalyticsPlugin.java index b6be1885e..73b0cdc24 100644 --- a/playkit/src/main/java/com/kaltura/playkit/plugins/TVPAPIAnalyticsPlugin.java +++ b/playkit/src/main/java/com/kaltura/playkit/plugins/TVPAPIAnalyticsPlugin.java @@ -20,6 +20,7 @@ public class TVPAPIAnalyticsPlugin extends PhoenixAnalyticsPlugin { private static final PKLog log = PKLog.get("TVPAPIAnalyticsPlugin"); private static final String TAG = "TVPAPIAnalytics"; private JsonObject testInitObj = new JsonObject(); + private long lastKnownPlayerPosition = 0; public static final Factory factory = new Factory() { @Override @@ -52,9 +53,11 @@ protected void sendAnalyticsEvent(final PhoenixActionType eventType){ if (initObj == null) { return; } - + if (!"stop".equals(action)) { + lastKnownPlayerPosition = player.getCurrentPosition(); + } RequestBuilder requestBuilder = MediaMarkService.sendTVPAPIEVent(baseUrl + "m=" + method, initObj, action, - mediaConfig.getMediaEntry().getId(), /*mediaConfig.getMediaEntry().getFileId()*/ fileId, player.getCurrentPosition()); + mediaConfig.getMediaEntry().getId(), /*mediaConfig.getMediaEntry().getFileId()*/ fileId, lastKnownPlayerPosition); requestBuilder.completion(new OnRequestCompletion() { @Override