Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cdrag.quadroid;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.hardware.Sensor;
- import android.hardware.SensorManager;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import android.widget.Toast;
- public class quadroid extends Activity {
- private TextView introtext;
- private WifiManager wifimanager;
- private WifiInfo wifiinfo;
- private View buttonview;
- private View quadroidlogoview;
- private String dssid,cssid;
- private int state;
- private SensorManager sensormanager;
- private List<Sensor> sensors;
- private CheckBox joystickcheckbox;
- private CheckBox accelerometerckcheckbox;
- private boolean accelerometerflag = false;
- private boolean accsupp = false;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
- sensors = sensormanager.getSensorList(Sensor.TYPE_ACCELEROMETER);
- if(sensors.size() > 0) // check if the device has an accelerometer
- {
- accsupp = true; // accelerometer flag true
- }
- wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- wifiinfo = wifimanager.getConnectionInfo();
- cssid = wifiinfo.getSSID(); //check the current SSID
- dssid = "QUADROID"; //desired SSID
- state = wifimanager.getWifiState(); //check wifi current state
- switch(state){
- case 0: //wifi state disabling
- case 1: //wifi state disabled
- case 4: //wifi state unknown
- wifimanager.setWifiEnabled(true); // turn on wifi
- Toast.makeText(this, "Enabling Wifi", Toast.LENGTH_SHORT).show();
- Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_LONG).show();
- startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
- break;
- case 2: //wifi state enabling
- case 3: //wifi state enabled
- if (cssid == null){ //wifi is on, but it's not connected to any AP
- Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_SHORT).show();
- startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
- }
- else if(cssid.equals(dssid)){ //wifi is on and it's connected to the right AP
- Toast.makeText(this, "Ready to proceed", Toast.LENGTH_SHORT).show();
- }
- else{
- wifimanager.disconnect(); //wifi is on, but it's not connected to right AP
- Toast.makeText(this, "Currently AP disconnected", Toast.LENGTH_SHORT).show();
- Toast.makeText(this, "Select Quadroid AP", Toast.LENGTH_LONG).show();
- startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); // start the settings intent in order to choose the right AP
- }
- break;
- }
- quadroidlogoview = (View) findViewById(R.id.QuadroidLogoView); // initiate view
- quadroidlogoview.setBackgroundDrawable(getResources().getDrawable(R.drawable.quadroid_logo)); //set the background
- joystickcheckbox = (CheckBox) findViewById(R.id.JoystickCheckBox); //initiate checkbox
- joystickcheckbox.setChecked(true); //set to "pressed"
- joystickcheckbox.setOnClickListener(new OnClickListener() { //do something on click
- public void onClick(View v) {
- if(accelerometerflag == true){ //if clicked and if the other checkbox was clicked
- joystickcheckbox.setChecked(true); //checkbox "pressed"state
- accelerometerckcheckbox.setChecked(false); //turn the other checkbox to "unpressed" state, so there will be always one way to control
- accelerometerflag = false;
- }
- else { //if clicked and if the other checkbox wasn't clicked
- joystickcheckbox.setChecked(true); //preserve the "pressed" state
- }
- }
- });
- accelerometerckcheckbox = (CheckBox) findViewById(R.id.AccelerometerCheckBox); //initiate checkbox
- accelerometerckcheckbox.setChecked(false); //set to "unpressed"
- accelerometerckcheckbox.setOnClickListener(new OnClickListener() { //do something on click
- public void onClick(View v) {
- if (accelerometerflag == false){ //if clicked and if the other checkbox was clicked
- if (accsupp == true){ //check if the accelerometer is supported on the device
- accelerometerckcheckbox.setChecked(true); //checkbox "pressed"state
- joystickcheckbox.setChecked(false); //turn the other checkbox to "unpressed" state, so there will be always one way to control
- accelerometerflag = true;
- }
- else{ //if clicked and accelerometer is not supported
- accelerometerckcheckbox.setChecked(false);
- accelerometerflag = false; //maintain the "pressed" state
- missingaccelerometer(); //call method
- }
- }
- else { //if clicked and if the other checkbox wasn't clicked
- accelerometerckcheckbox.setChecked(true); //preserved the "pressed" state
- }
- }
- });
- introtext = (TextView) findViewById(R.id.IntroText); //initiate text to display
- introtext.setTextSize(40); //set text size
- introtext.setTextColor(Color.GREEN); //set text color
- introtext.setText("Quadroid"); //set text to display
- buttonview = (View) findViewById(R.id.ButtonView); //initiate the "start" button
- buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_1)); //set background
- buttonview.setOnTouchListener(new View.OnTouchListener() { //do something on touch
- @Override
- public boolean onTouch(View v, MotionEvent event){
- int action = event.getAction(); //get the current "touched" state
- action = action & MotionEvent.ACTION_MASK;
- switch (action){
- case MotionEvent.ACTION_DOWN: //if the finger touched the view
- case MotionEvent.ACTION_MOVE: //if the finger was moved on the view
- buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_2)); //set the current state's background
- break;
- case MotionEvent.ACTION_UP: //if the finger was lifted up from the view
- cssid = wifiinfo.getSSID(); //get one more time the current SSID
- buttonview.setBackgroundDrawable(getResources().getDrawable(R.drawable.start_1)); //set the current state's background
- if(dssid.equals(cssid)){ //check if the user is connected to right AP one more time, and only then it can proceed
- initializing(); //call method
- if (accelerometerflag == false){ //if joystick control was chosen, then start the intent with joystick
- startActivity(new Intent(quadroid.this, appmain_joystick.class));
- }
- else{ //if accelerometer control was chosen, then start the intent with acceleromemter support
- startActivity(new Intent(quadroid.this, appmain_accelerometer.class));
- }
- }
- else{ //prevent from proceed because the user is still not connected to right
- wrongap(); //call method
- }
- break;
- }
- return true;
- }
- });
- }
- @Override
- public void onBackPressed() {
- wifimanager.setWifiEnabled(false);
- quadroid.this.finish();
- }
- public void initializing(){
- Toast.makeText(this, "<> initializing <>", Toast.LENGTH_SHORT).show(); //display info
- }
- public void wrongap(){
- Toast.makeText(this, "Connect to Quadroid AP", Toast.LENGTH_SHORT).show(); //display info
- }
- public void missingaccelerometer(){
- Toast.makeText(this, "Accelerometer unsupported", Toast.LENGTH_SHORT).show(); //display info
- }
- }
Add Comment
Please, Sign In to add comment