1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-15 13:35:09 -05:00

permanent service with notification icon, stop service if not needed (work in progress)

This commit is contained in:
Sebastian Kaspari 2010-03-21 19:57:30 +01:00
parent 8bfe1d1a75
commit 41cf6b60d0
6 changed files with 228 additions and 24 deletions

View File

@ -22,11 +22,14 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.yaaic" package="org.yaaic"
android:versionCode="2" android:versionCode="2"
android:versionName="@string/app_version"> android:versionName="0.2">
<application android:icon="@drawable/icon" android:label="@string/app_name"> <application
android:icon="@drawable/icon"
android:label="Yaaic">
<activity <activity
android:name=".view.ServersActivity" android:name=".view.ServersActivity"
android:label="@string/app_name"> android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />

View File

@ -23,8 +23,13 @@ package org.yaaic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import org.yaaic.db.Database;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Server; import org.yaaic.model.Server;
import android.content.Context;
import android.content.Intent;
/** /**
* Global Master Class :) * Global Master Class :)
* *
@ -35,6 +40,7 @@ public class Yaaic
public static Yaaic instance; public static Yaaic instance;
private HashMap<Integer, Server> servers; private HashMap<Integer, Server> servers;
private boolean serversLoaded = false;
/** /**
* Private constructor, you may want to use static getInstance() * Private constructor, you may want to use static getInstance()
@ -44,6 +50,23 @@ public class Yaaic
servers = new HashMap<Integer, Server>(); 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 * Get global Yaaic instance
* *

View File

@ -1036,6 +1036,10 @@ public class IRCConnection extends PircBot
@Override @Override
public void quitServer() public void quitServer()
{ {
new Thread() {
public void run() {
quitServer("Yaaic - Yet another Android IRC client - http://www.yaaic.org"); quitServer("Yaaic - Yet another Android IRC client - http://www.yaaic.org");
} }
}.start();
}
} }

View File

@ -20,12 +20,20 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.yaaic.irc; package org.yaaic.irc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
import org.yaaic.R;
import org.yaaic.Yaaic; import org.yaaic.Yaaic;
import org.yaaic.db.Database; import org.yaaic.model.Server;
import org.yaaic.model.Broadcast; import org.yaaic.view.ServersActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
@ -42,6 +50,17 @@ public class IRCService extends Service
private IRCBinder binder; private IRCBinder binder;
private HashMap<Integer, IRCConnection> connections; private HashMap<Integer, IRCConnection> connections;
@SuppressWarnings("unchecked")
private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class };
@SuppressWarnings("unchecked")
private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
/** /**
* Create new service * Create new service
*/ */
@ -55,27 +74,126 @@ public class IRCService extends Service
this.binder = new IRCBinder(this); this.binder = new IRCBinder(this);
} }
/**
* On create
*/
@Override @Override
public void onCreate() public void onCreate()
{ {
super.onCreate(); mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Log.d(TAG, "Loading servers from database"); 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 // Load servers from Database
/*
Database db = new Database(this); Database db = new Database(this);
Yaaic.getInstance().setServers(db.getServers()); Yaaic.getInstance().setServers(db.getServers());
db.close(); db.close();
*/
// Broadcast changed server list // Broadcast changed server list
sendBroadcast(new Intent(Broadcast.SERVER_UPDATE)); //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 @Override
public void onStart(Intent intent, int startId) public void onStart(Intent intent, int startId)
{ {
Log.d(TAG, "onStart()"); handleCommand(intent);
super.onStart(intent, startId); }
/**
* 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)
{
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.icon, "Mama", 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), "Papa", contentIntent);
startForegroundCompat(R.string.app_name, notification);
}
/**
* 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);
}
} }
/** /**
@ -96,6 +214,43 @@ public class IRCService extends Service
return connection; return connection;
} }
public void checkServiceStatus()
{
Log.d("Yaaic", "(1)");
boolean shutDown = true;
ArrayList<Server> mServers = Yaaic.getInstance().getServersAsArrayList();
int mSize = mServers.size();
Server server;
Log.d("Yaaic", "(2)");
for (int i = 0; i < mSize; i++) {
Log.d("Yaaic", " (3)");
server = mServers.get(i);
if (server.isDisconnected()) {
connections.remove(server.getId());
} else {
shutDown = false;
}
}
if (shutDown) {
stopSelf();
}
}
/**
*
*/
@Override
public void onDestroy()
{
// Make sure our notification is gone.
stopForegroundCompat(R.string.app_name);
}
/** /**
* On Activity binding to this service * On Activity binding to this service
* *

View File

@ -123,7 +123,9 @@ public class ConversationActivity extends Activity implements ServiceConnection,
((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon()); ((ImageView) findViewById(R.id.status)).setImageResource(server.getStatusIcon());
// Start service
Intent intent = new Intent(this, IRCService.class); Intent intent = new Intent(this, IRCService.class);
startService(intent);
bindService(intent, this, 0); bindService(intent, this, 0);
channelReceiver = new ConversationReceiver(server.getId(), this); channelReceiver = new ConversationReceiver(server.getId(), this);
@ -145,6 +147,10 @@ public class ConversationActivity extends Activity implements ServiceConnection,
while (conversation.hasBufferedMessages()) { while (conversation.hasBufferedMessages()) {
if (conversation.getMessageListAdapter() != null) { if (conversation.getMessageListAdapter() != null) {
conversation.getMessageListAdapter().addMessage(conversation.pollBufferedMessage()); conversation.getMessageListAdapter().addMessage(conversation.pollBufferedMessage());
} else {
// There's no adapter... so let's remove the buffer
conversation.clearBuffer();
Log.d("Yaaic", "No MessageListAdapter found - clear buffer");
} }
} }
} }
@ -158,6 +164,15 @@ public class ConversationActivity extends Activity implements ServiceConnection,
{ {
super.onPause(); 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); unbindService(this);
unregisterReceiver(channelReceiver); unregisterReceiver(channelReceiver);
unregisterReceiver(serverReceiver); unregisterReceiver(serverReceiver);
@ -209,10 +224,12 @@ public class ConversationActivity extends Activity implements ServiceConnection,
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.disconnect: case R.id.disconnect:
binder.getService().getConnection(serverId).quitServer(); binder.getService().getConnection(serverId).quitServer();
server.setStatus(Status.DISCONNECTED);
server.clearConversations(); server.clearConversations();
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
break; break;
case R.id.join: case R.id.join:
startActivityForResult(new Intent(this, JoinActivity.class), 0); startActivityForResult(new Intent(this, JoinActivity.class), 0);
break; break;

View File

@ -60,7 +60,7 @@ import org.yaaic.receiver.ServerReceiver;
public class ServersActivity extends ListActivity implements ServiceConnection, ServerListener, OnItemLongClickListener { public class ServersActivity extends ListActivity implements ServiceConnection, ServerListener, OnItemLongClickListener {
public static final String TAG = "Yaaic/ServersActivity"; public static final String TAG = "Yaaic/ServersActivity";
private IRCBinder binder; //private IRCBinder binder;
private ServerReceiver receiver; private ServerReceiver receiver;
private ServerListAdapter adapter; private ServerListAdapter adapter;
@ -73,6 +73,8 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.servers); setContentView(R.layout.servers);
Yaaic.getInstance().loadServers(this);
adapter = new ServerListAdapter(); adapter = new ServerListAdapter();
setListAdapter(adapter); setListAdapter(adapter);
@ -89,9 +91,9 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
super.onResume(); super.onResume();
// Start and connect to service // Start and connect to service
Intent intent = new Intent(this, IRCService.class); //Intent intent = new Intent(this, IRCService.class);
startService(intent); //startService(intent);
bindService(intent, this, 0); //bindService(intent, this, 0);
receiver = new ServerReceiver(this); receiver = new ServerReceiver(this);
registerReceiver(receiver, new IntentFilter(Broadcast.SERVER_UPDATE)); registerReceiver(receiver, new IntentFilter(Broadcast.SERVER_UPDATE));
@ -107,7 +109,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
{ {
super.onPause(); super.onPause();
unbindService(this); //unbindService(this);
unregisterReceiver(receiver); unregisterReceiver(receiver);
} }
@ -116,7 +118,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
*/ */
public void onServiceConnected(ComponentName name, IBinder service) public void onServiceConnected(ComponentName name, IBinder service)
{ {
binder = (IRCBinder) service; //binder = (IRCBinder) service;
} }
/** /**
@ -124,7 +126,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
*/ */
public void onServiceDisconnected(ComponentName name) public void onServiceDisconnected(ComponentName name)
{ {
binder = null; //binder = null;
} }
/** /**
@ -165,19 +167,19 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int item) {
switch (item) { switch (item) {
case 0: // Connect case 0: // Connect
binder.connect(server); //binder.connect(server);
server.setStatus(Status.CONNECTING); server.setStatus(Status.CONNECTING);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
break; break;
case 1: // Disconnect case 1: // Disconnect
server.clearConversations(); server.clearConversations();
binder.getService().getConnection(server.getId()).quitServer(); //binder.getService().getConnection(server.getId()).quitServer();
break; break;
case 2: // Edit case 2: // Edit
editServer(server.getId()); editServer(server.getId());
break; break;
case 3: // Delete case 3: // Delete
binder.getService().getConnection(server.getId()).quitServer(); //binder.getService().getConnection(server.getId()).quitServer();
deleteServer(server.getId()); deleteServer(server.getId());
break; break;
} }