Back to project page matrix-android-sdk.
The source code is released under:
Apache License
If you think the Android project matrix-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.matrix.matrixandroidsdk; /*from w w w . j a va2 s . c o m*/ import android.content.Context; import org.matrix.androidsdk.MXDataHandler; import org.matrix.androidsdk.MXSession; import org.matrix.androidsdk.data.MXMemoryStore; import org.matrix.androidsdk.rest.model.login.Credentials; import org.matrix.matrixandroidsdk.store.LoginStorage; import java.util.ArrayList; import java.util.List; /** * Singleton to control access to the Matrix SDK and providing point of control for MXSessions. */ public class Matrix { private static Matrix instance = null; private LoginStorage mLoginStorage; private MXSession mDefaultSession; protected Matrix(Context appContext) { mLoginStorage = new LoginStorage(appContext.getApplicationContext()); } public synchronized static Matrix getInstance(Context appContext) { if (instance == null) { instance = new Matrix(appContext); } return instance; } /** * Retrieve the default session if one exists. * * The default session may be user-configured, or it may be the last session the user was using. * @return The default session or null. */ public synchronized MXSession getDefaultSession() { if (mDefaultSession != null) { return mDefaultSession; } Credentials creds = mLoginStorage.getDefaultCredentials(); if (creds == null) { return null; } mDefaultSession = createSession(creds); return mDefaultSession; } /** * Clears the default session and the login credentials. */ public synchronized void clearDefaultSessionAndCredentials() { mDefaultSession = null; mLoginStorage.setDefaultCredentials(null); } /** * Clears the default session. */ public synchronized void clearDefaultSession() { mDefaultSession = null; } /** * Set a default session. * @param session The session to store as the default session. */ public synchronized void setDefaultSession(MXSession session) { mLoginStorage.setDefaultCredentials(session.getCredentials()); mDefaultSession = session; } /** * Creates an MXSession from some credentials. * @param credentials The credentials to create a session from. * @return The session. */ public MXSession createSession(Credentials credentials) { return createSession(credentials, true); } /** * Creates an MXSession from some credentials. * @param credentials The credentials to create a session from. * @param useHttps True to enforce https URIs on the home server. * @return The session. */ public MXSession createSession(Credentials credentials, boolean useHttps) { if (!credentials.homeServer.startsWith("http")) { if (useHttps) { credentials.homeServer = "https://" + credentials.homeServer; } else { credentials.homeServer = "http://" + credentials.homeServer; } } return new MXSession(new MXDataHandler(new MXMemoryStore(), credentials), credentials); } /** * Retrieve a list of possible credentials to use. * @param context Application context * @return A list of credentials, or an empty list. */ public List<Credentials> getCredentialsList(Context context) { List<Credentials> credList = new ArrayList<Credentials>(); Credentials creds = mLoginStorage.getDefaultCredentials(); if (creds != null) { credList.add(creds); } // TODO support >1 creds. return credList; } }