summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/IRCService.java
blob: 6f76c1b5d83dc017fb2d58c873a85b91502d7afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package net.brokenfox.vulpirc;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 * Created by ninji on 1/30/14.
 */
public class IRCService extends Service implements Connection.LoginStateListener {
	private final IBinder mBinder = new LocalBinder();
	public class LocalBinder extends Binder {
		IRCService getService() {
			return IRCService.this;
		}
	}


	// Lifecycle junk.


	@Override
	public void onCreate() {
		super.onCreate();
		Connection.get().registerLoginStateListener(this);
		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onDestroy() {
		Connection.get().deregisterLoginStateListener(this);
		disableForeground();
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// If we're killed, don't automatically restart -- we only want to be
		// started if the main VulpIRC app requests it.

		return START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}




	private NotificationManager mNM = null;

	private boolean mInForeground = false;
	private void enableForeground() {
		Notification n = generateNotification();

		if (mInForeground) {
			// Just refresh the notification
			mNM.notify(1, n);
		} else {
			mInForeground = true;

			startForeground(1, n);
		}
	}
	private void disableForeground() {
		if (!mInForeground)
			return;

		stopForeground(true);
		mInForeground = false;
	}


	private Notification generateNotification() {
		boolean active = Connection.get().getSessionActive();
		String desc1, desc2 = "";
		desc1 = active ? "Logged in, " : "Not logged in, ";
		switch (Connection.get().getSocketState()) {
			case DISCONNECTED:
				desc2 = "disconnected";
				break;
			case DISCONNECTING:
				desc2 = "disconnecting";
				break;
			case CONNECTING:
				desc2 = "connecting...";
				break;
			case CONNECTED:
				desc2 = "connected";
				break;
		}

		Intent i = new Intent(this, active ? MainActivity.class : LoginActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//i.setFlags(i.getFlags() | 0x2000); // enables halo intent, maybe?
		PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

		Notification n = new Notification.Builder(this)
				.setContentTitle("VulpIRC")
				.setContentText(desc1 + desc2)
				.setContentIntent(pi)
				.setSmallIcon(R.drawable.ic_launcher)
				.setOngoing(true)
				.setDefaults(0)
				.getNotification();

		return n;
	}


	// IRC FUNTIMES

	@Override
	public void handleLoginStateChanged() {
		if (Connection.get().getSessionActive()) {
			enableForeground();
		} else {
			if (Connection.get().getSocketState() == BaseConn.SocketState.DISCONNECTED) {
				disableForeground();
				stopSelf();
			} else
				enableForeground();
		}
	}
}