Merge branch 'master' into optimization

Conflicts:
	src/org/yaaic/activity/ConversationActivity.java
	src/org/yaaic/irc/IRCService.java
This commit is contained in:
Sebastian Kaspari 2010-03-29 22:04:38 +02:00
commit 60f4412096
21 changed files with 606 additions and 208 deletions

View File

@ -22,18 +22,21 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.yaaic"
android:versionCode="2"
android:versionName="@string/app_version">
<application android:icon="@drawable/icon" android:label="@string/app_name">
android:versionName="0.2">
<application
android:icon="@drawable/icon"
android:label="Yaaic">
<activity
android:name=".view.ServersActivity"
android:label="@string/app_name">
android:name=".activity.ServersActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".view.AddServerActivity"
android:name=".activity.AddServerActivity"
android:label="@string/add_server_label">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
@ -43,20 +46,20 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
</intent-filter>
</activity>
<activity
android:name=".view.ConversationActivity"
android:name=".activity.ConversationActivity"
android:windowSoftInputMode="adjustResize|stateHidden">
</activity>
<activity
android:name=".view.AboutActivity"
android:name=".activity.AboutActivity"
android:label="@string/about_label"
android:theme="@android:style/Theme.Dialog">
</activity>
<activity
android:name=".view.SettingsActivity"
android:name=".activity.SettingsActivity"
android:label="@string/settings_label">
</activity>
<activity
android:name=".view.JoinActivity"
android:name=".activity.JoinActivity"
android:label="@string/join_label"
android:theme="@android:style/Theme.Dialog">
</activity>

View File

