-
-
Save anonymous/0d3b2e8dde556e111887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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