packagesg131971.mini2440;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.http.HttpResponse;
importorg.apache.http.NameValuePair;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
importAndroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.view.Gravity;
importandroid.view.KeyEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
importandroid.widget.Toast;
publicclassMini2440ActivityextendsActivity {
privatestaticintConnect_flag =0;
privatestaticintAutoRefresh_flag =0;
privatestaticintLED_flag[] =newint[4];
privateBitmap m_Bitmap;
privateImageView myImageView;
privateHandler m_Handler =newHandler();
privateString Board_IP;
privateEditText myEditText;
privateButton myButtonConnect;
privateButton myButton0;
privateButton myButton1;
privateButton myButton2;
privateButton myButton3;
privateButton myButtonStatus;
privateButton autoButton;
privateButton manualButton;
/** Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText = (EditText) findViewById(R.id.editText);
myButtonConnect = (Button) findViewById(R.id.myButtonConnect);
myButtonConnect.setOnClickListener(newConnectListener());
myImageView = (ImageView) findViewById(R.id.imageView);
autoButton = (Button) findViewById(R.id.autoButton);
manualButton = (Button) findViewById(R.id.manualButton);
myButton0 = (Button) findViewById(R.id.myButton0);
myButton1 = (Button) findViewById(R.id.myButton1);
myButton2 = (Button) findViewById(R.id.myButton2);
myButton3 = (Button) findViewById(R.id.myButton3);
myButtonStatus = (Button) findViewById(R.id.myButtonStatus);
StopService();
}
publicbooleanonKeyDown(intkeyCode, KeyEvent msg) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
System.exit(0);
}
returnfalse;
}
privatevoidStartService() {
// TODO Auto-generated method stub
m_Handler.postDelayed(m_RefreshImage,0);
myButton0.setOnClickListener(newLED0Listener());
myButton1.setOnClickListener(newLED1Listener());
myButton2.setOnClickListener(newLED2Listener());
myButton3.setOnClickListener(newLED3Listener());
myButtonStatus.setOnClickListener(newStatusListener());
autoButton.setOnClickListener(newAutoRefresh());
manualButton.setOnClickListener(newManualRefresh());
}
privatevoidStopService() {
// TODO Auto-generated method stub
m_Handler.removeCallbacks(m_RefreshImage);
myImageView.setImageResource(R.drawable.first);
myButton0.setOnClickListener(newDefaultListener());
myButton1.setOnClickListener(newDefaultListener());
myButton2.setOnClickListener(newDefaultListener());
myButton3.setOnClickListener(newDefaultListener());
myButtonStatus.setOnClickListener(newDefaultListener());
autoButton.setOnClickListener(newDefaultListener());
manualButton.setOnClickListener(newDefaultListener());
}
publicclassConnectListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
Board_IP = myEditText.getText().toString();
if(Connect_flag ==0) {
Connect_flag =1;
StartService();
myButtonConnect.setText("Disconnect");
showMessage("已连接:"+ Board_IP);
}else{
Connect_flag =0;
AutoRefresh_flag =0;
StopService();
myButtonConnect.setText("Connect");
showMessage("已断开:"+ Board_IP);
}
}
}
publicclassDefaultListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
showMessage("请先连接ARM板!");
}
}
publicclassManualRefreshimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
if(AutoRefresh_flag ==1)
AutoRefresh_flag =0;
m_Handler.postDelayed(m_RefreshImage,0);
showMessage("手动刷新成功!");
}
}
publicclassAutoRefreshimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
if(AutoRefresh_flag ==0)
AutoRefresh_flag =1;
m_Handler.postDelayed(m_RefreshImage,0);
showMessage("自动刷新设置成功!");
}
}
privateRunnable m_RefreshImage =newRunnable() {
publicvoidrun() {
// TODO Auto-generated method stub
RefreshImage();
if(AutoRefresh_flag ==1)
m_Handler.postDelayed(m_RefreshImage,100);
else
m_Handler.removeCallbacks(m_RefreshImage);
}
privatevoidRefreshImage() {
// TODO Auto-generated method stub
try{
URL m_URL =newURL("http://"+ Board_IP
+":8080/?action=snapshot");
URLConnection m_URL_Connection = m_URL.openConnection();
m_URL_Connection.connect();
InputStream m_InputStream = m_URL_Connection.getInputStream();
m_Bitmap = BitmapFactory.decodeStream(m_InputStream);
myImageView.setImageBitmap(m_Bitmap);
m_InputStream.close();
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
publicclassLED0ListenerimplementsOnClickListener {
publicvoidonClick(View arg0) {
// TODO Auto-generated method stub
LED_flag[0]++;
if(LED_flag[0] %2==1) {
HttpSendCmd("58400F01");
showMessage("LED0已打开!");
}else{
HttpSendCmd("58400F00");
showMessage("LED0已关闭!");
}
}
}
publicclassLED1ListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
LED_flag[1]++;
if(LED_flag[1] %2==1) {
HttpSendCmd("58401F01");
showMessage("LED1已打开!");
}else{
HttpSendCmd("58401F00");
showMessage("LED1已关闭!");
}
}
}
publicclassLED2ListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
LED_flag[2]++;
if(LED_flag[2] %2==1) {
HttpSendCmd("58402F01");
showMessage("LED2已打开!");
}else{
HttpSendCmd("58402F00");
showMessage("LED2已关闭!");
}
}
}
publicclassLED3ListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
LED_flag[3]++;
if(LED_flag[3] %2==1) {
HttpSendCmd("58403F01");
showMessage("LED3已打开!");
}else{
HttpSendCmd("58403F00");
showMessage("LED3已关闭!");
}
}
}
publicclassStatusListenerimplementsOnClickListener {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
String display_info ="";
// 以后此处可以通过发送http请求查询硬件状态
HttpSendCmd("5840FFFF");
if(LED_flag[0] %2==1)
display_info +="LED0:开";
else
display_info +="LED0:关";
display_info +=" ";
if(LED_flag[1] %2==1)
display_info +="LED1:开";
else
display_info +="LED1:关";
display_info +=" ";
if(LED_flag[2] %2==1)
display_info +="LED2:开";
else
display_info +="LED2:关";
display_info +=" ";
if(LED_flag[3] %2==1)
display_info +="LED3:开";
else
display_info +="LED3:关";
showMessage(display_info);
}
}
publicvoidHttpSendCmd(String cmd) {
// TODO Auto-generated method stub
HttpClient httpclient =newDefaultHttpClient();
HttpPost httppost =newHttpPost("http://"+ Board_IP
+"/mjpg-streamer/post_data.php");
try{
List nameValuePairs =newArrayList(3);
nameValuePairs.add(newBasicNameValuePair("user","root"));
nameValuePairs.add(newBasicNameValuePair("passwd","shiguang"));
nameValuePairs.add(newBasicNameValuePair("para", cmd));
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String tmpString = EntityUtils.toString(response.getEntity());
System.out.println(tmpString);
}catch(ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoidshowMessage(String str) {
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,0,550);
toast.show();
}
}