diff options
Diffstat (limited to 'client/Piztor/src')
10 files changed, 174 insertions, 97 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/AppMgr.java b/client/Piztor/src/com/macaroon/piztor/AppMgr.java index e86bf3e..99171cc 100644 --- a/client/Piztor/src/com/macaroon/piztor/AppMgr.java +++ b/client/Piztor/src/com/macaroon/piztor/AppMgr.java @@ -1,6 +1,7 @@ package com.macaroon.piztor; import java.util.HashMap; +import java.util.HashSet; import android.annotation.SuppressLint; import android.content.Intent; @@ -21,12 +22,33 @@ public class AppMgr { static Tracker tracker = null; static Thread tTransam, tGPS; // Event - final static int noToken = 101; final static int loginSuccess = 102; final static int errorToken = 103; final static int hasToken = 104; + final static int toSettings = 105; + static HashMap<Class<?>, HashMap<Integer, Class<?>>> mp; + static HashSet<PiztorAct> acts; + + static void addAct(PiztorAct act) { + if (acts == null) + acts = new HashSet<PiztorAct>(); + acts.add(act); + } + + static void removeAct(PiztorAct act) { + if (acts.contains(act)) + acts.remove(act); + else + System.out.println("Piztor has a bug!!!!"); + } + + static void exit() { + for (PiztorAct act : acts) { + act.finish(); + } + } static void setStatus(ActivityStatus st) { status = st; @@ -85,12 +107,14 @@ public class AppMgr { addStatus(InitAct.class); addStatus(Login.class); addStatus(Main.class); + addStatus(Settings.class); addTransition(InitAct.class, noToken, Login.class); addTransition(Login.class, loginSuccess, Main.class); addTransition(Main.class, errorToken, Login.class); addTransition(Settings.class, errorToken, Login.class); addTransition(InitAct.class, hasToken, Main.class); addTransition(InitAct.class, errorToken, Login.class); + addTransition(Main.class, toSettings, Settings.class); } } diff --git a/client/Piztor/src/com/macaroon/piztor/InitAct.java b/client/Piztor/src/com/macaroon/piztor/InitAct.java index 7eba1e3..fb31346 100644 --- a/client/Piztor/src/com/macaroon/piztor/InitAct.java +++ b/client/Piztor/src/com/macaroon/piztor/InitAct.java @@ -1,8 +1,6 @@ package com.macaroon.piztor; -import android.content.Intent; import android.os.Bundle; -import android.os.Handler; import android.view.Menu; public class InitAct extends PiztorAct { @@ -12,6 +10,7 @@ public class InitAct extends PiztorAct { id = "initAct"; super.onCreate(savedInstanceState); AppMgr.init(); + AppMgr.transam.setTimeOutTime(10000); setContentView(R.layout.activity_init); } @@ -25,11 +24,18 @@ public class InitAct extends PiztorAct { AppMgr.trigger(AppMgr.hasToken); } } + + @Override + protected void onDestroy() { + super.onDestroy(); + //TODO 减少频率 + + } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.init, menu); - return true; + return false; } } diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index 4ddd603..52df2c9 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -1,26 +1,33 @@ package com.macaroon.piztor; +import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Login extends PiztorAct { - - + ActMgr actMgr; Button btnLogin; EditText edtUser, edtPass; int loginButtonClick = 1, retryButtonClick = 2, loginFailed = 3; - + + @SuppressLint("HandlerLeak") Handler hand = new Handler() { @Override public void handleMessage(Message m) { System.out.println("receive what : " + m.what); + if (m.what == -1) { + ((Exception) m.obj).printStackTrace(); + actMgr.trigger(loginFailed); + return; + } if (m.what == 0) { ResLogin res = (ResLogin) m.obj; UserInfo.token = res.t; @@ -33,7 +40,7 @@ public class Login extends PiztorAct { } } }; - + class StartStatus extends ActStatus { @Override @@ -44,7 +51,7 @@ public class Login extends PiztorAct { void leave(int e) { } } - + class LoginStatus extends ActStatus { @Override @@ -53,14 +60,14 @@ public class Login extends PiztorAct { String pass = edtPass.getText().toString(); long nowtime = System.currentTimeMillis(); System.out.println(user + " : " + pass + "\n"); - AppMgr.transam.send(new ReqLogin(user, pass, nowtime, 10000)); + AppMgr.transam.send(new ReqLogin(user, pass, nowtime, 5000)); } @Override void leave(int e) { - + } - + } @Override @@ -94,7 +101,16 @@ public class Login extends PiztorAct { @Override protected void onResume() { super.onResume(); - + + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + AppMgr.exit(); + return true; + } + return super.onKeyDown(keyCode, event); } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index 573c753..fe49ab6 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -2,16 +2,16 @@ package com.macaroon.piztor; import java.util.Timer; import java.util.TimerTask; -import java.util.Vector; - import android.annotation.SuppressLint; import android.location.Location; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.widget.ImageButton; +import android.widget.ImageView; public class Main extends PiztorAct { final static int SearchButtonPress = 1; @@ -41,13 +41,14 @@ public class Main extends PiztorAct { } }; + @SuppressLint("HandlerLeak") Handler fromTransam = new Handler() { @Override public void handleMessage(Message m) { switch (m.what) { case 1: ResUpdate update = (ResUpdate) m.obj; - if (update.t == 0) + if (update.s == 0) System.out.println("update success"); else { System.out.println("update failed"); @@ -203,6 +204,8 @@ public class Main extends PiztorAct { autodate = new Timer(); AppMgr.transam.setHandler(fromTransam); setContentView(R.layout.activity_main); + ImageView view = (ImageView) findViewById(R.id.main_mapview); + view.setOnTouchListener(new MultiTouchListener()); } @Override @@ -213,25 +216,41 @@ public class Main extends PiztorAct { btnSearch = (ImageButton) findViewById(R.id.footbar_btn_search); btnSettings = (ImageButton) findViewById(R.id.footbar_btn_settings); btnFetch.setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View arg0) { - actMgr.trigger(FetchButtonPress); + // actMgr.trigger(FetchButtonPress); } }); btnFocus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - actMgr.trigger(FocuseButtonPress); + // actMgr.trigger(FocuseButtonPress); + } + }); + btnSettings.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View arg0) { + actMgr.trigger(AppMgr.toSettings); } }); - autodate.schedule(new AutoUpdate(), 0, 5000); + //autodate.schedule(new AutoUpdate(), 0, 5000); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + AppMgr.exit(); + return true; + } + return super.onKeyDown(keyCode, event); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); - return true; + return false; } -}
\ No newline at end of file +} diff --git a/client/Piztor/src/com/macaroon/piztor/MultiTouchListener.java b/client/Piztor/src/com/macaroon/piztor/MultiTouchListener.java index 2bd379d..cf4911b 100644 --- a/client/Piztor/src/com/macaroon/piztor/MultiTouchListener.java +++ b/client/Piztor/src/com/macaroon/piztor/MultiTouchListener.java @@ -1,19 +1,15 @@ package com.macaroon.piztor; -import android.app.Activity; +import android.annotation.SuppressLint; import android.graphics.Matrix; import android.graphics.PointF; -import android.os.Bundle; -import android.os.SystemClock; import android.util.FloatMath; -import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; -import android.view.GestureDetector; -import android.view.GestureDetector.SimpleOnGestureListener; import android.widget.ImageView; +@SuppressLint("FloatMath") public class MultiTouchListener implements OnTouchListener { private Matrix matrix = new Matrix(); @@ -27,9 +23,9 @@ public class MultiTouchListener implements OnTouchListener { private PointF start = new PointF(); private PointF mid = new PointF(); private float preDis = 1f; - private float d = 0f; + /*private float d = 0f; private float newRot = 0f; - private float[] values; + private float[] values;*/ @Override public boolean onTouch(View v, MotionEvent event) { diff --git a/client/Piztor/src/com/macaroon/piztor/PiztorAct.java b/client/Piztor/src/com/macaroon/piztor/PiztorAct.java index ad12b17..1093fb9 100644 --- a/client/Piztor/src/com/macaroon/piztor/PiztorAct.java +++ b/client/Piztor/src/com/macaroon/piztor/PiztorAct.java @@ -9,6 +9,7 @@ public class PiztorAct extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println(id + " on create"); + AppMgr.addAct(this); AppMgr.setStatus(AppMgr.ActivityStatus.create); AppMgr.nowAct = this; } @@ -53,6 +54,7 @@ public class PiztorAct extends Activity { @Override protected void onDestroy() { super.onDestroy(); + AppMgr.removeAct(this); System.out.println(id + " on destroy"); AppMgr.setStatus(AppMgr.ActivityStatus.destroy); } diff --git a/client/Piztor/src/com/macaroon/piztor/PiztorExcepiton.java b/client/Piztor/src/com/macaroon/piztor/PiztorExcepiton.java index 6d1861a..4d5e738 100644 --- a/client/Piztor/src/com/macaroon/piztor/PiztorExcepiton.java +++ b/client/Piztor/src/com/macaroon/piztor/PiztorExcepiton.java @@ -5,6 +5,3 @@ class PiztorExcepiton extends Exception{ } -class ClassCannotFind extends PiztorExcepiton { - //private class -} diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index 812de05..f97e397 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -4,6 +4,7 @@ import java.io.DataInputStream; import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
+import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.Vector;
@@ -14,18 +15,21 @@ import android.os.Message; public class SocketClient {
static Socket client;
- public SocketClient(String site, int port) throws UnknownHostException,
+ public SocketClient(String site, int port, int retime) throws UnknownHostException,
IOException {
try {
client = new Socket(site, port);
+ client.setSoTimeout(retime);
} catch (UnknownHostException e) {
+ e.printStackTrace();
throw e;
} catch (IOException e) {
+ e.printStackTrace();
throw e;
}
}
- public void sendMsg(Req req,Handler recall) throws IOException {
+ public int sendMsg(Req req,Handler recall) throws IOException,SocketTimeoutException {
try {
DataOutputStream out = new DataOutputStream(
client.getOutputStream());
@@ -161,10 +165,14 @@ public class SocketClient { recall.sendMessage(msg);
break;
}
+ return 0;
+ } catch (SocketTimeoutException e){
+ System.out.println("Time out!");
+ return 1;
} catch (IOException e) {
throw e;
- }
+ }
}
public void closeSocket() throws IOException{
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java index fae9252..53750d5 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -1,18 +1,16 @@ package com.macaroon.piztor;
import java.io.IOException;
-
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Timer;
-import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
-// Piztor Transmission Protocol v0.3a //
+// Piztor Transmission Protocol v0.4 //
//------------------------------------------------//
// //
@@ -51,10 +49,11 @@ import android.os.Message; public class Transam implements Runnable {
public Timer timer;
- public Timer mtimer;
public boolean running = false;
public boolean flag = true;
- public int cnt = 4;
+ public int cnt = 4; //retry times
+ public int tcnt; //current remain retry times
+ public int retime = 1000; //timeout time
Res res;
Req req;
public int p; //port
@@ -76,16 +75,21 @@ public class Transam implements Runnable { }
+ public void setTimeOutTime(int msec){
+ retime = msec;
+ }
+
+ public void setRetryTimes(int times){
+ cnt = times;
+ }
+
+
public void setHandler(Handler Recall){
recall = Recall;
reqtask.clear();
}
- public void run() { //start the main timer
- //TimerTask tmain = new Timertk();
- //mtimer = new Timer();
- //mtimer.schedule(tmain, 100, 100); //check the queue for every 100 msec
-
+ public void run() { //start the main thread
while(true){
if(running == false){
@@ -98,49 +102,49 @@ public class Transam implements Runnable { ret.what = -1;
recall.sendMessage(ret);
}
- else{ //run the request
- final thd t = new thd();
- flag = false;
- thread = new Thread(t);
- cnt = 4;
+ else{ //run the request
running = true;
- thread.start();
- timer = new Timer();
- TimerTask task = new Timertk();
- timer.schedule(task, 20000, 20000);
+ tcnt = cnt;
+ connect();
}
}
}
}
}
- class tmain extends TimerTask {
- public void run() {
-
- }
- };
+ private void connect(){
+ final thd t = new thd();
+ thread = new Thread(t);
+ thread.start();
+ }
class thd implements Runnable {
public void run() {
try {
- SocketClient client = new SocketClient(i,p);
- client.sendMsg(req,recall);
- Message msg = new Message();
- msg.what = 1;
- handler.sendMessage(msg);
- client.closeSocket();
+ SocketClient client = new SocketClient(i,p,retime);
+ int out = client.sendMsg(req,recall);
+ if(out == 0){
+ client.closeSocket();
+ running = false;
+ }
+ else {
+ client.closeSocket();
+ Message msg = new Message();
+ msg.what = 0;
+ handler.sendMessage(msg);
+ }
} catch (UnknownHostException e) {
e.printStackTrace();
Message msg = new Message();
- msg.obj = e;
msg.what = -1;
- recall.sendMessage(msg);
+ msg.obj = e;
+ handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
- msg.obj = e;
msg.what = -1;
- recall.sendMessage(msg);
+ msg.obj = e;
+ handler.sendMessage(msg);
}
}
@@ -150,40 +154,36 @@ public class Transam implements Runnable { Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
- case 1:
- flag = true;
+ case -1:
+ if (tcnt > 0) {
+ tcnt--;
+ System.out.println(tcnt);
+ connect();
+ } else if (tcnt == 0) {
+ Message m = new Message();
+ m.obj = msg.obj;
+ m.what = -1;
+ recall.sendMessage(m);
+ running = false;
+ }
break;
- case 2:
- final thd t = new thd();
- thread.interrupt();
- thread = new Thread(t);
- thread.start();
+ case 0:
+ if (tcnt > 0) {
+ tcnt--;
+ connect();
+ } else if (tcnt == 0) {
+ Message m = new Message();
+ ConnectFailedException c = new ConnectFailedException();
+ m.obj = c;
+ m.what = -1;
+ recall.sendMessage(m);
+ running = false;
+ }
break;
}
super.handleMessage(msg);
}
};
-
- class Timertk extends TimerTask {
- public void run() {
- if (flag == false && cnt > 0) {
- cnt--;
- Message m = new Message();
- m.what = 2;
- handler.sendMessage(m);
- } else if (cnt == 0) {
- Message msg = new Message();
- ConnectFailedException c = new ConnectFailedException();
- msg.obj = c;
- msg.what = -1;
- recall.sendMessage(msg);
- timer.cancel();
- } else if (flag == true) {
- timer.cancel();
- running = false;
- }
- }
- };
class ConnectFailedException extends Exception{
private static final long serialVersionUID = 101L;
@@ -200,4 +200,12 @@ public class Transam implements Runnable { }
+ class JavaHostException extends Exception{
+ private static final long serialVersionUID = 103L;
+ public JavaHostException() {
+ super();
+ }
+
+ }
+
}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/UserInfo.java b/client/Piztor/src/com/macaroon/piztor/UserInfo.java index 63f413e..9c78fc3 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -2,6 +2,7 @@ package com.macaroon.piztor; public class UserInfo { static String ip = "69.85.86.42"; +// static String ip = "192.168.1.101"; static int port = 2222; static String token = null; static String username = null; |