Guest User

Untitled

a guest
Oct 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. package com.cdrag.quadroid;
  2.  
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.hardware.Sensor;
  9. import android.hardware.SensorManager;
  10. import android.net.wifi.WifiInfo;
  11. import android.net.wifi.WifiManager;
  12. import android.os.Bundle;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.CheckBox;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. public class quadroid extends Activity {
  21.  
  22. private TextView introtext;
  23. private WifiManager wifimanager;
  24. private WifiInfo wifiinfo;
  25. private View buttonview;
  26. private View quadroidlogoview;
  27. private String dssid,cssid;
  28. private int state;
  29. private SensorManager sensormanager;
  30. private List<Sensor> sensors;
  31. private CheckBox joystickcheckbox;
  32. private CheckBox accelerometerckcheckbox;
  33. private boolean accelerometerflag = false;
  34. private boolean accsupp = false;
  35.  
  36. /** Called when the activity is first created. */
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.main);
  41.  
  42. sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  43. sensors = sensormanager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  44. if(sensors.size() > 0) // check if the device has an accelerometer
  45. {
  46. accsupp = true; // accelerometer flag true
  47. }
  48.  
  49. wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  50. wifiinfo = wifimanager.getConnectionInfo();
  51. cssid = wifiinfo.getSSID(); //check the current SSID
  52. dssid = "QUADROID"; //desired SSID
  53. state = wifimanager.getWifiState(); //check wifi current state
  54.  
  55. switch(state){
  56. case 0: //wifi state disabling
  57. case 1: //wifi state disabled
  58. case 4: //wifi state unknown
  59. wifimanager.setWifiEnabled(true); // turn on wifi
  60. Toast.makeText(this, "Enabling Wifi", Toast.LENGTH_SHORT).show();
  61. Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_LONG).show();
  62. startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
  63. break;
  64. case 2: //wifi state enabling
  65. case 3: //wifi state enabled
  66. if (cssid == null){ //wifi is on, but it's not connected to any AP
  67. Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_SHORT).show();
  68. startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
  69. }
  70. else if(cssid.equals(dssid)){ //wifi is on and it's connected to the right AP
  71. Toast.makeText(this, "Ready to proceed", Toast.LENGTH_SHORT).show();
  72. }
  73. else{
  74. wifimanager.disconnect(); //wifi is on, but it's not connected to right AP
  75. Toast.makeText(this, "Currently AP disconnected", Toast.LENGTH_SHORT).show();
  76. Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_LONG).show();
  77. startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
  78. }
  79. break;
  80. }
  81.  
  82. quadroidlogoview = (View) findViewById(R.id.QuadroidLogoView); // initiate view
  83. quadroidlogoview.setBackgroundDrawable(getResources().getDrawable(R.drawable.quadroid_logo)); //set the background
  84.  
  85.  
  86. joystickcheckbox = (CheckBox) findViewById(R.id.JoystickCheckBox); //initiate checkbox
  87. joystickcheckbox.setChecked(true); //set to "pressed"
  88. joystickcheckbox.setOnClickListener(new OnClickListener() { //do something on click
  89. public void onClick(View v) {
  90. if(accelerometerflag == true){ //if clicked and if the other checkbox was clicked
  91. joystickcheckbox.setChecked(true); //checkbox "pressed"state
  92. accelerometerckcheckbox.setChecked(false); //turn the other checkbox to "unpressed" state, so there will be always one way to control
  93. accelerometerflag = false;
  94. }
  95. else { //if clicked and if the other checkbox wasn't clicked
  96. joystickcheckbox.setChecked(true); //preserve the "pressed" state
  97. }
  98.  
  99. }
  100. });
  101.  
  102. accelerometerckcheckbox = (CheckBox) findViewById(R.id.AccelerometerCheckBox); //initiate checkbox
  103. accelerometerckcheckbox.setChecked(false); //set to "unpressed"
  104. accelerometerckcheckbox.setOnClickListener(new OnClickListener() { //do something on click
  105. public void onClick(View v) {
  106. if (accelerometerflag == false){ //if clicked and if the other checkbox was clicked
  107. if (accsupp == true){ //check if the accelerometer is supported on the device
  108. accelerometerckcheckbox.setChecked(true); //checkbox "pressed"state
  109. joystickcheckbox.setChecked(false); //turn the other checkbox to "unpressed" state, so there will be always one way to control
  110. accelerometerflag = true;
  111. }
  112. else{ //if clicked and accelerometer is not supported
  113. accelerometerckcheckbox.setChecked(false);
  114. accelerometerflag = false; //maintain the "pressed" state
  115. missingaccelerometer(); //call method
  116. }
  117. }
  118. else { //if clicked and if the other checkbox wasn't clicked
  119. accelerometerckcheckbox.setChecked(true); //preserved the "pressed" state
  120. }
  121. }
  122. });
  123.  
  124. introtext = (TextView) findViewById(R.id.IntroText); //initiate text to display
  125. introtext.setTextSize(40); //set text size
  126. introtext.setTextColor(Color.GREEN); //set text color
  127. introtext.setText("Quadroid"); //set text to display
  128.  
  129. buttonview = (View) findViewById(R.id.ButtonView); //initiate the "start" button
  130. buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_1)); //set background
  131. buttonview.setOnTouchListener(new View.OnTouchListener() { //do something on touch
  132. @Override
  133. public boolean onTouch(View v, MotionEvent event){
  134. int action = event.getAction(); //get the current "touched" state
  135. action = action & MotionEvent.ACTION_MASK;
  136. switch (action){
  137. case MotionEvent.ACTION_DOWN: //if the finger touched the view
  138. case MotionEvent.ACTION_MOVE: //if the finger was moved on the view
  139. buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_2)); //set the current state's background
  140. break;
  141. case MotionEvent.ACTION_UP: //if the finger was lifted up from the view
  142. cssid = wifiinfo.getSSID(); //get one more time the current SSID
  143. buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_1)); //set the current state's background
  144. if(dssid.equals(cssid)){ //check if the user is connected to right AP one more time, and only then it can proceed
  145. initializing(); //call method
  146. if (accelerometerflag == false){ //if joystick control was chosen, then start the intent with joystick
  147. startActivity(new Intent(quadroid.this, appmain_joystick.class));
  148. }
  149. else{ //if accelerometer control was chosen, then start the intent with acceleromemter support
  150. startActivity(new Intent(quadroid.this, appmain_accelerometer.class));
  151. }
  152. }
  153. else{ //prevent from proceed because the user is still not connected to right
  154. wrongap(); //call method
  155. }
  156. break;
  157. }
  158. return true;
  159. }
  160. });
  161. }
  162.  
  163. @Override
  164. public void onBackPressed() {
  165. wifimanager.setWifiEnabled(false);
  166. quadroid.this.finish();
  167. }
  168.  
  169.  
  170. public void initializing(){
  171. Toast.makeText(this, "<> initializing <>", Toast.LENGTH_SHORT).show(); //display info
  172. }
  173.  
  174. public void wrongap(){
  175. Toast.makeText(this, "Connect to Quadroid AP", Toast.LENGTH_SHORT).show(); //display info
  176. }
  177.  
  178. public void missingaccelerometer(){
  179. Toast.makeText(this, "Accelerometer unsupported", Toast.LENGTH_SHORT).show(); //display info
  180. }
  181.  
  182. }
Add Comment
Please, Sign In to add comment