summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/LogWindow.java
blob: 6c3e1ff7b01b37e7520c46e32d03070c961d4c72 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package de.blinkt.openvpn;

import java.util.Vector;

import android.app.ListActivity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import de.blinkt.openvpn.OpenVPN.LogListener;

public class LogWindow extends ListActivity  {
	
	class LogWindowListAdapter implements ListAdapter,LogListener, Callback {

		private static final int LIST_CHANGED = 0;

		private Vector<String> myEntries=new Vector<String>();
		
		private Handler mHandler;

		private Vector<DataSetObserver> observers=new Vector<DataSetObserver>();

		public LogWindowListAdapter() {
			for (String litem : OpenVPN.getlogbuffer()) {
				myEntries.add(litem);				
			}
			  
				if (mHandler == null) {
					mHandler = new Handler(this);
				}
			  
			OpenVPN.addLogListener(this);
		}
		
		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			observers.add(observer);
			
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			observers.remove(observer);
		}

		@Override
		public int getCount() {
			return myEntries.size();
		}

		@Override
		public Object getItem(int position) {
			return myEntries.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView v;
			if(convertView==null)
				v = new TextView(getBaseContext());
			else
				v = (TextView) convertView;
			v.setText(myEntries.get(position));
			return v;
		}

		@Override
		public int getItemViewType(int position) {
			return 0;
		}

		@Override
		public int getViewTypeCount() {
			return 1;
		}

		@Override
		public boolean isEmpty() {
			return myEntries.isEmpty();
			
		}

		@Override
		public boolean areAllItemsEnabled() {
			return true;
		}

		@Override
		public boolean isEnabled(int position) {
			return true;
		}

		@Override
		public void newLog(String logmessage) {
			Message msg = Message.obtain();
			Bundle mbundle=new Bundle();
			mbundle.putString("logmessage", logmessage);
			msg.setData(mbundle);
			mHandler.sendMessage(msg);
		}

		@Override
		public boolean handleMessage(Message msg) {
			// We have been called
			String logmessage = msg.getData().getString("logmessage");
			myEntries.add(logmessage);

			for (DataSetObserver observer : observers) {
				observer.onChanged();
			}
			
			
			return true;
		}
		
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		
		  ListView lv = getListView();
		  //lv.setTextFilterEnabled(true);
		  LogWindowListAdapter adapter = new LogWindowListAdapter();
		  lv.setAdapter(adapter);

		}

	
	
}