Android Open Source - matrix-android-sdk Room Members Dialog Fragment






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

package org.matrix.matrixandroidsdk.fragments;
/*from   w  ww  .j a va 2  s .c om*/
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.matrix.androidsdk.MXSession;
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.listeners.MXEventListener;
import org.matrix.androidsdk.rest.callback.ApiCallback;
import org.matrix.androidsdk.rest.callback.SimpleApiCallback;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.MatrixError;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.matrix.androidsdk.rest.model.User;
import org.matrix.androidsdk.util.JsonUtils;
import org.matrix.matrixandroidsdk.Matrix;
import org.matrix.matrixandroidsdk.R;
import org.matrix.matrixandroidsdk.adapters.RoomMembersAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * A dialog fragment showing a list of room members for a given room.
 */
public class RoomMembersDialogFragment extends DialogFragment {
    private static final String LOG_TAG = "RoomMembersDialogFragment";

    public static final String ARG_ROOM_ID = "org.matrix.matrixandroidsdk.fragments.RoomMembersDialogFragment.ARG_ROOM_ID";

    public static RoomMembersDialogFragment newInstance(String roomId) {
        RoomMembersDialogFragment f= new RoomMembersDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_ROOM_ID, roomId);
        f.setArguments(args);
        return f;
    }

    private ListView mListView;
    private RoomMembersAdapter mAdapter;
    private String mRoomId;
    private MXSession mSession;

    private Handler uiThreadHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRoomId = getArguments().getString(ARG_ROOM_ID);
        Context context = getActivity().getApplicationContext();
        uiThreadHandler = new Handler();

        mSession = Matrix.getInstance(context).getDefaultSession();
        if (mSession == null) {
            throw new RuntimeException("No MXSession.");
        }

        mSession.getDataHandler().getRoom(mRoomId).addEventListener(new MXEventListener() {
            @Override
            public void onPresenceUpdate(Event event, final User user) {
                // Someone's presence has changed, reprocess the whole list
                uiThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.saveUser(user);
                        mAdapter.sortMembers();
                    }
                });
            }

            @Override
            public void onLiveEvent(final Event event, RoomState roomState) {
                uiThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (Event.EVENT_TYPE_STATE_ROOM_MEMBER.equals(event.type)) {
                            mAdapter.updateMember(event.stateKey, JsonUtils.toRoomMember(event.content));
                        }
                        else if (Event.EVENT_TYPE_STATE_ROOM_POWER_LEVELS.equals(event.type)) {
                            mAdapter.setPowerLevels(JsonUtils.toPowerLevels(event.content));
                        }
                    }
                });
            }
        });
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog d = super.onCreateDialog(savedInstanceState);
        d.setTitle("Member List");
        return d;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_dialog_member_list, container, false);
        mListView = ((ListView)v.findViewById(R.id.listView_members));
        mAdapter = new RoomMembersAdapter(getActivity(), R.layout.adapter_item_room_members);

        final Room room = mSession.getDataHandler().getRoom(mRoomId);
        if (room != null) {
            Collection<RoomMember> members = room.getMembers();
            if (members != null) {
                for (RoomMember m : members) {
                    mAdapter.add(m);
                    mAdapter.saveUser(mSession.getDataHandler().getStore().getUser(m.getUserId()));
                }
                mAdapter.sortMembers();
            }
        }

        mAdapter.setPowerLevels(room.getLiveState().getPowerLevels());

        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private static final int OPTION_CANCEL = 0;
            private static final int OPTION_KICK = 1;
            private static final int OPTION_BAN = 2;
            private static final int OPTION_UNBAN = 3;
            private static final int OPTION_INVITE = 4;

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the member and display the possible actions for them
                final RoomMember roomMember = mAdapter.getItem(position);

                // TODO: Filter out forbidden options based on power levels
                final List<Integer> options = new ArrayList<Integer>();
                if (RoomMember.MEMBERSHIP_LEAVE.equals(roomMember.membership)) {
                    options.add(OPTION_INVITE);
                }
                if (RoomMember.MEMBERSHIP_INVITE.equals(roomMember.membership)
                        || RoomMember.MEMBERSHIP_JOIN.equals(roomMember.membership)) {
                    options.add(OPTION_KICK);
                }
                if (!RoomMember.MEMBERSHIP_BAN.equals(roomMember.membership)) {
                    options.add(OPTION_BAN);
                }
                else {
                    options.add(OPTION_UNBAN);
                }
                if (options.size() == 0) {
                    return;
                }
                options.add(OPTION_CANCEL);

                final ApiCallback callback = new SimpleApiCallback() {
                    @Override
                    public void onMatrixError(MatrixError e) {
                        if (MatrixError.FORBIDDEN.equals(e.errcode)) {
                            Toast.makeText(getActivity(), e.error, Toast.LENGTH_LONG).show();
                        }
                    }
                };

                new AlertDialog.Builder(getActivity())
                        .setItems(buildOptionLabels(options), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (options.get(which)) {
                                    case OPTION_CANCEL:
                                        dialog.cancel();
                                        break;
                                    case OPTION_KICK:
                                        room.kick(roomMember.getUserId(), callback);
                                        dialog.dismiss();
                                        break;
                                    case OPTION_BAN:
                                        room.ban(roomMember.getUserId(), callback);
                                        dialog.dismiss();
                                        break;
                                    case OPTION_UNBAN:
                                        room.unban(roomMember.getUserId(), callback);
                                        dialog.dismiss();
                                        break;
                                    case OPTION_INVITE:
                                        room.invite(roomMember.getUserId(), callback);
                                        dialog.dismiss();
                                        break;
                                    default:
                                        Log.e(LOG_TAG, "Unknown option: " + which);
                                }
                            }
                        })
                        .create()
                        .show();
            }

            private String[] buildOptionLabels(List<Integer> options) {
                String[] labels = new String[options.size()];
                for (int i = 0; i < options.size(); i++) {
                    String label = "";
                    switch (options.get(i)) {
                        case OPTION_CANCEL:
                            label = getString(R.string.cancel);
                            break;
                        case OPTION_KICK:
                            label = getString(R.string.kick);
                            break;
                        case OPTION_BAN:
                            label = getString(R.string.ban);
                            break;
                        case OPTION_UNBAN:
                            label = getString(R.string.unban);
                            break;
                        case OPTION_INVITE:
                            label = getString(R.string.invite);
                            break;
                    }
                    labels[i] = label;
                }

                return labels;
            }
        });

        return v;
    }
}




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