mirror of
https://github.com/moparisthebest/Yaaic
synced 2025-01-07 03:38:10 -05:00
Persistent service if connected to a server
This commit is contained in:
parent
13dde9ed03
commit
5fc8788a19
@ -27,6 +27,8 @@ 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.view.ServersActivity;
|
||||
|
||||
@ -48,11 +50,15 @@ 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;
|
||||
@ -90,14 +96,12 @@ public class IRCService extends Service
|
||||
}
|
||||
|
||||
// Load servers from Database
|
||||
/*
|
||||
Database db = new Database(this);
|
||||
Yaaic.getInstance().setServers(db.getServers());
|
||||
db.close();
|
||||
*/
|
||||
|
||||
// Broadcast changed server list
|
||||
//sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
|
||||
sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,6 +111,8 @@ public class IRCService extends Service
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId)
|
||||
{
|
||||
Log.d(TAG, "Service onStart()");
|
||||
|
||||
handleCommand(intent);
|
||||
}
|
||||
|
||||
@ -120,6 +126,8 @@ public class IRCService extends Service
|
||||
*/
|
||||
public int onStartCommand(Intent intent, int flags, int startId)
|
||||
{
|
||||
Log.d(TAG, "Service onStartCommand()");
|
||||
|
||||
handleCommand(intent);
|
||||
|
||||
// We want this service to continue running until it is explicitly
|
||||
@ -136,17 +144,25 @@ public class IRCService extends Service
|
||||
*/
|
||||
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);
|
||||
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
|
||||
@ -176,7 +192,8 @@ public class IRCService extends Service
|
||||
* This is a wrapper around the new stopForeground method, using the older
|
||||
* APIs if it is not available.
|
||||
*/
|
||||
private void stopForegroundCompat(int id) {
|
||||
private void stopForegroundCompat(int id)
|
||||
{
|
||||
// If we have the new stopForeground API, then use it.
|
||||
if (mStopForeground != null) {
|
||||
mStopForegroundArgs[0] = Boolean.TRUE;
|
||||
@ -213,19 +230,17 @@ public class IRCService extends Service
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check status of service
|
||||
*/
|
||||
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());
|
||||
@ -235,12 +250,13 @@ public class IRCService extends Service
|
||||
}
|
||||
|
||||
if (shutDown) {
|
||||
foreground = false;
|
||||
stopSelf();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* On Destroy
|
||||
*/
|
||||
@Override
|
||||
public void onDestroy()
|
||||
|
@ -125,6 +125,7 @@ public class ConversationActivity extends Activity implements ServiceConnection,
|
||||
|
||||
// Start service
|
||||
Intent intent = new Intent(this, IRCService.class);
|
||||
intent.setAction(IRCService.ACTION_FOREGROUND);
|
||||
startService(intent);
|
||||
bindService(intent, this, 0);
|
||||
|
||||
|
@ -43,6 +43,8 @@ import org.yaaic.R;
|
||||
import org.yaaic.Yaaic;
|
||||
import org.yaaic.adapter.ServerListAdapter;
|
||||
import org.yaaic.db.Database;
|
||||
import org.yaaic.irc.IRCBinder;
|
||||
import org.yaaic.irc.IRCService;
|
||||
import org.yaaic.layout.NonScalingBackgroundDrawable;
|
||||
import org.yaaic.listener.ServerListener;
|
||||
import org.yaaic.model.Broadcast;
|
||||
@ -58,7 +60,7 @@ import org.yaaic.receiver.ServerReceiver;
|
||||
public class ServersActivity extends ListActivity implements ServiceConnection, ServerListener, OnItemLongClickListener {
|
||||
public static final String TAG = "Yaaic/ServersActivity";
|
||||
|
||||
//private IRCBinder binder;
|
||||
private IRCBinder binder;
|
||||
private ServerReceiver receiver;
|
||||
private ServerListAdapter adapter;
|
||||
|
||||
@ -71,8 +73,6 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.servers);
|
||||
|
||||
Yaaic.getInstance().loadServers(this);
|
||||
|
||||
adapter = new ServerListAdapter();
|
||||
setListAdapter(adapter);
|
||||
|
||||
@ -89,9 +89,10 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
super.onResume();
|
||||
|
||||
// Start and connect to service
|
||||
//Intent intent = new Intent(this, IRCService.class);
|
||||
//startService(intent);
|
||||
//bindService(intent, this, 0);
|
||||
Intent intent = new Intent(this, IRCService.class);
|
||||
intent.setAction(IRCService.ACTION_BACKGROUND);
|
||||
startService(intent);
|
||||
bindService(intent, this, 0);
|
||||
|
||||
receiver = new ServerReceiver(this);
|
||||
registerReceiver(receiver, new IntentFilter(Broadcast.SERVER_UPDATE));
|
||||
@ -107,7 +108,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
{
|
||||
super.onPause();
|
||||
|
||||
//unbindService(this);
|
||||
unbindService(this);
|
||||
unregisterReceiver(receiver);
|
||||
}
|
||||
|
||||
@ -116,7 +117,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
*/
|
||||
public void onServiceConnected(ComponentName name, IBinder service)
|
||||
{
|
||||
//binder = (IRCBinder) service;
|
||||
binder = (IRCBinder) service;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -124,7 +125,7 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
*/
|
||||
public void onServiceDisconnected(ComponentName name)
|
||||
{
|
||||
//binder = null;
|
||||
binder = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,19 +166,19 @@ public class ServersActivity extends ListActivity implements ServiceConnection,
|
||||
public void onClick(DialogInterface dialog, int item) {
|
||||
switch (item) {
|
||||
case 0: // Connect
|
||||
//binder.connect(server);
|
||||
binder.connect(server);
|
||||
server.setStatus(Status.CONNECTING);
|
||||
adapter.notifyDataSetChanged();
|
||||
break;
|
||||
case 1: // Disconnect
|
||||
server.clearConversations();
|
||||
//binder.getService().getConnection(server.getId()).quitServer();
|
||||
binder.getService().getConnection(server.getId()).quitServer();
|
||||
break;
|
||||
case 2: // Edit
|
||||
editServer(server.getId());
|
||||
break;
|
||||
case 3: // Delete
|
||||
//binder.getService().getConnection(server.getId()).quitServer();
|
||||
binder.getService().getConnection(server.getId()).quitServer();
|
||||
deleteServer(server.getId());
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user