dmitrij999

activity

Apr 22nd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.17 KB | None | 0 0
  1. package com.pmeter.dmitrij999.photometer;
  2.  
  3.  
  4. //import android.support.v4.app.FragmentTransaction;
  5. import android.app.Activity;
  6. import android.app.FragmentTransaction;
  7. import android.app.Fragment;
  8. import android.os.AsyncTask;
  9. import android.support.v4.app.FragmentActivity;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.KeyEvent;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.FrameLayout;
  18. import android.widget.RadioGroup;
  19. import android.widget.TabHost;
  20. import android.widget.Toast;
  21.  
  22. import com.jjoe64.graphview.GraphView;
  23.  
  24.  
  25.  
  26. public class Screen extends Activity {
  27.  
  28.     RadioGroup scrView;
  29.     Graph_Frag graph_frag;
  30.     Console_Frag console_frag;
  31.     public client tcp_net;
  32.     FragmentTransaction fTrans;
  33.     public static String measures[];
  34.     public static String reply;
  35.     boolean measured = false;
  36.     boolean connect = false;
  37.     String ip;
  38.  
  39.     final String LOG_TAG = "myLogs";
  40.     @Override
  41.     protected void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.  
  44.  
  45.         setContentView(R.layout.activity_screen);
  46.         graph_frag = new Graph_Frag();
  47.         console_frag = new Console_Frag();
  48.         scrView = (RadioGroup) findViewById(R.id.scr);
  49.         scrView.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  50.  
  51.             @Override
  52.             public void onCheckedChanged(RadioGroup group, int checkedId) {
  53.                 // TODO Auto-generated method stub
  54.                 fTrans = getFragmentManager().beginTransaction();
  55.                 //FrameLayout frame = (FrameLayout) findViewById(R.id.screen_frag);
  56.                 switch (checkedId) {
  57.                     case -1:
  58.                         //Toast.makeText(getApplicationContext(), "No choice", Toast.LENGTH_SHORT).show();
  59.                         break;
  60.                     case R.id.graphic:
  61.  
  62.                         //fTrans.remove(console_frag);
  63.                         if (measured) fTrans.replace(R.id.screen_frag, graph_frag);
  64.                         else Toast.makeText(getApplicationContext(), "Невозможно показать график без измерений!", Toast.LENGTH_SHORT).show();
  65.                         //Toast.makeText(getApplicationContext(), "Radio0", Toast.LENGTH_SHORT).show();
  66.                         break;
  67.                     case R.id.cons:
  68.                         //Toast.makeText(getApplicationContext(), "Radio1", Toast.LENGTH_SHORT).show();
  69.  
  70.                         fTrans.replace(R.id.screen_frag, console_frag);
  71.  
  72.                         break;
  73.                     default:
  74.                         break;
  75.                 }
  76.                 fTrans.commit();
  77.             }
  78.         });
  79.         if (savedInstanceState == null) {
  80.             connect = false;
  81.         }
  82.         else {
  83.             connect = savedInstanceState.getBoolean("connect");
  84.         }
  85.         Button con = (Button) findViewById(R.id.connect);
  86.         final EditText ipaddress = (EditText) findViewById(R.id.ip);
  87.         Button measure = (Button) findViewById(R.id.measure);
  88.  
  89.  
  90.  
  91.  
  92.         //
  93.         //
  94.         connection();
  95.  
  96.         measure.setOnClickListener(new View.OnClickListener() {
  97.             @Override
  98.             public void onClick(View v) {
  99.                 if (tcp_net == null || !connect || client.getError())  Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show();
  100.                 else {
  101.                     int i = 0;
  102.                     measures = new String[16];
  103.                     for (i = 0;i<16;i++) measures[i] = "0";
  104.                     for (i = 0;i<16;i++) {
  105.  
  106.                         //reply = null;
  107.                         tcp_net.sendMessage("dat" + i + "\n\r\n");
  108.                         Log.d("Send", "dat" + i + "\n\r");
  109.                         if (reply != null) {
  110.                             measures[i] = reply;
  111.                             Log.d("Meas ", " " + i + " " + reply);
  112.                             //i++;
  113.                         }
  114.                     }
  115.  
  116.                     measured = true;
  117.                 }
  118.             }
  119.         });
  120.         con.setOnClickListener(new View.OnClickListener() {
  121.             @Override
  122.             public void onClick(View v) {
  123.                 ip = ipaddress.getText().toString();
  124.                 connect = !connect;
  125.                 connection();
  126.             }
  127.         });
  128.         Log.d(LOG_TAG, "onCreate");
  129.     }
  130.  
  131.     public static String getMeasure(int j) {
  132.         if (measures != null) return measures[j];
  133.         else return null;
  134.     }
  135.  
  136.  
  137.     int toInt(String input) {
  138.         int out = 0;
  139.         int len = input.length();
  140.         for (int l=0;l<len;l++) {
  141.             switch (input.indexOf(l)) {
  142.                 case '0':
  143.                     break;
  144.                 case '1':
  145.                     out=out+1*(10^(len-l-1));
  146.                     break;
  147.                 case '2':
  148.                     out=out+2*(10^(len-l-1));
  149.                     break;
  150.                 case '3':
  151.                     out=out+3*(10^(len-l-1));
  152.                     break;
  153.                 case '4':
  154.                     out=out+4*(10^(len-l-1));
  155.                     break;
  156.                 case '5':
  157.                     out=out+5*(10^(len-l-1));
  158.                     break;
  159.                 case '6':
  160.                     out=out+6*(10^(len-l-1));
  161.                     break;
  162.                 case '7':
  163.                     out=out+7*(10^(len-l-1));
  164.                     break;
  165.                 case '8':
  166.                     out=out+8*(10^(len-l-1));
  167.                     break;
  168.                 case '9':
  169.                     out=out+9*(10^(len-l-1));
  170.                     break;
  171.                 default:
  172.                     break;
  173.             }
  174.         }
  175.         return out;
  176.     }
  177.     protected void connection() {
  178.         Button con = (Button) findViewById(R.id.connect);
  179.         connectTask cn = new connectTask();
  180.         if (!connect) {
  181.             con.setText(R.string.connect);
  182.             cn.cancel(true);
  183.  
  184.         }
  185.         else {
  186.             cn.execute("");
  187.             con.setText(R.string.disconnect);
  188.  
  189.             if (tcp_net != null) {
  190.                 tcp_net.sendMessage("ip?\n\r");
  191.             }
  192.  
  193.         }
  194.         if (client.getError()) {
  195.             connect = false;
  196.             con.setText(R.string.connect);
  197.             cn.cancel(true);
  198.             Toast.makeText(getApplicationContext(), R.string.con_error, Toast.LENGTH_SHORT).show();
  199.         }
  200.  
  201.     }
  202.     protected void onDestroy() {
  203.         super.onDestroy();
  204.         Log.d(LOG_TAG, "onDestroy");
  205.     }
  206.  
  207.     protected void onPause() {
  208.         super.onPause();
  209.         Log.d(LOG_TAG, "onPause");
  210.     }
  211.  
  212.     protected void onRestart() {
  213.         super.onRestart();
  214.         Log.d(LOG_TAG, "onRestart");
  215.     }
  216.  
  217.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  218.         super.onRestoreInstanceState(savedInstanceState);
  219.         Log.d(LOG_TAG, "onRestoreInstanceState");
  220.     }
  221.  
  222.     protected void onResume() {
  223.         super.onResume();
  224.         Log.d(LOG_TAG, "onResume ");
  225.     }
  226.  
  227.     protected void onSaveInstanceState(Bundle outState) {
  228.         super.onSaveInstanceState(outState);
  229.         outState.putBoolean("connect", connect);
  230.         Log.d(LOG_TAG, "onSaveInstanceState");
  231.     }
  232.  
  233.     protected void onStart() {
  234.         super.onStart();
  235.         Log.d(LOG_TAG, "onStart");
  236.     }
  237.  
  238.     protected void onStop() {
  239.         super.onStop();
  240.         Log.d(LOG_TAG, "onStop");
  241.     }
  242.  
  243.     public static void putRes(String msg) {
  244.         reply = msg;
  245.     }
  246.  
  247.     public class connectTask extends AsyncTask<String,String,client> {
  248.  
  249.         @Override
  250.         protected client doInBackground(String... message) {
  251.  
  252.             //we create a TCPClient object and
  253.             tcp_net = new client(new client.OnMessageReceived() {
  254.                 @Override
  255.                 //here the messageReceived method is implemented
  256.                 public void messageReceived(String message) {
  257.                     //this method calls the onProgressUpdate
  258.                     publishProgress(message);
  259.                 }
  260.             });
  261.             tcp_net.Set(ip);
  262.             tcp_net.run();
  263.  
  264.             return null;
  265.         }
  266.  
  267.         @Override
  268.         protected void onProgressUpdate(String... values) {
  269.             super.onProgressUpdate(values);
  270.             //reply = values[0];
  271.             //Toast.makeText(getApplicationContext(), reply, Toast.LENGTH_SHORT).show();
  272.  
  273.  
  274.             //in the arrayList we add the messaged received from server
  275.             //arrayList.add(values[0]);
  276.             // notify the adapter that the data set has changed. This means that new message received
  277.             // from server was added to the list
  278.  
  279.         }
  280.  
  281.         @Override
  282.         protected void onCancelled() {
  283.             super.onCancelled();
  284.             //tvInfo.setText("Cancel");
  285.  
  286.             //tcp_net.stopClient();
  287.             Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show();
  288.             Log.d(LOG_TAG, "Cancel");
  289.  
  290.         }
  291.  
  292.     }
  293. }
Add Comment
Please, Sign In to add comment