/**
  * Finds an open device that has a name containing keypad. This probably is the event node
  * associated with the keypad Its purpose is to handle all hardware Android buttons such as Back,
  * Home, Volume, etc Key codes are defined in input.h (see NDK) , or use the Event Monitor to see
  * keypad messages This function sends the BACK key
  */
 public void SendBackKeyToKeypad() {
   for (InputDevice idev : events.m_Devs) {
     // * Finds an open device that has a name containing keypad. This probably is the keypad
     // associated event node
     if (idev.getOpen() && idev.getName().contains("keypad")) {
       idev.SendKey(158, true); // Back key down
       idev.SendKey(158, false); // back key up
     }
   }
 }
 /**
  * Finds an open device that has a name containing keypad. This probably is the event node
  * associated with the keypad Its purpose is to handle all hardware Android buttons such as Back,
  * Home, Volume, etc Key codes are defined in input.h (see NDK) , or use the Event Monitor to see
  * keypad messages This function sends the HOME key
  */
 public void SendHomeKeyToKeypad() {
   boolean found = false;
   for (InputDevice idev : events.m_Devs) {
     // * Finds an open device that has a name containing keypad. This probably is the keypad
     // associated event node
     if (idev.getOpen() && idev.getName().contains("keypad")) {
       idev.SendKey(102, true); // home key down
       idev.SendKey(102, false); // home key up
       found = true;
       break;
     }
   }
   if (found == false) Toast.makeText(this, "Keypad not found.", Toast.LENGTH_SHORT).show();
 }
  /**
   * Handle events when the user selects a new item in the spinner. The spinner is used to select a
   * target for the Key and Touch buttons So what we do here, is to find which open-dev has been
   * selected from within our global events.m_Devs structure result saved in m_selectedDev, as the
   * index of our selected open dev.
   */
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    int sel = arg2;

    int i = 0, j = 0;
    m_selectedDev = -1;
    for (InputDevice idev : events.m_Devs) {
      if (idev.getOpen()) {
        if (i == sel) {
          m_selectedDev = j;
          break;
        } else i++;
      }
      j++;
    }
    if (m_selectedDev != -1) {
      String name = events.m_Devs.get(m_selectedDev).getName();
      Log.d(LT, "spinner selected:" + sel + " Name:" + name);
      Toast.makeText(this, "New device selected:" + name, Toast.LENGTH_SHORT).show();
    } else Toast.makeText(this, "Invalid device selection!", Toast.LENGTH_SHORT).show();
  }
  public void onClick(View v) {
    int id = v.getId();
    if (id >= idLVFirstItem) {
      int nLVIndexClick = id - idLVFirstItem;
      Log.d(LT, "LV Item Click:" + nLVIndexClick);
      for (InputDevice idev : events.m_Devs) {
        if (idev.getId() == nLVIndexClick) {
          if (idev.Open(true)) {
            // refresh listview
            PopulateListView();
            // inform user
            Toast.makeText(this, "Device opened successfully!", Toast.LENGTH_SHORT).show();
          } else {
            Toast.makeText(this, "Device failed to open. Do you have root?", Toast.LENGTH_SHORT)
                .show();
          }
          break;
        }
      }
    }

    switch (id) {
      case idButScan:
        {
          Log.d(LT, "Scanning for input dev files.");
          // init event node files
          int res = events.Init();
          // debug results
          Log.d(LT, "Event files:" + res);
          // try opening all
          PopulateListView();
        }
      case idButInjectKey:
        if (m_selectedDev != -1) {
          // see input.h in Android NDK, sequence represents the codes for pocketmagic.net
          final int keys[] = new int[] {25, 24, 46, 37, 18, 20, 50, 30, 34, 23, 46, 52, 49, 18, 20};
          // send all these keys with half a second delay
          Thread sender =
              new Thread(
                  new Runnable() {
                    public void run() {
                      for (int i = 0; i < keys.length; i++) {
                        Log.d(
                            LT,
                            "Sending:"
                                + keys[i]
                                + " to:"
                                + events.m_Devs.get(m_selectedDev).getName());
                        events.m_Devs.get(m_selectedDev).SendKey(keys[i], true); // key down
                        events.m_Devs.get(m_selectedDev).SendKey(keys[i], false); // key up
                        // a short delay before next character, just for testing purposes
                        try {
                          Thread.sleep(1000);
                        } catch (InterruptedException e) {
                          e.printStackTrace();
                        }
                      }
                    }
                  });
          sender.start();
        } else
          Toast.makeText(
                  this,
                  "Select a valid device first, using the spinner to the left.",
                  Toast.LENGTH_SHORT)
              .show();
        break;

      case idButInjectTouch:
        // absolute coordinates, on my device they go up to 570x960
        if (m_selectedDev != -1) events.m_Devs.get(m_selectedDev).SendTouchDownAbs(155, 183);
        else
          Toast.makeText(
                  this,
                  "Select a valid device first, using the spinner to the left.",
                  Toast.LENGTH_SHORT)
              .show();
        break;

      case idButTest:
        Thread sender =
            new Thread(
                new Runnable() {
                  public void run() {
                    for (int i = 0; i < 5; i++) {
                      SendHomeKeyToKeypad();
                      // a short delay before next character, just for testing purposes
                      try {
                        Thread.sleep(2000);
                      } catch (InterruptedException e) {
                        e.printStackTrace();
                      }
                    }
                  }
                });
        sender.start();

        break;
      case idButMonitorStart:
        if (m_bMonitorOn)
          Toast.makeText(
                  this,
                  "Event monitor already working. Consider opening more devices to monitor.",
                  Toast.LENGTH_SHORT)
              .show();
        else {
          m_tvMonitor.post(
              new Runnable() {

                public void run() {
                  m_tvMonitor.setText("Event Monitor running, waiting for data.");
                }
              });
          StartEventMonitor();
        }
        break;
      case idButMonitorStop:
        Toast.makeText(this, "Event monitor stopped.", Toast.LENGTH_SHORT).show();

        StopEventMonitor();
        m_tvMonitor.post(
            new Runnable() {

              public void run() {
                m_tvMonitor.setText("Event Monitor stopped.");
              }
            });
        break;
    }
  }