Android Open Source - tetris3d-controller Main Activity






From Project

Back to project page tetris3d-controller.

License

The source code is released under:

MIT License

If you think the Android project tetris3d-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.ac.brighton.uni.ab607.tetris;
/*www .j a v a 2  s  .  c o  m*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.*;

import uk.ac.brighton.uni.ab607.libs.net.*;

public class MainActivity extends Activity implements SensorEventListener {

    private TCPClient client;

    private ProgressDialog pDialog;

    private TextView ipField;
    private TextView ipLabel;
    
    private TextView labelRL, labelRR;
    
    private SensorManager sensorManager;
    //private Sensor sensor;
    
    private boolean running = true;
    
    private String dataToSend = "N";
    
    boolean RL = false, RR = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ipLabel = (TextView) findViewById(R.id.textViewIP);
        ipField = (TextView) findViewById(R.id.editTextIP);
        
        labelRL = (TextView) findViewById(R.id.textViewRL);
        labelRR = (TextView) findViewById(R.id.textViewRR);
        
        Button connect = (Button) findViewById(R.id.btn_Connect);
        connect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ipField.getText().length() < 1) // TODO:really need to be checking for valid ip
                    return;
                
                new ConnectionTask().execute();
            }
        });
        
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        //sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

        /*pDialog = new ProgressDialog(this);
        pDialog.setMessage("Connecting to TetrisV game...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();*/

        labelRR.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                RR = !RR;
                return false;
            }
        });
        
        labelRL.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                RL = !RL;
                return false;
            }
        });
    }

    /**
     * This class is used to send requests to the server
     * 
     * The requests are executed in a different thread
     * 
     */
    class ConnectionTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            while (running) {
                try {
                    if (client == null)
                        client = new TCPClient(ipField.getText().toString(), 55555,
                                new ResponseParser());
                    else
                        client.sendRawBytes(dataToSend.getBytes());
                    
                    Thread.sleep(16);
                }
                catch (Exception e) {
                } // can't do much if happens, normally won't happen
            }
            
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                // updateUI("Author: Almas Baimagambetov (ab607@uni.brighton.ac.uk)");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * Parses the replies from the server
     * 
     * The methods are called back from the server response listening thread, so
     * there is no need to call them manually
     */
    class ResponseParser implements DataPacketParser {

        @Override
        public void parseClientPacket(DataPacket packet) {} // we are clients

        @Override
        public void parseServerPacket(DataPacket packet) {
            final String data = packet.stringData;
        }
    }
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        /*runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ipLabel.setText(view.getTouchPoint());
            }
        });*/
        
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            /*String tmp = "X:" + String.format("%.3f", event.values[0])
                    + " Y:" + String.format("%.3f", event.values[1])
                    + " Z:" + String.format("%.3f", event.values[2]);*/
            
        
            
            String tmp = "N";
            
            if (event.values[0] > 2) {
                // A
                tmp += "A";
            }
            else if (event.values[0] < -2) {
                // D
                tmp += "D";
            }
            else {
                // no movement
            }
                    
                    
            
            if (event.values[1] > 2) {
                // S
                tmp += "S";
            }
            else if (event.values[1] < -2) {
                // W
                tmp += "W";
            }
            else {
                // no movement
            }
            
            if (RL) tmp += "L";
            if (RR) tmp += "R";
            
            //RL = false; RR = false;

            dataToSend = tmp;
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    @Override
    protected void onPause() {
        // unregister listener
        super.onPause();
        sensorManager.unregisterListener(this);
    }
}




Java Source Code List

uk.ac.brighton.uni.ab607.tetris.MainActivity.java