summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowData.java
blob: 09c6606868a53ae2121be682e304c4934544dd3d (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
package net.brokenfox.vulpirc;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by ninji on 2/3/14.
 */
public class WindowData {
	public int id;
	public String title;
	public int unreadLevel = 0;

	public ArrayList<CharSequence> messages = new ArrayList<CharSequence>();

	protected Fragment instantiateFragmentClass() {
		return new WindowFragment();
	}
	public Fragment createFragment() {
		Bundle args = new Bundle();
		args.putInt("winID", id);

		Fragment wf = instantiateFragmentClass();
		wf.setArguments(args);
		return wf;
	}

	public void setUnreadLevel(int newLevel) {
		unreadLevel = newLevel;
		Connection.get().notifyWindowsUpdated();
	}

	public void setTitle(String newTitle) {
		title = newTitle;
		Connection.get().notifyWindowsUpdated();
	}


	public interface WindowListener {
		void handleMessagesChanged();
	}

	protected ArrayList<WindowListener> mListeners = new ArrayList<WindowListener>();

	public void registerListener(WindowListener l) {
		mListeners.add(l);
	}
	public void deregisterListener(WindowListener l) {
		mListeners.remove(l);
	}



	// This is a kludge.
	private final DateFormat timestampFormat = new SimpleDateFormat("\u0001[\u0002\u0010\u001DHH:mm:ss\u0018\u0001]\u0002 ", new DateFormatSymbols());
	private String generateTimestamp() {
		return timestampFormat.format(new Date());
	}

	public void pushMessage(String message) {
		messages.add(RichText.process(generateTimestamp() + message));
		for (WindowListener l : mListeners)
			l.handleMessagesChanged();
	}


	public void processInitialSync(ByteBuffer p) {
		id = p.getInt();
		title = Util.readStringFromBuffer(p);

		int messageCount = p.getInt();
		Log.i("VulpIRC", "id=" + id + ", title=[" + title + "]");

		if (messageCount > 0) {
			messages.ensureCapacity(messageCount);
			for (int j = 0; j < messageCount; j++) {
				String msg = Util.readStringFromBuffer(p);
				//Log.i("VulpIRC", "msg " + j + ": " + msg);
				messages.add(RichText.process(msg));
			}
		}
	}



	public void sendUserInput(CharSequence message) {
		byte[] enc = Util.encodeString(message);

		ByteBuffer buf = ByteBuffer.allocate(8 + enc.length);
		buf.order(ByteOrder.LITTLE_ENDIAN);

		buf.putInt(id);
		buf.putInt(enc.length);
		buf.put(enc);

		Connection.get().sendPacket(0x102, buf.array());
	}
}