From ac55b507e9745b60d4c677ee356a470cb68266e4 Mon Sep 17 00:00:00 2001 From: Goblin911 Date: Mon, 26 Aug 2013 22:05:40 +0800 Subject: network has fixed and can reach settingsAct --- client/Piztor/AndroidManifest.xml | 4 +- client/Piztor/res/values/strings.xml | 2 +- client/Piztor/src/com/macaroon/piztor/AppMgr.java | 26 ++- client/Piztor/src/com/macaroon/piztor/InitAct.java | 12 +- client/Piztor/src/com/macaroon/piztor/Login.java | 30 +++- client/Piztor/src/com/macaroon/piztor/Main.java | 21 ++- .../com/macaroon/piztor/MultiTouchListener.java | 12 +- .../Piztor/src/com/macaroon/piztor/PiztorAct.java | 2 + .../src/com/macaroon/piztor/PiztorExcepiton.java | 3 - .../src/com/macaroon/piztor/SocketClient.java | 16 +- client/Piztor/src/com/macaroon/piztor/Transam.java | 198 +++++++++++---------- .../Piztor/src/com/macaroon/piztor/UserInfo.java | 3 +- 12 files changed, 197 insertions(+), 132 deletions(-) diff --git a/client/Piztor/AndroidManifest.xml b/client/Piztor/AndroidManifest.xml index b18d8e2..ef91844 100644 --- a/client/Piztor/AndroidManifest.xml +++ b/client/Piztor/AndroidManifest.xml @@ -31,8 +31,8 @@ android:label="@string/title_activity_main" > + android:name="com.macaroon.piztor.Settings" + android:label="@string/title_activity_settings" > diff --git a/client/Piztor/res/values/strings.xml b/client/Piztor/res/values/strings.xml index 5a5ccc4..4268ca1 100644 --- a/client/Piztor/res/values/strings.xml +++ b/client/Piztor/res/values/strings.xml @@ -16,6 +16,6 @@ Mine Login Main - Setting + Settings 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, HashMap>> mp; + static HashSet acts; + + static void addAct(PiztorAct act) { + if (acts == null) + acts = new HashSet(); + 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 cb3b182..52df2c9 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -1,28 +1,31 @@ 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(); + ((Exception) m.obj).printStackTrace(); + actMgr.trigger(loginFailed); return; } if (m.what == 0) { @@ -37,7 +40,7 @@ public class Login extends PiztorAct { } } }; - + class StartStatus extends ActStatus { @Override @@ -48,7 +51,7 @@ public class Login extends PiztorAct { void leave(int e) { } } - + class LoginStatus extends ActStatus { @Override @@ -62,9 +65,9 @@ public class Login extends PiztorAct { @Override void leave(int e) { - + } - + } @Override @@ -98,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 d5d260b..5873afe 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -2,13 +2,12 @@ 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; @@ -41,6 +40,7 @@ public class Main extends PiztorAct { } }; + @SuppressLint("HandlerLeak") Handler fromTransam = new Handler() { @Override public void handleMessage(Message m) { @@ -224,14 +224,29 @@ public class Main extends PiztorAct { actMgr.trigger(FocuseButtonPress); } }); + btnSettings.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View arg0) { + actMgr.trigger(AppMgr.toSettings); + } + }); 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 703b974..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,21 +15,22 @@ 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(1000); + 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()); int tmp = req.type; @@ -163,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 9ac6bea..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,98 +49,102 @@ import android.os.Message; public class Transam implements Runnable { public Timer timer; - public Timer mtimer; - public boolean running = false; + 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 - public String i; // ip + public int p; //port + public String i; //ip Thread thread; Handler core; - Handler recall; // recall - Queue reqtask; // request task + Handler recall; //recall + Queue reqtask ; //request task - Transam(String ip, int port, Handler Recall) { + Transam(String ip, int port,Handler Recall) { p = port; i = ip; recall = Recall; reqtask = new LinkedList(); } - - public void send(Req r) { + + public void send(Req r){ reqtask.offer(r); - + } - - public void setHandler(Handler Recall) { + + 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 - - while (true) { - if (running == false) { - - if (!reqtask.isEmpty()) { // poll the head request - req = reqtask.poll(); - if (req.time + req.alive < System.currentTimeMillis()) { // time - // out! + public void run() { //start the main thread + while(true){ + if(running == false){ + + if(!reqtask.isEmpty()){ //poll the head request + req = reqtask.poll(); + if(req.time + req.alive < System.currentTimeMillis()){ //time out! Message ret = new Message(); TimeOutException t = new TimeOutException(); ret.obj = t; 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, 2000, 2000); + 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); } } @@ -152,54 +154,58 @@ 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 = 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 { + + class ConnectFailedException extends Exception{ private static final long serialVersionUID = 101L; - - public ConnectFailedException() { - super(); - } + public ConnectFailedException() { + super(); + } } - - class TimeOutException extends Exception { + + class TimeOutException extends Exception{ private static final long serialVersionUID = 102L; - public TimeOutException() { - super(); - } - + public TimeOutException() { + super(); + } + } - + + 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 86a7a4c..9c78fc3 100644 --- a/client/Piztor/src/com/macaroon/piztor/UserInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/UserInfo.java @@ -1,7 +1,8 @@ package com.macaroon.piztor; public class UserInfo { - static String ip = "192.168.1.101"; + 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; -- cgit v1.2.3