Last active
October 8, 2016 15:53
-
-
Save pchettiy/24c91420415c79fd4e578f9d6c69a05d to your computer and use it in GitHub Desktop.
Retromania app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="spider.app.retromania"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
</activity> | |
<activity android:name=".NetworkDetailsActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 23 | |
buildToolsVersion "23.0.3" | |
defaultConfig { | |
applicationId "spider.app.retromania" | |
minSdkVersion 15 | |
targetSdkVersion 23 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(include: ['*.jar'], dir: 'libs') | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:23.4.0' | |
compile files('libs/json-20090211.jar') | |
compile files('libs/okhttp-3.4.1.jar') | |
compile files('libs/okhttp-ws-3.4.1.jar') | |
compile files('libs/okio-1.9.0.jar') | |
compile files('libs/socket.io-client-0.8.1.jar') | |
compile files('libs/engine.io-client-0.8.1.jar') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://imgur.com/a/y3NTb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spider.app.retromania; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
import java.net.URISyntaxException; | |
import java.util.EventListener; | |
import io.socket.client.IO; | |
import io.socket.client.Socket; | |
import io.socket.emitter.Emitter; | |
public class MainActivity extends AppCompatActivity implements SensorEventListener { | |
// Socket properties | |
private Socket socket; | |
private String ipAddress; | |
private String port; | |
private String fullNetAddress; | |
// Sensor data | |
private SensorManager sensorManager; | |
private Sensor accelerometer; | |
private float yValue; // tracks y-axis position of the phone tilt | |
// Layout data | |
private TextView connectionStatus; | |
private TextView accelData; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
connectionStatus = (TextView) findViewById(R.id.connStatus); | |
accelData = (TextView) findViewById(R.id.accel); | |
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
Intent intent = getIntent(); | |
ipAddress = intent.getStringExtra("ipAddress"); | |
port = intent.getStringExtra("port"); | |
fullNetAddress = "http://" + ipAddress + ":" + port; | |
connectSocket(); | |
} | |
private void connectSocket() { | |
try { | |
socket = IO.socket(fullNetAddress); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... objects) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
connectionStatus.setText("Connected"); | |
} | |
}); | |
} | |
}); | |
socket.connect(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
yValue = event.values[1]; // [x, y, z] are available values | |
accelData.setText(String.valueOf(yValue)); | |
socket.emit("app-message", yValue); | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) {} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
sensorManager.unregisterListener(this); | |
socket.disconnect(); | |
socket.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spider.app.retromania; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
public class NetworkDetailsActivity extends AppCompatActivity { | |
String ipAddress; | |
String port; | |
EditText ipAddressContainer; | |
EditText portContainer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_network_details); | |
ipAddressContainer = (EditText) findViewById(R.id.ipAddr); | |
portContainer = (EditText) findViewById(R.id.portNo); | |
} | |
public void startSocketTransfer(View view) { | |
Intent intent = new Intent(this, MainActivity.class); | |
// get the strings | |
ipAddress = ipAddressContainer.getText().toString().trim(); | |
port = portContainer.getText().toString().trim(); | |
intent.putExtra("ipAddress", (ipAddress.isEmpty())? "127.0.0.1": ipAddress); | |
intent.putExtra("port", (port.isEmpty())? "3000": port); | |
startActivity(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment