Created
May 29, 2019 12:02
-
-
Save Qamar4P/c1e40199817051a51117816676ae040f to your computer and use it in GitHub Desktop.
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 com.irisvision.controllers; | |
import android.view.KeyEvent; | |
import android.view.MotionEvent; | |
public interface ExternalEventListener { | |
public boolean onControllerKeyEvent(KeyEvent event); | |
public boolean onControllerTouchEvent(MotionEvent event); | |
} |
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 com.irisvision.controllers; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Build; | |
import android.util.Log; | |
import android.view.KeyEvent; | |
import android.view.MotionEvent; | |
import com.irisvision.realityviewer.parameters.Params; | |
import com.picovr.client.HbController; | |
import com.picovr.client.HbListener; | |
import com.picovr.picovrlib.hummingbirdclient.HbClientActivity; | |
/** | |
* Created by Qamar4P on 5/29/19. | |
*/ | |
public class PicoVrController { | |
private final String TAG = "PicoVrController"; | |
private Context mContext = null; | |
private boolean isHbServiceExist = false; | |
private boolean isPhone = false; | |
private boolean isAssistantInstall = false; | |
private HbController mController = null; | |
private boolean mHbConnected; | |
private BroadcastReceiver mReceiver = new BroadcastReceiver() { | |
public void onReceive(Context context, Intent intent) { | |
Log.d("PicoVrController", "" + intent.getAction()); | |
if (intent.getAction().equals("com.picovr.wing.ble.broadcast.hbservice.binded")) { | |
if (PicoVrController.this.mController.getConnectState() == 0 && PicoVrController.this.isPhone) { | |
PicoVrController.this.connnectHB(); | |
} | |
} else if (intent.getAction().equals("com.picovr.hummingbird.service.picobroadcast.created")) { | |
PicoVrController.this.bindHbService(); | |
} | |
} | |
}; | |
// MARK: Class Methods | |
public PicoVrController(Context context) { | |
mContext = context; | |
eventListener = (ExternalEventListener) context; | |
if(mController == null){ | |
IntentFilter filter = new IntentFilter(); | |
filter.addAction("com.picovr.wing.ble.broadcast.hbservice.binded"); | |
filter.addAction("com.picovr.hummingbird.service.picobroadcast.created"); | |
this.mContext.registerReceiver(this.mReceiver, filter); | |
this.mController = new HbController(this.mContext); | |
Log.d("PicoVrController", " created"); | |
InitServices(); | |
mController.setHbListener(new HbListener() { | |
@Override | |
public void onConnect() { | |
mHbConnected = true; | |
} | |
@Override | |
public void onDisconnect() { | |
mHbConnected = false; | |
} | |
@Override | |
public void onDataUpdate() { | |
if (getHbController().keyData != null) { | |
mHbConnected = true; | |
parseControllerData(getHbController().keyData); | |
// String keys = ""; | |
// for (int keyDatum : getHbController().keyData) { | |
// keys += " "+keyDatum; | |
// } | |
// android.util.Log.d(TAG,"KeyData: "+keys); | |
} | |
} | |
@Override | |
public void onReCenter() { | |
} | |
@Override | |
public void onBindService() { | |
} | |
}); | |
} | |
} | |
public void InitServices() { | |
Log.d("PicoVrController", " Init"); | |
this.isPhone = this.isPhone(); | |
this.isAssistantInstall = HbClientActivity.isControllerServiceInstalled(this.mContext); | |
this.isHbServiceExist = HbClientActivity.isHbServiceExisted(this.mContext); | |
Log.d("PicoVrController", "isPhone " + this.isPhone + " isAssistantInstall " + this.isAssistantInstall + " isHbServiceExist " + this.isHbServiceExist); | |
if (this.isPhone) { | |
if (this.isAssistantInstall) { | |
if (this.isHbServiceExist) { | |
HbClientActivity.bindHbService(this.mContext); | |
} else { | |
HbClientActivity.startControllerService(this.mContext); | |
} | |
} else { | |
Log.e("PicoVrController", "Need install assistant for phone!"); | |
} | |
} else if (this.isHbServiceExist) { | |
HbClientActivity.bindHbService(this.mContext); | |
} else { | |
Log.e("PicoVrController", "System HbService not Exist!"); | |
} | |
} | |
public void Pause() { | |
Log.d("PicoVrController", " Pause"); | |
this.mController.Stop(); | |
this.unbindHbService(); | |
} | |
public void Resume() { | |
Log.d("PicoVrController", " Resume"); | |
this.bindHbService(); | |
this.mController.Start(); | |
} | |
public void connnectHB() { | |
if (HbClientActivity.isAutoConnectServiceExisted(this.mContext)) { | |
HbClientActivity.autoConnectHbController(10000, "test"); | |
} | |
} | |
public void setHbListener(HbListener listener) { | |
Log.d("PicoVrController", "setHbListener"); | |
if (this.mController != null) { | |
this.mController.setHbListener(listener); | |
this.mController.Start(); | |
} | |
} | |
public HbController getHbController() { | |
return this.mController; | |
} | |
private void bindHbService() { | |
this.isHbServiceExist = HbClientActivity.isHbServiceExisted(this.mContext); | |
if (this.isHbServiceExist) { | |
Log.d("PicoVrController", "bindHbService"); | |
HbClientActivity.bindHbService(this.mContext); | |
} else { | |
Log.d("PicoVrController", "HbService not Exist"); | |
} | |
} | |
private void unbindHbService() { | |
HbClientActivity.unbindHbService(this.mContext); | |
} | |
private boolean isPhone() { | |
String model = Build.MODEL; | |
Log.d("PicoVrController", "model " + Build.MODEL); | |
String Device = "Pico Goblin,Pico Neo,Pico Neo DKS,Neptune VR9 Triton"; | |
return !Device.contains(model); | |
} | |
private long eventDownTime; | |
private ExternalEventListener eventListener; | |
public void setEventListener(ExternalEventListener l) { | |
eventListener = l; | |
} | |
// MARK: start scan-connect process | |
public void startScan() { | |
connnectHB(); | |
Resume(); | |
getHbController().startUpdateThread(); | |
} | |
public void stop() { | |
Pause(); | |
getHbController().stopUpdateThread(); | |
} | |
public boolean isConnected() { | |
return mHbConnected; | |
} | |
// | |
// Controller data parsing | |
// | |
// MARK: Event Codes | |
public static final int EVENT_TOUCHPAD_CLICK_UP = 11; | |
public static final int EVENT_TOUCHPAD_CLICK_DOWN = 12; | |
private int prev_tx = 0, prev_ty = 0; | |
private boolean prev_isTouching = false; | |
private boolean prev_clickButton = false, prev_homeButton = false, prev_appButton = false, prev_plusButton = false, prev_minusButton = false; | |
private void parseControllerData(int[] data) { | |
int tx, ty; | |
boolean clickButton, homeButton, appButton, plusButton, minusButton; | |
boolean clickButtonUp, clickButtonDown, homeButtonUp, homeButtonDown, appButtonUp, appButtonDown; | |
boolean plusButtonUp, plusButtonDown, minusButtonUp, minusButtonDown; | |
int b16, b17, b17a, b18; | |
Log.d(TAG, "parseControllerData"); | |
clickButton = false; | |
homeButton = false; | |
appButton = false; | |
plusButton = false; | |
minusButton = false; | |
clickButtonUp = false; | |
clickButtonDown = false; | |
homeButtonUp = false; | |
homeButtonDown = false; | |
appButtonUp = false; | |
appButtonDown = false; | |
plusButtonUp = false; | |
plusButtonDown = false; | |
minusButtonUp = false; | |
minusButtonDown = false; | |
tx = data[0]; | |
ty = -data[1]; | |
// get button details | |
clickButton = data[4] == 1; | |
homeButton = data[2] == 1; | |
appButton = data[3] == 1; | |
plusButton = data[5] == 1; | |
minusButton = data[6] == 1; | |
clickButtonUp = (!clickButton && prev_clickButton); | |
clickButtonDown = (clickButton && !prev_clickButton); | |
homeButtonUp = (!homeButton && prev_homeButton); | |
homeButtonDown = (homeButton && !prev_homeButton); | |
appButtonUp = (!appButton && prev_appButton); | |
appButtonDown = (appButton && !prev_appButton); | |
plusButtonUp = (!plusButton && prev_plusButton); | |
plusButtonDown = (plusButton && !prev_plusButton); | |
minusButtonUp = (!minusButton && prev_minusButton); | |
minusButtonDown = (minusButton && !prev_minusButton); | |
// Send key events to event listener | |
if (clickButton != prev_clickButton) { | |
if (clickButtonUp) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BUTTON_A)); | |
else if (clickButtonDown) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_A)); | |
} | |
if (homeButton != prev_homeButton) { | |
if (homeButtonUp) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HOME)); | |
else if (homeButtonDown) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME)); | |
} | |
if (appButton != prev_appButton) { | |
if (appButtonUp) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); | |
else if (appButtonDown) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); | |
} | |
if (plusButton != prev_plusButton) { | |
if (plusButtonUp) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_UP)); | |
else if (plusButtonDown) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_UP)); | |
} | |
if (minusButton != prev_minusButton) { | |
if (minusButtonUp) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_DOWN)); | |
else if (minusButtonDown) | |
eventListener.onControllerKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN)); | |
} | |
tx *= Params.getControllerMultiplier(); | |
ty *= Params.getControllerMultiplier(); | |
// Send touch events to event listener | |
//if (tx > 0 || ty > 0) { | |
if (tx != prev_tx || ty != prev_ty) { | |
MotionEvent event; | |
float ftx = (float) tx / 255; | |
float fty = (float) ty / 255; | |
int action; | |
if (tx == 0 && ty == 0) { | |
action = MotionEvent.ACTION_UP; | |
ftx = (float) prev_tx / 255; | |
fty = (float) prev_ty / 255; | |
} else if (prev_tx == 0 && prev_ty == 0) | |
action = MotionEvent.ACTION_DOWN; | |
else | |
action = MotionEvent.ACTION_MOVE; | |
event = MotionEvent.obtain(android.os.SystemClock.uptimeMillis(), | |
android.os.SystemClock.uptimeMillis(), | |
action, | |
ftx, | |
fty, | |
0); | |
if (event != null) | |
eventListener.onControllerTouchEvent(event); | |
} | |
//} | |
// Remember state for next time through | |
prev_clickButton = clickButton; | |
prev_homeButton = homeButton; | |
prev_appButton = appButton; | |
prev_plusButton = plusButton; | |
prev_minusButton = minusButton; | |
prev_tx = tx; | |
prev_ty = ty; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment