Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pmeter.dmitrij999.photometer;
- //import android.support.v4.app.FragmentTransaction;
- import android.app.Activity;
- import android.app.FragmentTransaction;
- import android.app.Fragment;
- import android.os.AsyncTask;
- import android.support.v4.app.FragmentActivity;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.FrameLayout;
- import android.widget.RadioGroup;
- import android.widget.TabHost;
- import android.widget.Toast;
- import com.jjoe64.graphview.GraphView;
- public class Screen extends Activity {
- RadioGroup scrView;
- Graph_Frag graph_frag;
- Console_Frag console_frag;
- public client tcp_net;
- FragmentTransaction fTrans;
- public static String measures[];
- public static String reply;
- boolean measured = false;
- boolean connect = false;
- String ip;
- final String LOG_TAG = "myLogs";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_screen);
- graph_frag = new Graph_Frag();
- console_frag = new Console_Frag();
- scrView = (RadioGroup) findViewById(R.id.scr);
- scrView.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- fTrans = getFragmentManager().beginTransaction();
- //FrameLayout frame = (FrameLayout) findViewById(R.id.screen_frag);
- switch (checkedId) {
- case -1:
- //Toast.makeText(getApplicationContext(), "No choice", Toast.LENGTH_SHORT).show();
- break;
- case R.id.graphic:
- //fTrans.remove(console_frag);
- if (measured) fTrans.replace(R.id.screen_frag, graph_frag);
- else Toast.makeText(getApplicationContext(), "Невозможно показать график без измерений!", Toast.LENGTH_SHORT).show();
- //Toast.makeText(getApplicationContext(), "Radio0", Toast.LENGTH_SHORT).show();
- break;
- case R.id.cons:
- //Toast.makeText(getApplicationContext(), "Radio1", Toast.LENGTH_SHORT).show();
- fTrans.replace(R.id.screen_frag, console_frag);
- break;
- default:
- break;
- }
- fTrans.commit();
- }
- });
- if (savedInstanceState == null) {
- connect = false;
- }
- else {
- connect = savedInstanceState.getBoolean("connect");
- }
- Button con = (Button) findViewById(R.id.connect);
- final EditText ipaddress = (EditText) findViewById(R.id.ip);
- Button measure = (Button) findViewById(R.id.measure);
- //
- //
- connection();
- measure.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (tcp_net == null || !connect || client.getError()) Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show();
- else {
- int i = 0;
- measures = new String[16];
- for (i = 0;i<16;i++) measures[i] = "0";
- for (i = 0;i<16;i++) {
- //reply = null;
- tcp_net.sendMessage("dat" + i + "\n\r\n");
- Log.d("Send", "dat" + i + "\n\r");
- if (reply != null) {
- measures[i] = reply;
- Log.d("Meas ", " " + i + " " + reply);
- //i++;
- }
- }
- measured = true;
- }
- }
- });
- con.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ip = ipaddress.getText().toString();
- connect = !connect;
- connection();
- }
- });
- Log.d(LOG_TAG, "onCreate");
- }
- public static String getMeasure(int j) {
- if (measures != null) return measures[j];
- else return null;
- }
- int toInt(String input) {
- int out = 0;
- int len = input.length();
- for (int l=0;l<len;l++) {
- switch (input.indexOf(l)) {
- case '0':
- break;
- case '1':
- out=out+1*(10^(len-l-1));
- break;
- case '2':
- out=out+2*(10^(len-l-1));
- break;
- case '3':
- out=out+3*(10^(len-l-1));
- break;
- case '4':
- out=out+4*(10^(len-l-1));
- break;
- case '5':
- out=out+5*(10^(len-l-1));
- break;
- case '6':
- out=out+6*(10^(len-l-1));
- break;
- case '7':
- out=out+7*(10^(len-l-1));
- break;
- case '8':
- out=out+8*(10^(len-l-1));
- break;
- case '9':
- out=out+9*(10^(len-l-1));
- break;
- default:
- break;
- }
- }
- return out;
- }
- protected void connection() {
- Button con = (Button) findViewById(R.id.connect);
- connectTask cn = new connectTask();
- if (!connect) {
- con.setText(R.string.connect);
- cn.cancel(true);
- }
- else {
- cn.execute("");
- con.setText(R.string.disconnect);
- if (tcp_net != null) {
- tcp_net.sendMessage("ip?\n\r");
- }
- }
- if (client.getError()) {
- connect = false;
- con.setText(R.string.connect);
- cn.cancel(true);
- Toast.makeText(getApplicationContext(), R.string.con_error, Toast.LENGTH_SHORT).show();
- }
- }
- protected void onDestroy() {
- super.onDestroy();
- Log.d(LOG_TAG, "onDestroy");
- }
- protected void onPause() {
- super.onPause();
- Log.d(LOG_TAG, "onPause");
- }
- protected void onRestart() {
- super.onRestart();
- Log.d(LOG_TAG, "onRestart");
- }
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- Log.d(LOG_TAG, "onRestoreInstanceState");
- }
- protected void onResume() {
- super.onResume();
- Log.d(LOG_TAG, "onResume ");
- }
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putBoolean("connect", connect);
- Log.d(LOG_TAG, "onSaveInstanceState");
- }
- protected void onStart() {
- super.onStart();
- Log.d(LOG_TAG, "onStart");
- }
- protected void onStop() {
- super.onStop();
- Log.d(LOG_TAG, "onStop");
- }
- public static void putRes(String msg) {
- reply = msg;
- }
- public class connectTask extends AsyncTask<String,String,client> {
- @Override
- protected client doInBackground(String... message) {
- //we create a TCPClient object and
- tcp_net = new client(new client.OnMessageReceived() {
- @Override
- //here the messageReceived method is implemented
- public void messageReceived(String message) {
- //this method calls the onProgressUpdate
- publishProgress(message);
- }
- });
- tcp_net.Set(ip);
- tcp_net.run();
- return null;
- }
- @Override
- protected void onProgressUpdate(String... values) {
- super.onProgressUpdate(values);
- //reply = values[0];
- //Toast.makeText(getApplicationContext(), reply, Toast.LENGTH_SHORT).show();
- //in the arrayList we add the messaged received from server
- //arrayList.add(values[0]);
- // notify the adapter that the data set has changed. This means that new message received
- // from server was added to the list
- }
- @Override
- protected void onCancelled() {
- super.onCancelled();
- //tvInfo.setText("Cancel");
- //tcp_net.stopClient();
- Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show();
- Log.d(LOG_TAG, "Cancel");
- }
- }
- }
Add Comment
Please, Sign In to add comment