Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2013 18:18
Show Gist options
  • Save anonymous/0d3b2e8dde556e111887 to your computer and use it in GitHub Desktop.
Save anonymous/0d3b2e8dde556e111887 to your computer and use it in GitHub Desktop.
package com.example.mywidget;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.example.inputsticklibrary.usb.USBDevice;
import com.example.inputsticklibrary.usb.USBDeviceListener;
import com.example.inputsticklibrary.usb.devices.USBKeyboard;
public class WidgetService extends Service implements USBDeviceListener {
private static final USBKeyboard keyb = new USBKeyboard();
private static boolean running;
@Override
public void onUSBEvent(int code) {
// TODO Auto-generated method stub
int state = keyb.getState();
if (state == USBDevice.STATE_READY) {
if (!running) {
running = true;
Thread t = new Thread() {
@Override
public void run() {
try {
// wait a bit to be sure its 100% ready
Thread.sleep(1000);
// type password
keyb.type("passwordgoeshere");
// press enter
keyb.pressAndRelease(USBKeyboard.NONE,
USBKeyboard.KEY_ENTER);
// wait for long enough to allow USB host read all
// characters
Thread.sleep(1000);
keyb.disconnect();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
} else {
this.stopSelf();
}
}
if (state == USBDevice.STATE_NONE) {
keyb.unregisterListener();
this.stopSelf();
}
}
@Override
public void onUSBData(int endpoint, byte[] data) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
running = false;
keyb.setContext(this.getApplicationContext());
keyb.registerListener(this);
if (keyb.getState() == USBDevice.STATE_NONE) {
keyb.connect();
} else {
this.stopSelf();
}
return START_STICKY;
}
@Override
public void onDestroy() {
Intent i = new Intent(this, MyWidget.class);
i.setAction(MyWidget.BTN_READY);
this.sendBroadcast(i);
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment