summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/Settings.java
blob: 457d916cbe0ae20f5052b75aedb04f80a1e84295 (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
package com.macaroon.piztor;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Settings extends PiztorAct {
	Button logout;
	MapInfo mapInfo;

	// Event
	final static int logoutButtonPressed = 1;
	final static int logoutFailed = 2;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message m) {
			System.out.println("!!!!!!!!!!!!!!!!!!!!!settings" +  m.what);
			switch (m.what) {
			case 1:// 上传自己信息成功or失败
				ResUpdate update = (ResUpdate) m.obj;
				if (update.s == 0)
					System.out.println("update success");
				else {
					System.out.println("update failed");
					actMgr.trigger(AppMgr.errorToken);
				}
				break;
			case 2:// 得到别人的信息
				ResLocation location = (ResLocation) m.obj;
				if (location.s == 0) {
					mapInfo.clear();
					for (Rlocation i : location.l) {
						System.out.println(i.i + " : " + i.lat + " " + i.lot);
						UserInfo info = new UserInfo(i.i);
						info.setLocation(i.lat, i.lot);
						mapInfo.addUserInfo(info);
					}
				} else {
					System.out.println("resquest for location failed!");
					actMgr.trigger(AppMgr.errorToken);
				}
				break;
			case 3:// 得到用户信息
				ResUserinfo r = (ResUserinfo) m.obj;
				if (r.s == 0) {
					System.out.println("id : " + r.uid + " sex :  " + r.sex
							+ " group : " + r.gid);
					UserInfo user = mapInfo.getUserInfo(r.uid);
					user.setInfo(r.gid, r.sex);
				} else {
					System.out.println("reqest for userInfo must be wrong!!!");
					actMgr.trigger(AppMgr.errorToken);
				}
				break;
			case 4:// 登出
				ResLogout logout = (ResLogout) m.obj;
				System.out.println("logout status" + logout.s);
				if (logout.s == 0) {
					Infomation.token = null;
					Infomation.gid = -1;
					Infomation.uid = -1;
					Infomation.username = null;
					actMgr.trigger(AppMgr.logout);
					break;
				} else {
					Toast toast = Toast.makeText(getApplicationContext(),
							"logout failed", Toast.LENGTH_LONG);
					toast.show();
					actMgr.trigger(logoutFailed);
				}
				break;
			default:
				break;
			}
		}
	};

	class StartStatus extends ActStatus {

		@Override
		void enter(int e) {
		}

		@Override
		void leave(int e) {
		}
	}

	class LogoutStatus extends ActStatus {

		@Override
		void enter(int e) {
			AppMgr.transam.send(new ReqLogout(Infomation.token,
					Infomation.username, System.currentTimeMillis(), 2000));
		}

		@Override
		void leave(int e) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActStatus[] r = new ActStatus[3];
		ActStatus start = r[0] = new StartStatus();
		ActStatus logout = r[2] = new LogoutStatus();
		actMgr = new ActMgr(this, start, r);
		actMgr.add(start, logoutButtonPressed, logout);
		actMgr.add(logout, logoutFailed, start);
		AppMgr.transam.setHandler(handler);
		mapInfo = AppMgr.mapInfo;
		setContentView(R.layout.activity_settings);
	}

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting, menu);
		return true;
	}

}