Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make writing to usbd work asynchronously.

Change-Id: Ib6a9f2918dd6dbae20beebbf45bab1d28aa796e2
  • Loading branch information...
commit 732eefe5e05ee331b2f89b65cddb466dd7fb8b9b 1 parent f975cc8
@maniac103 maniac103 authored
Showing with 33 additions and 4 deletions.
  1. +33 −4 src/com/motorola/usb/UsbListener.java
View
37 src/com/motorola/usb/UsbListener.java
@@ -20,6 +20,8 @@
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
@@ -32,6 +34,7 @@
private OutputStream mOutputStream;
private Handler mHandler;
+ private WriteCommandThread mWriteThread;
public static final String EVENT_CABLE_CONNECTED = "cable_connected";
public static final String EVENT_CABLE_CONNECTED_FACTORY = "cable_connected_factory";
@@ -74,6 +77,9 @@
public UsbListener(Handler handler) {
mHandler = handler;
+ mWriteThread = new WriteCommandThread();
+
+ mWriteThread.start();
}
private void handleEvent(String event) {
@@ -122,7 +128,9 @@ private void listenToSocket() {
usbdSocket.connect(socketAddress);
InputStream usbdInputStream = usbdSocket.getInputStream();
- mOutputStream = usbdSocket.getOutputStream();
+ synchronized (this) {
+ mOutputStream = usbdSocket.getOutputStream();
+ }
byte[] buffer = new byte[100];
@@ -147,7 +155,7 @@ private void listenToSocket() {
}
//clean up
- synchronized(this) {
+ synchronized (this) {
if (mOutputStream != null) {
try {
mOutputStream.close();
@@ -200,10 +208,31 @@ public void run() {
}
}
- public void sendUsbModeSwitchCmd(String cmd) {
+ public void sendUsbModeSwitchCmd(final String cmd) {
if (cmd != null) {
Log.d(TAG, "received usb mode change command from UI: " + cmd);
- writeCommand(cmd, null);
+
+ Handler handler = mWriteThread.mHandler;
+ handler.sendMessage(handler.obtainMessage(WriteCommandThread.MSG_WRITE, cmd));
+ }
+ }
+
+ private class WriteCommandThread extends Thread {
+ public Handler mHandler;
+ public static final int MSG_WRITE = 1;
+
+ public void run() {
+ Looper.prepare();
+
+ mHandler = new Handler() {
+ public void handleMessage(Message msg) {
+ if (msg.what == MSG_WRITE) {
+ writeCommand((String) msg.obj, null);
+ }
+ }
+ };
+
+ Looper.loop();
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.