Advertisement
qubas

InputStickAPIDemoMain

Feb 11th, 2015
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.95 KB | None | 0 0
  1. package com.inputstick.apps.apidemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Intent;
  6. import android.graphics.LightingColorFilter;
  7. import android.graphics.drawable.Drawable;
  8. import android.net.Uri;
  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.basic.InputStickHID;
  20.  
  21. public class MainActivity extends Activity implements InputStickStateListener {
  22.  
  23.   private TextView textViewState;
  24.  
  25.   private Button buttonConnect;
  26.   private Button buttonKeyboard;
  27.   private Button buttonMouse;
  28.   private Button buttonMedia;
  29.   private Button buttonGamepad;
  30.   private Button buttonWeb;
  31.  
  32.   private static final LightingColorFilter f = new LightingColorFilter(0xFFFFFFFF, 0xFFFFFFFF);
  33.  
  34.   @Override
  35.   protected void onCreate(Bundle savedInstanceState) {
  36.     super.onCreate(savedInstanceState);
  37.     setContentView(R.layout.activity_main);
  38.    
  39.     textViewState = (TextView)findViewById(R.id.textViewState);
  40.    
  41.     buttonConnect = (Button)findViewById(R.id.buttonConnect);
  42.     buttonConnect.setOnClickListener(new OnClickListener() {
  43.       public void onClick(View arg0) {      
  44.         //depending on current state:, choose appropriate action:
  45.         int state = InputStickHID.getState();
  46.         switch (state) {
  47.           case ConnectionManager.STATE_CONNECTED:
  48.           case ConnectionManager.STATE_CONNECTING:
  49.           case ConnectionManager.STATE_READY:
  50.             //close Bluetooth connection
  51.             InputStickHID.disconnect();
  52.             break;
  53.           case ConnectionManager.STATE_DISCONNECTED:
  54.           case ConnectionManager.STATE_FAILURE:  
  55.             InputStickHID.connect(MainActivity.this.getApplication());
  56.            
  57.             /* note: you may also use direct Bluetooth connection.
  58.              * In such case it is not longer necessary that InputStickUtility is installed on the device.
  59.              * However there are other requirements:
  60.              * -BLUETOOTH and BLUETOOTH_ADMIN permissions must be added to manifest file,
  61.              * -MAC address must be manually provided
  62.              * -if InputStick is password protected, encryption key must be also provided (MD5(password)), use null otherwise
  63.              *
  64.              * InputStickHID.connect(MainActivity.this.getApplication(), "30:14:07:31:43:59", null); //InputStick BT2.0
  65.              * InputStickHID.connect(MainActivity.this.getApplication(), "20:CD:39:B0:D0:26", null, true); //InputStick BT4.0
  66.              */          
  67.            
  68.             break;                      
  69.         }                    
  70.       }
  71.     });
  72.    
  73.     buttonKeyboard = (Button)findViewById(R.id.buttonKeyboard);
  74.     buttonKeyboard.setOnClickListener(new OnClickListener() {
  75.       public void onClick(View arg0) {      
  76.         startActivity(new Intent(MainActivity.this, KeyboardDemoActivity.class));
  77.       }
  78.     });
  79.     buttonMouse = (Button)findViewById(R.id.buttonMouse);
  80.     buttonMouse.setOnClickListener(new OnClickListener() {
  81.       public void onClick(View arg0) {      
  82.         startActivity(new Intent(MainActivity.this, MouseDemoActivity.class));
  83.       }
  84.     });
  85.     buttonMedia = (Button)findViewById(R.id.buttonMedia);
  86.     buttonMedia.setOnClickListener(new OnClickListener() {
  87.       public void onClick(View arg0) {      
  88.         startActivity(new Intent(MainActivity.this, MediaDemoActivity.class));
  89.       }
  90.     });
  91.     buttonGamepad = (Button)findViewById(R.id.buttonGamepad);
  92.     buttonGamepad.setOnClickListener(new OnClickListener() {
  93.       public void onClick(View arg0) {      
  94.         startActivity(new Intent(MainActivity.this, GamepadDemoActivity.class));
  95.       }
  96.     });
  97.     buttonWeb = (Button)findViewById(R.id.buttonWeb);
  98.     buttonWeb.setOnClickListener(new OnClickListener() {
  99.       public void onClick(View arg0) {      
  100.         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.inputstick.com/developers")));  
  101.       }
  102.     });
  103.   }
  104.  
  105.   @Override
  106.   public void onPause() {  
  107.     //state updates are no longer needed
  108.     InputStickHID.removeStateListener(this);
  109.     super.onPause();  
  110.   }  
  111.  
  112.   @Override
  113.   public void onResume() {
  114.     super.onResume();
  115.     //onStateChanged will be called when connection state changes
  116.     InputStickHID.addStateListener(MainActivity.this);
  117.     manageUI(InputStickHID.getState());  
  118.   }  
  119.  
  120.   @Override
  121.   public void onBackPressed() {
  122.     InputStickHID.disconnect();  
  123.     //you may also choose to keep the connection alive and disconnect only when "buttonConnect" is clicked
  124.     super.onBackPressed();
  125.   }  
  126.  
  127.    
  128.   @Override
  129.   public void onStateChanged(int state) {
  130.     //make UI adjustments for new connection state
  131.     manageUI(state);  
  132.   }
  133.  
  134.   private void manageUI(int state) {
  135.     /*
  136.      * Depending on connection state:
  137.      * - set appropriate text on "Connect" button
  138.      * - display information about connection state
  139.      * - change color filter of button icons.
  140.      * Since the buttons are not responsible for sending any USB reports it is not necessary to enable/disabele them.
  141.      */
  142.     switch (state) {
  143.       case ConnectionManager.STATE_DISCONNECTED:
  144.         textViewState.setText("State: Disconnected");
  145.         buttonConnect.setText("Connect");
  146.         enableUI(false);        
  147.         break;
  148.       case ConnectionManager.STATE_CONNECTING:
  149.         textViewState.setText("State: Connecting");
  150.         buttonConnect.setText("Cancel");
  151.         enableUI(false);
  152.         break;
  153.       case ConnectionManager.STATE_CONNECTED:
  154.         textViewState.setText("State: Connected");
  155.         //Bluetooth connection was established, it is now possible to send commands to InputStick
  156.         //however USB host is not ready yet to accept keyboard or mouse reports!
  157.         buttonConnect.setText("Disconnect");
  158.         enableUI(false);
  159.         break;
  160.       case ConnectionManager.STATE_READY:
  161.         textViewState.setText("State: Ready");
  162.         //InputStick is ready to communicate with USB host
  163.         buttonConnect.setText("Disconnect");
  164.         enableUI(true);
  165.         break;
  166.       case ConnectionManager.STATE_FAILURE:
  167.         textViewState.setText("State: Failure");
  168.         //after STATE_FAILURE, STATE_DISCONNECTED will occur, so it is not necessary to manage UI here
  169.         //buttonConnect.setText("Connect");
  170.         //enableUI(false);
  171.        
  172.         //maybe InputStickUtility is not installed?
  173.         AlertDialog ad = InputStickHID.getDownloadDialog(MainActivity.this);
  174.         //if that caused the problem, ad will be != null
  175.         if (ad != null) {
  176.           ad.show();
  177.         } else {
  178.           //something else has caused the problem.
  179.           //Get error code & show error message              
  180.           Toast.makeText(MainActivity.this, "Connection failed: " + InputStickError.getFullErrorMessage(InputStickHID.getErrorCode()), Toast.LENGTH_LONG).show();      
  181.         }                
  182.         break;
  183.       default:  
  184.     }
  185.   }
  186.  
  187.   private void enableUI(boolean enabled) {
  188.     Drawable d;
  189.     if (enabled) {    
  190.       d = buttonKeyboard.getCompoundDrawables()[2];
  191.       d.clearColorFilter();
  192.       d = buttonMouse.getCompoundDrawables()[2];
  193.       d.clearColorFilter();
  194.       d = buttonMedia.getCompoundDrawables()[2];
  195.       d.clearColorFilter();
  196.       d = buttonGamepad.getCompoundDrawables()[2];
  197.       d.clearColorFilter();
  198.     } else {
  199.       d = buttonKeyboard.getCompoundDrawables()[2];
  200.       d.setColorFilter(f);
  201.       d = buttonMouse.getCompoundDrawables()[2];
  202.       d.setColorFilter(f);
  203.       d = buttonMedia.getCompoundDrawables()[2];
  204.       d.setColorFilter(f);
  205.       d = buttonGamepad.getCompoundDrawables()[2];
  206.       d.setColorFilter(f);
  207.     }    
  208.   }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement