diff --git a/doric-android/app/src/main/java/pub/doric/demo/MainActivity.java b/doric-android/app/src/main/java/pub/doric/demo/MainActivity.java index fe7d6e4f..3689388a 100644 --- a/doric-android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/doric-android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -33,7 +33,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import pub.doric.devkit.ui.DemoDebugActivity; +import pub.doric.DoricActivity; +import pub.doric.devkit.ui.DoricDevActivity; import pub.doric.refresh.DoricSwipeLayout; import pub.doric.utils.DoricUtils; @@ -106,7 +107,25 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + { + for (int i = 0; i != data.length; i++) { + if (data[i].equals("Snake.js")) { + Intent intent = new Intent(tv.getContext(), DoricActivity.class); + intent.putExtra("source", "assets://src/" + data[i]); + intent.putExtra("alias", data[i]); + tv.getContext().startActivity(intent); + } + } + } + { + tv.postDelayed(new Runnable() { + @Override + public void run() { + Intent intent = new Intent(tv.getContext(), DoricDevActivity.class); + tv.getContext().startActivity(intent); + } + }, 4000); + } } }); } else { @@ -115,7 +134,7 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent intent = new Intent(tv.getContext(), DemoDebugActivity.class); + Intent intent = new Intent(tv.getContext(), DoricActivity.class); intent.putExtra("source", "assets://src/" + data[position - 1]); intent.putExtra("alias", data[position - 1]); tv.getContext().startActivity(intent); diff --git a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java index 225a1cda..f6d46fb9 100644 --- a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java +++ b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java @@ -19,6 +19,7 @@ import android.app.Application; import pub.doric.Doric; import pub.doric.DoricRegistry; +import pub.doric.devkit.DoricDev; public class MyApplication extends Application { @Override @@ -26,5 +27,6 @@ public class MyApplication extends Application { super.onCreate(); Doric.init(this); DoricRegistry.register(new DemoLibrary()); + DoricDev.getInstance().init(this); } } diff --git a/doric-android/devkit/src/main/AndroidManifest.xml b/doric-android/devkit/src/main/AndroidManifest.xml index db8ffc55..a9b13be5 100644 --- a/doric-android/devkit/src/main/AndroidManifest.xml +++ b/doric-android/devkit/src/main/AndroidManifest.xml @@ -5,9 +5,7 @@ + - diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java index d0c90775..77395c3c 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java @@ -1,5 +1,98 @@ package pub.doric.devkit; -public class DoricDev { +import android.app.Application; +import android.widget.Toast; +import com.lahm.library.EasyProtectorLib; +import com.lahm.library.EmulatorCheckCallback; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; +import pub.doric.devkit.event.ConnectExceptionEvent; +import pub.doric.devkit.event.EOFExceptionEvent; +import pub.doric.devkit.event.EnterDebugEvent; +import pub.doric.devkit.event.OpenEvent; +import pub.doric.devkit.event.ReloadEvent; +import pub.doric.devkit.event.StartDebugEvent; +import pub.doric.devkit.event.StopDebugEvent; + +public class DoricDev { + private static class Inner { + private static final DoricDev sInstance = new DoricDev(); + } + + private DoricDev() { + EventBus.getDefault().register(this); + } + + public static DoricDev getInstance() { + return Inner.sInstance; + } + + private Application application; + public boolean devKitConnected = false; + private DoricContextDebuggable doricContextDebuggable; + + public void init(Application application) { + this.application = application; + + DevKit.isRunningInEmulator = EasyProtectorLib.checkIsRunningInEmulator(application, new EmulatorCheckCallback() { + @Override + public void findEmulator(String emulatorInfo) { + System.out.println(emulatorInfo); + } + }); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onOpenEvent(OpenEvent openEvent) { + devKitConnected = true; + Toast.makeText(application, "dev kit connected", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { + devKitConnected = false; + Toast.makeText(application, "dev kit eof exception", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { + devKitConnected = false; + Toast.makeText(application, "dev kit connection exception", Toast.LENGTH_LONG).show(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onStartDebugEvent(StartDebugEvent startDebugEvent) { + doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId()); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { + doricContextDebuggable.startDebug(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) { + doricContextDebuggable.stopDebug(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onReloadEvent(ReloadEvent reloadEvent) { + for (DoricContext context : DoricContextManager.aliveContexts()) { + if (reloadEvent.source.contains(context.getSource())) { + if (doricContextDebuggable != null && + doricContextDebuggable.isDebugging && + doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) { + System.out.println("is debugging context id: " + context.getContextId()); + } else { + context.reload(reloadEvent.script); + } + } + } + } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java b/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java index cbc5cd9c..59c850ca 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java @@ -33,7 +33,6 @@ import pub.doric.devkit.event.EOFExceptionEvent; import pub.doric.devkit.event.EnterDebugEvent; import pub.doric.devkit.event.OpenEvent; import pub.doric.devkit.event.ReloadEvent; -import pub.doric.devkit.ui.DevPanel; /** * @Description: com.github.penfeizhou.doric.dev @@ -61,7 +60,6 @@ public class WSClient extends WebSocketListener { public void onOpen(WebSocket webSocket, Response response) { super.onOpen(webSocket, response); - DevPanel.isDevConnected = true; EventBus.getDefault().post(new OpenEvent()); } @@ -106,10 +104,8 @@ public class WSClient extends WebSocketListener { super.onFailure(webSocket, t, response); if (t instanceof EOFException) { - DevPanel.isDevConnected = false; EventBus.getDefault().post(new EOFExceptionEvent()); } else if (t instanceof ConnectException) { - DevPanel.isDevConnected = false; EventBus.getDefault().post(new ConnectExceptionEvent()); } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java deleted file mode 100644 index de6f2a27..00000000 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DebugContextPanel.java +++ /dev/null @@ -1,86 +0,0 @@ -package pub.doric.devkit.ui; - -import android.app.Dialog; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.Window; -import android.widget.LinearLayout; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.DialogFragment; - -import com.google.gson.JsonObject; - -import org.greenrobot.eventbus.EventBus; - -import pub.doric.DoricContext; -import pub.doric.DoricContextManager; -import pub.doric.devkit.DevKit; -import pub.doric.devkit.IDevKit; -import pub.doric.devkit.R; -import pub.doric.devkit.event.StartDebugEvent; - -public class DebugContextPanel extends DialogFragment { - - public DebugContextPanel() { - } - - @Nullable - @Override - public View onCreateView( - @NonNull LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState - ) { - return inflater.inflate(R.layout.layout_debug_context, container, false); - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Dialog dialog = super.onCreateDialog(savedInstanceState); - dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); - - return dialog; - } - - @Override - public void onStart() { - super.onStart(); - - Dialog dialog = getDialog(); - if (dialog != null) { - dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); - } - - LinearLayout container = getView().findViewById(R.id.container); - LayoutInflater inflater = LayoutInflater.from(getContext()); - - for (final DoricContext doricContext : DoricContextManager.aliveContexts()) { - View cell = inflater.inflate(R.layout.layout_debug_context_cell, container, false); - - TextView contextIdTextView = cell.findViewById(R.id.context_id_text_view); - contextIdTextView.setText(doricContext.getContextId()); - - TextView sourceTextView = cell.findViewById(R.id.source_text_view); - sourceTextView.setText(doricContext.getSource()); - - cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - EventBus.getDefault().post(new StartDebugEvent(doricContext.getContextId())); - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("contextId", doricContext.getContextId()); - jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts")); - DevKit.getInstance().sendDevCommand(IDevKit.Command.DEBUG, jsonObject); - dismissAllowingStateLoss(); - } - }); - - container.addView(cell); - } - } -} diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java deleted file mode 100644 index 06feba1e..00000000 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DemoDebugActivity.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright [2019] [Doric.Pub] - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package pub.doric.devkit.ui; - -import android.os.Bundle; -import android.view.KeyEvent; - -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; - -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; - -import pub.doric.DoricActivity; -import pub.doric.DoricContext; -import pub.doric.DoricContextManager; -import pub.doric.devkit.DoricContextDebuggable; -import pub.doric.devkit.event.EnterDebugEvent; -import pub.doric.devkit.event.ReloadEvent; -import pub.doric.devkit.event.StartDebugEvent; -import pub.doric.devkit.event.StopDebugEvent; -import pub.doric.devkit.util.SensorManagerHelper; - -/** - * @Description: pub.doric.demo - * @Author: pengfei.zhou - * @CreateDate: 2019-11-19 - */ -public class DemoDebugActivity extends DoricActivity { - private SensorManagerHelper sensorHelper; - private DoricContextDebuggable doricContextDebuggable; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - sensorHelper = new SensorManagerHelper(this); - sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { - @Override - public void onShake() { - Fragment devPanel = getSupportFragmentManager().findFragmentByTag("DevPanel"); - if (devPanel != null && devPanel.isAdded()) { - return; - } - new DevPanel().show(getSupportFragmentManager(), "DevPanel"); - } - }); - } - - @Override - public void onAttachedToWindow() { - super.onAttachedToWindow(); - EventBus.getDefault().register(this); - } - - @Override - public void onDetachedFromWindow() { - super.onDetachedFromWindow(); - EventBus.getDefault().unregister(this); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - sensorHelper.stop(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onStartDebugEvent(StartDebugEvent startDebugEvent) { - doricContextDebuggable = new DoricContextDebuggable(startDebugEvent.getContextId()); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) { - doricContextDebuggable.startDebug(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onQuitDebugEvent(StopDebugEvent quitDebugEvent) { - doricContextDebuggable.stopDebug(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onReloadEvent(ReloadEvent reloadEvent) { - for (DoricContext context : DoricContextManager.aliveContexts()) { - if (reloadEvent.source.contains(context.getSource())) { - if (doricContextDebuggable != null && - doricContextDebuggable.isDebugging && - doricContextDebuggable.getContext().getContextId().equals(context.getContextId())) { - System.out.println("is debugging context id: " + context.getContextId()); - } else { - context.reload(reloadEvent.script); - } - } - } - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { - new DevPanel().show(getSupportFragmentManager(), "DevPanel"); - } - return super.onKeyDown(keyCode, event); - } -} diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java deleted file mode 100644 index 17242276..00000000 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DevPanel.java +++ /dev/null @@ -1,135 +0,0 @@ -package pub.doric.devkit.ui; - -import android.Manifest; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Toast; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.google.android.material.bottomsheet.BottomSheetDialogFragment; -import com.lahm.library.EasyProtectorLib; -import com.lahm.library.EmulatorCheckCallback; -import com.tbruyelle.rxpermissions2.RxPermissions; - -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; - -import io.reactivex.disposables.Disposable; -import io.reactivex.functions.Consumer; -import pub.doric.devkit.DevKit; -import pub.doric.devkit.R; -import pub.doric.devkit.event.ConnectExceptionEvent; -import pub.doric.devkit.event.EOFExceptionEvent; -import pub.doric.devkit.event.OpenEvent; - -public class DevPanel extends BottomSheetDialogFragment { - - public static boolean isDevConnected = false; - - public DevPanel() { - } - - @Nullable - @Override - public View onCreateView( - @NonNull LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState - ) { - return inflater.inflate(R.layout.layout_dev, container, false); - } - - @Override - public void onStart() { - super.onStart(); - - updateUI(); - - getView().findViewById(R.id.connect_dev_kit_text_view).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (DevKit.isRunningInEmulator) { - DevKit.ip = "10.0.2.2"; - DevKit.getInstance().connectDevKit("ws://" + DevKit.ip + ":7777"); - } else { - final RxPermissions rxPermissions = new RxPermissions(DevPanel.this); - Disposable disposable = rxPermissions - .request(Manifest.permission.CAMERA) - .subscribe(new Consumer() { - @Override - public void accept(Boolean grant) throws Exception { - if (grant) { - Intent intent = new Intent(getContext(), ScanQRCodeActivity.class); - getContext().startActivity(intent); - } - } - }); - } - } - }); - - getView().findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - DebugContextPanel debugContextPanel = new DebugContextPanel(); - debugContextPanel.show(getActivity().getSupportFragmentManager(), "DebugContextPanel"); - dismissAllowingStateLoss(); - } - }); - } - - @Override - public void onAttach(@NonNull Context context) { - super.onAttach(context); - EventBus.getDefault().register(this); - DevKit.isRunningInEmulator = EasyProtectorLib.checkIsRunningInEmulator(context, new EmulatorCheckCallback() { - @Override - public void findEmulator(String emulatorInfo) { - System.out.println(emulatorInfo); - } - }); - } - - @Override - public void onDetach() { - super.onDetach(); - EventBus.getDefault().unregister(this); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onOpenEvent(OpenEvent openEvent) { - updateUI(); - Toast.makeText(getContext(), "dev kit connected", Toast.LENGTH_LONG).show(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { - updateUI(); - Toast.makeText(getContext(), "dev kit eof exception", Toast.LENGTH_LONG).show(); - } - - @Subscribe(threadMode = ThreadMode.MAIN) - public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { - updateUI(); - Toast.makeText(getContext(), "dev kit connection exception", Toast.LENGTH_LONG).show(); - } - - private void updateUI() { - if (isDevConnected) { - getView().findViewById(R.id.connect_dev_kit_text_view).setVisibility(View.GONE); - getView().findViewById(R.id.debug_text_view).setVisibility(View.VISIBLE); - getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.VISIBLE); - } else { - getView().findViewById(R.id.connect_dev_kit_text_view).setVisibility(View.VISIBLE); - getView().findViewById(R.id.debug_text_view).setVisibility(View.GONE); - getView().findViewById(R.id.hot_reload_text_view).setVisibility(View.GONE); - } - } -} diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java new file mode 100644 index 00000000..8a41d6a1 --- /dev/null +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -0,0 +1,115 @@ +package pub.doric.devkit.ui; + +import android.Manifest; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import com.google.gson.JsonObject; +import com.tbruyelle.rxpermissions2.RxPermissions; + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import io.reactivex.disposables.Disposable; +import io.reactivex.functions.Consumer; +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; +import pub.doric.devkit.DevKit; +import pub.doric.devkit.DoricDev; +import pub.doric.devkit.IDevKit; +import pub.doric.devkit.R; +import pub.doric.devkit.event.ConnectExceptionEvent; +import pub.doric.devkit.event.EOFExceptionEvent; +import pub.doric.devkit.event.OpenEvent; +import pub.doric.devkit.event.StartDebugEvent; + +public class DoricDevActivity extends AppCompatActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EventBus.getDefault().register(this); + + if (DoricDev.getInstance().devKitConnected) { + setContentView(R.layout.layout_debug_context); + initViews(); + } else { + if (DevKit.isRunningInEmulator) { + DevKit.ip = "10.0.2.2"; + DevKit.getInstance().connectDevKit("ws://" + DevKit.ip + ":7777"); + } else { + final RxPermissions rxPermissions = new RxPermissions(this); + Disposable disposable = rxPermissions + .request(Manifest.permission.CAMERA) + .subscribe(new Consumer() { + @Override + public void accept(Boolean grant) throws Exception { + if (grant) { + Intent intent = new Intent(DoricDevActivity.this, ScanQRCodeActivity.class); + startActivity(intent); + } + } + }); + } + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + EventBus.getDefault().unregister(this); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onOpenEvent(OpenEvent openEvent) { + setContentView(R.layout.layout_debug_context); + initViews(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEOFEvent(EOFExceptionEvent eofExceptionEvent) { + finish(); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onConnectExceptionEvent(ConnectExceptionEvent connectExceptionEvent) { + finish(); + } + + private void initViews() { + LinearLayout container = findViewById(R.id.container); + LayoutInflater inflater = LayoutInflater.from(this); + + for (final DoricContext doricContext : DoricContextManager.aliveContexts()) { + View cell = inflater.inflate(R.layout.layout_debug_context_cell, container, false); + + TextView contextIdTextView = cell.findViewById(R.id.context_id_text_view); + contextIdTextView.setText(doricContext.getContextId()); + + TextView sourceTextView = cell.findViewById(R.id.source_text_view); + sourceTextView.setText(doricContext.getSource()); + + cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + EventBus.getDefault().post(new StartDebugEvent(doricContext.getContextId())); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("contextId", doricContext.getContextId()); + jsonObject.addProperty("source", doricContext.getSource().replace(".js", ".ts")); + DevKit.getInstance().sendDevCommand(IDevKit.Command.DEBUG, jsonObject); + } + }); + + container.addView(cell); + } + } +} diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/util/SensorManagerHelper.java b/doric-android/devkit/src/main/java/pub/doric/devkit/util/SensorManagerHelper.java deleted file mode 100644 index c9ae1838..00000000 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/util/SensorManagerHelper.java +++ /dev/null @@ -1,113 +0,0 @@ -package pub.doric.devkit.util; - -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(); - } - } -}