kopia lustrzana https://github.com/mik3y/usb-serial-for-android
fixed some warnings
rodzic
b6e9dbe40f
commit
f1d73c04dc
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="NullableNotNullManager">
|
||||
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
|
||||
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
|
||||
<option name="myDefaultNullable" value="androidx.annotation.Nullable" />
|
||||
<option name="myDefaultNotNull" value="androidx.annotation.NonNull" />
|
||||
<option name="myNullables">
|
||||
<value>
|
||||
<list size="12">
|
||||
|
|
|
@ -5,6 +5,8 @@ import android.content.Context;
|
|||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.ListFragment;
|
||||
import android.view.Menu;
|
||||
|
@ -25,7 +27,7 @@ import java.util.Locale;
|
|||
|
||||
public class DevicesFragment extends ListFragment {
|
||||
|
||||
class ListItem {
|
||||
static class ListItem {
|
||||
UsbDevice device;
|
||||
int port;
|
||||
UsbSerialDriver driver;
|
||||
|
@ -37,7 +39,7 @@ public class DevicesFragment extends ListFragment {
|
|||
}
|
||||
}
|
||||
|
||||
private ArrayList<ListItem> listItems = new ArrayList<>();
|
||||
private final ArrayList<ListItem> listItems = new ArrayList<>();
|
||||
private ArrayAdapter<ListItem> listAdapter;
|
||||
private int baudRate = 19200;
|
||||
private boolean withIoManager = true;
|
||||
|
@ -47,8 +49,9 @@ public class DevicesFragment extends ListFragment {
|
|||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
listAdapter = new ArrayAdapter<ListItem>(getActivity(), 0, listItems) {
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, View view, ViewGroup parent) {
|
||||
public View getView(int position, View view, @NonNull ViewGroup parent) {
|
||||
ListItem item = listItems.get(position);
|
||||
if (view == null)
|
||||
view = getActivity().getLayoutInflater().inflate(R.layout.device_list_item, parent, false);
|
||||
|
@ -78,7 +81,7 @@ public class DevicesFragment extends ListFragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.menu_devices, menu);
|
||||
}
|
||||
|
||||
|
@ -142,7 +145,7 @@ public class DevicesFragment extends ListFragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
|
||||
ListItem item = listItems.get(position-1);
|
||||
if(item.driver == null) {
|
||||
Toast.makeText(getActivity(), "no driver", Toast.LENGTH_SHORT).show();
|
||||
|
|
|
@ -34,7 +34,7 @@ public class MainActivity extends AppCompatActivity implements FragmentManager.O
|
|||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
if(intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_ATTACHED")) {
|
||||
if("android.hardware.usb.action.USB_DEVICE_ATTACHED".equals(intent.getAction())) {
|
||||
TerminalFragment terminal = (TerminalFragment)getSupportFragmentManager().findFragmentByTag("terminal");
|
||||
if (terminal != null)
|
||||
terminal.status("USB device detected");
|
||||
|
|
|
@ -42,7 +42,7 @@ import java.util.concurrent.Executors;
|
|||
|
||||
public class TerminalFragment extends Fragment implements SerialInputOutputManager.Listener {
|
||||
|
||||
private enum UsbPermission { Unknown, Requested, Granted, Denied };
|
||||
private enum UsbPermission { Unknown, Requested, Granted, Denied }
|
||||
|
||||
private static final String INTENT_ACTION_GRANT_USB = BuildConfig.APPLICATION_ID + ".GRANT_USB";
|
||||
private static final int WRITE_WAIT_MILLIS = 2000;
|
||||
|
@ -51,8 +51,8 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||
private int deviceId, portNum, baudRate;
|
||||
private boolean withIoManager;
|
||||
|
||||
private BroadcastReceiver broadcastReceiver;
|
||||
private Handler mainLooper;
|
||||
private final BroadcastReceiver broadcastReceiver;
|
||||
private final Handler mainLooper;
|
||||
private TextView receiveText;
|
||||
private ControlLines controlLines;
|
||||
|
||||
|
@ -65,7 +65,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||
broadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if(intent.getAction().equals(INTENT_ACTION_GRANT_USB)) {
|
||||
if(INTENT_ACTION_GRANT_USB.equals(intent.getAction())) {
|
||||
usbPermission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
|
||||
? UsbPermission.Granted : UsbPermission.Denied;
|
||||
connect();
|
||||
|
@ -258,10 +258,10 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||
return;
|
||||
}
|
||||
try {
|
||||
byte[] data = (str + '\n').getBytes();
|
||||
byte[] data = (str + '\n').getBytes();
|
||||
SpannableStringBuilder spn = new SpannableStringBuilder();
|
||||
spn.append("send " + data.length + " bytes\n");
|
||||
spn.append(HexDump.dumpHexString(data)+"\n");
|
||||
spn.append(HexDump.dumpHexString(data)).append("\n");
|
||||
spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
receiveText.append(spn);
|
||||
usbSerialPort.write(data, WRITE_WAIT_MILLIS);
|
||||
|
@ -291,7 +291,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||
SpannableStringBuilder spn = new SpannableStringBuilder();
|
||||
spn.append("receive " + data.length + " bytes\n");
|
||||
if(data.length > 0)
|
||||
spn.append(HexDump.dumpHexString(data)+"\n");
|
||||
spn.append(HexDump.dumpHexString(data)).append("\n");
|
||||
receiveText.append(spn);
|
||||
}
|
||||
|
||||
|
@ -304,8 +304,8 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
|
|||
class ControlLines {
|
||||
private static final int refreshInterval = 200; // msec
|
||||
|
||||
private Runnable runnable;
|
||||
private ToggleButton rtsBtn, ctsBtn, dtrBtn, dsrBtn, cdBtn, riBtn;
|
||||
private final Runnable runnable;
|
||||
private final ToggleButton rtsBtn, ctsBtn, dtrBtn, dsrBtn, cdBtn, riBtn;
|
||||
|
||||
ControlLines(View view) {
|
||||
runnable = this::run; // w/o explicit Runnable, a new lambda would be created on each postDelayed, which would not be found again by removeCallbacks
|
||||
|
|
|
@ -22,7 +22,7 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation "androidx.annotation:annotation:1.1.0"
|
||||
implementation "androidx.annotation:annotation:1.2.0"
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'org.mockito:mockito-core:1.10.19'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
|
|
|
@ -142,7 +142,7 @@ public class DeviceTest {
|
|||
}
|
||||
|
||||
private static class TestBuffer {
|
||||
private byte[] buf;
|
||||
private final byte[] buf;
|
||||
private int len;
|
||||
|
||||
private TestBuffer(int length) {
|
||||
|
@ -937,7 +937,7 @@ public class DeviceTest {
|
|||
int purgeTimeout = 250;
|
||||
TestBuffer tbuf;
|
||||
long begin;
|
||||
int duration1, duration2, retries, i, timeout;
|
||||
int duration1, duration2, retries, i;
|
||||
retries = purge ? 10 : 1;
|
||||
tbuf = new TestBuffer(writeBufferSize);
|
||||
|
||||
|
@ -1260,7 +1260,7 @@ public class DeviceTest {
|
|||
usb.setParameters(2400, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
telnet.setParameters(2400, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
byte[] buf = new byte[64];
|
||||
for(int i=0; i<buf.length; i++) buf[i]='a';
|
||||
Arrays.fill(buf, (byte) 'a');
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
usb.write(buf);
|
||||
|
@ -1449,17 +1449,14 @@ public class DeviceTest {
|
|||
public void readTimeout() throws Exception {
|
||||
final Boolean[] closed = {Boolean.FALSE};
|
||||
|
||||
Runnable closeThread = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
usb.close();
|
||||
closed[0] = true;
|
||||
}
|
||||
Runnable closeThread = () -> {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
usb.close();
|
||||
closed[0] = true;
|
||||
};
|
||||
|
||||
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
|
||||
|
|
|
@ -287,7 +287,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
||||
supportedDevices.put(UsbId.VENDOR_ARDUINO,
|
||||
new int[] {
|
||||
UsbId.ARDUINO_UNO,
|
||||
|
|
|
@ -363,7 +363,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
||||
supportedDevices.put(UsbId.VENDOR_QINHENG, new int[]{
|
||||
UsbId.QINHENG_CH340,
|
||||
UsbId.QINHENG_CH341A,
|
||||
|
|
|
@ -322,7 +322,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
||||
supportedDevices.put(UsbId.VENDOR_SILABS,
|
||||
new int[] {
|
||||
UsbId.SILABS_CP2102, // same ID for CP2101, CP2103, CP2104, CP2109
|
||||
|
|
|
@ -415,7 +415,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
||||
supportedDevices.put(UsbId.VENDOR_FTDI,
|
||||
new int[] {
|
||||
UsbId.FTDI_FT232R,
|
||||
|
|
|
@ -21,7 +21,7 @@ import java.util.Map;
|
|||
public class ProbeTable {
|
||||
|
||||
private final Map<Pair<Integer, Integer>, Class<? extends UsbSerialDriver>> mProbeTable =
|
||||
new LinkedHashMap<Pair<Integer,Integer>, Class<? extends UsbSerialDriver>>();
|
||||
new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Adds or updates a (vendor, product) pair in the table.
|
||||
|
|
|
@ -35,7 +35,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||
28800, 38400, 57600, 115200, 128000, 134400, 161280, 201600, 230400, 268800,
|
||||
403200, 460800, 614400, 806400, 921600, 1228800, 2457600, 3000000, 6000000
|
||||
};
|
||||
private enum DeviceType { DEVICE_TYPE_01, DEVICE_TYPE_HX};
|
||||
private enum DeviceType { DEVICE_TYPE_01, DEVICE_TYPE_HX}
|
||||
|
||||
private final UsbDevice mDevice;
|
||||
private final UsbSerialPort mPort;
|
||||
|
@ -502,7 +502,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<>();
|
||||
supportedDevices.put(UsbId.VENDOR_PROLIFIC,
|
||||
new int[] { UsbId.PROLIFIC_PL2303, });
|
||||
return supportedDevices;
|
||||
|
|
|
@ -46,11 +46,11 @@ public class UsbSerialProber {
|
|||
* not require permission from the Android USB system, since it does not
|
||||
* open any of the devices.
|
||||
*
|
||||
* @param usbManager
|
||||
* @param usbManager usb manager
|
||||
* @return a list, possibly empty, of all compatible drivers
|
||||
*/
|
||||
public List<UsbSerialDriver> findAllDrivers(final UsbManager usbManager) {
|
||||
final List<UsbSerialDriver> result = new ArrayList<UsbSerialDriver>();
|
||||
final List<UsbSerialDriver> result = new ArrayList<>();
|
||||
|
||||
for (final UsbDevice usbDevice : usbManager.getDeviceList().values()) {
|
||||
final UsbSerialDriver driver = probeDevice(usbDevice);
|
||||
|
|
|
@ -52,12 +52,12 @@ public class SerialInputOutputManager implements Runnable {
|
|||
/**
|
||||
* Called when new incoming data is available.
|
||||
*/
|
||||
public void onNewData(byte[] data);
|
||||
void onNewData(byte[] data);
|
||||
|
||||
/**
|
||||
* Called when {@link SerialInputOutputManager#run()} aborts due to an error.
|
||||
*/
|
||||
public void onRunError(Exception e);
|
||||
void onRunError(Exception e);
|
||||
}
|
||||
|
||||
public SerialInputOutputManager(UsbSerialPort serialPort) {
|
||||
|
@ -200,7 +200,7 @@ public class SerialInputOutputManager implements Runnable {
|
|||
|
||||
private void step() throws IOException {
|
||||
// Handle incoming data.
|
||||
byte[] buffer = null;
|
||||
byte[] buffer;
|
||||
synchronized (mReadBufferLock) {
|
||||
buffer = mReadBuffer.array();
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public class FtdiSerialDriverTest {
|
|||
len = port.readFilter(buf, 0);
|
||||
assertEquals(len, 0);
|
||||
|
||||
assertThrows(IOException.class, () -> {port.readFilter(buf, 1);});
|
||||
assertThrows(IOException.class, () -> port.readFilter(buf, 1));
|
||||
|
||||
initBuf(buf);
|
||||
len = port.readFilter(buf, 2);
|
||||
|
@ -69,7 +69,7 @@ public class FtdiSerialDriverTest {
|
|||
assertEquals(len, 62);
|
||||
assertTrue(testBuf(buf, len));
|
||||
|
||||
assertThrows(IOException.class, () -> {port.readFilter(buf, 65);});
|
||||
assertThrows(IOException.class, () -> port.readFilter(buf, 65));
|
||||
|
||||
initBuf(buf);
|
||||
len = port.readFilter(buf, 66);
|
||||
|
|
Ładowanie…
Reference in New Issue