package com.google.samples.button;

import android.app.Activity;
import android.os.Bundle;
import android.hardware.pio.Gpio;
import android.hardware.pio.PeripheralManagerService;
import android.system.ErrnoException;
import android.util.Log;

import com.google.brillo.driver.button.Button;

/**
 * Example of using Button driver for toggling a LED.
 *
 * This activity will flip the state of the LED GPIO pin whenever the button is pressed.
 * You need to connect an LED and a push button switch to pins specified in {@link BoardDefaults}.
 */
public class ButtonActivity extends Activity {
    private static final String TAG = "ButtonActivity";

    private Gpio mLedGpio;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Starting ButtonActivity");

        PeripheralManagerService pioService = new PeripheralManagerService();
        try {
            Log.i(TAG, "Configuring GPIO pins");
            mLedGpio = pioService.openGpio(BoardDefaults.getGPIOForLED());
            mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
            mButton = new Button(BoardDefaults.getGPIOForButton(),
                    Button.LogicState.PRESSED_WHEN_HIGH);
            mButton.setOnButtonEventListener((gpio, pressed) -> {
                Log.d(TAG, "onButtonEvent:" + pressed);
                try {
                    mLedGpio.setValue(pressed);
                } catch (ErrnoException e) {
                    Log.e(TAG, "Error toggling GPIO pin", e);
                }
                return true;
         });

        } catch (ErrnoException e) {
            Log.e(TAG, "Error configuring GPIO pins", e);
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (mButton != null) {
            mButton.close();
            mButton = null;
        }
        if (mLedGpio != null) {
            mLedGpio.close();
            mLedGpio = null;
        }
    }
}

@@ -58,16 +58,118 @@
 tivity;%0A
+import android.hardware.userdriver.InputDriver;%0Aimport android.hardware.userdriver.UserDriverManager;%0A
 import a
@@ -336,16 +336,46 @@
 til.Log;
+%0Aimport android.view.KeyEvent;
 %0A%0Aimport
@@ -558,18 +558,21 @@
 ton 
-is pressed
+state changes
 .%0A *
@@ -776,16 +776,95 @@
 vity%22;%0A%0A
+    // Linux input code for Escape%0A    private static final int KEY_CODE = 1;%0A%0A
     priv
@@ -909,16 +909,55 @@
 mButton;
+%0A    private InputDriver mButtonDriver;
 %0A%0A    @O
@@ -1397,16 +1397,96 @@
 LY_LOW);
+%0A%0A            // Initialize GPIO for a button connected to Vcc through a pull-up
 %0A       
@@ -1600,88 +1600,252 @@
 HEN_
-HIGH);%0A            mButton.setOnButtonEventListener((gpio, pressed) -%3E %7B%0A       
+LOW);%0A%0A            Log.i(TAG, %22Registering framework driver%22);%0A            mButtonDriver = mButton.createInputDriver(KEY_CODE);%0A            UserDriverManager.getManager().registerInputDriver(mButtonDriver);%0A        %7D catch (ErrnoException e) %7B%0A
     
@@ -1853,45 +1853,54 @@
     
+   
 Log.
-d
+e
 (TAG, %22
-onButtonEvent:%22 + pressed
+Error configuring GPIO pins%22, e
 );%0A 
@@ -1910,238 +1910,726 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-try %7B%0A                    mLedGpio.setValue(pressed);%0A                %7D catch (ErrnoException e) %7B%0A                    Log.e(TAG, %22Error toggling GPIO pin%22, e
+@Override%0A    public boolean onKeyDown(int keyCode, KeyEvent event) %7B%0A        if (keyCode == KeyEvent.KEYCODE_ESCAPE) %7B%0A            // Turn on the LED%0A            setLedValue(true);%0A            return true;%0A        %7D%0A%0A        return super.onKeyDown(keyCode, event);%0A    %7D%0A%0A    @Override%0A    public boolean onKeyUp(int keyCode, KeyEvent event) %7B%0A        if (keyCode == KeyEvent.KEYCODE_ESCAPE) %7B%0A            // Turn off the LED%0A            setLedValue(false);%0A            return true;%0A        %7D%0A%0A        return super.onKeyUp(keyCode, event
 );%0A    
+%7D%0A%0A
     
+/**%0A
      
-   %7D%0A     
+* Update the value of the LED output.%0A
      
+*/%0A
     
-  return true;%0A         %7D
+private void setLedValue(boolean value) %7B%0A        try %7B%0A            mLedGpio.setValue(value
 );%0A
-%0A
     
@@ -2687,32 +2687,29 @@
 TAG, %22Error 
-configur
+updat
 ing GPIO pin
@@ -2697,36 +2697,37 @@
 r updating GPIO 
-pins
+value
 %22, e);%0A        %7D
@@ -2803,24 +2803,25 @@
 nDestroy();%0A
+%0A
         if (
@@ -2831,32 +2831,113 @@
 tton != null) %7B%0A
+            UserDriverManager.getManager().unregisterInputDriver(mButtonDriver);%0A
             mBut
@@ -2983,24 +2983,25 @@
 ;%0A        %7D%0A
+%0A
         if (

Use input driver in button sample.