1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

update connection manager

This commit is contained in:
Luke Pulverenti 2015-05-28 19:37:43 -04:00
parent 6235ee51eb
commit 6cbc470b0e
14 changed files with 209 additions and 47 deletions

View file

@ -88,10 +88,18 @@
return appVersion;
};
self.capabilities = function () {
return capabilities;
};
self.deviceId = function () {
return deviceId;
};
self.credentialProvider = function () {
return credentialProvider;
};
self.connectUserId = function () {
return credentialProvider.credentials().ConnectUserId;
};
@ -103,7 +111,7 @@
self.getLastUsedApiClient = function () {
var servers = credentialProvider.credentials().servers;
var servers = credentialProvider.credentials().Servers;
servers.sort(function (a, b) {
return b.DateLastAccessed - a.DateLastAccessed;
@ -122,7 +130,7 @@
apiClients.push(apiClient);
var existingServer = credentialProvider.credentials().servers.filter(function (s) {
var existingServer = credentialProvider.credentials().Servers.filter(function (s) {
return stringEqualsIgnoreCase(s.ManualAddress, apiClient.serverAddress());
@ -145,7 +153,7 @@
var credentials = credentialProvider.credentials();
var server = credentials.servers.filter(function (s) {
var server = credentials.Servers.filter(function (s) {
return s.Id == systemInfo.Id;
@ -159,7 +167,7 @@
apiClient.serverInfo(server);
credentialProvider.addOrUpdateServer(credentials.servers, server);
credentialProvider.addOrUpdateServer(credentials.Servers, server);
credentialProvider.credentials(credentials);
});
};
@ -213,7 +221,7 @@
}
var credentials = credentialProvider.credentials();
var server = credentials.servers.filter(function (s) {
var server = credentials.Servers.filter(function (s) {
return stringEqualsIgnoreCase(s.Id, serverId);
})[0];
@ -238,7 +246,7 @@
server.AccessToken = null;
}
credentialProvider.addOrUpdateServer(credentials.servers, server);
credentialProvider.addOrUpdateServer(credentials.Servers, server);
saveUserInfoIntoCredentials(server, result.User);
credentialProvider.credentials(credentials);
@ -513,7 +521,7 @@
var credentials = credentialProvider.credentials();
var servers = credentials.servers.filter(function (u) {
var servers = credentials.Servers.filter(function (u) {
return u.UserLinkType != "Guest";
});
@ -533,7 +541,7 @@
}
}
credentials.servers = servers;
credentials.Servers = servers;
credentials.ConnectAccessToken = null;
credentials.ConnectUserId = null;
@ -622,7 +630,7 @@
findServersPromise.done(function (foundServers) {
var servers = credentials.servers.slice(0);
var servers = credentials.Servers.slice(0);
mergeServers(servers, foundServers);
mergeServers(servers, connectServers);
@ -632,7 +640,7 @@
return b.DateLastAccessed - a.DateLastAccessed;
});
credentials.servers = servers;
credentials.Servers = servers;
credentialProvider.credentials(credentials);
@ -921,7 +929,7 @@
server.DateLastAccessed = new Date().getTime();
server.LastConnectionMode = connectionMode;
credentialProvider.addOrUpdateServer(credentials.servers, server);
credentialProvider.addOrUpdateServer(credentials.Servers, server);
credentialProvider.credentials(credentials);
var result = {
@ -1089,7 +1097,7 @@
var credentials = credentialProvider.credentials();
var serverInfo = credentials.servers = credentials.servers.filter(function (s) {
var serverInfo = credentials.Servers = credentials.Servers.filter(function (s) {
return s.ConnectServerId == serverId;
});
@ -1097,7 +1105,7 @@
credentials = credentialProvider.credentials();
credentials.servers = credentials.servers.filter(function (s) {
credentials.Servers = credentials.Servers.filter(function (s) {
return s.ConnectServerId != serverId;
});

View file

@ -8,12 +8,12 @@
var self = this;
var credentials;
var key = 'servercredentials3';
var key = 'servercredentials4';
function ensure() {
credentials = credentials || JSON.parse(appStorage.getItem(key) || '{}');
credentials.servers = credentials.servers || [];
credentials.Servers = credentials.Servers || [];
}
function get() {
@ -30,6 +30,8 @@
} else {
self.clear();
}
Events.trigger(self, 'credentialsupdated');
}
self.clear = function () {

View file

@ -0,0 +1,102 @@
(function () {
function updateCredentials() {
console.log('sending updated credentials to ApiClientBridge');
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() {
console.log('initNativeConnectionManager');
var capabilities = ConnectionManager.capabilities();
ApiClientBridge.init(AppInfo.appName, AppInfo.appVersion, AppInfo.deviceId, AppInfo.deviceName, JSON.stringify(capabilities));
//initAjax();
}
var baseAjaxMethod;
var currentId = 0;
function getNewRequestId() {
var id = currentId++;
return id.toString();
}
function initAjax() {
baseAjaxMethod = AjaxApi.ajax;
AjaxApi.ajax = sendRequest;
}
function sendRequest(request) {
if (request.data || request.contentType || request.dataType != 'json') {
return baseAjaxMethod(request);
}
var deferred = DeferredBuilder.Deferred();
var id = getNewRequestId();
request.headers = request.headers || {};
if (request.dataType == 'json') {
request.headers.accept = 'application/json';
}
var requestHeaders = [];
for (name in request.headers) {
requestHeaders.push(name + "=" + request.headers[name]);
}
ApiClientBridge.sendRequest(request.url, request.type, requestHeaders.join('|||||'), "window.AndroidAjax.onResponse", id);
Events.on(AndroidAjax, 'response' + id, function (e, status, response) {
Events.off(AndroidAjax, 'response' + id);
response = decodeURIComponent(response);
if (status >= 400) {
alert(status);
deferred.reject();
}
else if (request.dataType == 'json') {
deferred.resolveWith(null, [JSON.parse(response)]);
}
else {
deferred.resolveWith(null, [response]);
}
});
return deferred.promise();
}
Events.on(ConnectionManager.credentialProvider(), 'credentialsupdated', updateCredentials);
updateCredentials();
initNativeConnectionManager();
window.AndroidAjax = {
onResponse: function (id, status, response) {
Events.trigger(AndroidAjax, 'response' + id, [status, response]);
}
};
})();

View file

@ -468,11 +468,11 @@
currentWebAppSession = session.acquire();
currentWebAppSession.on('message', handleMessage);
currentWebAppSession.on('disconnect', handleSessionDisconnect);
currentWebAppSession.connect().success(function () {
console.log('session.connect succeeded');
currentWebAppSession.on('disconnect', handleSessionDisconnect);
MediaController.setActivePlayer(PlayerName, convertDeviceToTarget(device));
currentDeviceFriendlyName = device.getFriendlyName();
@ -507,12 +507,57 @@
session.release();
}
if (currentDevice != null) {
currentDevice.off("ready");
currentDevice.off("disconnect");
currentDevice.disconnect();
}
currentWebAppSession = null;
currentPairedDeviceId = null;
currentDeviceFriendlyName = null;
currentDevice = null;
}
function tryLaunchWebSession(device) {
console.log('calling launchWebApp');
device.getWebAppLauncher().launchWebApp(ApplicationID).success(function (session) {
console.log('launchWebApp success. calling onSessionConnected');
setupWebAppSession(device, session);
}).error(function (err1) {
console.log('launchWebApp error:' + JSON.stringify(err1));
});
}
function tryJoinWebSession(device, enableRetry) {
console.log('calling joinWebApp');
device.getWebAppLauncher().joinWebApp(ApplicationID).success(function (session) {
console.log('joinWebApp success. calling onSessionConnected');
setupWebAppSession(device, session);
}).error(function (err) {
console.log('joinWebApp error: ' + JSON.stringify(err));
if (enableRetry) {
tryJoinWebSession(device, false);
return;
}
console.log('calling launchWebApp');
tryLaunchWebSession(device);
});
}
function launchWebApp(device) {
// First try to join existing session. If it fails, launch a new one
@ -524,7 +569,7 @@
}).error(function (err) {
console.log('joinWebApp error: ' + JSON.stringify(err) + '. calling joinWebApp');
console.log('joinWebApp error: ' + JSON.stringify(err) + '. calling launchWebApp');
device.getWebAppLauncher().launchWebApp(ApplicationID).success(function (session) {
@ -547,13 +592,9 @@
return;
}
console.log('calling launchWebApp');
console.log('creating webAppSession');
setTimeout(function () {
launchWebApp(device);
}, 0);
launchWebApp(device);
}
self.tryPair = function (target) {
@ -610,7 +651,7 @@
if (newTarget.id != currentPairedDeviceId) {
if (currentWebAppSession) {
console.log('Disconnecting from chromecast');
currentWebAppSession.disconnect();
cleanupSession();
}
}
}