feat(android): add non-root impl
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
tools:ignore="GoogleAppIndexingWarning">
|
||||
<activity android:name=".ui.MainActivity">
|
||||
<activity android:name=".ui.MainActivity"
|
||||
android:screenOrientation="landscape">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
|
||||
@@ -2,8 +2,11 @@ package com.slightech.mynteye.demo.ui;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.ArrayAdapter;
|
||||
@@ -12,6 +15,7 @@ import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import com.slightech.mynteye.Device;
|
||||
@@ -24,18 +28,24 @@ import com.slightech.mynteye.StreamRequest;
|
||||
import com.slightech.mynteye.demo.R;
|
||||
import com.slightech.mynteye.demo.camera.Mynteye;
|
||||
import com.slightech.mynteye.demo.util.RootUtils;
|
||||
import com.slightech.mynteye.usb.CameraDialog;
|
||||
import com.slightech.mynteye.usb.USBMonitor;
|
||||
import com.slightech.mynteye.usb.USBMonitor.OnDeviceConnectListener;
|
||||
import com.slightech.mynteye.usb.USBMonitor.UsbControlBlock;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataReceiveListener,
|
||||
Mynteye.OnMotionDataReceiveListener{
|
||||
public class MainActivity extends BaseActivity implements CameraDialog.CameraDialogParent,
|
||||
Mynteye.OnStreamDataReceiveListener, Mynteye.OnMotionDataReceiveListener {
|
||||
|
||||
@BindView(R.id.text) TextView mTextView;
|
||||
@BindView(R.id.image_left) ImageView mLeftImageView;
|
||||
@BindView(R.id.image_right) ImageView mRightImageView;
|
||||
|
||||
private USBMonitor mUSBMonitor;
|
||||
|
||||
private Mynteye mMynteye;
|
||||
private Bitmap mLeftBitmap, mRightBitmap;
|
||||
|
||||
@@ -44,8 +54,92 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
mUSBMonitor = new USBMonitor(this, mOnDeviceConnectListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mUSBMonitor.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (mUSBMonitor != null) {
|
||||
mUSBMonitor.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (mMynteye != null) {
|
||||
mMynteye.close();
|
||||
mMynteye = null;
|
||||
}
|
||||
if (mUSBMonitor != null) {
|
||||
mUSBMonitor.destroy();
|
||||
mUSBMonitor = null;
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private final OnDeviceConnectListener mOnDeviceConnectListener = new OnDeviceConnectListener() {
|
||||
|
||||
@Override
|
||||
public void onAttach(final UsbDevice device) {
|
||||
toast("USB_DEVICE_ATTACHED");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnect(final UsbDevice device, final UsbControlBlock ctrlBlock, final boolean createNew) {
|
||||
toast(String.format(Locale.getDefault(), "CONNECT, %s: %s", ctrlBlock.getProductName(), ctrlBlock.getSerial()));
|
||||
openDevice(new DeviceUsbInfo(
|
||||
ctrlBlock.getVenderId(),
|
||||
ctrlBlock.getProductId(),
|
||||
ctrlBlock.getFileDescriptor(),
|
||||
ctrlBlock.getBusNum(),
|
||||
ctrlBlock.getDevNum(),
|
||||
getUSBFSName(ctrlBlock),
|
||||
ctrlBlock.getProductName(),
|
||||
ctrlBlock.getSerial()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnect(final UsbDevice device, final UsbControlBlock ctrlBlock) {
|
||||
toast(String.format(Locale.getDefault(), "DISCONNECT, %s: %s", ctrlBlock.getProductName(), ctrlBlock.getSerial()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetach(final UsbDevice device) {
|
||||
toast("USB_DEVICE_DETACHED");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(final UsbDevice device) {
|
||||
}
|
||||
|
||||
private static final String DEFAULT_USBFS = "/dev/bus/usb";
|
||||
|
||||
private final String getUSBFSName(final UsbControlBlock ctrlBlock) {
|
||||
String result = null;
|
||||
final String name = ctrlBlock.getDeviceName();
|
||||
final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null;
|
||||
if ((v != null) && (v.length > 2)) {
|
||||
final StringBuilder sb = new StringBuilder(v[0]);
|
||||
for (int i = 1; i < v.length - 2; i++)
|
||||
sb.append("/").append(v[i]);
|
||||
result = sb.toString();
|
||||
}
|
||||
if (TextUtils.isEmpty(result)) {
|
||||
Timber.w("failed to get USBFS path, try to use default path: %s", name);
|
||||
result = DEFAULT_USBFS;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||
@@ -70,6 +164,9 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
}
|
||||
|
||||
private void actionOpen(final Runnable completeEvent) {
|
||||
if (completeEvent != null) completeEvent.run();
|
||||
CameraDialog.showDialog(this);
|
||||
/*
|
||||
if (!RootUtils.isRooted()) {
|
||||
if (completeEvent != null) completeEvent.run();
|
||||
alert("Warning", "Root denied :(");
|
||||
@@ -84,8 +181,10 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
alert("Warning", "There are no devices accessible.");
|
||||
}
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
private void showDevices() {
|
||||
ArrayList<DeviceUsbInfo> infos = Device.query();
|
||||
if (infos.isEmpty()) {
|
||||
@@ -110,6 +209,7 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private void openDevice(DeviceUsbInfo info) {
|
||||
mMynteye = new Mynteye(info);
|
||||
@@ -138,6 +238,15 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public USBMonitor getUSBMonitor() {
|
||||
return mUSBMonitor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDialogResult(boolean canceled) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStreamDataReceive(Stream stream, StreamData data, Handler handler) {
|
||||
}
|
||||
@@ -182,15 +291,6 @@ public class MainActivity extends BaseActivity implements Mynteye.OnStreamDataRe
|
||||
mTextView.post(() -> mTextView.setText(datas.get(0).imu().toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (mMynteye != null) {
|
||||
mMynteye.close();
|
||||
mMynteye = null;
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private void toast(CharSequence text) {
|
||||
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
tools:context=".ui.MainActivity"
|
||||
>
|
||||
|
||||
@@ -15,32 +16,44 @@
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toTopOf="@+id/image_left"
|
||||
app:layout_constraintBottom_toTopOf="@id/layout_image"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image_left"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/image_right"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/text"
|
||||
app:layout_constraintVertical_weight="1"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image_right"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/layout_image"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/image_left"
|
||||
app:layout_constraintVertical_weight="1"
|
||||
/>
|
||||
app:layout_constraintTop_toBottomOf="@id/text"
|
||||
>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
<ImageView
|
||||
android:id="@+id/image_left"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@id/image_right"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_weight="1"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image_right"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/image_left"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_weight="1"
|
||||
/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
Reference in New Issue
Block a user