package com.example.josh.x_control; import android.content.Context; import android.content.pm.ActivityInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private static String IPaddress; int oldX; int oldY; DisplayMetrics metrics = new DisplayMetrics(); private TextView tvXPos; private TextView tvYPos; private TextView resolution; private Button BRight; private Button BTop; private Button BLeft; private Button BBottom; //TextView leBox; @Override protected void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //4 buttons // setIP("192.168.0.14"); BRight = findViewById(R.id.BRight); BLeft = findViewById(R.id.BLeft); BTop = findViewById(R.id.BTop); BBottom = findViewById(R.id.BBottom); //X Y debugging tvYPos = findViewById(R.id.tvYPos); tvXPos = findViewById(R.id.tvXPos); //resolution of screen resolution = findViewById(R.id.resolution); getWindowManager().getDefaultDisplay().getMetrics(metrics); int xres = metrics.widthPixels; int yres = metrics.heightPixels;//setting the resolution resolution.setText ((Integer.toString(xres) + "x" + (Integer.toString(yres)))); if(isNetworkAvailable()){ JSONObject jo = new JSONObject(); JSONObject jo2 = new JSONObject(); try { jo2.put("x", xres); jo2.put("y", yres); jo.put("resolution", jo2); sendJSON sJSON = new sendJSON();//used to send json to the server sJSON.execute(jo.toString());//sending the resolution in the background to a tcp an async event and handles establishing a valid connection } catch (JSONException e) { e.printStackTrace(); } findViewById(R.id.constraint).setOnTouchListener(this); } } @Override public boolean onTouch(View view, MotionEvent event) { oldX = (int) event.getRawX(); oldY = (int) event.getRawY(); if(oldX > 65536 || oldY > 65536) { tvXPos.setText("Screen too large"); tvYPos.setText("Over max resolution supported"); } else { if(oldX < 0 || oldY < 0){ oldY = 0; oldX = 0; } if(isNetworkAvailable()) { sendJSON json = new sendJSON(); JSONObject udpJ1 = new JSONObject(); JSONObject udpJ2 = new JSONObject(); try { udpJ2.put("x",oldX); udpJ2.put("y",oldY); udpJ1.put("position", udpJ2); resolution.setText(udpJ1.toString()); json.execute(udpJ1.toString()); tvXPos.setText("X" + Integer.toString(oldX, 10)); tvYPos.setText("Y" + Integer.toString(oldY, 10)); } catch (JSONException e) { e.printStackTrace(); } } else resolution.setText("No network"); } view.invalidate(); return true; } public boolean isNetworkAvailable() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); // if no network is available networkInfo will be null // otherwise check if we are connected if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; } // public static String getIP() { // return IPaddress; //} // public void setIP(String IP) { // this.IPaddress = IP; // } // }