summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/Settings.java
blob: 276d4f658096eda6439d102a03057aa951997fb3 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.macaroon.piztor;

import java.lang.ref.WeakReference;
import java.util.Vector;

import com.baidu.platform.comapi.basestruct.GeoPoint;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class Settings extends PiztorAct {
	
	public final int show_by_team = 1;
	public final int show_by_sex = 2;
	
	Button logout, subscribe, account;
	MapInfo mapInfo;
	Transam transam;
	private int currentRate;
	OnCheckedChangeListener radioButtonListener = null;
	RadioGroup radioGroup;
	
	// Event
	final static int logoutButtonPressed = 10;
	final static int logoutFailed = 11;

	static class ReCall extends Handler {
		WeakReference<Settings> outerClass;

		ReCall(Settings activity) {
			outerClass = new WeakReference<Settings>(activity);
		}

		@Override
		public void handleMessage(Message m) {
			Settings out = outerClass.get();
			if (out == null) {
				System.out.println("act被回收了");
			}
			switch (m.what) {
			case Res.Login:// 上传自己信息成功or失败
				Log.d("update location", "successfull");
				break;
			case Res.UserInfo:// 得到用户信息
				ResUserInfo userInfo = (ResUserInfo) m.obj;
				System.out.println("revieve ........" + userInfo.uinfo.size());
				Vector<RUserInfo> uinfo = userInfo.uinfo;
				for (RUserInfo info : uinfo) {
					System.out
							.println(info.latitude + "     " + info.longitude);
					UserInfo r = out.mapInfo.getUserInfo(info.uid);
					if (r != null) {
						r.setInfo(info.gid.company, info.gid.section, info.sex,
								info.nickname);
						r.setLocation(info.latitude, info.longitude);
					} else {
						r = new UserInfo(info.uid);
						r.setInfo(info.gid.company, info.gid.section, info.sex,
								info.nickname);
						r.setLocation(info.latitude, info.longitude);
						out.mapInfo.addUserInfo(r);
					}
				}
				System.out.println("now has info number : "
						+ out.mapInfo.allUsers.size());
				break;
			case Res.Logout:// 登出
				out.actMgr.trigger(AppMgr.logout);
				break;
			case Res.PushMessage:
				ResPushMessage pushMessage = (ResPushMessage) m.obj;
				out.receiveMessage(pushMessage.message);
				break;
			case Res.SendMessage:
				Log.d(LogInfo.resquest, "send message successfully");
				break;
			case Res.PushLocation:
				ResPushLocation pushLocation = (ResPushLocation) m.obj;
				out.upMapInfo(pushLocation.l);
				break;
			case Res.PushMarker:
				ResPushMarker pushMarker = (ResPushMarker) m.obj;
				MarkerInfo markerInfo = new MarkerInfo();
				markerInfo.level = pushMarker.level;
				markerInfo.markerPoint = new GeoPoint((int)(pushMarker.latitude * 1e6), (int)(pushMarker.longitude * 1e6));
				markerInfo.markerTimestamp = pushMarker.deadline;
				break;
			case -1:
				out.actMgr.trigger(AppMgr.logout);
			default:
				break;
			}
		}
	}
	ReCall handler = new ReCall(this);
	void upMapInfo(Vector<RLocation> l) {
		for (RLocation i : l) {
			UserInfo info = mapInfo.getUserInfo(i.id);
			if (info != null) {
				info.setLocation(i.latitude, i.longitude);
			} else {
				info = new UserInfo(i.id);
				info.setLocation(i.latitude, i.longitude);
				mapInfo.addUserInfo(info);
			}
		}
	}

	void receiveMessage(String msg) {
		Log.d("recieve message", msg);
		Toast toast = Toast.makeText(getApplicationContext(), msg,
				Toast.LENGTH_LONG);
		toast.show();
	}

	class LogoutStatus extends ActStatus {

		@Override
		void enter(int e) {
			System.out.println("!!!!!!!logout info send!!!!!!!!");
			transam.send(new ReqLogout(app.token, app.username,
					System.currentTimeMillis(), 2000));
		}

		@Override
		void leave(int e) {

		}

	}

	public class mySeekBarListener implements SeekBar.OnSeekBarChangeListener {
		
		private SeekBar seekBar;
		private TextView refreshrate;
	    		
		public mySeekBarListener() {
			Log.d("seek", "new");
	        seekBar = (SeekBar) Settings.this.findViewById(R.id.settings_GPSrefreshrate_bar);
	        refreshrate = (TextView) Settings.this.findViewById(R.id.settings_GPSrefreshrate);
	        seekBar.setOnSeekBarChangeListener(this);
	    }
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			if (progress == 0) progress = 1;
			refreshrate.setText(progress + "s一次更新");
			currentRate = progress;
			Log.d("seek", "cur " + progress);
		}
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {

		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (myApp) getApplication();
		appMgr = app.appMgr;
		mapInfo = app.mapInfo;
		transam = app.transam;
		transam.setHandler(handler);
		ActStatus[] r = new ActStatus[3];
		ActStatus start = r[0] = new EmptyStatus();
		ActStatus logout = r[2] = new LogoutStatus();
		actMgr = new ActMgr(appMgr, this, start, r);
		actMgr.add(start, logoutButtonPressed, logout);
		actMgr.add(logout, logoutFailed, start);
		setContentView(R.layout.activity_settings);
		radioGroup = (RadioGroup)this.findViewById(R.id.radioGroup);
		if (Main.colorMode == Main.show_by_sex) radioGroup.check(R.id.show_by_sex);
		else radioGroup.check(R.id.show_by_team);
        radioButtonListener = new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.show_by_team){
					setColorMode(show_by_team);
				}
				if (checkedId == R.id.show_by_sex){
					setColorMode(show_by_sex);
					Toast.makeText(Settings.this, "蓝色表示同性,红色表示异性", Toast.LENGTH_LONG).show();
				}
			}
		};
		radioGroup.setOnCheckedChangeListener(radioButtonListener);
        
	}

	@Override
	protected void onStart() {
		super.onStart();
		logout = (Button) findViewById(R.id.settings_btn_logout);
		subscribe = (Button) findViewById(R.id.settings_btn_subscribe);
		account = (Button) findViewById(R.id.settings_btn_updateinfo);
		initGPSrate();
		logout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				actMgr.trigger(logoutButtonPressed);
			}
		});
		subscribe.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				actMgr.trigger(AppMgr.subscribe);
			}
		});
		account.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				actMgr.trigger(AppMgr.account);
			}
		});
	}

	private void setGPSrate() {
		if (currentRate == 0) currentRate = 1;
		Main.GPSrefreshrate = currentRate;
	}
	
	private void initGPSrate() {
		currentRate = Main.GPSrefreshrate;
		TextView text1 = (TextView)