2009-12-17 15:27:57 -05:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
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.irc;
|
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
2009-12-17 15:27:57 -05:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
import org.yaaic.R;
|
2009-12-17 15:27:57 -05:00
|
|
|
import org.yaaic.Yaaic;
|
|
|
|
import org.yaaic.db.Database;
|
|
|
|
import org.yaaic.model.Broadcast;
|
2010-03-21 14:57:30 -04:00
|
|
|
import org.yaaic.model.Server;
|
2010-03-29 15:50:36 -04:00
|
|
|
import org.yaaic.activity.ServersActivity;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2009-12-17 15:27:57 -05:00
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The background service for managing the irc connections
|
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
|
|
|
public class IRCService extends Service
|
|
|
|
{
|
|
|
|
private IRCBinder binder;
|
|
|
|
private HashMap<Integer, IRCConnection> connections;
|
2010-03-29 15:48:23 -04:00
|
|
|
private boolean foreground = false;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class };
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
|
2010-03-29 15:48:23 -04:00
|
|
|
|
|
|
|
public static final String ACTION_FOREGROUND = "org.yaaic.service.foreground";
|
|
|
|
public static final String ACTION_BACKGROUND = "org.yaaic.service.background";
|
2010-03-21 14:57:30 -04:00
|
|
|
|
2010-04-14 13:13:32 -04:00
|
|
|
private NotificationManager notificationManager;
|
2010-03-21 14:57:30 -04:00
|
|
|
private Method mStartForeground;
|
|
|
|
private Method mStopForeground;
|
|
|
|
private Object[] mStartForegroundArgs = new Object[2];
|
|
|
|
private Object[] mStopForegroundArgs = new Object[1];
|
2010-04-11 17:49:43 -04:00
|
|
|
private Notification notification;
|
2010-03-21 14:57:30 -04:00
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
/**
|
|
|
|
* Create new service
|
|
|
|
*/
|
|
|
|
public IRCService()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.connections = new HashMap<Integer, IRCConnection>();
|
|
|
|
this.binder = new IRCBinder(this);
|
|
|
|
}
|
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
/**
|
|
|
|
* On create
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public void onCreate()
|
|
|
|
{
|
|
|
|
super.onCreate();
|
2010-04-14 13:13:32 -04:00
|
|
|
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
2010-03-21 14:57:30 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
mStartForeground = getClass().getMethod("startForeground", mStartForegroundSignature);
|
|
|
|
mStopForeground = getClass().getMethod("stopForeground", mStopForegroundSignature);
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
// Running on an older platform.
|
|
|
|
mStartForeground = mStopForeground = null;
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
// Load servers from Database
|
2010-03-21 14:57:30 -04:00
|
|
|
Database db = new Database(this);
|
2009-12-17 15:27:57 -05:00
|
|
|
Yaaic.getInstance().setServers(db.getServers());
|
|
|
|
db.close();
|
|
|
|
|
|
|
|
// Broadcast changed server list
|
|
|
|
sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
|
|
|
|
}
|
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
/**
|
|
|
|
* On start (will be called on pre-2.0 platform. On 2.0 or later onStartCommand()
|
|
|
|
* will be called)
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
@Override
|
|
|
|
public void onStart(Intent intent, int startId)
|
|
|
|
{
|
|
|
|
super.onStart(intent, startId);
|
2010-03-21 14:57:30 -04:00
|
|
|
handleCommand(intent);
|
2009-12-17 15:27:57 -05:00
|
|
|
}
|
2010-03-21 14:57:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2010-03-29 15:48:23 -04:00
|
|
|
if (ACTION_FOREGROUND.equals(intent.getAction())) {
|
2010-04-11 17:49:43 -04:00
|
|
|
if (foreground) {
|
|
|
|
return; // XXX: We are already in foreground...
|
|
|
|
}
|
|
|
|
|
2010-03-29 15:48:23 -04:00
|
|
|
foreground = true;
|
|
|
|
|
|
|
|
// Set the icon, scrolling text and timestamp
|
2010-04-11 17:55:00 -04:00
|
|
|
notification = new Notification(R.drawable.icon, "", System.currentTimeMillis());
|
2010-03-29 15:48:23 -04:00
|
|
|
|
|
|
|
// 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.
|
2010-04-11 17:55:00 -04:00
|
|
|
notification.setLatestEventInfo(this, getText(R.string.app_name), "", contentIntent);
|
2010-03-29 15:48:23 -04:00
|
|
|
|
|
|
|
startForegroundCompat(R.string.app_name, notification);
|
|
|
|
} else if (ACTION_BACKGROUND.equals(intent.getAction()) && !foreground) {
|
|
|
|
stopForegroundCompat(R.string.app_name);
|
|
|
|
}
|
2010-03-21 14:57:30 -04:00
|
|
|
}
|
2010-03-29 15:48:23 -04:00
|
|
|
|
2010-04-14 13:13:32 -04:00
|
|
|
/**
|
|
|
|
* Update the notification
|
|
|
|
*
|
|
|
|
* @param text The text to display
|
|
|
|
*/
|
2010-04-11 17:49:43 -04:00
|
|
|
public void updateNotification(String text)
|
|
|
|
{
|
|
|
|
if (foreground) {
|
2010-04-14 13:13:32 -04:00
|
|
|
notificationManager.cancel(R.string.app_name);
|
2010-04-11 17:49:43 -04:00
|
|
|
notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ServersActivity.class), 0);
|
|
|
|
notification.setLatestEventInfo(this, getText(R.string.app_name), text, contentIntent);
|
2010-04-14 13:13:32 -04:00
|
|
|
notificationManager.notify(R.string.app_name, notification);
|
2010-04-11 17:49:43 -04:00
|
|
|
}
|
|
|
|
}
|
2010-03-21 14:57:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2010-04-14 13:13:32 -04:00
|
|
|
notificationManager.notify(id, notification);
|
2010-03-21 14:57:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a wrapper around the new stopForeground method, using the older
|
|
|
|
* APIs if it is not available.
|
|
|
|
*/
|
2010-03-29 15:48:23 -04:00
|
|
|
private void stopForegroundCompat(int id)
|
|
|
|
{
|
2010-04-13 16:04:46 -04:00
|
|
|
foreground = false;
|
|
|
|
|
2010-03-21 14:57:30 -04:00
|
|
|
// 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.
|
2010-04-14 13:13:32 -04:00
|
|
|
notificationManager.cancel(id);
|
2010-03-21 14:57:30 -04:00
|
|
|
setForeground(false);
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get connection for given server
|
|
|
|
*
|
|
|
|
* @param serverId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public synchronized IRCConnection getConnection(int serverId)
|
|
|
|
{
|
|
|
|
IRCConnection connection = connections.get(serverId);
|
|
|
|
|
|
|
|
if (connection == null) {
|
|
|
|
connection = new IRCConnection(this, serverId);
|
|
|
|
connections.put(serverId, connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2010-03-29 15:48:23 -04:00
|
|
|
/**
|
|
|
|
* Check status of service
|
|
|
|
*/
|
2010-03-21 14:57:30 -04:00
|
|
|
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) {
|
2010-03-29 15:48:23 -04:00
|
|
|
foreground = false;
|
2010-03-21 14:57:30 -04:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-29 15:48:23 -04:00
|
|
|
* On Destroy
|
2010-03-21 14:57:30 -04:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onDestroy()
|
|
|
|
{
|
|
|
|
// Make sure our notification is gone.
|
2010-04-13 15:28:13 -04:00
|
|
|
if (foreground) {
|
|
|
|
stopForegroundCompat(R.string.app_name);
|
|
|
|
}
|
2010-03-21 14:57:30 -04:00
|
|
|
}
|
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
/**
|
|
|
|
* On Activity binding to this service
|
|
|
|
*
|
|
|
|
* @param intent
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public IRCBinder onBind(Intent intent)
|
|
|
|
{
|
|
|
|
return binder;
|
|
|
|
}
|
|
|
|
}
|