Commit bdd0ce76 authored by Harshith Goka's avatar Harshith Goka

ArrayOutOfBound , Last resort

parent 72299da0
......@@ -13,13 +13,18 @@ public class Constants {
public static int READ_STATUS_OK = 0;
public static int READ_STATUS_NOT_OK = 1;
public static String MUSIC_PLAYER = "Music Player";
public static String PLAY_PAUSE = "Play/Pause";
public static String OK_GOOGLE = "Ok Google";
public static String OPEN_CAMERA = "Camera";
public static String GOOGLE_NOW = "GoogleApp";
public static String CAMERA_CLICK = "CameraClick";
public static String VOLUME_UP="VolumeUp";
public static String VOLUME_DOWN="VolumeDown";
public static String VOLUME_CONTROL="Volume Control";
public static String END_CALL="EndCall";
public static String HOME="Home";
public static String BACK = "Back";
public static int MODE_NORMAL = 0;
public static int MODE_VOLUME = 255;
}
......@@ -4,13 +4,13 @@ package com.example.harshith.ddc;
public class Gesture{
public int dataPoints = 50;
public int sensors = 10;
public int sensors = 11;
public int adcLevels = 2;
public int adcUpper = 0;
public int adcLower = 100;
public int mpuLevels = 3;
public int mpuUpper = 18000;
public int mpuLower = -18000;
public int mpuLevels = 100;
public int mpuUpper = 50;
public int mpuLower = -50;
public Gesture(){}
......
......@@ -66,7 +66,7 @@ public class MainActivity extends AppCompatActivity {
checkBTState();
if(checkSelfPermission(android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED){
requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},1);
requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE,"android.permission.READ_EXTERNAL_STORAGE"},1);
}
mPairedDevicesArrayAdapter.clear();
......
package com.example.harshith.ddc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
/**
* Created by harshith on 18/6/16.
*/
public class ProcessService extends Service {
@Override
public void onCreate() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
package com.example.harshith.ddc;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
/**
* Created by harshith on 18/6/16.
*/
public class ProcessThread extends Thread{
int[] liveReadings;
Handler handler;
GlobalClass globalClass;
public ProcessThread(GlobalClass globalClass) {
liveReadings = null;
this.globalClass = globalClass;
}
@Override
public void run() {
Looper.prepare();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
liveReadings = (int[]) msg.obj;
L.m(liveReadings.toString());
}
};
globalClass.setProcessHandler(handler);
Looper.loop();
while (true){
if(liveReadings != null){
}
}
}
}
......@@ -4,6 +4,7 @@ import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
......@@ -15,6 +16,7 @@ import java.io.InputStream;
*/
public class ReceiveDataThread extends Thread {
int mode;
private BluetoothSocket bluetoothSocket;
private InputStream inputStream;
Handler handler;
......@@ -25,12 +27,12 @@ public class ReceiveDataThread extends Thread {
int[] readings;
Gesture []a;
public ReceiveDataThread(BluetoothSocket bluetoothSocket, Handler handler,GlobalClass globalClass) {
public ReceiveDataThread(BluetoothSocket bluetoothSocket, Handler handler,GlobalClass globalClass,int mode) {
this.bluetoothSocket = bluetoothSocket;
this.handler = handler;
this.globalClass = globalClass;
stringBuilder = new StringBuilder();
this.mode = mode;
a = new Gesture[8];
for (int i = 0; i<8; i++) {
a[i] = new StaticGesture();
......@@ -38,36 +40,36 @@ public class ReceiveDataThread extends Thread {
int[]hand; int q=0;
q=0; hand = new int [] {0,1,1,0,8,8,8,8,8,8};
// neutral
q=0; hand = new int [] {0,0,1,1,0,8,8,8,8,8,8};
// Music Player
a[q].updateFrame(hand);
q=1; hand = new int [] {1,1,1,1,8,8,8,8,1,8};
// voice search
q=1; hand = new int [] {0,0,1,1,1,8,8,8,8,8,8};
// Volume
a[q].updateFrame(hand);
q=2; hand = new int [] {0,1,1,1,8,8,8,8,1,8};
// tap
q=2; hand = new int [] {1,0,1,1,1,8,8,8,8,1,8};
// OK Google
a[q].updateFrame(hand);
q=3; hand = new int [] {0,1,1,1,8,8,8,8,1,8};
// call
q=3; hand = new int [] {0,1,1,1,0,8,8,8,8,1,8};
// End Call
a[q].updateFrame(hand);
q=4; hand = new int [] {0,0,1,1,8,8,8,-1,8,8};
q=4; hand = new int [] {0,0,0,1,1,8,8,8,-1,8,8};
// cam open
a[q].updateFrame(hand);
q=5; hand = new int [] {0,0,0,1,8,8,8,-1,8,8};
q=5; hand = new int [] {0,1,1,1,1,8,8,8,8,1,8};
// cam click
a[q].updateFrame(hand);
q=6; hand = new int [] {1,0,1,1,8,8,8,8,1,8};
// music open
q=6; hand = new int [] {1,0,0,1,1,8,8,8,8,1,8};
// Music Play
a[q].updateFrame(hand);
q=7; hand = new int [] {0,0,1,1,8,8,8,8,1,8};
// music play
q=7; hand = new int [] {1,0,0,1,0,8,8,8,8,1,8};
// Home
a[q].updateFrame(hand);
}
......@@ -86,28 +88,23 @@ public class ReceiveDataThread extends Thread {
byte[] buffer = new byte[64];
int bytes = -1;
// ProcessThread processThread = new ProcessThread(globalClass);
// processThread.start();
//
// processHandler = globalClass.getProcessHandler();
Live staticLive = new Live();
int no = 50;
//StaticLive.print();
int count = 0;
int gestActive = -1; boolean stay = true;
while(true) {
if(mode == Constants.MODE_NORMAL) {
while (true) {
bytes = inputStream.read(buffer);
readStatus = Constants.READ_STATUS_OK;
String readMessage = new String(buffer,0,bytes);
String readMessage = new String(buffer, 0, bytes);
stringBuilder.append(readMessage);
int endOfLineIndex = stringBuilder.indexOf("~");
if (endOfLineIndex > 0) {
int startOfLineIndex = stringBuilder.indexOf("#");
if (startOfLineIndex > endOfLineIndex || startOfLineIndex == -1) {
startOfLineIndex = 0;
}
else if (startOfLineIndex == 0) {
} else if (startOfLineIndex == 0) {
startOfLineIndex = 1;
}
String dataIn = stringBuilder.substring(startOfLineIndex, endOfLineIndex);
......@@ -119,8 +116,7 @@ public class ReceiveDataThread extends Thread {
readings[i] = Integer.valueOf(readingStrings[i]);
} catch (NumberFormatException e) {
}
catch (NullPointerException e) {
} catch (NullPointerException e) {
}
}
......@@ -131,46 +127,41 @@ public class ReceiveDataThread extends Thread {
L.m(testConvert);
stringBuilder.delete(0, endOfLineIndex + 2);
if(count == no){
Log.d("Gesture","Gesture " + gestActive + " is activated");
if(gestActive == 0){
if (count == no) {
Log.d("Gesture", "Gesture " + gestActive + " is activated");
mode = Constants.MODE_NORMAL;
if (gestActive == 0) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.OPEN_CAMERA).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.MUSIC_PLAYER).sendToTarget();
Looper.loop();
}
else if(gestActive == 1){
} else if (gestActive == 1) {
mode = Constants.MODE_VOLUME;
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.OK_GOOGLE).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.VOLUME_CONTROL).sendToTarget();
Looper.loop();
}
else if(gestActive == 2){
} else if (gestActive == 2) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.GOOGLE_NOW).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.OK_GOOGLE).sendToTarget();
Looper.loop();
}
else if(gestActive == 3){
} else if (gestActive == 3) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.PLAY_PAUSE).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.PLAY_PAUSE).sendToTarget();
Looper.loop();
}
else if(gestActive == 4){
} else if (gestActive == 4) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.CAMERA_CLICK).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.BACK).sendToTarget();
Looper.loop();
}
else if(gestActive == 5){
} else if (gestActive == 5) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.VOLUME_UP).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.OPEN_CAMERA).sendToTarget();
Looper.loop();
}
else if(gestActive == 6){
} else if (gestActive == 6) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.VOLUME_DOWN).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.CAMERA_CLICK).sendToTarget();
Looper.loop();
}
else if(gestActive == 7){
} else if (gestActive == 7) {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,0,Constants.END_CALL).sendToTarget();
handler.obtainMessage(Constants.READ_STATUS, readStatus, mode, Constants.HOME).sendToTarget();
Looper.loop();
}
......@@ -179,25 +170,77 @@ public class ReceiveDataThread extends Thread {
//if( str.equals("n") ) { stay = false; break; }
}
if(count<no){
if(readings.length == 10) {
if (count < no) {
if (readings.length >= 11) {
staticLive.update(readings);
}
}
for (int i=0; i<8; i++) {
if(a[i].isInFrame(staticLive)){
if(gestActive == i){
count++; break;
for (int i = 0; i < 8; i++) {
if (a[i].isInFrame(staticLive)) {
if (gestActive == i) {
count++;
break;
} else {
gestActive = i;
count = 1;
break;
}
else{
gestActive = i; count = 1; break;
}
}
}
}
}
else if(mode == Constants.MODE_VOLUME){
while (true) {
bytes = inputStream.read(buffer);
readStatus = Constants.READ_STATUS_OK;
String readMessage = new String(buffer, 0, bytes);
stringBuilder.append(readMessage);
int endOfLineIndex = stringBuilder.indexOf("~");
if (endOfLineIndex > 0) {
int startOfLineIndex = stringBuilder.indexOf("#");
if (startOfLineIndex > endOfLineIndex || startOfLineIndex == -1) {
startOfLineIndex = 0;
} else if (startOfLineIndex == 0) {
startOfLineIndex = 1;
}
String dataIn = stringBuilder.substring(startOfLineIndex, endOfLineIndex);
String[] readingStrings = dataIn.split("\\+");
readings = new int[readingStrings.length];
for (int i = 0; i != readingStrings.length; i++) {
try {
readings[i] = Integer.valueOf(readingStrings[i]);
} catch (NumberFormatException e) {
} catch (NullPointerException e) {
}
}
String testConvert = "";
for (int reading : readings) {
testConvert += " " + reading;
}
L.m(testConvert);
stringBuilder.delete(0, endOfLineIndex + 2);
if(readings.length == 11){
staticLive.update(readings);
}
if(!a[1].isInFrame(staticLive)){
mode = Constants.MODE_NORMAL;
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,mode,null).sendToTarget();
Looper.loop();
}
else {
Looper.prepare();
handler.obtainMessage(Constants.READ_STATUS,readStatus,mode,readings[8]).sendToTarget();
Looper.loop();
}
}
}
}
}
catch (IOException e){
......
......@@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
......@@ -19,10 +20,13 @@ import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.CircularArray;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;
import static android.content.ContentValues.TAG;
......@@ -48,7 +52,7 @@ public class ReceiveService extends Service {
if(message.arg1 == Constants.CONNECTION_STATUS_OK){
Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
bluetoothSocket = (BluetoothSocket) message.obj;
receiveDataThread = new ReceiveDataThread((BluetoothSocket) message.obj,handler,globalClass);
receiveDataThread = new ReceiveDataThread((BluetoothSocket) message.obj,handler,globalClass, Constants.MODE_NORMAL);
receiveDataThread.start();
}
else if(message.arg1 == Constants.CONNECTION_STATUS_NOT_CONNECTED){
......@@ -59,36 +63,53 @@ public class ReceiveService extends Service {
if (message.arg1 == Constants.READ_STATUS_OK) {
if (((String) message.obj).equals(Constants.OPEN_CAMERA)) {
openCamera();
resumeReading();
L.s(getBaseContext(), "Camera Open");
resumeReading(message.arg2);
} else if (message.obj.equals(Constants.OK_GOOGLE)) {
okGoogle();
L.s(getBaseContext(), "Ok Google");
resumeReading();
resumeReading(message.arg2);
} else if (message.obj.equals(Constants.GOOGLE_NOW)) {
googleNow();
resumeReading();
resumeReading(message.arg2);
} else if (message.obj.equals(Constants.PLAY_PAUSE)) {
audioPlayPause();
resumeReading();
L.s(getBaseContext(), "Play/Pause");
resumeReading(message.arg2);
} else if (message.obj.equals(Constants.CAMERA_CLICK)) {
CameraClick();
resumeReading();
} else if (message.obj.equals(Constants.VOLUME_UP)) {
VolumeUp();
resumeReading();
} else if (message.obj.equals(Constants.VOLUME_DOWN)) {
VolumeDown();
resumeReading();
} else if (message.obj.equals(Constants.END_CALL)) {
EndCall();
resumeReading();
cameraClick();
L.s(getBaseContext(), "Picture Taken");
resumeReading(message.arg2);
}
else if (message.obj.equals(Constants.END_CALL)) {
endCall();
L.s(getBaseContext(), "Call Ended");
resumeReading(message.arg2);
}
else if(message.obj.equals(Constants.MUSIC_PLAYER)){
openMusicPlayer();
L.s(getBaseContext(), "Music Player");
resumeReading(message.arg2);
}
else if(message.obj.equals(Constants.HOME)){
home();
L.s(getBaseContext(), "Home");
resumeReading(message.arg2);
}
else if(message.obj.equals(Constants.BACK)){
back();
L.s(getBaseContext(), "Back");
resumeReading(message.arg2);
}
else if(message.obj.equals(Constants.VOLUME_CONTROL)){
updateVolume((int) message.obj);
}
}
else if(message.arg1 == Constants.READ_STATUS_NOT_OK){
L.s(getBaseContext(),"Domini Disconnected");
}
}
}
}
};
}
@Override
......@@ -115,6 +136,25 @@ public class ReceiveService extends Service {
}
public void updateVolume(int roll){
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int volume = ((roll + 50)/100)*maxVolume;
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volume,AudioManager.FLAG_SHOW_UI);
}
public void home(){
inputKeyEvent("" + KeyEvent.KEYCODE_HOME);
}
public void back(){
inputKeyEvent("" + KeyEvent.KEYCODE_BACK);
}
public void nowOnTap(){
inputLongPressKeyEvent("" + KeyEvent.KEYCODE_HOME);
}
public void openCamera(){
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
......@@ -131,6 +171,38 @@ public class ReceiveService extends Service {
} catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
}
public void cameraClick(){
inputKeyEvent("" + KeyEvent.KEYCODE_CAMERA);
}
public void gallery(){
if(checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") == PackageManager.PERMISSION_DENIED){
L.s(getBaseContext(),"Please grant Permission to read storage data");
}
else {
// Get last taken photo
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
};
final Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
// Open in Gallery
if (cursor.moveToFirst()) {
String imageLocation = cursor.getString(1);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + imageLocation), "image/*");
startActivity(intent);
}
}
}
public void dialPhoneNumber(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber));
......@@ -139,8 +211,8 @@ public class ReceiveService extends Service {
}
}
public void resumeReading(){
receiveDataThread = new ReceiveDataThread(bluetoothSocket,handler,globalClass);
public void resumeReading(int mode){
receiveDataThread = new ReceiveDataThread(bluetoothSocket,handler,globalClass,mode);
receiveDataThread.start();
}
......@@ -156,6 +228,15 @@ public class ReceiveService extends Service {
startActivity(intent);
}
public void openMusicPlayer(){
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.music", "com.android.music.MusicBrowserActivity");
intent.setComponent(comp);
startActivity(intent);
}
public void audioPlayPause(){
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
......@@ -164,65 +245,61 @@ public class ReceiveService extends Service {
audioManager.dispatchMediaKeyEvent(upEvent);
}
public void CameraClick(){
Thread t= new Thread(new Runnable() {
@Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_CAMERA);
}
catch (Exception e){
}
}
});
t.start();
public void nextSong(){
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
audioManager.dispatchMediaKeyEvent(downEvent);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_NEXT);
audioManager.dispatchMediaKeyEvent(upEvent);
}
public void VolumeUp(){
Thread t= new Thread(new Runnable() {
@Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_UP);
public void previousSong(){
AudioManager audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
audioManager.dispatchMediaKeyEvent(downEvent);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PREVIOUS);
audioManager.dispatchMediaKeyEvent(upEvent);
}
catch (Exception e){
public void endCall(){
inputKeyEvent(KeyEvent.KEYCODE_ENDCALL+"");
}
}
});
t.start();
}
public void VolumeDown(){
Thread t= new Thread(new Runnable() {
public static void inputKeyEvent(final String keyCodeString) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int keyCode = Integer.parseInt(keyCodeString);
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_DOWN);
}
catch (Exception e){
Process processKeyEvent = Runtime.getRuntime().exec("/system/xbin/su");
DataOutputStream os = new DataOutputStream(processKeyEvent.getOutputStream());
os.writeBytes("input keyevent " + keyCode + "\n");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
t.start();
thread.start();
}
public void EndCall(){
Thread t= new Thread(new Runnable() {
public static void inputLongPressKeyEvent(final String keyCodeString) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int keyCode = Integer.parseInt(keyCodeString);
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_ENDCALL);
}
catch (Exception e){
Process processKeyEvent = Runtime.getRuntime().exec("/system/xbin/su");
DataOutputStream os = new DataOutputStream(processKeyEvent.getOutputStream());
os.writeBytes("input keyevent --longpress " + keyCode + "\n");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
t.start();
thread.start();
}
}
......@@ -49,17 +49,16 @@ public class StaticGesture extends Gesture {
Frame []f = new Frame[sensors];
for (int i=0; i<4; i++) {
f[i] = new Frame(hand[i], false, adcUpper, adcLower, 2);
for (int i=0; i<5; i++) {
f[i] = new Frame(hand[i], false, adcUpper, adcLower, adcLevels);
}
for (int i=4; i<sensors; i++) {
f[i] = new Frame(hand[i], true, mpuUpper, mpuLower, 3);
for (int i=5; i<sensors; i++) {
f[i] = new Frame(hand[i], true, mpuUpper, mpuLower, mpuLevels);
}
for (int i=0; i<sensors; i++) {
f[i].pushLimits(data[i]);
}
this.updateFrame(data);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment