Commit 279d3510 authored by Grigory Fedorov's avatar Grigory Fedorov

New RoomSelectActivity, RoomSelectFragment and HostedRoomsAdapter - allows to...

New RoomSelectActivity, RoomSelectFragment and HostedRoomsAdapter - allows to get list of hosted rooms from MUC server. #503
parent 6dc379f7
......@@ -177,12 +177,12 @@
<activity
android:name=".ui.ConferenceAdd"
android:label="@string/muc_add"
android:parentActivityName=".ui.ContactList" >
android:parentActivityName=".ui.RoomSelectActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xabber.android.ui.ContactList" />
android:value="com.xabber.android.ui.RoomSelectActivity" />
</activity>
<activity
android:name=".ui.StatusEditor"
......@@ -368,6 +368,16 @@
android:value="com.xabber.android.ui.preferences.PreferenceEditor" />
</activity>
<activity
android:name=".ui.RoomSelectActivity"
android:parentActivityName=".ui.ContactList" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xabber.android.ui.ContactList" />
</activity>
<service android:name=".service.XabberService" />
<service
android:name=".service.SyncAdapterService"
......
......@@ -39,6 +39,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.HostedRoom;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.packet.MUCUser;
......@@ -451,4 +452,34 @@ public class MUCManager implements OnLoadListener, OnPacketListener {
authorizationErrorProvider.add(new RoomAuthorizationError(account, room), null);
}
public interface HostedRoomsListener {
void onHostedRoomsReceived(Collection<HostedRoom> hostedRooms);
}
public static void requestHostedRooms(final String account, final String serviceName, final HostedRoomsListener listener) {
final XMPPConnection xmppConnection = AccountManager.getInstance().getAccount(account).getConnectionThread().getXMPPConnection();
final Thread thread = new Thread("Get hosted rooms on server " + serviceName + " for account " + account) {
@Override
public void run() {
Collection<HostedRoom> hostedRooms = null;
try {
hostedRooms = MultiUserChat.getHostedRooms(xmppConnection, serviceName);
} catch (XMPPException e) {
e.printStackTrace();
}
final Collection<HostedRoom> finalHostedRooms = hostedRooms;
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onHostedRoomsReceived(finalHostedRooms);
}
});
}
};
thread.start();
}
}
......@@ -429,7 +429,7 @@ public class ContactList extends ManagedActivity implements OnAccountChangedList
closeAllChats();
return true;
case R.id.action_join_conference:
startActivity(ConferenceAdd.createIntent(this));
startActivity(RoomSelectActivity.createIntent(this));
return true;
case R.id.action_chat_list:
startActivity(ChatViewer.createRecentChatsIntent(this));
......
package com.xabber.android.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.xabber.android.R;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.ui.helper.BarPainter;
import com.xabber.android.ui.helper.ManagedActivity;
public class RoomSelectActivity extends ManagedActivity implements RoomSelectFragment.Listener {
private BarPainter barPainter;
public static Intent createIntent(Context context) {
return new EntityIntentBuilder(context, RoomSelectActivity.class).build();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_toolbar_and_container);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_default);
toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(RoomSelectActivity.this);
}
});
toolbar.setTitle(getString(R.string.muc_choose_conference));
barPainter = new BarPainter(this, toolbar);
barPainter.setDefaultColor();
getFragmentManager().beginTransaction().add(R.id.fragment_container, new RoomSelectFragment()).commit();
}
@Override
public void onAccountSelected(String account) {
barPainter.updateWithAccountName(account);
}
}
package com.xabber.android.ui;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.xabber.android.R;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.extension.muc.MUCManager;
import com.xabber.android.ui.adapter.AccountChooseAdapter;
import com.xabber.android.ui.adapter.HostedRoomsAdapter;
import com.xabber.android.ui.helper.AccountPainter;
import org.jivesoftware.smackx.muc.HostedRoom;
import java.util.Collection;
public class RoomSelectFragment extends ListFragment implements AdapterView.OnItemSelectedListener,
View.OnClickListener, MUCManager.HostedRoomsListener, AdapterView.OnItemClickListener {
private Spinner accountView;
private EditText serverView;
private EditText roomView;
private HostedRoomsAdapter hostedRoomsAdapter;
private View roomsProgressBar;
private String account;
Listener listener;
private AccountPainter accountPainter;
private Button nextButton;
public RoomSelectFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
listener = (Listener) activity;
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.room_select_fragment, container, false);
accountView = (Spinner) view.findViewById(R.id.contact_account);
serverView = (EditText) view.findViewById(R.id.muc_server);
roomView = (EditText) view.findViewById(R.id.muc_room);
roomsProgressBar = view.findViewById(R.id.muc_rooms_progress_bar);
view.findViewById(R.id.muc_get_hosted_rooms).setOnClickListener(this);
accountView.setAdapter(new AccountChooseAdapter(getActivity()));
accountView.setOnItemSelectedListener(this);
if (AccountManager.getInstance().getAccounts().size() == 1) {
accountView.setSelection(0);
}
accountPainter = new AccountPainter(getActivity());
nextButton = (Button) view.findViewById(R.id.muc_next);
nextButton.setTextColor(accountPainter.getDefaultDarkColor());
nextButton.setOnClickListener(this);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
hostedRoomsAdapter = new HostedRoomsAdapter(getActivity(), android.R.layout.simple_list_item_2);
ListView listView = getListView();
listView.setAdapter(hostedRoomsAdapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(ConferenceAdd.createIntent(getActivity(), account,
hostedRoomsAdapter.getItem(position).getJid()));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String newAccount = (String) accountView.getSelectedItem();
if (account != null && account.equals(newAccount)) {
return;
}
account = newAccount;
listener.onAccountSelected(account);
nextButton.setTextColor(accountPainter.getAccountDarkColor(account));
hostedRoomsAdapter.clear();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
account = null;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.muc_get_hosted_rooms:
onRequestHostedRoomsClick();
break;
case R.id.muc_next:
onNextClick();
}
}
private void onNextClick() {
if (account == null) {
Toast.makeText(getActivity(), getString(R.string.EMPTY_ACCOUNT), Toast.LENGTH_SHORT).show();
return;
}
String server = serverView.getText().toString();
if ("".equals(server)) {
Toast.makeText(getActivity(), getString(R.string.EMPTY_SERVER_NAME), Toast.LENGTH_SHORT).show();
return;
}
String room = roomView.getText().toString();
if ("".equals(room)) {
Toast.makeText(getActivity(), getString(R.string.EMPTY_ROOM_NAME), Toast.LENGTH_LONG).show();
return;
}
room = room + "@" + server;
startActivity(ConferenceAdd.createIntent(getActivity(), account, room));
}
private void onRequestHostedRoomsClick() {
if (account == null) {
Toast.makeText(getActivity(), getString(R.string.EMPTY_ACCOUNT), Toast.LENGTH_SHORT).show();
return;
}
String server = serverView.getText().toString();
if ("".equals(server)) {
Toast.makeText(getActivity(), getString(R.string.EMPTY_SERVER_NAME), Toast.LENGTH_SHORT).show();
return;
}
ChatViewer.hideKeyboard(getActivity());
MUCManager.requestHostedRooms(account, server, this);
hostedRoomsAdapter.clear();
roomsProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onHostedRoomsReceived(Collection<HostedRoom> hostedRooms) {
roomsProgressBar.setVisibility(View.GONE);
if (hostedRooms == null) {
Toast.makeText(getActivity(), "Error getting rooms", Toast.LENGTH_SHORT).show();
return;
}
hostedRoomsAdapter.clear();
hostedRoomsAdapter.addAll(hostedRooms);
}
interface Listener {
void onAccountSelected(String account);
}
}
package com.xabber.android.ui.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.jivesoftware.smackx.muc.HostedRoom;
public class HostedRoomsAdapter extends ArrayAdapter<HostedRoom> {
public HostedRoomsAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(android.R.layout.simple_list_item_2, parent, false);
} else {
view = convertView;
}
TextView mainText = (TextView) view.findViewById(android.R.id.text1);
TextView secondText = (TextView) view.findViewById(android.R.id.text2);
HostedRoom room = getItem(position);
mainText.setText(room.getName());
secondText.setText(room.getJid());
return view;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xabber.android.ui.RoomSelectFragment"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@+id/bottom_bar"
android:layout_alignParentTop="true"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<com.xabber.android.ui.widget.NoDefaultSpinner
android:id="@+id/contact_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/avatar_normal_size"
android:prompt="@string/choose_account" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/muc_server" />
<EditText
android:id="@+id/muc_server"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/conference_jabber_org"
android:inputType="textEmailAddress"
android:singleLine="true" />
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_search_grey600_24dp"
android:padding="16dp"
android:background="@drawable/drawer_touch"
android:id="@+id/muc_get_hosted_rooms"
android:contentDescription="@string/muc_get_hosted_rooms"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/muc_room" />
<EditText
android:id="@+id/muc_room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/muc_rooms_progress_bar"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
>
</ListView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_bar"
>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/grey_400"
android:layout_alignParentTop="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/muc_next"
android:id="@+id/muc_next"
android:textColor="@color/green_500"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_alignParentRight="true"
android:background="@drawable/drawer_touch"
/>
</RelativeLayout>
</RelativeLayout>
......@@ -20,4 +20,8 @@
<string name="muc_invite_confirm_reason">%2$s invites you to join conference %3$s: %4$s. Join from account %1$s?</string>
<string name="muc_leave">Leave conference</string>
<string name="muc_save">Save conference</string>
<string name="muc_choose_conference">Choose conference</string>
<string name="muc_next">Next</string>
<string name="muc_get_hosted_rooms">Get hosted rooms</string>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment