1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-15 13:35:09 -05:00
Yaaic/src/org/yaaic/view/ConversationActivity.java

418 lines
11 KiB
Java
Raw Normal View History

2009-12-17 15:27:57 -05:00
/*
2010-03-12 14:35:25 -05:00
Yaaic - Yet Another Android IRC Client
2009-12-17 15:27:57 -05:00
2010-03-13 10:52:20 -05:00
Copyright 2009-2010 Sebastian Kaspari
2009-12-17 15:27:57 -05:00
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.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;
import android.util.Log;
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.OnKeyListener;
import android.widget.AdapterView;
2009-12-17 15:27:57 -05:00
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.ListView;
2009-12-17 15:27:57 -05:00
import android.widget.TextView;
2010-03-05 09:56:57 -05:00
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
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;
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.layout.NonScalingBackgroundDrawable;
import org.yaaic.listener.ConversationListener;
import org.yaaic.listener.ServerListener;
2010-03-05 09:56:57 -05:00
import org.yaaic.model.Broadcast;
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.ConversationReceiver;
import org.yaaic.receiver.ServerReceiver;
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>
*/
public class ConversationActivity extends Activity implements ServiceConnection, ServerListener, ConversationListener, OnItemClickListener, OnKeyListener, OnItemSelectedListener
2009-12-17 15:27:57 -05:00
{
public static final String TAG = "Yaaic/ConversationActivity";
2010-03-05 09:56:57 -05:00
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;
private ConversationReceiver channelReceiver;
private ServerReceiver serverReceiver;
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.conversations);
2009-12-17 15:27:57 -05:00
((TextView) findViewById(R.id.title)).setText(server.getTitle());
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
2010-03-10 17:12:01 -05:00
((EditText) findViewById(R.id.input)).setOnKeyListener(this);
deckAdapter = new DeckAdapter();
2010-03-05 09:56:57 -05:00
deck = (Gallery) findViewById(R.id.deck);
deck.setOnItemSelectedListener(this);
2010-03-05 09:56:57 -05:00
deck.setAdapter(deckAdapter);
deck.setOnItemClickListener(this);
deck.setBackgroundDrawable(new NonScalingBackgroundDrawable(this, deck, R.drawable.icon));
2010-03-05 09:56:57 -05:00
switcher = (ViewSwitcher) findViewById(R.id.switcher);
for (Conversation conversation : server.getConversations()) {
onNewConversation(conversation.getName());
}
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
channelReceiver = new ConversationReceiver(server.getId(), this);
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE));
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_NEW));
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_REMOVE));
serverReceiver = new ServerReceiver(this);
registerReceiver(serverReceiver, new IntentFilter(Broadcast.SERVER_UPDATE));
if (!server.isConnected()) {
((EditText) findViewById(R.id.input)).setEnabled(false);
}
// Fill view with messages that have been buffered while paused
for (Conversation conversation : server.getConversations()) {
while (conversation.hasBufferedMessages()) {
conversation.getMessageListAdapter().addMessage(conversation.pollBufferedMessage());
}
}
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);
unregisterReceiver(channelReceiver);
unregisterReceiver(serverReceiver);
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();
2010-03-13 12:04:59 -05:00
inflater.inflate(R.menu.conversations, menu);
2009-12-17 15:27:57 -05:00
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();
server.clearConversations();
2009-12-17 15:27:57 -05:00
setResult(RESULT_OK);
finish();
break;
case R.id.join:
startActivityForResult(new Intent(this, JoinActivity.class), 0);
2009-12-17 15:27:57 -05:00
break;
}
return true;
}
/**
* Get server object assigned to this activity
*
* @return the server object
*/
public Server getServer()
{
return server;
}
2010-03-02 14:18:44 -05:00
/**
2010-03-10 19:10:21 -05:00
* On conversation message
2010-03-02 14:18:44 -05:00
*/
public void onConversationMessage(String target)
2010-03-02 14:18:44 -05:00
{
Conversation conversation = server.getConversation(target);
MessageListAdapter adapter = conversation.getMessageListAdapter();
while(conversation.hasBufferedMessages()) {
Message message = conversation.pollBufferedMessage();
if (adapter != null) {
adapter.addMessage(message);
} else {
Log.d(TAG, "MessageListAdapter is null (conversation " + conversation.getName() + " has no adapter assigned)");
}
}
2010-03-02 14:18:44 -05:00
}
/**
2010-03-10 19:10:21 -05:00
* On new conversation
2010-03-02 14:18:44 -05:00
*/
public void onNewConversation(String target)
2010-03-02 14:18:44 -05:00
{
deckAdapter.addItem(server.getConversation(target));
2010-03-10 16:10:40 -05:00
if (!deckAdapter.isSwitched()) {
// Scroll to new conversation
deck.setSelection(deckAdapter.getCount() - 1);
}
}
/**
2010-03-10 19:10:21 -05:00
* On conversation remove
*/
public void onRemoveConversation(String target)
{
2010-03-10 19:10:21 -05:00
deckAdapter.removeItem(target);
if (deckAdapter.isSwitched()) {
onBackPressed();
}
}
/**
* On server status update
*/
public void onStatusUpdate()
{
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
EditText input = (EditText) findViewById(R.id.input);
if (server.isConnected()) {
input.setEnabled(true);
} else {
input.setEnabled(false);
}
}
/**
2010-03-10 19:10:21 -05:00
* On conversation item clicked
*/
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
Conversation conversation = deckAdapter.getItem(position);
2010-03-10 19:10:21 -05:00
MessageListView canvas = deckAdapter.renderConversation(conversation, switcher);
canvas.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
canvas.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
canvas.setDelegateTouchEvents(false); // Do not delegate
2010-03-10 19:10:21 -05:00
deckAdapter.setSwitched(conversation.getName(), canvas);
switcher.addView(canvas);
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 key pressed (input line)
*/
public boolean onKey(View view, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
if (!server.isConnected()) {
Message message = new Message("Not connected");
message.setColor(Message.COLOR_RED);
message.setIcon(R.drawable.error);
server.getConversation(server.getSelectedConversation()).addMessage(message);
onConversationMessage(server.getSelectedConversation());
}
EditText input = (EditText) view;
String text = input.getText().toString();
input.setText("");
2010-03-10 16:17:53 -05:00
if (text == "") {
// ignore empty messages
return true;
}
Conversation conversation = deckAdapter.getItem(deck.getSelectedItemPosition());
if (conversation != null) {
2010-03-09 13:46:02 -05:00
if (!text.trim().startsWith("/")) {
if (conversation.getType() != Conversation.TYPE_SERVER) {
String nickname = this.binder.getService().getConnection(serverId).getNick();
conversation.addMessage(new Message("<" + nickname + "> " + text));
this.binder.getService().getConnection(serverId).sendMessage(conversation.getName(), text);
} else {
Message message = new Message("You can only chat from within a channel or a query");
message.setColor(Message.COLOR_YELLOW);
message.setIcon(R.drawable.warning);
conversation.addMessage(message);
}
onConversationMessage(conversation.getName());
2010-03-09 13:46:02 -05:00
} else {
CommandParser.getInstance().parse(text, server, conversation, binder.getService());
2010-03-09 13:46:02 -05:00
}
}
return true;
}
return false;
}
/**
* On activity result
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
// currently there's only the "join channel" activity
binder.getService().getConnection(serverId).joinChannel(
data.getExtras().getString("channel")
);
}
}
/**
* On channel selected/focused
*/
public void onItemSelected(AdapterView<?> deck, View view, int position, long id)
{
2010-03-10 15:34:20 -05:00
Conversation conversation = (Conversation) deck.getItemAtPosition(position);
if (conversation != null && conversation.getType() != Conversation.TYPE_SERVER) {
2010-03-10 15:34:20 -05:00
((TextView) findViewById(R.id.title)).setText(server.getTitle() + " - " + conversation.getName());
} else {
onNothingSelected(deck);
}
// Remember selection
if (conversation != null) {
server.setSelectedConversation(conversation.getName());
}
}
/**
* On no channel selected/focused
*/
public void onNothingSelected(AdapterView<?> deck)
{
((TextView) findViewById(R.id.title)).setText(server.getTitle());
}
2009-12-17 15:27:57 -05:00
}