add sensor detect & phone debug
This commit is contained in:
parent
c0a3fc073b
commit
b9d0a68146
@ -18,13 +18,11 @@ package pub.doric.demo;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.Window;
|
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
|
||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
import org.greenrobot.eventbus.ThreadMode;
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
@ -32,6 +30,7 @@ import org.greenrobot.eventbus.ThreadMode;
|
|||||||
import pub.doric.DoricContext;
|
import pub.doric.DoricContext;
|
||||||
import pub.doric.DoricDriver;
|
import pub.doric.DoricDriver;
|
||||||
import pub.doric.dev.DevPanel;
|
import pub.doric.dev.DevPanel;
|
||||||
|
import pub.doric.dev.SensorManagerHelper;
|
||||||
import pub.doric.dev.event.EnterDebugEvent;
|
import pub.doric.dev.event.EnterDebugEvent;
|
||||||
import pub.doric.dev.event.QuitDebugEvent;
|
import pub.doric.dev.event.QuitDebugEvent;
|
||||||
import pub.doric.engine.ChangeEngineCallback;
|
import pub.doric.engine.ChangeEngineCallback;
|
||||||
@ -45,6 +44,7 @@ import pub.doric.utils.DoricUtils;
|
|||||||
public class DemoActivity extends AppCompatActivity {
|
public class DemoActivity extends AppCompatActivity {
|
||||||
private DoricContext doricContext;
|
private DoricContext doricContext;
|
||||||
private DevPanel devPanel = new DevPanel();
|
private DevPanel devPanel = new DevPanel();
|
||||||
|
private SensorManagerHelper sensorHelper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
@ -56,6 +56,17 @@ public class DemoActivity extends AppCompatActivity {
|
|||||||
doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/" + source), source);
|
doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/" + source), source);
|
||||||
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
||||||
doricContext.getRootNode().setRootView(frameLayout);
|
doricContext.getRootNode().setRootView(frameLayout);
|
||||||
|
|
||||||
|
sensorHelper = new SensorManagerHelper(this);
|
||||||
|
sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() {
|
||||||
|
@Override
|
||||||
|
public void onShake() {
|
||||||
|
if (devPanel.isAdded()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
devPanel.show(getSupportFragmentManager(), "DevPanel");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,6 +93,7 @@ public class DemoActivity extends AppCompatActivity {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
doricContext.teardown();
|
doricContext.teardown();
|
||||||
EventBus.getDefault().unregister(this);
|
EventBus.getDefault().unregister(this);
|
||||||
|
sensorHelper.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
@ -6,7 +6,8 @@ import pub.doric.utils.DoricUtils;
|
|||||||
|
|
||||||
public class DevKit implements IDevKit {
|
public class DevKit implements IDevKit {
|
||||||
|
|
||||||
public static boolean isRunningInEmulator = false;
|
static boolean isRunningInEmulator = false;
|
||||||
|
public static String ip = "";
|
||||||
|
|
||||||
private static class Inner {
|
private static class Inner {
|
||||||
private static final DevKit sInstance = new DevKit();
|
private static final DevKit sInstance = new DevKit();
|
||||||
|
@ -57,7 +57,8 @@ public class DevPanel extends BottomSheetDialogFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (DevKit.isRunningInEmulator) {
|
if (DevKit.isRunningInEmulator) {
|
||||||
Doric.connectDevKit("ws://" + "10.0.2.2" + ":7777");
|
DevKit.ip = "10.0.2.2";
|
||||||
|
Doric.connectDevKit("ws://" + DevKit.ip + ":7777");
|
||||||
} else {
|
} else {
|
||||||
final RxPermissions rxPermissions = new RxPermissions(DevPanel.this);
|
final RxPermissions rxPermissions = new RxPermissions(DevPanel.this);
|
||||||
Disposable disposable = rxPermissions
|
Disposable disposable = rxPermissions
|
||||||
|
@ -49,6 +49,7 @@ public class ScanQRCodeActivity extends AppCompatActivity implements QRCodeView.
|
|||||||
@Override
|
@Override
|
||||||
public void onScanQRCodeSuccess(String result) {
|
public void onScanQRCodeSuccess(String result) {
|
||||||
setTitle("扫描结果为:" + result);
|
setTitle("扫描结果为:" + result);
|
||||||
|
DevKit.ip = result;
|
||||||
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
|
Toast.makeText(this, "dev kit connecting to " + result, Toast.LENGTH_LONG).show();
|
||||||
Doric.connectDevKit("ws://" + result + ":7777");
|
Doric.connectDevKit("ws://" + result + ":7777");
|
||||||
finish();
|
finish();
|
||||||
|
@ -0,0 +1,113 @@
|
|||||||
|
package pub.doric.dev;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.Sensor;
|
||||||
|
import android.hardware.SensorEvent;
|
||||||
|
import android.hardware.SensorEventListener;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
|
||||||
|
public class SensorManagerHelper implements SensorEventListener {
|
||||||
|
|
||||||
|
// 速度阈值,当摇晃速度达到这值后产生作用
|
||||||
|
private final int SPEED_SHRESHOLD = 5000;
|
||||||
|
// 两次检测的时间间隔
|
||||||
|
private final int UPTATE_INTERVAL_TIME = 50;
|
||||||
|
// 传感器管理器
|
||||||
|
private SensorManager sensorManager;
|
||||||
|
// 传感器
|
||||||
|
private Sensor sensor;
|
||||||
|
// 重力感应监听器
|
||||||
|
private OnShakeListener onShakeListener;
|
||||||
|
// 上下文对象context
|
||||||
|
private Context context;
|
||||||
|
// 手机上一个位置时重力感应坐标
|
||||||
|
private float lastX;
|
||||||
|
private float lastY;
|
||||||
|
private float lastZ;
|
||||||
|
// 上次检测时间
|
||||||
|
private long lastUpdateTime;
|
||||||
|
|
||||||
|
public SensorManagerHelper(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始检测
|
||||||
|
*/
|
||||||
|
public void start() {
|
||||||
|
// 获得传感器管理器
|
||||||
|
sensorManager = (SensorManager) context
|
||||||
|
.getSystemService(Context.SENSOR_SERVICE);
|
||||||
|
if (sensorManager != null) {
|
||||||
|
// 获得重力传感器
|
||||||
|
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||||
|
}
|
||||||
|
// 注册
|
||||||
|
if (sensor != null) {
|
||||||
|
sensorManager.registerListener(this, sensor,
|
||||||
|
SensorManager.SENSOR_DELAY_GAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止检测
|
||||||
|
*/
|
||||||
|
public void stop() {
|
||||||
|
sensorManager.unregisterListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 摇晃监听接口
|
||||||
|
*/
|
||||||
|
public interface OnShakeListener {
|
||||||
|
void onShake();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置重力感应监听器
|
||||||
|
*/
|
||||||
|
public void setOnShakeListener(OnShakeListener listener) {
|
||||||
|
onShakeListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重力感应器感应获得变化数据
|
||||||
|
* android.hardware.SensorEventListener#onSensorChanged(android.hardware
|
||||||
|
* .SensorEvent)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onSensorChanged(SensorEvent event) {
|
||||||
|
// 现在检测时间
|
||||||
|
long currentUpdateTime = System.currentTimeMillis();
|
||||||
|
// 两次检测的时间间隔
|
||||||
|
long timeInterval = currentUpdateTime - lastUpdateTime;
|
||||||
|
// 判断是否达到了检测时间间隔
|
||||||
|
if (timeInterval < UPTATE_INTERVAL_TIME) return;
|
||||||
|
// 现在的时间变成last时间
|
||||||
|
lastUpdateTime = currentUpdateTime;
|
||||||
|
// 获得x,y,z坐标
|
||||||
|
float x = event.values[0];
|
||||||
|
float y = event.values[1];
|
||||||
|
float z = event.values[2];
|
||||||
|
// 获得x,y,z的变化值
|
||||||
|
float deltaX = x - lastX;
|
||||||
|
float deltaY = y - lastY;
|
||||||
|
float deltaZ = z - lastZ;
|
||||||
|
// 将现在的坐标变成last坐标
|
||||||
|
lastX = x;
|
||||||
|
lastY = y;
|
||||||
|
lastZ = z;
|
||||||
|
double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
|
||||||
|
* deltaZ)
|
||||||
|
/ timeInterval * 10000;
|
||||||
|
// 达到速度阀值,发出提示
|
||||||
|
if (speed >= SPEED_SHRESHOLD) {
|
||||||
|
onShakeListener.onShake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -37,13 +37,7 @@ public class RemoteJSExecutor {
|
|||||||
.readTimeout(10, TimeUnit.SECONDS)
|
.readTimeout(10, TimeUnit.SECONDS)
|
||||||
.writeTimeout(10, TimeUnit.SECONDS)
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
.build();
|
.build();
|
||||||
String ip;
|
final Request request = new Request.Builder().url("ws://" + DevKit.ip + ":2080").build();
|
||||||
if (DevKit.isRunningInEmulator) {
|
|
||||||
ip = "10.0.2.2";
|
|
||||||
} else {
|
|
||||||
ip = "192.168.24.79";
|
|
||||||
}
|
|
||||||
final Request request = new Request.Builder().url("ws://" + ip + ":2080").build();
|
|
||||||
|
|
||||||
final Thread current = Thread.currentThread();
|
final Thread current = Thread.currentThread();
|
||||||
webSocket = okHttpClient.newWebSocket(request, new WebSocketListener() {
|
webSocket = okHttpClient.newWebSocket(request, new WebSocketListener() {
|
||||||
|
Reference in New Issue
Block a user