Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.inputstick.apps.typetextfile;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.inputstick.api.ConnectionManager;
- import com.inputstick.api.InputStickError;
- import com.inputstick.api.InputStickStateListener;
- import com.inputstick.api.OnEmptyBufferListener;
- import com.inputstick.api.basic.InputStickHID;
- import com.inputstick.api.basic.InputStickKeyboard;
- import com.inputstick.api.hid.HIDKeycodes;
- import com.inputstick.api.layout.KeyboardLayout;
- public class MainActivity extends Activity implements InputStickStateListener, OnEmptyBufferListener {
- //file is located in /res/raw
- private static final int FILE = R.raw.adobe;
- //make sure layout matches keyboard layout used by USB host!!!
- private static final String LAYOUT_CODE = "en-US"; // pl-PL, de-DE etc, see: com.inputstick.api.layout
- private Button buttonConnect;
- private Button buttonKeyboardAction;
- private TextView textViewInfo;
- private static long startTime;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textViewInfo = (TextView)findViewById(R.id.textViewInfo);
- textViewInfo.setText("");
- buttonConnect = (Button)findViewById(R.id.buttonConnect);
- buttonConnect.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- //choose action, basing on current state of InputStick device
- int state = InputStickHID.getState();
- switch (state) {
- case ConnectionManager.STATE_CONNECTED:
- case ConnectionManager.STATE_CONNECTING:
- case ConnectionManager.STATE_READY:
- InputStickHID.disconnect();
- break;
- case ConnectionManager.STATE_DISCONNECTED:
- case ConnectionManager.STATE_FAILURE:
- InputStickHID.connect(MainActivity.this.getApplication());
- break;
- }
- }
- });
- buttonKeyboardAction = (Button)findViewById(R.id.buttonKeyboardAction);
- buttonKeyboardAction.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- //if remote buffer is not empty, text is curently being types
- if (InputStickHID.isKeyboardRemoteBufferEmpty()) {
- //make sure layout matches keyboard layout used by USB host!!!
- KeyboardLayout layout = KeyboardLayout.getLayout(LAYOUT_CODE);
- BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(MainActivity.this.getResources().openRawResource(FILE)));
- String line;
- boolean firstLine = true;
- try {
- while ((line = bufferedreader.readLine()) != null) {
- if (!firstLine) {
- //press Enter key
- InputStickKeyboard.pressAndRelease((byte)0, HIDKeycodes.KEY_ENTER);
- } else {
- MainActivity.startTime = System.currentTimeMillis();
- firstLine = false;
- }
- //type text:
- layout.type(line);
- }
- //we want to receive callback, once all text is received by USB host
- InputStickHID.addBufferEmptyListener(MainActivity.this);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- bufferedreader.close();
- }
- catch(IOException e) {
- }
- }
- buttonKeyboardAction.setText("Cancel");
- } else {
- //cancel typing
- InputStickHID.removeBufferEmptyListener(MainActivity.this);
- InputStickHID.clearKeyboardBuffer();
- MainActivity.startTime = 0;
- buttonKeyboardAction.setText("Type");
- }
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- //we want to receive updates about change of state, in order to update UI
- InputStickHID.addStateListener(this);
- manageUI(InputStickHID.getState());
- }
- @Override
- protected void onPause() {
- super.onPause();
- //state updates are no longer needed
- InputStickHID.removeStateListener(this);
- }
- @Override
- public void onStateChanged(int state) {
- manageUI(state);
- }
- private void enableUI(boolean enabled) {
- buttonKeyboardAction.setEnabled(enabled);
- if (!enabled) {
- buttonKeyboardAction.setText("Type");
- }
- }
- private void manageUI(int state) {
- if (MainActivity.startTime == 0) {
- buttonKeyboardAction.setText("Type");
- } else {
- buttonKeyboardAction.setText("Cancel");
- }
- //set UI according to current state
- switch (state) {
- case ConnectionManager.STATE_DISCONNECTED:
- buttonConnect.setText("Connect");
- enableUI(false);
- break;
- case ConnectionManager.STATE_CONNECTING:
- buttonConnect.setText("Cancel");
- enableUI(false);
- break;
- case ConnectionManager.STATE_CONNECTED:
- buttonConnect.setText("Disconnect");
- enableUI(false);
- break;
- case ConnectionManager.STATE_READY:
- buttonConnect.setText("Disconnect");
- enableUI(true);
- break;
- case ConnectionManager.STATE_FAILURE:
- buttonConnect.setText("Connect");
- //check if the problem is caused by missing InputStickUtility app:
- AlertDialog ad = InputStickHID.getDownloadDialog(MainActivity.this);
- if (ad != null) {
- //InputStickUtility is missing, display dialog, (asking user to download the Utility app)
- ad.show();
- }
- //get error code and provide user with error message describing the problem:
- int errorCode = InputStickHID.getErrorCode();
- textViewInfo.setText("Error (" + Integer.toHexString(errorCode) + ") : " + InputStickError.getFullErrorMessage(errorCode));
- Toast.makeText(this, "Failed!", Toast.LENGTH_LONG).show();
- enableUI(false);
- break;
- default:
- }
- }
- @Override
- public void onLocalBufferEmpty(int interfaceId) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onRemoteBufferEmpty(int interfaceId) {
- //in this case we are interested only in keyboard buffer:
- if (interfaceId == InputStickHID.INTERFACE_KEYBOARD) {
- //we are no longer interested in receiving buffer-related callbacks
- InputStickHID.removeBufferEmptyListener(this);
- long totalTime = System.currentTimeMillis() - MainActivity.startTime;
- textViewInfo.setText("Total time: " + totalTime + "ms");
- buttonKeyboardAction.setText("Type");
- MainActivity.startTime = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement