Android Open Source - matrix-android-sdk M X Session






From Project

Back to project page matrix-android-sdk.

License

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.

Java Source Code

/*
 * Copyright 2014 OpenMarket Ltd/*www .  ja  v  a  2 s . co m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk;

import android.util.Log;

import org.matrix.androidsdk.data.DataRetriever;
import org.matrix.androidsdk.data.MyUser;
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.rest.callback.ApiCallback;
import org.matrix.androidsdk.rest.callback.ApiFailureCallback;
import org.matrix.androidsdk.rest.callback.SimpleApiCallback;
import org.matrix.androidsdk.rest.client.EventsRestClient;
import org.matrix.androidsdk.rest.client.PresenceRestClient;
import org.matrix.androidsdk.rest.client.ProfileRestClient;
import org.matrix.androidsdk.rest.client.RoomsRestClient;
import org.matrix.androidsdk.rest.model.CreateRoomResponse;
import org.matrix.androidsdk.rest.model.User;
import org.matrix.androidsdk.rest.model.login.Credentials;
import org.matrix.androidsdk.sync.DefaultEventsThreadListener;
import org.matrix.androidsdk.sync.EventsThread;
import org.matrix.androidsdk.sync.EventsThreadListener;
import org.matrix.androidsdk.util.ContentManager;

/**
 * Class that represents one user's session with a particular home server.
 * There can potentially be multiple sessions for handling multiple accounts.
 */
public class MXSession {

    private static final String LOG_TAG = "MXSession";

    private MXDataHandler mDataHandler;
    private EventsThread mEventsThread;
    private Credentials mCredentials;
    private MyUser mMyUser;

    // Api clients
    private EventsRestClient mEventsRestClient;
    private ProfileRestClient mProfileRestClient;
    private PresenceRestClient mPresenceRestClient;
    private RoomsRestClient mRoomsRestClient;

    private ApiFailureCallback mFailureCallback;

    private ContentManager mContentManager;

    /**
     * Create a basic session for direct API calls.
     * @param credentials the user credentials
     */
    public MXSession(Credentials credentials) {
        mCredentials = credentials;

        mEventsRestClient = new EventsRestClient(credentials);
        mProfileRestClient = new ProfileRestClient(credentials);
        mPresenceRestClient = new PresenceRestClient(credentials);
        mRoomsRestClient = new RoomsRestClient(credentials);

        mContentManager = new ContentManager(credentials.homeServer, credentials.accessToken);
    }

    /**
     * Create a user session with a data handler.
     * @param dataHandler the data handler
     * @param credentials the user credentials
     */
    public MXSession(MXDataHandler dataHandler, Credentials credentials) {
        this(credentials);
        mDataHandler = dataHandler;

        // Initialize a data retriever with rest clients
        DataRetriever dataRetriever = new DataRetriever();
        dataRetriever.setRoomsRestClient(mRoomsRestClient);

        mDataHandler.setDataRetriever(dataRetriever);
    }

    /**
     * Get the data handler.
     * @return the data handler.
     */
    public MXDataHandler getDataHandler() {
        return mDataHandler;
    }

    /**
     * Get the user credentials.
     * @return the credentials
     */
    public Credentials getCredentials() {
        return mCredentials;
    }

    /**
     * Get the API client for requests to the events API.
     * @return the events API client
     */
    public EventsRestClient getEventsApiClient() {
        return mEventsRestClient;
    }

    /**
     * Get the API client for requests to the profile API.
     * @return the profile API client
     */
    public ProfileRestClient getProfileApiClient() {
        return mProfileRestClient;
    }

    /**
     * Get the API client for requests to the presence API.
     * @return the presence API client
     */
    public PresenceRestClient getPresenceApiClient() {
        return mPresenceRestClient;
    }

    /**
     * Get the API client for requests to the rooms API.
     * @return the rooms API client
     */
    public RoomsRestClient getRoomsApiClient() {
        return mRoomsRestClient;
    }

    protected void setEventsApiClient(EventsRestClient eventsRestClient) {
        this.mEventsRestClient = eventsRestClient;
    }

    protected void setProfileApiClient(ProfileRestClient profileRestClient) {
        this.mProfileRestClient = profileRestClient;
    }

    protected void setPresenceApiClient(PresenceRestClient presenceRestClient) {
        this.mPresenceRestClient = presenceRestClient;
    }

    protected void setRoomsApiClient(RoomsRestClient roomsRestClient) {
        this.mRoomsRestClient = roomsRestClient;
    }

    /**
     * Get the content manager (for uploading and downloading content) associated with the session.
     * @return the content manager
     */
    public ContentManager getContentManager() {
        return mContentManager;
    }

    /**
     * Get the session's current user. The MyUser object provides methods for updating user properties which are not possible for other users.
     * @return the session's MyUser object
     */
    public MyUser getMyUser() {
        // MyUser is initialized as late as possible to have a better chance at having the info in storage,
        // which should be the case if this is called after the initial sync
        if (mMyUser == null) {
            // TODO: Handle the case where the user is null by loading the user information from the server
            mMyUser = new MyUser(mDataHandler.getStore().getUser(mCredentials.userId));
            mMyUser.setProfileRestClient(mProfileRestClient);
        }
        return mMyUser;
    }

    /**
     * Start the event stream (events thread that listens for events) with an event listener.
     * @param eventsListener the event listener or null if using a DataHandler
     */
    public void startEventStream(EventsThreadListener eventsListener) {
        if (mEventsThread != null) {
            Log.w(LOG_TAG, "Ignoring startEventStream() : Thread already created.");
            return;
        }

        if (eventsListener == null) {
            if (mDataHandler == null) {
                Log.e(LOG_TAG, "Error starting the event stream: No data handler is defined");
                return;
            }
            eventsListener = new DefaultEventsThreadListener(mDataHandler);
        }

        mEventsThread = new EventsThread(mEventsRestClient, eventsListener);
        if (mFailureCallback != null) {
            mEventsThread.setFailureCallback(mFailureCallback);
        }
        if (mCredentials.accessToken != null && !mEventsThread.isAlive()) {
            mEventsThread.start();
        }
    }

    /**
     * Shorthand for {@link #startEventStream(org.matrix.androidsdk.sync.EventsThreadListener)} with no eventListener
     * using a DataHandler and no specific failure callback.
     */
    public void startEventStream() {
        startEventStream(null);
    }

    /**
     * Gracefully stop the event stream.
     */
    public void stopEventStream() {
        mEventsThread.kill();
        mEventsThread = null;
    }

    public void pauseEventStream() {
        mEventsThread.pause();
    }

    public void resumeEventStream() {
        mEventsThread.unpause();
    }

    /**
     * Set a global failure callback implementation.
     * @param failureCallback the failure callback
     */
    public void setFailureCallback(ApiFailureCallback failureCallback) {
        mFailureCallback = failureCallback;
        if (mEventsThread != null) {
            mEventsThread.setFailureCallback(failureCallback);
        }
    }

    /**
     * Create a new room with given properties. Needs the data handler.
     * @param name the room name
     * @param topic the room topic
     * @param visibility the room visibility
     * @param alias the room alias
     * @param callback the async callback once the room is ready
     */
    public void createRoom(String name, String topic, String visibility, String alias, final ApiCallback<String> callback) {
        mRoomsRestClient.createRoom(name, topic, visibility, alias, new SimpleApiCallback<CreateRoomResponse>(callback) {
            @Override
            public void onSuccess(CreateRoomResponse info) {
                final String roomId = info.roomId;
                Room createdRoom = mDataHandler.getRoom(roomId);
                createdRoom.initialSync(new SimpleApiCallback<Void>(callback) {
                    @Override
                    public void onSuccess(Void aVoid) {
                        callback.onSuccess(roomId);
                    }
                });
            }
        });
    }
}




Java Source Code List

org.matrix.androidsdk.MXDataHandler.java
org.matrix.androidsdk.MXSession.java
org.matrix.androidsdk.RestClient.java
org.matrix.androidsdk.data.DataRetriever.java
org.matrix.androidsdk.data.IMXStore.java
org.matrix.androidsdk.data.MXMemoryStore.java
org.matrix.androidsdk.data.MyUser.java
org.matrix.androidsdk.data.RoomState.java
org.matrix.androidsdk.data.RoomSummary.java
org.matrix.androidsdk.data.Room.java
org.matrix.androidsdk.listeners.IMXEventListener.java
org.matrix.androidsdk.listeners.MXEventListener.java
org.matrix.androidsdk.rest.api.EventsApi.java
org.matrix.androidsdk.rest.api.LoginApi.java
org.matrix.androidsdk.rest.api.PresenceApi.java
org.matrix.androidsdk.rest.api.ProfileApi.java
org.matrix.androidsdk.rest.api.RegistrationApi.java
org.matrix.androidsdk.rest.api.RoomsApi.java
org.matrix.androidsdk.rest.callback.ApiCallback.java
org.matrix.androidsdk.rest.callback.ApiFailureCallback.java
org.matrix.androidsdk.rest.callback.RestAdapterCallback.java
org.matrix.androidsdk.rest.callback.SimpleApiCallback.java
org.matrix.androidsdk.rest.client.EventsRestClient.java
org.matrix.androidsdk.rest.client.LoginRestClient.java
org.matrix.androidsdk.rest.client.PresenceRestClient.java
org.matrix.androidsdk.rest.client.ProfileRestClient.java
org.matrix.androidsdk.rest.client.RegistrationRestClient.java
org.matrix.androidsdk.rest.client.RestClientTest.java
org.matrix.androidsdk.rest.client.RoomsRestClient.java
org.matrix.androidsdk.rest.model.BannedUser.java
org.matrix.androidsdk.rest.model.ContentResponse.java
org.matrix.androidsdk.rest.model.CreateRoomResponse.java
org.matrix.androidsdk.rest.model.Event.java
org.matrix.androidsdk.rest.model.ImageInfo.java
org.matrix.androidsdk.rest.model.ImageMessage.java
org.matrix.androidsdk.rest.model.InitialSyncResponse.java
org.matrix.androidsdk.rest.model.MatrixError.java
org.matrix.androidsdk.rest.model.MessageFeedback.java
org.matrix.androidsdk.rest.model.Message.java
org.matrix.androidsdk.rest.model.PowerLevels.java
org.matrix.androidsdk.rest.model.PublicRoom.java
org.matrix.androidsdk.rest.model.RoomMember.java
org.matrix.androidsdk.rest.model.RoomResponse.java
org.matrix.androidsdk.rest.model.TextMessage.java
org.matrix.androidsdk.rest.model.TokensChunkResponse.java
org.matrix.androidsdk.rest.model.User.java
org.matrix.androidsdk.rest.model.login.Credentials.java
org.matrix.androidsdk.rest.model.login.LoginFlowResponse.java
org.matrix.androidsdk.rest.model.login.LoginFlow.java
org.matrix.androidsdk.rest.model.login.LoginParams.java
org.matrix.androidsdk.rest.model.login.PasswordLoginParams.java
org.matrix.androidsdk.sync.DefaultEventsThreadListener.java
org.matrix.androidsdk.sync.EventsThreadListener.java
org.matrix.androidsdk.sync.EventsThreadTest.java
org.matrix.androidsdk.sync.EventsThread.java
org.matrix.androidsdk.util.ContentManager.java
org.matrix.androidsdk.util.ContentUtils.java
org.matrix.androidsdk.util.JsonUtils.java
org.matrix.matrixandroidsdk.ApplicationTest.java
org.matrix.matrixandroidsdk.ErrorListener.java
org.matrix.matrixandroidsdk.Matrix.java
org.matrix.matrixandroidsdk.ViewedRoomTracker.java
org.matrix.matrixandroidsdk.activity.CommonActivityUtils.java
org.matrix.matrixandroidsdk.activity.HomeActivity.java
org.matrix.matrixandroidsdk.activity.LoginActivity.java
org.matrix.matrixandroidsdk.activity.PublicRoomsActivity.java
org.matrix.matrixandroidsdk.activity.RoomActivity.java
org.matrix.matrixandroidsdk.activity.RoomInfoActivity.java
org.matrix.matrixandroidsdk.activity.SettingsActivity.java
org.matrix.matrixandroidsdk.activity.SplashActivity.java
org.matrix.matrixandroidsdk.adapters.AdapterUtils.java
org.matrix.matrixandroidsdk.adapters.MessageRow.java
org.matrix.matrixandroidsdk.adapters.MessagesAdapter.java
org.matrix.matrixandroidsdk.adapters.RoomMembersAdapter.java
org.matrix.matrixandroidsdk.adapters.RoomSummaryAdapter.java
org.matrix.matrixandroidsdk.adapters.RoomsAdapter.java
org.matrix.matrixandroidsdk.fragments.MatrixMessageListFragment.java
org.matrix.matrixandroidsdk.fragments.MatrixMessagesFragment.java
org.matrix.matrixandroidsdk.fragments.RoomMembersDialogFragment.java
org.matrix.matrixandroidsdk.services.EventStreamService.java
org.matrix.matrixandroidsdk.store.LoginStorage.java
org.matrix.matrixandroidsdk.util.EventUtils.java
org.matrix.matrixandroidsdk.util.ResourceUtils.java
org.matrix.matrixandroidsdk.util.UIUtils.java
org.matrix.matrixandroidsdk.view.PieFractionView.java