Advertisement
qubas

typetextfile

Aug 11th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. package com.inputstick.apps.typetextfile;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import android.app.Activity;
  8. import android.app.AlertDialog;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.inputstick.api.ConnectionManager;
  17. import com.inputstick.api.InputStickError;
  18. import com.inputstick.api.InputStickStateListener;
  19. import com.inputstick.api.OnEmptyBufferListener;
  20. import com.inputstick.api.basic.InputStickHID;
  21. import com.inputstick.api.basic.InputStickKeyboard;
  22. import com.inputstick.api.hid.HIDKeycodes;
  23. import com.inputstick.api.layout.KeyboardLayout;
  24.  
  25. public class MainActivity extends Activity  implements InputStickStateListener, OnEmptyBufferListener {
  26.    
  27.     //file is located in /res/raw
  28.     private static final int FILE = R.raw.adobe;
  29.     //make sure layout matches keyboard layout used by USB host!!!
  30.     private static final String LAYOUT_CODE = "en-US"; // pl-PL, de-DE etc, see: com.inputstick.api.layout
  31.  
  32.     private Button buttonConnect;
  33.     private Button buttonKeyboardAction;
  34.     private TextView textViewInfo;
  35.    
  36.     private static long startTime;
  37.    
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_main);
  42.        
  43.        
  44.         textViewInfo = (TextView)findViewById(R.id.textViewInfo);
  45.         textViewInfo.setText("");
  46.        
  47.         buttonConnect = (Button)findViewById(R.id.buttonConnect);
  48.         buttonConnect.setOnClickListener(new OnClickListener() {
  49.             public void onClick(View arg0) {               
  50.                 //choose action, basing on current state of InputStick device
  51.                 int state = InputStickHID.getState();
  52.                 switch (state) {
  53.                     case ConnectionManager.STATE_CONNECTED:
  54.                     case ConnectionManager.STATE_CONNECTING:
  55.                     case ConnectionManager.STATE_READY:
  56.                         InputStickHID.disconnect();
  57.                         break;
  58.                     case ConnectionManager.STATE_DISCONNECTED:
  59.                     case ConnectionManager.STATE_FAILURE:  
  60.                         InputStickHID.connect(MainActivity.this.getApplication());                     
  61.                         break;                                         
  62.                 }                  
  63.             }
  64.         });
  65.        
  66.         buttonKeyboardAction = (Button)findViewById(R.id.buttonKeyboardAction);
  67.         buttonKeyboardAction.setOnClickListener(new OnClickListener() {
  68.             public void onClick(View arg0) {
  69.                 //if remote buffer is not empty, text is curently being types
  70.                 if (InputStickHID.isKeyboardRemoteBufferEmpty()) {
  71.                     //make sure layout matches keyboard layout used by USB host!!!
  72.                     KeyboardLayout layout = KeyboardLayout.getLayout(LAYOUT_CODE);     
  73.                     BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(MainActivity.this.getResources().openRawResource(FILE)));
  74.                     String line;
  75.                     boolean firstLine = true;
  76.                     try {
  77.                         while ((line = bufferedreader.readLine()) != null) {
  78.                             if (!firstLine) {
  79.                                 //press Enter key
  80.                                 InputStickKeyboard.pressAndRelease((byte)0, HIDKeycodes.KEY_ENTER);
  81.                             } else {
  82.                                 MainActivity.startTime = System.currentTimeMillis();
  83.                                 firstLine = false;
  84.                             }
  85.                             //type text:
  86.                             layout.type(line);
  87.                                                        
  88.                         }
  89.                         //we want to receive callback, once all text is received by USB host
  90.                         InputStickHID.addBufferEmptyListener(MainActivity.this);
  91.                     } catch (IOException e) {
  92.                         e.printStackTrace();
  93.                     } finally {
  94.                         try {
  95.                             bufferedreader.close();
  96.                         }
  97.                         catch(IOException e) {
  98.                         }                      
  99.                     }
  100.                     buttonKeyboardAction.setText("Cancel");
  101.                 } else {
  102.                     //cancel typing
  103.                     InputStickHID.removeBufferEmptyListener(MainActivity.this);
  104.                     InputStickHID.clearKeyboardBuffer();
  105.                     MainActivity.startTime = 0;                
  106.                     buttonKeyboardAction.setText("Type");
  107.                 }
  108.             }
  109.         });                
  110.     }
  111.    
  112.     @Override
  113.     protected void onResume() {
  114.         super.onResume();
  115.         //we want to receive updates about change of state, in order to update UI
  116.         InputStickHID.addStateListener(this);
  117.         manageUI(InputStickHID.getState());  
  118.     }
  119.    
  120.     @Override
  121.     protected void onPause() {     
  122.         super.onPause();
  123.         //state updates are no longer needed
  124.         InputStickHID.removeStateListener(this);       
  125.     }
  126.  
  127.     @Override
  128.     public void onStateChanged(int state) {
  129.         manageUI(state);       
  130.     }
  131.    
  132.     private void enableUI(boolean enabled) {
  133.         buttonKeyboardAction.setEnabled(enabled);  
  134.         if (!enabled) {
  135.             buttonKeyboardAction.setText("Type");      
  136.         }
  137.     }
  138.    
  139.     private void manageUI(int state) {
  140.         if (MainActivity.startTime == 0) {
  141.             buttonKeyboardAction.setText("Type");      
  142.         } else {
  143.             buttonKeyboardAction.setText("Cancel");    
  144.         }
  145.         //set UI according to current state
  146.         switch (state) {
  147.         case ConnectionManager.STATE_DISCONNECTED:
  148.             buttonConnect.setText("Connect");
  149.             enableUI(false);
  150.             break;
  151.         case ConnectionManager.STATE_CONNECTING:
  152.             buttonConnect.setText("Cancel");
  153.             enableUI(false);
  154.             break;
  155.         case ConnectionManager.STATE_CONNECTED:
  156.             buttonConnect.setText("Disconnect");
  157.             enableUI(false);
  158.             break;
  159.         case ConnectionManager.STATE_READY:
  160.             buttonConnect.setText("Disconnect");
  161.             enableUI(true);
  162.             break;
  163.         case ConnectionManager.STATE_FAILURE:          
  164.             buttonConnect.setText("Connect");
  165.             //check if the problem is caused by missing InputStickUtility app:         
  166.             AlertDialog ad = InputStickHID.getDownloadDialog(MainActivity.this);
  167.             if (ad != null) {
  168.                 //InputStickUtility is missing, display dialog, (asking user to download the Utility app)
  169.                 ad.show();
  170.             }
  171.             //get error code and provide user with error message describing the problem:
  172.             int errorCode = InputStickHID.getErrorCode();
  173.             textViewInfo.setText("Error (" + Integer.toHexString(errorCode) + ") : " + InputStickError.getFullErrorMessage(errorCode));
  174.             Toast.makeText(this, "Failed!", Toast.LENGTH_LONG).show();
  175.             enableUI(false);
  176.             break;
  177.         default:
  178.         }
  179.     }
  180.  
  181.     @Override
  182.     public void onLocalBufferEmpty(int interfaceId) {
  183.         // TODO Auto-generated method stub     
  184.     }
  185.  
  186.     @Override
  187.     public void onRemoteBufferEmpty(int interfaceId) {
  188.         //in this case we are interested only in keyboard buffer:
  189.         if (interfaceId == InputStickHID.INTERFACE_KEYBOARD) {
  190.             //we are no longer interested in receiving buffer-related callbacks
  191.             InputStickHID.removeBufferEmptyListener(this);
  192.             long totalTime = System.currentTimeMillis() - MainActivity.startTime;
  193.             textViewInfo.setText("Total time: " + totalTime + "ms");   
  194.             buttonKeyboardAction.setText("Type");
  195.             MainActivity.startTime = 0;
  196.         }      
  197.     }  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement