2015-05-28 19:37:43 -04:00
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
|
|
function updateCredentials() {
|
|
|
|
|
|
2015-06-26 23:27:38 -04:00
|
|
|
|
Logger.log('sending updated credentials to ApiClientBridge');
|
2015-05-28 19:37:43 -04:00
|
|
|
|
|
|
|
|
|
var json = JSON.stringify(ConnectionManager.credentialProvider().credentials());
|
|
|
|
|
var credentials = JSON.parse(json);
|
|
|
|
|
|
|
|
|
|
for (var i = 0, length = credentials.Servers.length; i < length; i++) {
|
|
|
|
|
var server = credentials.Servers[i];
|
|
|
|
|
|
|
|
|
|
if (server.DateLastAccessed != null) {
|
|
|
|
|
server.DateLastAccessed = new Date(server.DateLastAccessed).toISOString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json = JSON.stringify(credentials);
|
|
|
|
|
ApiClientBridge.updateCredentials(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initNativeConnectionManager() {
|
|
|
|
|
|
2015-06-26 23:27:38 -04:00
|
|
|
|
Logger.log('initNativeConnectionManager');
|
2015-05-28 19:37:43 -04:00
|
|
|
|
|
|
|
|
|
var capabilities = ConnectionManager.capabilities();
|
|
|
|
|
|
|
|
|
|
ApiClientBridge.init(AppInfo.appName, AppInfo.appVersion, AppInfo.deviceId, AppInfo.deviceName, JSON.stringify(capabilities));
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-06 00:53:37 -04:00
|
|
|
|
function getDownloadSpeed(bytes, url) {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
|
|
|
|
ApiClientBridge.getDownloadSpeed(bytes, url);
|
|
|
|
|
|
|
|
|
|
Events.on(AndroidAjax, 'downloadspeedresponse', function (e, response) {
|
|
|
|
|
|
|
|
|
|
Events.off(AndroidAjax, 'downloadspeedresponse');
|
|
|
|
|
|
|
|
|
|
if (response) {
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [response]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
// Need to mimic the jquery ajax error response
|
|
|
|
|
deferred.reject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initApiClient(newApiClient) {
|
|
|
|
|
newApiClient.getDownloadSpeed = function (bytes) {
|
|
|
|
|
return getDownloadSpeed(bytes, newApiClient.getUrl('Playback/BitrateTest', {
|
|
|
|
|
api_key: newApiClient.accessToken(),
|
|
|
|
|
Size: bytes
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Events.on(ConnectionManager, 'apiclientcreated', function (e, newApiClient) {
|
|
|
|
|
|
|
|
|
|
initApiClient(newApiClient);
|
|
|
|
|
});
|
|
|
|
|
|
2015-05-28 19:37:43 -04:00
|
|
|
|
Events.on(ConnectionManager.credentialProvider(), 'credentialsupdated', updateCredentials);
|
|
|
|
|
|
|
|
|
|
updateCredentials();
|
|
|
|
|
initNativeConnectionManager();
|
|
|
|
|
|
2015-09-06 00:53:37 -04:00
|
|
|
|
if (window.ApiClient) {
|
|
|
|
|
initApiClient(window.ApiClient);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 19:37:43 -04:00
|
|
|
|
window.AndroidAjax = {
|
|
|
|
|
|
2015-09-06 00:53:37 -04:00
|
|
|
|
onDownloadSpeedResponse: function (response) {
|
|
|
|
|
|
|
|
|
|
Events.trigger(AndroidAjax, 'downloadspeedresponse', [response]);
|
2015-05-28 19:37:43 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|