@ -24,8 +24,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import org.yaaic.db.Database;
import org.yaaic.model.Server;
import android.content.Context;
/**
* Global Master Class :)
*
@ -36,6 +39,7 @@ public class Yaaic
public static Yaaic instance;
private HashMap<Integer, Server> servers;
private boolean serversLoaded = false;
/**
* Private constructor, you may want to use static getInstance()
@ -45,6 +49,23 @@ public class Yaaic
servers = new HashMap<Integer, Server>();
}
/**
* Load servers from database
*
* @param context
*/
public void loadServers(Context context)
{
if (!serversLoaded) {
Database db = new Database(context);
servers = db.getServers();
db.close();
//context.sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
serversLoaded = true;
}
}
/**
* Get global Yaaic instance
*

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import org.yaaic.R;

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import java.util.regex.Pattern;
@ -35,7 +35,7 @@ import org.yaaic.R;
import org.yaaic.Yaaic;
import org.yaaic.db.Database;
import org.yaaic.exception.ValidationException;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Extra;
import org.yaaic.model.Identity;
import org.yaaic.model.Server;
import org.yaaic.model.Status;
@ -63,10 +63,10 @@ public class AddServerActivity extends Activity implements OnClickListener
((Button) findViewById(R.id.cancel)).setOnClickListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey(Broadcast.EXTRA_SERVER)) {
if (extras != null && extras.containsKey(Extra.SERVER)) {
// Request to edit an existing server
Database db = new Database(this);
this.server = db.getServerById(extras.getInt(Broadcast.EXTRA_SERVER));
this.server = db.getServerById(extras.getInt(Extra.SERVER));
db.close();
// Set server values

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import java.util.Collection;
@ -63,6 +63,7 @@ import org.yaaic.model.Server;
import org.yaaic.model.Status;
import org.yaaic.receiver.ConversationReceiver;
import org.yaaic.receiver.ServerReceiver;
import org.yaaic.view.MessageListView;
/**
* The server view with a scrollable list of all channels
@ -97,7 +98,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
((TextView) findViewById(R.id.title)).setText(server.getTitle());
((EditText) findViewById(R.id.input)).setOnKeyListener(this);
deckAdapter = new DeckAdapter();
deck = (Gallery) findViewById(R.id.deck);
deck.setOnItemSelectedListener(this);
@ -125,7 +126,11 @@ public class ConversationActivity extends Activity implements ServiceConnection,
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
bindService(new Intent(this, IRCService.class), this, 0);
// Start service
Intent intent = new Intent(this, IRCService.class);
intent.setAction(IRCService.ACTION_FOREGROUND);
startService(intent);
bindService(intent, this, 0);
channelReceiver = new ConversationReceiver(server.getId(), this);
registerReceiver(channelReceiver, new IntentFilter(Broadcast.CONVERSATION_MESSAGE));
@ -163,6 +168,15 @@ public class ConversationActivity extends Activity implements ServiceConnection,
{
super.onPause();
binder.getService().checkServiceStatus();
/*if (!binder.getService().hasConnections()) {
Log.d("Yaaic", "Stopping service");
//binder.getService().stopSelf();
} else {
Log.d("Yaaic", "Unbinding service");
}*/
unbindService(this);
unregisterReceiver(channelReceiver);
unregisterReceiver(serverReceiver);
@ -214,10 +228,12 @@ public class ConversationActivity extends Activity implements ServiceConnection,
switch (item.getItemId()) {
case R.id.disconnect:
binder.getService().getConnection(serverId).quitServer();
server.setStatus(Status.DISCONNECTED);
server.clearConversations();
setResult(RESULT_OK);
finish();
break;
case R.id.join:
startActivityForResult(new Intent(this, JoinActivity.class), 0);
break;
@ -276,7 +292,9 @@ public class ConversationActivity extends Activity implements ServiceConnection,
deckAdapter.removeItem(target);
if (deckAdapter.isSwitched()) {
onBackPressed();
switcher.showNext();
switcher.removeView(deckAdapter.getSwitchedView());
deckAdapter.setSwitched(null, null);
}
}
@ -314,32 +332,21 @@ public class ConversationActivity extends Activity implements ServiceConnection,
/**
* On key down
*
* This is glue code to call onBackPressed() which
* will be automatically called by later android releases
*
* XXX: As we only track the back key: Android >= 2.0 will call a method called onBackPressed()
*/
@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();
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (deckAdapter.isSwitched()) {
switcher.showNext();
switcher.removeView(deckAdapter.getSwitchedView());
deckAdapter.setSwitched(null, null);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/**

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import org.yaaic.R;

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import android.app.AlertDialog;
import android.app.ListActivity;
@ -47,6 +47,7 @@ import org.yaaic.irc.IRCService;
import org.yaaic.layout.NonScalingBackgroundDrawable;
import org.yaaic.listener.ServerListener;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Extra;
import org.yaaic.model.Server;
import org.yaaic.model.Status;
import org.yaaic.receiver.ServerReceiver;
@ -87,6 +88,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
// Start and connect to service
Intent intent = new Intent(this, IRCService.class);
intent.setAction(IRCService.ACTION_BACKGROUND);
startService(intent);
bindService(intent, this, 0);
@ -199,7 +201,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
}
else {
Intent intent = new Intent(this, AddServerActivity.class);
intent.putExtra(Broadcast.EXTRA_SERVER, serverId);
intent.putExtra(Extra.SERVER, serverId);
startActivityForResult(intent, 0);
}
}

View File

@ -18,7 +18,7 @@ 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;
package org.yaaic.activity;
import org.yaaic.R;

View File

@ -22,6 +22,8 @@ package org.yaaic.command;
import java.util.HashMap;
import android.content.Intent;
import org.yaaic.command.handler.CloseHandler;
import org.yaaic.command.handler.DCCHandler;
import org.yaaic.command.handler.DeopHandler;
@ -47,8 +49,6 @@ import org.yaaic.model.Conversation;
import org.yaaic.model.Message;
import org.yaaic.model.Server;
import android.content.Intent;
/**
* Parser for commands
*
@ -58,6 +58,14 @@ public class CommandParser
{
private HashMap<String, BaseHandler> commands;
private static CommandParser instance;
private final static String[] serverCommands = {
"admin", "motd", "version", "away", "knock", "rules",
"vhost", "credits", "license", "setname", "watch", "pong",
"cycle", "links", "silence", "who", "dalinfo", "userhost",
"list", "stats", "whois", "invite", "lusers", "ping",
"time", "whowas", "ison", "map",
};
/**
* Create a new CommandParser instance
@ -106,14 +114,112 @@ public class CommandParser
}
/**
* Is the given command a valid command?
* Is the given command a valid client command?
*
* @param command
* @return
* @param command The (client) command to check (/command)
* @return true if the command can be handled by the client, false otherwise
*/
public boolean isCommand(String command)
public boolean isClientCommand(String command)
{
return commands.containsKey(command);
return commands.containsKey(command.toLowerCase());
}
/**
* Is the given command a valid server command?
*
* @param command The (server) command to check (/command)
* @return true if the command can be handled by a server, false otherwise
*/
public boolean isServerCommand(String command)
{
command = command.toLowerCase();
for (String validCommand : serverCommands) {
if (validCommand.equals(command)) {
return true;
}
}
return false;
}
/**
* Handle a client command
*
* @param type Type of the command (/type param1 param2 ..)
* @param params The parameters of the command (0 is the command itself)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleClientCommand(String type, String[] params, Server server, Conversation conversation, IRCService service)
{
BaseHandler command = commands.get(type);
try {
command.execute(params, server, conversation, service);
} catch(CommandException e) {
// Command could not be executed
if (conversation != null) {
Message errorMessage = new Message(type + ": " + e.getMessage());
errorMessage.setColor(Message.COLOR_RED);
conversation.addMessage(errorMessage);
Message usageMessage = new Message("Syntax: " + command.getUsage());
conversation.addMessage(usageMessage);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
}
}
/**
* Handle a server command
*
* @param type Type of the command (/type param1 param2 ..)
* @param params The parameters of the command (0 is the command itself)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleServerCommand(String type, String[] params, Server server, Conversation conversation, IRCService service)
{
if (params.length > 1) {
service.getConnection(server.getId()).sendRawLineViaQueue(
type.toUpperCase() + " " + BaseHandler.mergeParams(params)
);
} else {
service.getConnection(server.getId()).sendRawLineViaQueue(type.toUpperCase());
}
}
/**
* Handle an unknown command
*
* @param type Type of the command (/type param1 param2 ..)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleUnknownCommand(String type, Server server, Conversation conversation, IRCService service)
{
if (conversation != null) {
Message message = new Message("Unknown command: " + type);
message.setColor(Message.COLOR_RED);
conversation.addMessage(message);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
}
/**
@ -127,45 +233,12 @@ public class CommandParser
String[] params = line.split(" ");
String type = params[0];
if (isCommand(type)) {
BaseHandler command = commands.get(type);
try {
command.execute(params, server, conversation, service);
} catch(CommandException e) {
// Wrong number of params
if (conversation != null) {
Message errorMessage = new Message(type + ": " + e.getMessage());
errorMessage.setColor(Message.COLOR_RED);
conversation.addMessage(errorMessage);
Message usageMessage = new Message("Syntax: " + command.getUsage());
conversation.addMessage(usageMessage);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
service.sendBroadcast(intent);
}
}
if (isClientCommand(type)) {
handleClientCommand(type, params, server, conversation, service);
} else if (isServerCommand(type)) {
handleServerCommand(type, params, server, conversation, service);
} else {
// Unknown command
if (params.length > 1) {
// Send command to server
service.getConnection(server.getId()).sendRawLineViaQueue(
params[0].toUpperCase() + " " + BaseHandler.mergeParams(params)
);
} else {
if (conversation != null) {
Message message = new Message("Unknown command: " + type);
message.setColor(Message.COLOR_RED);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
service.sendBroadcast(intent);
}
}
handleUnknownCommand(type, server, conversation, service);
}
}
}

View File

@ -55,9 +55,11 @@ public class CloseHandler extends BaseHandler
if (conversation.getType() == Conversation.TYPE_QUERY) {
server.removeConversation(conversation.getName());
Intent intent = new Intent(Broadcast.CONVERSATION_REMOVE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_REMOVE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
}

View File

@ -47,9 +47,11 @@ public class EchoHandler extends BaseHandler
Message message = new Message(BaseHandler.mergeParams(params));
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
} else {
throw new CommandException("Text is missing");

View File

@ -56,9 +56,11 @@ public class MeHandler extends BaseHandler
message.setIcon(R.drawable.action);
server.getConversation(conversation.getName()).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
service.getConnection(server.getId()).sendAction(conversation.getName(), action);

View File

@ -63,9 +63,11 @@ public class NamesHandler extends BaseHandler
message.setColor(Message.COLOR_YELLOW);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}

View File

@ -53,9 +53,11 @@ public class NoticeHandler extends BaseHandler
message.setIcon(R.drawable.info);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
service.getConnection(server.getId()).sendNotice(params[1], text);

View File

@ -59,9 +59,11 @@ public class QueryHandler extends BaseHandler
server.addConversationl(new Query(params[1]));
Intent intent = new Intent(Broadcast.CONVERSATION_NEW);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, params[1]);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
} else {
throw new CommandException("Invalid number of params");

View File

@ -77,8 +77,8 @@ public class IRCBinder extends Binder
}
catch (Exception e) {
server.setStatus(Status.DISCONNECTED);
Intent sIntent = new Intent(Broadcast.SERVER_UPDATE);
sIntent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
Intent sIntent = Broadcast.createServerIntent(Broadcast.SERVER_UPDATE, server.getId());
service.sendBroadcast(sIntent);
IRCConnection connection = getService().getConnection(server.getId());
@ -97,9 +97,11 @@ public class IRCBinder extends Binder
message.setIcon(R.drawable.error);
server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message);
Intent cIntent = new Intent(Broadcast.CONVERSATION_MESSAGE);
cIntent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
cIntent.putExtra(Broadcast.EXTRA_CONVERSATION, ServerInfo.DEFAULT_NAME);
Intent cIntent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
ServerInfo.DEFAULT_NAME
);
service.sendBroadcast(cIntent);
}
}

View File

@ -133,9 +133,12 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_GREEN);
server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, ServerInfo.DEFAULT_NAME);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
ServerInfo.DEFAULT_NAME
);
service.sendBroadcast(intent);
}
@ -164,24 +167,30 @@ public class IRCConnection extends PircBot
conversation = new Query(sender);
server.addConversationl(conversation);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_NEW);
intent.putExtra(Broadcast.EXTRA_CONVERSATION, sender);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_NEW,
server.getId(),
sender
);
service.sendBroadcast(intent);
} else {
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, sender);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
sender
);
service.sendBroadcast(intent);
}
} else {
// A action in a channel
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -205,9 +214,12 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_BLUE);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -222,9 +234,12 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.voice);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -239,18 +254,22 @@ public class IRCConnection extends PircBot
Message message = new Message(sourceNick + " invites you into " + target);
server.getConversation(server.getSelectedConversation()).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, server.getSelectedConversation());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
server.getSelectedConversation()
);
service.sendBroadcast(intent);
} else {
// Someone is invited
Message message = new Message(sourceNick + " invites " + targetNick + " into " + target);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -265,9 +284,11 @@ public class IRCConnection extends PircBot
// We joined a new channel
server.addConversationl(new Channel(target));
Intent intent = new Intent(Broadcast.CONVERSATION_NEW);
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_NEW,
server.getId(),
target
);
service.sendBroadcast(intent);
} else {
Message message = new Message(sender + " joins");
@ -275,9 +296,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_GREEN);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -292,18 +315,22 @@ public class IRCConnection extends PircBot
// We are kicked
server.removeConversation(target);
Intent intent = new Intent(Broadcast.CONVERSATION_REMOVE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_REMOVE,
server.getId(),
target
);
service.sendBroadcast(intent);
} else {
Message message = new Message(kickerNick + " kicks " + recipientNick);
message.setColor(Message.COLOR_GREEN);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -326,9 +353,11 @@ public class IRCConnection extends PircBot
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -361,9 +390,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_GREEN);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -389,9 +420,11 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.info);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
@ -406,9 +439,11 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.op);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -422,9 +457,11 @@ public class IRCConnection extends PircBot
// We parted a channel
server.removeConversation(target);
Intent intent = new Intent(Broadcast.CONVERSATION_REMOVE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_REMOVE,
server.getId(),
target
);
service.sendBroadcast(intent);
} else {
Message message = new Message(sender + " parts");
@ -432,9 +469,11 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.part);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
}
@ -462,16 +501,20 @@ public class IRCConnection extends PircBot
conversation.addMessage(message);
server.addConversationl(conversation);
Intent intent = new Intent(Broadcast.CONVERSATION_NEW);
intent.putExtra(Broadcast.EXTRA_CONVERSATION, sender);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_NEW,
server.getId(),
sender
);
service.sendBroadcast(intent);
} else {
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, sender);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
sender
);
service.sendBroadcast(intent);
}
}
@ -490,9 +533,11 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.quit);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -505,9 +550,11 @@ public class IRCConnection extends PircBot
message.setIcon(R.drawable.quit);
conversation.addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
service.sendBroadcast(intent);
}
@ -538,9 +585,11 @@ public class IRCConnection extends PircBot
// remember channel's topic
((Channel) server.getConversation(target)).setTopic(topic);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -564,9 +613,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_BLUE);
server.getConversation(target).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, target);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
target
);
service.sendBroadcast(intent);
}
@ -851,9 +902,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_GREY);
server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, ServerInfo.DEFAULT_NAME);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
ServerInfo.DEFAULT_NAME
);
service.sendBroadcast(intent);
}
@ -887,9 +940,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_GREY);
server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message);
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CONVERSATION, ServerInfo.DEFAULT_NAME);
Intent intent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
ServerInfo.DEFAULT_NAME
);
service.sendBroadcast(intent);
}
@ -900,8 +955,8 @@ public class IRCConnection extends PircBot
public void onDisconnect()
{
server.setStatus(Status.DISCONNECTED);
Intent sIntent = new Intent(Broadcast.SERVER_UPDATE);
sIntent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
Intent sIntent = Broadcast.createServerIntent(Broadcast.SERVER_UPDATE, server.getId());
service.sendBroadcast(sIntent);
Message message = new Message("Disconnected");
@ -909,9 +964,11 @@ public class IRCConnection extends PircBot
message.setColor(Message.COLOR_RED);
server.getConversation(ServerInfo.DEFAULT_NAME).addMessage(message);
Intent cIntent = new Intent(Broadcast.CONVERSATION_MESSAGE);
cIntent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
cIntent.putExtra(Broadcast.EXTRA_CONVERSATION, ServerInfo.DEFAULT_NAME);
Intent cIntent = Broadcast.createConversationIntent(
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
ServerInfo.DEFAULT_NAME
);
service.sendBroadcast(cIntent);
}
@ -945,6 +1002,10 @@ public class IRCConnection extends PircBot
@Override
public void quitServer()
{
quitServer("Yaaic - Yet another Android IRC client - http://www.yaaic.org");
new Thread() {
public void run() {
quitServer("Yaaic - Yet another Android IRC client - http://www.yaaic.org");
}
}.start();
}
}

View File

@ -20,12 +20,21 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yaaic.irc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import org.yaaic.R;
import org.yaaic.Yaaic;
import org.yaaic.db.Database;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Server;
import org.yaaic.activity.ServersActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
@ -38,7 +47,22 @@ public class IRCService extends Service
{
private IRCBinder binder;
private HashMap<Integer, IRCConnection> connections;
private boolean foreground = false;
@SuppressWarnings("unchecked")
private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class };
@SuppressWarnings("unchecked")
private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
public static final String ACTION_FOREGROUND = "org.yaaic.service.foreground";
public static final String ACTION_BACKGROUND = "org.yaaic.service.background";
private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
/**
* Create new service
*/
@ -50,13 +74,25 @@ public class IRCService extends Service
this.binder = new IRCBinder(this);
}
/**
* On create
*/
@Override
public void onCreate()
{
super.onCreate();
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
try {
mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature);
mStopForeground = getClass().getMethod("stopForeground", mStopForegroundSignature);
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
}
// Load servers from Database
Database db = new Database(this);
Database db = new Database(this);
Yaaic.getInstance().setServers(db.getServers());
db.close();
@ -64,11 +100,110 @@ public class IRCService extends Service
sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
}
/**
* On start (will be called on pre-2.0 platform. On 2.0 or later onStartCommand()
* will be called)
*/
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
handleCommand(intent);
}
/**
* On start command (Android >= 2.0)
*
* @param intent
* @param flags
* @param startId
* @return
*/
public int onStartCommand(Intent intent, int flags, int startId)
{
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
//return START_STICKY;
return 1;
}
/**
* Handle command
*
* @param intent
*/
private void handleCommand(Intent intent)
{
if (ACTION_FOREGROUND.equals(intent.getAction())) {
foreground = true;
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.icon, "Connected", System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ServersActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.app_name), "Connected", contentIntent);
startForegroundCompat(R.string.app_name, notification);
} else if (ACTION_BACKGROUND.equals(intent.getAction()) && !foreground) {
stopForegroundCompat(R.string.app_name);
}
}
/**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
private void startForegroundCompat(int id, Notification notification)
{
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
} catch (IllegalAccessException e) {
// Should not happen.
}
} else {
// Fall back on the old API.
setForeground(true);
mNM.notify(id, notification);
}
}
/**
* This is a wrapper around the new stopForeground method, using the older
* APIs if it is not available.
*/
private void stopForegroundCompat(int id)
{
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
} catch (IllegalAccessException e) {
// Should not happen.
}
} else {
// Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
mNM.cancel(id);
setForeground(false);
}
}
/**
* Get connection for given server
@ -88,6 +223,42 @@ public class IRCService extends Service
return connection;
}
/**
* Check status of service
*/
public void checkServiceStatus()
{
boolean shutDown = true;
ArrayList<Server> mServers = Yaaic.getInstance().getServersAsArrayList();
int mSize = mServers.size();
Server server;
for (int i = 0; i < mSize; i++) {
server = mServers.get(i);
if (server.isDisconnected()) {
connections.remove(server.getId());
} else {
shutDown = false;
}
}
if (shutDown) {
foreground = false;
stopSelf();
}
}
/**
* On Destroy
*/
@Override
public void onDestroy()
{
// Make sure our notification is gone.
stopForegroundCompat(R.string.app_name);
}
/**
* On Activity binding to this service
*

View File

@ -34,24 +34,37 @@ public abstract class Broadcast
public static final String CONVERSATION_MESSAGE = "org.yaaic.conversation.message";
public static final String CONVERSATION_NEW = "org.yaaic.conversation.new";
public static final String CONVERSATION_REMOVE = "org.yaaic.conversation.remove";
public static final String EXTRA_SERVER = "server";
public static final String EXTRA_CONVERSATION = "conversation";
/**
* Create an intent for conversation broadcasting
* Create an Intent for conversation broadcasting
*
* @param broadcastType The type of the broadcast, some constant of Broadcast.*
* @param serverId The id of the server
* @param conversationName The unique name of the conversation
* @return
* @return The created Intent
*/
public static Intent createConversationIntent(String broadcastType, int serverId, String conversationName)
{
Intent intent = new Intent(broadcastType);
intent.putExtra(Broadcast.EXTRA_SERVER, serverId);
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversationName);
intent.putExtra(Extra.SERVER, serverId);
intent.putExtra(Extra.CONVERSATION, conversationName);
return intent;
}
/**
* Create an Intent for server broadcasting
*
* @param broadcastType The typo of the broadcast, some constant of Broadcast.*
* @param serverId The id of the server
* @return The created Intent
*/
public static Intent createServerIntent(String broadcastType, int serverId)
{
Intent intent = new Intent(broadcastType);
intent.putExtra(Extra.SERVER, serverId);
return intent;
}

