2009-12-17 15:27:57 -05:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
|
|
|
Copyright 2009 Sebastian Kaspari
|
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.yaaic.view;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Intent;
|
2010-03-02 14:18:44 -05:00
|
|
|
import android.content.IntentFilter;
|
2009-12-17 15:27:57 -05:00
|
|
|
import android.content.ServiceConnection;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.IBinder;
|
2010-03-05 09:56:57 -05:00
|
|
|
import android.util.Log;
|
2010-03-05 13:34:05 -05:00
|
|
|
import android.view.KeyEvent;
|
2009-12-17 15:27:57 -05:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
2010-03-08 18:30:17 -05:00
|
|
|
import android.view.View.OnKeyListener;
|
2010-03-05 13:34:05 -05:00
|
|
|
import android.widget.AdapterView;
|
2009-12-17 15:27:57 -05:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2010-03-05 09:56:57 -05:00
|
|
|
import android.widget.Gallery;
|
2009-12-17 15:27:57 -05:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2010-03-05 09:56:57 -05:00
|
|
|
import android.widget.ViewSwitcher;
|
2010-03-05 13:34:05 -05:00
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2010-03-08 20:11:34 -05:00
|
|
|
import android.widget.AdapterView.OnItemSelectedListener;
|
2010-03-05 13:34:05 -05:00
|
|
|
import android.widget.TableLayout.LayoutParams;
|
2010-03-05 09:56:57 -05:00
|
|
|
|
|
|
|
import org.yaaic.R;
|
|
|
|
import org.yaaic.Yaaic;
|
|
|
|
import org.yaaic.adapter.DeckAdapter;
|
2010-03-08 15:36:36 -05:00
|
|
|
import org.yaaic.adapter.MessageListAdapter;
|
2010-03-09 13:46:02 -05:00
|
|
|
import org.yaaic.command.CommandParser;
|
2010-03-05 09:56:57 -05:00
|
|
|
import org.yaaic.irc.IRCBinder;
|
|
|
|
import org.yaaic.irc.IRCService;
|
|
|
|
import org.yaaic.listener.ChannelListener;
|
|
|
|
import org.yaaic.model.Broadcast;
|
2010-03-05 13:34:05 -05:00
|
|
|
import org.yaaic.model.Channel;
|
2010-03-10 14:56:14 -05:00
|
|
|
import org.yaaic.model.Conversation;
|
2010-03-06 11:52:22 -05:00
|
|
|
import org.yaaic.model.Message;
|
2010-03-05 09:56:57 -05:00
|
|
|
import org.yaaic.model.Server;
|
|
|
|
import org.yaaic.receiver.ChannelReceiver;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
/**
|
2010-03-08 15:38:29 -05:00
|
|
|
* The server view with a scrollable list of all channels
|
2009-12-17 15:27:57 -05:00
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
2010-03-08 20:11:34 -05:00
|
|
|
public class ServerActivity extends Activity implements ServiceConnection, ChannelListener, OnItemClickListener, OnKeyListener, OnItemSelectedListener
|
2009-12-17 15:27:57 -05:00
|
|
|
{
|
2010-03-05 09:56:57 -05:00
|
|
|
public static final String TAG = "Yaaic/ServerActivity";
|
|
|
|
|
|
|
|
private int serverId;
|
2010-03-02 14:18:44 -05:00
|
|
|
private Server server;
|
2009-12-17 15:27:57 -05:00
|
|
|
private IRCBinder binder;
|
2010-03-05 09:56:57 -05:00
|
|
|
private ChannelReceiver receiver;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
private ViewSwitcher switcher;
|
|
|
|
private Gallery deck;
|
|
|
|
private DeckAdapter deckAdapter;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On create
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
serverId = getIntent().getExtras().getInt("serverId");
|
2010-03-02 14:18:44 -05:00
|
|
|
server = (Server) Yaaic.getInstance().getServerById(serverId);
|
2010-03-05 09:56:57 -05:00
|
|
|
setTitle("Yaaic - " + server.getTitle());
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
setContentView(R.layout.server);
|
|
|
|
|
|
|
|
((TextView) findViewById(R.id.title)).setText(server.getTitle());
|
|
|
|
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
|
2010-03-08 18:30:17 -05:00
|
|
|
((EditText) findViewById(R.id.input)).setOnKeyListener(this);
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
deck = (Gallery) findViewById(R.id.deck);
|
2010-03-08 20:11:34 -05:00
|
|
|
deck.setOnItemSelectedListener(this);
|
2010-03-08 17:57:38 -05:00
|
|
|
deckAdapter = new DeckAdapter();
|
2010-03-05 09:56:57 -05:00
|
|
|
deck.setAdapter(deckAdapter);
|
2010-03-05 13:34:05 -05:00
|
|
|
deck.setOnItemClickListener(this);
|
|
|
|
|
2010-03-05 09:56:57 -05:00
|
|
|
switcher = (ViewSwitcher) findViewById(R.id.switcher);
|
2010-03-05 13:34:05 -05:00
|
|
|
|
2010-03-10 14:56:14 -05:00
|
|
|
for (Conversation conversation : server.getConversations()) {
|
|
|
|
onNewConversation(conversation.getName());
|
2010-03-05 13:34:05 -05:00
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On resume
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public void onResume()
|
|
|
|
{
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
Intent intent = new Intent(this, IRCService.class);
|
|
|
|
bindService(intent, this, 0);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2010-03-05 13:54:04 -05:00
|
|
|
receiver = new ChannelReceiver(server.getId(), this);
|
2010-03-05 09:56:57 -05:00
|
|
|
registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_MESSAGE));
|
|
|
|
registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_NEW));
|
|
|
|
registerReceiver(receiver, new IntentFilter(Broadcast.CHANNEL_REMOVE));
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On Pause
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public void onPause()
|
|
|
|
{
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
unbindService(this);
|
2010-03-05 09:56:57 -05:00
|
|
|
unregisterReceiver(receiver);
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On service connected
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
public void onServiceConnected(ComponentName name, IBinder service)
|
|
|
|
{
|
|
|
|
this.binder = (IRCBinder) service;
|
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On service disconnected
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
public void onServiceDisconnected(ComponentName name)
|
|
|
|
{
|
|
|
|
this.binder = null;
|
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On options menu requested
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
2010-03-08 15:38:29 -05:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
2009-12-17 15:27:57 -05:00
|
|
|
super.onCreateOptionsMenu(menu);
|
|
|
|
|
|
|
|
// inflate from xml
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.server, menu);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-08 15:38:29 -05:00
|
|
|
/**
|
|
|
|
* On menu item selected
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public boolean onMenuItemSelected(int featureId, MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.disconnect:
|
|
|
|
binder.getService().getConnection(serverId).quitServer();
|
2010-03-10 14:56:14 -05:00
|
|
|
server.clearConversations();
|
2009-12-17 15:27:57 -05:00
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
case R.id.join:
|
|
|
|
final Dialog dialog = new Dialog(this);
|
|
|
|
dialog.setContentView(R.layout.channeldialog);
|
|
|
|
dialog.setTitle(R.string.channel);
|
|
|
|
|
|
|
|
Button button = (Button) dialog.findViewById(R.id.join);
|
|
|
|
button.setOnClickListener(new OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
String channel = ((EditText) v.getRootView().findViewById(R.id.channel)).getText().toString();
|
|
|
|
binder.getService().getConnection(serverId).joinChannel(channel);
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-05 13:54:04 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get server object assigned to this activity
|
|
|
|
*
|
|
|
|
* @return the server object
|
|
|
|
*/
|
|
|
|
public Server getServer()
|
|
|
|
{
|
|
|
|
return server;
|
|
|
|
}
|
2010-03-02 14:18:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On channel message
|
|
|
|
*/
|
2010-03-10 14:56:14 -05:00
|
|
|
public void onConversationMessage(String target)
|
2010-03-02 14:18:44 -05:00
|
|
|
{
|
2010-03-10 14:56:14 -05:00
|
|
|
Conversation conversation = server.getConversation(target);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
2010-03-10 14:56:14 -05:00
|
|
|
while(conversation.hasBufferedMessages()) {
|
|
|
|
Message message = conversation.pollBufferedMessage();
|
2010-03-08 15:59:38 -05:00
|
|
|
|
2010-03-08 19:57:22 -05:00
|
|
|
int position = deckAdapter.getPositionByName(target);
|
|
|
|
|
|
|
|
if (position != -1) {
|
|
|
|
MessageListView view = (MessageListView) deck.getChildAt(position);
|
|
|
|
if (view != null) {
|
|
|
|
MessageListAdapter adapter = view.getAdapter();
|
|
|
|
adapter.addMessage(message);
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "MessageListView Adapter is null");
|
|
|
|
}
|
2010-03-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (deckAdapter.isSwitched()) {
|
|
|
|
MessageListView switchedView = deckAdapter.getSwitchedView();
|
|
|
|
switchedView.getAdapter().addMessage(message);
|
|
|
|
}
|
2010-03-08 15:44:42 -05:00
|
|
|
}
|
2010-03-02 14:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On new channel
|
|
|
|
*/
|
2010-03-10 14:56:14 -05:00
|
|
|
public void onNewConversation(String target)
|
2010-03-02 14:18:44 -05:00
|
|
|
{
|
2010-03-10 14:56:14 -05:00
|
|
|
deckAdapter.addItem(server.getConversation(target));
|
2010-03-05 13:34:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Channel item clicked
|
|
|
|
*/
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
|
|
|
|
{
|
|
|
|
Log.d(TAG, "Selected channel: " + position);
|
2010-03-05 09:56:57 -05:00
|
|
|
|
2010-03-10 14:56:14 -05:00
|
|
|
Conversation conversation = deckAdapter.getItem(position);
|
|
|
|
MessageListView canvas = deckAdapter.renderConversation(conversation, switcher);
|
2010-03-08 15:36:36 -05:00
|
|
|
canvas.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
|
|
|
|
canvas.setDelegateTouchEvents(false); // Do not delegate
|
2010-03-10 14:56:14 -05:00
|
|
|
deckAdapter.setSwitched(conversation.getName(), canvas);
|
2010-03-08 15:36:36 -05:00
|
|
|
switcher.addView(canvas);
|
2010-03-05 13:34:05 -05:00
|
|
|
switcher.showNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On key down
|
|
|
|
*
|
|
|
|
* This is glue code to call onBackPressed() which
|
|
|
|
* will be automatically called by later android releases
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event)
|
|
|
|
{
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On back key pressed
|
|
|
|
*/
|
|
|
|
public void onBackPressed()
|
|
|
|
{
|
|
|
|
if (deckAdapter.isSwitched()) {
|
|
|
|
switcher.showNext();
|
|
|
|
switcher.removeView(deckAdapter.getSwitchedView());
|
|
|
|
deckAdapter.setSwitched(null, null);
|
|
|
|
} else {
|
|
|
|
finish();
|
|
|
|
}
|
2010-03-02 14:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On channel remove
|
|
|
|
*/
|
2010-03-10 14:56:14 -05:00
|
|
|
public void onRemoveConversation(String target)
|
2010-03-02 14:18:44 -05:00
|
|
|
{
|
2010-03-06 15:05:23 -05:00
|
|
|
// XXX: Implement me :)
|
2010-03-02 14:18:44 -05:00
|
|
|
}
|
2010-03-08 18:30:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On key pressed (input line)
|
|
|
|
*/
|
|
|
|
public boolean onKey(View view, int keyCode, KeyEvent event)
|
|
|
|
{
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
|
EditText input = (EditText) view;
|
|
|
|
String text = input.getText().toString();
|
|
|
|
input.setText("");
|
|
|
|
|
2010-03-10 14:56:14 -05:00
|
|
|
Conversation conversation = deckAdapter.getItem(deck.getSelectedItemPosition());
|
2010-03-08 18:30:17 -05:00
|
|
|
|
2010-03-10 14:56:14 -05:00
|
|
|
if (conversation != null) {
|
2010-03-09 13:46:02 -05:00
|
|
|
if (!text.trim().startsWith("/")) {
|
|
|
|
String nickname = this.binder.getService().getConnection(serverId).getNick();
|
2010-03-10 14:56:14 -05:00
|
|
|
conversation.addMessage(new Message("<" + nickname + "> " + text));
|
|
|
|
onConversationMessage(conversation.getName());
|
|
|
|
this.binder.getService().getConnection(serverId).sendMessage(conversation.getName(), text);
|
2010-03-09 13:46:02 -05:00
|
|
|
} else {
|
2010-03-10 14:56:14 -05:00
|
|
|
CommandParser.getInstance().parse(text, server, conversation, binder.getService());
|
2010-03-09 13:46:02 -05:00
|
|
|
}
|
2010-03-08 18:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-08 20:11:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On channel selected/focused
|
|
|
|
*/
|
|
|
|
public void onItemSelected(AdapterView<?> deck, View view, int position, long id)
|
|
|
|
{
|
|
|
|
Channel channel = (Channel) deck.getItemAtPosition(position);
|
|
|
|
if (channel != null) {
|
|
|
|
((TextView) findViewById(R.id.title)).setText(server.getTitle() + " - " + channel.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On no channel selected/focused
|
|
|
|
*/
|
|
|
|
public void onNothingSelected(AdapterView<?> arg0)
|
|
|
|
{
|
|
|
|
((TextView) findViewById(R.id.title)).setText(server.getTitle());
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|