Skip to content

Commit

Permalink
FEM1203 - TVPapi Analyitics - STOP wrong position (kaltura#195)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
giladna authored Apr 4, 2017
1 parent 4f92748 commit ca55609
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void updatePluginConfig(@NonNull String pluginName, @Nullable Object plug

@Override
public void destroy() {
stop();
releasePlugins();
releasePlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import java.util.TimerTask;


/**
* Created by zivilan on 02/11/2016.
*/

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,
Expand All @@ -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
Expand All @@ -65,7 +69,7 @@ public PKPlugin newInstance() {

@Override
public void warmUp(Context context) {

}
};

Expand Down Expand Up @@ -99,7 +103,6 @@ protected void onApplicationResumed() {
@Override
public void onDestroy() {
log.d("onDestroy");
sendAnalyticsEvent(PhoenixActionType.STOP);
timer.cancel();
}

Expand All @@ -110,16 +113,22 @@ 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");
}

private PKEvent.Listener mEventListener = new PKEvent.Listener() {
@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();
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca55609

Please sign in to comment.