View File

@ -0,0 +1,32 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009-2010 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.model;
/**
* Helper class for constants used for bundle params (extras)
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public class Extra
{
public static final String SERVER = "server";
public static final String CONVERSATION = "conversation";
}

View File

@ -26,6 +26,7 @@ import android.content.Intent;
import org.yaaic.listener.ConversationListener;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Extra;
/**
* A channel receiver for receiving channel updates
@ -58,7 +59,7 @@ public class ConversationReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
{
int serverId = intent.getExtras().getInt(Broadcast.EXTRA_SERVER);
int serverId = intent.getExtras().getInt(Extra.SERVER);
if (serverId != this.serverId) {
return;
}
@ -66,11 +67,11 @@ public class ConversationReceiver extends BroadcastReceiver
String action = intent.getAction();
if (action.equals(Broadcast.CONVERSATION_MESSAGE)) {
listener.onConversationMessage(intent.getExtras().getString(Broadcast.EXTRA_CONVERSATION));
listener.onConversationMessage(intent.getExtras().getString(Extra.CONVERSATION));
} else if (action.equals(Broadcast.CONVERSATION_NEW)) {
listener.onNewConversation(intent.getExtras().getString(Broadcast.EXTRA_CONVERSATION));
listener.onNewConversation(intent.getExtras().getString(Extra.CONVERSATION));
} else if (action.equals(Broadcast.CONVERSATION_REMOVE)) {
listener.onRemoveConversation(intent.getExtras().getString(Broadcast.EXTRA_CONVERSATION));
listener.onRemoveConversation(intent.getExtras().getString(Extra.CONVERSATION));
}
}