From 36202b70bdd7e7de266e83917da0160ba5be5906 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Thu, 2 Mar 2017 15:28:40 +0200 Subject: [PATCH 01/10] updating demo --- .../src/main/java/com/kaltura/playkitdemo/MainActivity.java | 5 +++-- .../src/main/java/com/kaltura/playkitdemo/MockParams.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/playkitdemo/src/main/java/com/kaltura/playkitdemo/MainActivity.java b/playkitdemo/src/main/java/com/kaltura/playkitdemo/MainActivity.java index 3705334eb..74c8e52ca 100644 --- a/playkitdemo/src/main/java/com/kaltura/playkitdemo/MainActivity.java +++ b/playkitdemo/src/main/java/com/kaltura/playkitdemo/MainActivity.java @@ -56,6 +56,7 @@ import static com.kaltura.playkitdemo.MockParams.Format; import static com.kaltura.playkitdemo.MockParams.Format2; import static com.kaltura.playkitdemo.MockParams.Format_HD_Dash; +import static com.kaltura.playkitdemo.MockParams.Format_SD_Dash; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { @@ -172,9 +173,9 @@ public void onComplete(PrimitiveResult response) { if(response.error == null) { mediaProvider = new PhoenixMediaProvider() .setSessionProvider(ottSessionProvider) - .setAssetId(ArrivalMediaId) + .setAssetId(ArrivalMediaId) //bunny no horses id = "485380" .setAssetType(APIDefines.KalturaAssetType.Media) - .setFormats(MockParams.Format_SD_Dash, Format_HD_Dash, Format, Format2); + .setFormats(Format_SD_Dash, Format_HD_Dash, Format, Format2); mediaProvider.load(completion); } diff --git a/playkitdemo/src/main/java/com/kaltura/playkitdemo/MockParams.java b/playkitdemo/src/main/java/com/kaltura/playkitdemo/MockParams.java index 5be4cf2bb..964cc0f5c 100644 --- a/playkitdemo/src/main/java/com/kaltura/playkitdemo/MockParams.java +++ b/playkitdemo/src/main/java/com/kaltura/playkitdemo/MockParams.java @@ -10,7 +10,7 @@ public class MockParams { /*Ott Mock params: */ - public static final String PhoenixBaseUrl = "http://api-preprod.ott.kaltura.com/v4_2/api_v3/"; + public static final String PhoenixBaseUrl = "https://api-preprod.ott.kaltura.com/v4_2/api_v3/"; public static final int OttPartnerId = 198; //result of login with : [username: albert@gmail.com, pass: 123456] From c2208d79df8a049c43c306f9519fb517c9e3cd83 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Sun, 19 Mar 2017 16:15:03 +0200 Subject: [PATCH 02/10] updating OttSessionProvider - removing session refresh scheduler + adding session refresh listener ++ --- .../playkit/backend/PrimitiveResult.java | 2 +- .../backend/phoenix/OttSessionProvider.java | 138 ++++++++++-------- .../phoenix/data/KalturaLoginResponse.java | 8 + .../phoenix/data/OttResultAdapter.java | 29 +++- 4 files changed, 113 insertions(+), 64 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/PrimitiveResult.java b/playkit/src/main/java/com/kaltura/playkit/backend/PrimitiveResult.java index daf005ba2..774310dde 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/PrimitiveResult.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/PrimitiveResult.java @@ -6,7 +6,7 @@ */ public class PrimitiveResult extends BaseResult { - private String result; + private String result = null; public PrimitiveResult(String result) { super(); diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index 8be045a15..f434e72a0 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -1,6 +1,5 @@ package com.kaltura.playkit.backend.phoenix; -import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Base64; @@ -27,8 +26,8 @@ import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -38,23 +37,25 @@ public class OttSessionProvider extends BaseSessionProvider { - public static final long TimeDelta = 2 * 60 * 60;//hour in seconds private static final String TAG = "BaseResult"; + + public static final long TimeDelta = 2 * 60 * 60;//hour in seconds public static final int DeltaPercent = 12; - public static final long IMMEDIATE_REFRESH = 1; + public static final long NEED_REFRESH = 0; public static final String DummyUserId = "1"; - private OttSessionParams sessionParams; + //private OttSessionParams sessionParams; + private String sessionUdid; private String refreshToken; private long refreshDelta = 9 * 60 * 60 * 24;//TimeDelta; private int partnerId = 0; - private ScheduledFuture scheduledRefreshTask; - private ScheduledThreadPoolExecutor refreshScheduleExecutor; + private Future refreshTaskFurure; + private ThreadPoolExecutor refreshExecutor; private AtomicBoolean refreshInProgress = new AtomicBoolean(false); private OnCompletion sessionRecoveryCallback; - + private OnCompletion sessionRefreshListener; //region refresh callable private Callable refreshCallable = new Callable() { @@ -74,8 +75,8 @@ public Boolean call() throws Exception { PKLog.d(TAG, "start running refresh token"); - if(scheduledRefreshTask != null && scheduledRefreshTask.isCancelled()){ - scheduledRefreshTask = null; + if(refreshTaskFurure != null && refreshTaskFurure.isCancelled()){ + refreshTaskFurure = null; PKLog.d(TAG, "refresh operation got canceled"); return false; } @@ -97,14 +98,18 @@ public OttSessionProvider(String baseUrl, int partnerId) { } private void initRefreshExecutor() { - refreshScheduleExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2); + refreshExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2); + refreshExecutor.setKeepAliveTime(10, TimeUnit.SECONDS); + } - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - refreshScheduleExecutor.setRemoveOnCancelPolicy(true); - } - refreshScheduleExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); - refreshScheduleExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); - refreshScheduleExecutor.setKeepAliveTime(10, TimeUnit.SECONDS); + /** + * sets a listener to listen to auto session refreshes + * @param listener + * @return + */ + public OttSessionProvider setRefreshListener(OnCompletion listener){ + this.sessionRefreshListener = listener; + return this; } /** @@ -113,7 +118,8 @@ private void initRefreshExecutor() { * @param udid */ public void startAnonymousSession(@Nullable String udid, final OnCompletion completion) { - this.sessionParams = new OttSessionParams().setUdid(udid); + //this.sessionParams = new OttSessionParams().setUdid(udid); + this.sessionUdid = udid; MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); multiRequest.add(OttUserService.anonymousLogin(apiBaseUrl, partnerId, udid), @@ -139,10 +145,11 @@ public void onComplete(ResponseElement response) { public void startSession(@NonNull String username, @NonNull String password, @Nullable String udid, final OnCompletion completion) { // login user //get session data for expiration time - this.sessionParams = new OttSessionParams().setPassword(password).setUsername(username).setUdid(udid); + //this.sessionParams = new OttSessionParams().setPassword(password).setUsername(username).setUdid(udid); + this.sessionUdid = udid; MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); - multiRequest.add(OttUserService.userLogin(apiBaseUrl, partnerId, sessionParams.username, sessionParams.password, udid), + multiRequest.add(OttUserService.userLogin(apiBaseUrl, partnerId, username, password, udid), PhoenixSessionService.get(apiBaseUrl, "{1:result:loginSession:ks}")). completion(new OnRequestCompletion() { @Override @@ -209,7 +216,9 @@ private void handleStartSession(ResponseElement response, OnCompletion sessionRecoveryCallback){ - this.sessionParams = new OttSessionParams().setUdid(udid); + //this.sessionParams = new OttSessionParams().setUdid(udid); + this.sessionUdid = udid; + this.refreshToken = refreshToken; this.sessionRecoveryCallback = sessionRecoveryCallback; setSession(ks, Unset, userId); @@ -219,13 +228,13 @@ public void maintainSession(@NonNull String ks, String refreshToken, String user /** * try to re-login with current credentials, if available */ - private void renewSession(OnCompletion completion) { + /*private void renewSession(OnCompletion completion) { if (sessionParams != null) { if (sessionParams.username != null) { startSession(sessionParams.username, sessionParams.password, sessionParams.udid, completion); return; - } else if(isAnonymousSession() /*sessionParams.isAnonymous*/) { + } else if(isAnonymousSession() *//*sessionParams.isAnonymous*//*) { startAnonymousSession(sessionParams.udid, completion); return; } // ?? in case session with no user credential expires, should we login as anonymous or return empty ks? @@ -235,8 +244,7 @@ private void renewSession(OnCompletion completion) { Log.e(TAG, "parameters needed for background session renewal are not available."); //completion.onComplete(new PrimitiveResult().error(ErrorElement.SessionError.message("Session expired"))); completion.onComplete(new PrimitiveResult((String)null));//returns empty ks - - } + }*/ public boolean isAnonymousSession() { return getUserSessionType().equals(UserSessionType.Anonymous); @@ -260,7 +268,7 @@ public void endSession(final OnCompletion completion) { return; } - APIOkRequestsExecutor.getSingleton().queue(OttUserService.logout(apiBaseUrl, getSessionToken(), sessionParams.udid) + APIOkRequestsExecutor.getSingleton().queue(OttUserService.logout(apiBaseUrl, getSessionToken(), sessionUdid) .completion(new OnRequestCompletion() { @Override public void onComplete(ResponseElement response) { @@ -272,7 +280,7 @@ public void onComplete(ResponseElement response) { PKLog.e(TAG, "endSession: session logout failed. clearing session data. " + error.getMessage()); } OttSessionProvider.super.endSession(); - sessionParams = null; + sessionUdid = null; if (completion != null) { completion.onComplete(new BaseResult(error)); } @@ -280,7 +288,7 @@ public void onComplete(ResponseElement response) { }).build()); } else { - sessionParams = null; + sessionUdid = null; } } @@ -297,23 +305,23 @@ public void getSessionToken(final OnCompletion completion) { completion.onComplete(new PrimitiveResult(ks)); } } else { - renewSession(completion); + // to get the refreshed ks on the completion callback + sessionRecoveryCallback = completion; + submitRefreshSessionTask(); } } protected String validateSession() { long currentDate = System.currentTimeMillis() / 1000; - long timeLeft = expiryDate == Unset ? IMMEDIATE_REFRESH : expiryDate - currentDate; + long timeLeft = expiryDate == Unset ? NEED_REFRESH : expiryDate - currentDate; String token = null; - if (timeLeft > 0) { // validate refreshToken expiration time + // returns current session token if current time didn't exceed the ks expiry time minus delta + // otherwise return null to submit refresh + if (refreshDelta > 0 && timeLeft > refreshDelta) { token = getSessionToken(); - - if (timeLeft < refreshDelta) { - // call refreshToken - scheduleRefreshSessionTask(IMMEDIATE_REFRESH); - } } + return token; } @@ -321,7 +329,7 @@ protected String validateSession() { private void refreshSessionCall() { // multi request needed to fetch the new expiration date. MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); - multiRequest.add(OttUserService.refreshSession(apiBaseUrl, getSessionToken(), refreshToken, sessionParams.udid), + multiRequest.add(OttUserService.refreshSession(apiBaseUrl, getSessionToken(), refreshToken, sessionUdid), PhoenixSessionService.get(apiBaseUrl, "{1:result:ks}")) .completion(new OnRequestCompletion() { @Override @@ -349,13 +357,19 @@ public void onComplete(ResponseElement response) { refreshResult = new PrimitiveResult(getSessionToken()); } } - if(sessionRecoveryCallback != null){ - sessionRecoveryCallback.onComplete(refreshResult != null ? + + final PrimitiveResult refreshedKsResult = refreshResult != null ? refreshResult : - new PrimitiveResult(ErrorElement.SessionError.addMessage(" FAILED TO RECOVER SESSION!!"))); + new PrimitiveResult(ErrorElement.SessionError.addMessage(" FAILED TO RECOVER SESSION!!")); + if(sessionRecoveryCallback != null){ + sessionRecoveryCallback.onComplete(refreshedKsResult); sessionRecoveryCallback = null; } + + if(sessionRefreshListener != null && refreshResult != null){ + sessionRefreshListener.onComplete(refreshResult.getResult()); + } } }); APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); @@ -363,21 +377,20 @@ public void onComplete(ResponseElement response) { /** - * set scheduler to refresh tokens according to calculated delay. (expiration time and safety padding) - * @param delay - the time to schedule the refresh to. if the delay is 0, force refresh. + * submit task to refresh current session */ - private synchronized void scheduleRefreshSessionTask(long delay) { - PKLog.i(TAG, "scheduling refresh in about " + delay + " sec from now"); - scheduledRefreshTask = refreshScheduleExecutor.schedule(refreshCallable, delay, TimeUnit.SECONDS); + private synchronized void submitRefreshSessionTask() { + PKLog.i(TAG, "submit refresh session task"); + refreshTaskFurure = refreshExecutor.submit(refreshCallable); } - private void clearScheduled() { - Log.d(TAG, "clearScheduled: Thread - "+Thread.currentThread().getId()); + private void cancelCurrentRefreshTask() { + Log.d(TAG, "cancelCurrentRefreshTask: Thread - "+Thread.currentThread().getId()); - if(scheduledRefreshTask != null && !scheduledRefreshTask.isDone()){ - scheduledRefreshTask.cancel(true); + if(refreshTaskFurure != null && !refreshTaskFurure.isDone()){ + refreshTaskFurure.cancel(true); } - scheduledRefreshTask = null; + refreshTaskFurure = null; } @Override @@ -386,22 +399,23 @@ public void clearSession() { refreshToken = null; refreshDelta = TimeDelta; - clearScheduled(); + cancelCurrentRefreshTask(); } - private boolean isScheduled() { - return scheduledRefreshTask != null && !scheduledRefreshTask.isDone() && !scheduledRefreshTask.isCancelled(); + private boolean hasActiveRefreshTask() { + return refreshTaskFurure != null && !refreshTaskFurure.isDone() && !refreshTaskFurure.isCancelled(); } @Override protected void setSession(String sessionToken, long expiry, String userId) { super.setSession(sessionToken, expiry, userId); - long delay = 1; + //long delay = 1; if(expiry != Unset) { updateRefreshDelta(expiry); - delay = expiry - refreshDelta; + //delay = expiry - refreshDelta; + } else { + submitRefreshSessionTask(); } - scheduleRefreshSessionTask(delay); } private void updateRefreshDelta(long expiry) { @@ -415,7 +429,7 @@ private void updateRefreshDelta(long expiry) { */ public String encryptSession(){ StringBuilder data = new StringBuilder(getSessionToken()).append(" ~~ ") - .append(refreshToken).append(" ~~ ").append(sessionParams.udid()); + .append(refreshToken).append(" ~~ ").append(sessionUdid); return Base64.encodeToString(data.toString().getBytes(), Base64.NO_WRAP); } @@ -436,7 +450,7 @@ public boolean recoverSession(String encryptSession, OnCompletion constructor = typeOfT.getClass().getConstructor(ErrorElement.class); + + if (constructor != null) { + baseResult = (BaseResult) constructor.newInstance(error); + } + } catch (NoSuchMethodException e) { + + } catch (IllegalAccessException e) { + + } catch (InstantiationException e) { + + } catch (InvocationTargetException e) { + + } + + if (baseResult == null) { + baseResult = new BaseResult(error); + } } else if(result != null && result.has("objectType")){ + String objectType= result.getAsJsonPrimitive("objectType").getAsString(); if(objectType.equals("KalturaAPIException")) { baseResult = new BaseResult(new Gson().fromJson(result, ErrorElement.class)); @@ -44,6 +70,7 @@ public BaseResult deserialize(JsonElement json, Type typeOfT, JsonDeserializatio Class clz = Class.forName(clzName); baseResult = (BaseResult) new Gson().fromJson(result, clz); } catch (ClassNotFoundException e) { + PKLog.e("OttResultAdapter","can't find class "+objectType+ " in the provided package\n "); e.printStackTrace(); } } From f1e604e8990245eb9d18a4c176067e1bb19a3ea0 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Sun, 19 Mar 2017 17:34:29 +0200 Subject: [PATCH 03/10] adding socialLogin and switchUser support to OttSessionProvider --- .../backend/phoenix/OttSessionProvider.java | 41 +++++++++++++++++++ .../phoenix/services/OttUserService.java | 28 +++++++++++++ .../services/PhoenixSessionService.java | 20 +++++++++ 3 files changed, 89 insertions(+) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index f434e72a0..dcaea67bc 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -160,6 +160,47 @@ public void onComplete(ResponseElement response) { APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); } + + /** + * starts social network related session + * login and get the session expiration for refresh purposes. + * + * @param socialToken - social network valid token + * @param udid + * @param completion + */ + public void startSocialSession(@NonNull OttUserService.KalturaSocialNetwork socialNetwork, @NonNull String socialToken, @Nullable String udid, final OnCompletion completion) { + this.sessionUdid = udid; + + MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); + multiRequest.add(OttUserService.socialLogin(apiBaseUrl, partnerId, socialToken, socialNetwork.value, udid), + PhoenixSessionService.get(apiBaseUrl, "{1:result:loginSession:ks}")). + completion(new OnRequestCompletion() { + @Override + public void onComplete(ResponseElement response) { + handleStartSession(response, completion); + } + }); + APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); + } + + public void switchUser(@NonNull String userId, @Nullable String udid, final OnCompletion completion) { + // switchUser + //get session data for expiration time + this.sessionUdid = udid; + + MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); + multiRequest.add(PhoenixSessionService.switchUser(apiBaseUrl, getSessionToken(), userId, udid), + PhoenixSessionService.get(apiBaseUrl, "{1:result:loginSession:ks}")). + completion(new OnRequestCompletion() { + @Override + public void onComplete(ResponseElement response) { + handleStartSession(response, completion); + } + }); + APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); + } + /** * handles start session response. * if session was established update members and pass "ks" on the callback diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java index 3f413d0c5..38c278a10 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java @@ -1,5 +1,6 @@ package com.kaltura.playkit.backend.phoenix.services; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; @@ -83,4 +84,31 @@ public static PhoenixRequestBuilder logout(String baseUrl, String ks, @Nullable .params(params); } + public static PhoenixRequestBuilder socialLogin(@NonNull String baseUrl, int partnerId, @NonNull String token, String socialNetwork, @Nullable String udid) { + JsonObject params = new JsonObject(); + params.addProperty("token", token); + params.addProperty("partnerId", partnerId); + params.addProperty("type", socialNetwork); + if(udid != null) { + params.addProperty("udid", udid); + } + + return new PhoenixRequestBuilder() + .service("social") + .action("login") + .method("POST") + .url(baseUrl) + .tag("social-login") + .params(params); + } + + public enum KalturaSocialNetwork{ + FACEBOOK("facebook"); + + KalturaSocialNetwork(String value){ + this.value = value; + } + + public String value; + } } diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java index 132e990fa..cf4c113cd 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java @@ -1,5 +1,8 @@ package com.kaltura.playkit.backend.phoenix.services; +import android.support.annotation.Nullable; +import android.text.TextUtils; + import com.google.gson.JsonObject; import com.kaltura.playkit.backend.phoenix.PhoenixRequestBuilder; @@ -21,4 +24,21 @@ public static PhoenixRequestBuilder get(String baseUrl, String ks){ .tag("session-get") .params(params); } + + public static PhoenixRequestBuilder switchUser(String baseUrl, String ks, String userIdToSwitch, @Nullable String udid){ + JsonObject params = new JsonObject(); + params.addProperty("ks", ks); + params.addProperty("userIdToSwitch", userIdToSwitch); + if(!TextUtils.isEmpty(udid)){ + params.addProperty("udid", udid); + } + + return new PhoenixRequestBuilder() + .service("session") + .action("switchUser") + .method("POST") + .url(baseUrl) + .tag("session-switchUser") + .params(params); + } } From 4c0fab54a60bece60548b79c23be9ccc6bc125d5 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Sun, 19 Mar 2017 18:15:52 +0200 Subject: [PATCH 04/10] OttSessionProvider - remove udid from switchUser --- .../backend/SessionProviderAndroidTest.java | 10 ++++++++- .../backend/phoenix/OttSessionProvider.java | 21 +++++++++++-------- .../services/PhoenixSessionService.java | 8 +------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java b/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java index 90beb8d95..e77f52896 100644 --- a/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java +++ b/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java @@ -133,7 +133,7 @@ public void onComplete(PrimitiveResult response) { } - @Test + @Test public void testOttAnonymousSession() { final OttSessionProvider ottSessionProvider = new OttSessionProvider(PnxBaseUrl, PnxPartnerId); @@ -235,6 +235,14 @@ public void onComplete(ResultElement response) { wait(1); } + @Test + public void testOttSwitchUser(){ + + } + + + + @Test public void testOvpSessionProviderBaseFlow() { ovpSessionProvider = new OvpSessionProvider(OvpBaseUrl); diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index dcaea67bc..eb930a5a5 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -118,7 +118,7 @@ public OttSessionProvider setRefreshListener(OnCompletion listener){ * @param udid */ public void startAnonymousSession(@Nullable String udid, final OnCompletion completion) { - //this.sessionParams = new OttSessionParams().setUdid(udid); + this.sessionUdid = udid; MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); @@ -143,9 +143,9 @@ public void onComplete(ResponseElement response) { * @param completion */ public void startSession(@NonNull String username, @NonNull String password, @Nullable String udid, final OnCompletion completion) { - // login user - //get session data for expiration time - //this.sessionParams = new OttSessionParams().setPassword(password).setUsername(username).setUdid(udid); + //1. login user + //2. get session data: expiration time + this.sessionUdid = udid; MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); @@ -170,6 +170,7 @@ public void onComplete(ResponseElement response) { * @param completion */ public void startSocialSession(@NonNull OttUserService.KalturaSocialNetwork socialNetwork, @NonNull String socialToken, @Nullable String udid, final OnCompletion completion) { + this.sessionUdid = udid; MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); @@ -184,13 +185,15 @@ public void onComplete(ResponseElement response) { APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); } - public void switchUser(@NonNull String userId, @Nullable String udid, final OnCompletion completion) { - // switchUser - //get session data for expiration time - this.sessionUdid = udid; + /** + * switch to another user in household + * @param userId + * @param completion + */ + public void switchUser(@NonNull String userId, final OnCompletion completion) { MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); - multiRequest.add(PhoenixSessionService.switchUser(apiBaseUrl, getSessionToken(), userId, udid), + multiRequest.add(PhoenixSessionService.switchUser(apiBaseUrl, getSessionToken(), userId), PhoenixSessionService.get(apiBaseUrl, "{1:result:loginSession:ks}")). completion(new OnRequestCompletion() { @Override diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java index cf4c113cd..787fc88e9 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/PhoenixSessionService.java @@ -1,8 +1,5 @@ package com.kaltura.playkit.backend.phoenix.services; -import android.support.annotation.Nullable; -import android.text.TextUtils; - import com.google.gson.JsonObject; import com.kaltura.playkit.backend.phoenix.PhoenixRequestBuilder; @@ -25,13 +22,10 @@ public static PhoenixRequestBuilder get(String baseUrl, String ks){ .params(params); } - public static PhoenixRequestBuilder switchUser(String baseUrl, String ks, String userIdToSwitch, @Nullable String udid){ + public static PhoenixRequestBuilder switchUser(String baseUrl, String ks, String userIdToSwitch){ JsonObject params = new JsonObject(); params.addProperty("ks", ks); params.addProperty("userIdToSwitch", userIdToSwitch); - if(!TextUtils.isEmpty(udid)){ - params.addProperty("udid", udid); - } return new PhoenixRequestBuilder() .service("session") From 989dc7d94240f6dd8d76b7246d76df5a49f7d77b Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Sun, 19 Mar 2017 19:14:55 +0200 Subject: [PATCH 05/10] OttSessionProvider - switchUser fix --- .../com/kaltura/playkit/backend/SessionProviderAndroidTest.java | 2 +- .../com/kaltura/playkit/backend/phoenix/OttSessionProvider.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java b/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java index e77f52896..05adf4614 100644 --- a/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java +++ b/playkit/src/androidTest/java/com/kaltura/playkit/backend/SessionProviderAndroidTest.java @@ -237,7 +237,7 @@ public void onComplete(ResultElement response) { @Test public void testOttSwitchUser(){ - + } diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index eb930a5a5..4347d124c 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -194,7 +194,7 @@ public void switchUser(@NonNull String userId, final OnCompletion Date: Mon, 20 Mar 2017 14:45:01 +0200 Subject: [PATCH 06/10] codacity updates --- .../backend/phoenix/OttSessionProvider.java | 27 +++++++++++-------- .../phoenix/services/OttUserService.java | 4 +-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index 4347d124c..228ada6af 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -39,15 +39,20 @@ public class OttSessionProvider extends BaseSessionProvider { private static final String TAG = "BaseResult"; - public static final long TimeDelta = 2 * 60 * 60;//hour in seconds - public static final int DeltaPercent = 12; + private static final long TimeDelta = 2 * 60 * 60;//2 hours in seconds - default refresh delta for 24h session + private static final int DeltaPercent = 12; public static final long NEED_REFRESH = 0; - public static final String DummyUserId = "1"; + private static final String DummyUserId = "1"; + private static final int EXECUTOR_KEEP_ALIVE = 10; + private static final int MilliInSecond = 1000; + + public static final int KsParam = 0; + public static final int RefreshTokenParam = 1; + public static final int UdidParam = 2; - //private OttSessionParams sessionParams; private String sessionUdid; private String refreshToken; - private long refreshDelta = 9 * 60 * 60 * 24;//TimeDelta; + private long refreshDelta = TimeDelta; private int partnerId = 0; private Future refreshTaskFurure; @@ -99,7 +104,7 @@ public OttSessionProvider(String baseUrl, int partnerId) { private void initRefreshExecutor() { refreshExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2); - refreshExecutor.setKeepAliveTime(10, TimeUnit.SECONDS); + refreshExecutor.setKeepAliveTime(EXECUTOR_KEEP_ALIVE, TimeUnit.SECONDS); } /** @@ -298,7 +303,7 @@ public boolean isAnonymousSession() { * Ends current active session. if it's a {@link com.kaltura.playkit.backend.base.BaseSessionProvider.UserSessionType#User} session * logout, if {@link com.kaltura.playkit.backend.base.BaseSessionProvider.UserSessionType#Anonymous} will return, since * logout on anonymous session doesn't make the session invalid. - *

+ * * If logout was activated, session params are cleared. */ public void endSession(final OnCompletion completion) { @@ -446,9 +451,9 @@ public void clearSession() { cancelCurrentRefreshTask(); } - private boolean hasActiveRefreshTask() { + /*private boolean hasActiveRefreshTask() { return refreshTaskFurure != null && !refreshTaskFurure.isDone() && !refreshTaskFurure.isCancelled(); - } + }*/ @Override protected void setSession(String sessionToken, long expiry, String userId) { @@ -463,7 +468,7 @@ protected void setSession(String sessionToken, long expiry, String userId) { } private void updateRefreshDelta(long expiry) { - long currentDate = System.currentTimeMillis() / 1000; + long currentDate = System.currentTimeMillis() / MilliInSecond; refreshDelta = (expiry - currentDate) * DeltaPercent / 100; // 20% of total validation time } @@ -488,7 +493,7 @@ public boolean recoverSession(String encryptSession, OnCompletion= 3 && !data[2].equals("null") ? data[2] : null, sessionRecoveryCallback); + maintainSession(data[KsParam], data[RefreshTokenParam], DummyUserId, data.length >= 3 && !data[UdidParam].equals("null") ? data[UdidParam] : null, sessionRecoveryCallback); return true; } diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java index 38c278a10..d7a649238 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/services/OttUserService.java @@ -105,10 +105,10 @@ public static PhoenixRequestBuilder socialLogin(@NonNull String baseUrl, int par public enum KalturaSocialNetwork{ FACEBOOK("facebook"); + public String value; + KalturaSocialNetwork(String value){ this.value = value; } - - public String value; } } From d783c1755f2e5e9341dae415cd2635b7e8c1a842 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Tue, 21 Mar 2017 12:29:42 +0200 Subject: [PATCH 07/10] removing unused code in OttSessionProvider + SourceSelector check for empty on the DRM list in PKMediaSource --- .../backend/phoenix/OttSessionProvider.java | 71 +------------------ .../playkit/player/SourceSelector.java | 2 +- 2 files changed, 2 insertions(+), 71 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index 228ada6af..de918acf5 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -273,28 +273,6 @@ public void maintainSession(@NonNull String ks, String refreshToken, String user setSession(ks, Unset, userId); } - - /** - * try to re-login with current credentials, if available - */ - /*private void renewSession(OnCompletion completion) { - if (sessionParams != null) { - if (sessionParams.username != null) { - startSession(sessionParams.username, sessionParams.password, sessionParams.udid, completion); - return; - - } else if(isAnonymousSession() *//*sessionParams.isAnonymous*//*) { - startAnonymousSession(sessionParams.udid, completion); - return; - } // ?? in case session with no user credential expires, should we login as anonymous or return empty ks? - - } - - Log.e(TAG, "parameters needed for background session renewal are not available."); - //completion.onComplete(new PrimitiveResult().error(ErrorElement.SessionError.message("Session expired"))); - completion.onComplete(new PrimitiveResult((String)null));//returns empty ks - }*/ - public boolean isAnonymousSession() { return getUserSessionType().equals(UserSessionType.Anonymous); } @@ -451,17 +429,12 @@ public void clearSession() { cancelCurrentRefreshTask(); } - /*private boolean hasActiveRefreshTask() { - return refreshTaskFurure != null && !refreshTaskFurure.isDone() && !refreshTaskFurure.isCancelled(); - }*/ - @Override protected void setSession(String sessionToken, long expiry, String userId) { super.setSession(sessionToken, expiry, userId); - //long delay = 1; if(expiry != Unset) { updateRefreshDelta(expiry); - //delay = expiry - refreshDelta; + } else { submitRefreshSessionTask(); } @@ -496,46 +469,4 @@ public boolean recoverSession(String encryptSession, OnCompletion= 3 && !data[UdidParam].equals("null") ? data[UdidParam] : null, sessionRecoveryCallback); return true; } - - - - /*private class OttSessionParams { - private String udid = ""; - private String username; - private String password; - //private boolean isAnonymous = false; - - public String udid() { - return udid; - } - - public OttSessionParams setUdid(String udid) { - this.udid = udid; - return this; - } - - public String username() { - return username; - } - - public OttSessionParams setUsername(String username) { - this.username = username; - return this; - } - - public String password() { - return password; - } - - public OttSessionParams setPassword(String password) { - this.password = password; - return this; - } - - *//*public OttSessionParams setAnonymous() { - this.isAnonymous = true; - return this; - }*//* - }*/ - } diff --git a/playkit/src/main/java/com/kaltura/playkit/player/SourceSelector.java b/playkit/src/main/java/com/kaltura/playkit/player/SourceSelector.java index 024cfc24f..1f3893fc1 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/SourceSelector.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/SourceSelector.java @@ -55,7 +55,7 @@ PKMediaSource getPreferredSource() { } List drmParams = source.getDrmData(); - if (drmParams != null) { + if (drmParams != null && !drmParams.isEmpty()) { for (PKDrmParams params : drmParams) { if (params.isSchemeSupported()) { return source; From 346433ae024220b79471dd5a657d8e27644087f0 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Tue, 21 Mar 2017 14:42:51 +0200 Subject: [PATCH 08/10] OttSessionProvider - loading ks instead of using current to prevent expired ks --- .../backend/phoenix/OttSessionProvider.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index de918acf5..442b3ba30 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -195,18 +195,28 @@ public void onComplete(ResponseElement response) { * @param userId * @param completion */ - public void switchUser(@NonNull String userId, final OnCompletion completion) { + public void switchUser(@NonNull final String userId, final OnCompletion completion) { + + getSessionToken(new OnCompletion() { + @Override + public void onComplete(PrimitiveResult response) { + if(response.error == null) { // in case the session checked for expiry and ready to use: + MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); + multiRequest.add(PhoenixSessionService.switchUser(apiBaseUrl, getSessionToken(), userId), + PhoenixSessionService.get(apiBaseUrl, "{1:result:ks}")). + completion(new OnRequestCompletion() { + @Override + public void onComplete(ResponseElement response) { + handleStartSession(response, completion); + } + }); + APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); - MultiRequestBuilder multiRequest = PhoenixService.getMultirequest(apiBaseUrl, null); - multiRequest.add(PhoenixSessionService.switchUser(apiBaseUrl, getSessionToken(), userId), - PhoenixSessionService.get(apiBaseUrl, "{1:result:ks}")). - completion(new OnRequestCompletion() { - @Override - public void onComplete(ResponseElement response) { - handleStartSession(response, completion); - } - }); - APIOkRequestsExecutor.getSingleton().queue(multiRequest.build()); + } else { // in case ks retrieval failed: + completion.onComplete(response); + } + } + }); } /** From fab50abdeb04ef197b20b6138bfd19842a890c19 Mon Sep 17 00:00:00 2001 From: Tehila Rozin Date: Tue, 21 Mar 2017 15:08:39 +0200 Subject: [PATCH 09/10] OttSessionProvider passing updated ks --- .../com/kaltura/playkit/backend/phoenix/OttSessionProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java index 442b3ba30..9b7827692 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/OttSessionProvider.java @@ -202,7 +202,7 @@ public void switchUser(@NonNull final String userId, final OnCompletion Date: Tue, 21 Mar 2017 15:16:13 +0200 Subject: [PATCH 10/10] adding comment on OttResultAdapter --- .../playkit/backend/phoenix/data/OttResultAdapter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/data/OttResultAdapter.java b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/data/OttResultAdapter.java index d3c3c162d..0e7f6ab3a 100644 --- a/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/data/OttResultAdapter.java +++ b/playkit/src/main/java/com/kaltura/playkit/backend/phoenix/data/OttResultAdapter.java @@ -46,13 +46,13 @@ public BaseResult deserialize(JsonElement json, Type typeOfT, JsonDeserializatio baseResult = (BaseResult) constructor.newInstance(error); } } catch (NoSuchMethodException e) { - + // do nothing } catch (IllegalAccessException e) { - + // do nothing } catch (InstantiationException e) { - + // do nothing } catch (InvocationTargetException e) { - + // do nothing - next code section will handle this } if (baseResult == null) {