|
import android.app.Activity; |
|
import android.app.Application; |
|
import android.os.Bundle; |
|
import android.util.Log; |
|
|
|
import com.google.android.things.pio.Gpio; |
|
import com.google.android.things.pio.GpioCallback; |
|
import com.google.android.things.pio.PeripheralManagerService; |
|
|
|
import java.io.IOException; |
|
|
|
class RunnableButton { |
|
|
|
private final PeripheralManagerService service; |
|
private final Runnable runnable; |
|
private final String buttonPin; |
|
|
|
private Gpio bus; |
|
|
|
static RunnableButton create(String pin, Runnable runnable) { |
|
return new RunnableButton(new PeripheralManagerService(), pin, runnable); |
|
} |
|
|
|
private RunnableButton(PeripheralManagerService service, String buttonPin, Runnable runnable) { |
|
this.service = service; |
|
this.runnable = runnable; |
|
this.buttonPin = buttonPin; |
|
} |
|
|
|
void onCreateInjectInto(final Activity injectedActivity) { |
|
onCreate(); |
|
final Application application = (Application) injectedActivity.getApplicationContext(); |
|
Application.ActivityLifecycleCallbacks lifecycleCallbacks = new Application.ActivityLifecycleCallbacks() { |
|
@Override |
|
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { |
|
if (theSame(activity, injectedActivity)) { |
|
onCreate(); |
|
} |
|
} |
|
|
|
/** |
|
* @return true if same activity **assumes** one instance of an Activity i.e. compares activity name |
|
*/ |
|
private boolean theSame(Activity activity1, Activity activity2) { |
|
return activity1.getClass().getSimpleName().equals(activity2.getClass().getSimpleName()); |
|
} |
|
|
|
@Override |
|
public void onActivityStarted(Activity activity) { |
|
if (theSame(activity, injectedActivity)) { |
|
onStart(); |
|
} |
|
} |
|
|
|
@Override |
|
public void onActivityResumed(Activity activity) { |
|
// not used |
|
} |
|
|
|
@Override |
|
public void onActivityPaused(Activity activity) { |
|
// not used |
|
} |
|
|
|
@Override |
|
public void onActivityStopped(Activity activity) { |
|
if (theSame(activity, injectedActivity)) { |
|
onStop(); |
|
} |
|
} |
|
|
|
@Override |
|
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { |
|
// not used |
|
} |
|
|
|
@Override |
|
public void onActivityDestroyed(Activity activity) { |
|
if (theSame(activity, injectedActivity)) { |
|
onDestroy(); |
|
} |
|
application.unregisterActivityLifecycleCallbacks(this); |
|
} |
|
}; |
|
application.registerActivityLifecycleCallbacks(lifecycleCallbacks); |
|
} |
|
|
|
private void onCreate() { |
|
try { |
|
bus = service.openGpio(buttonPin); |
|
} catch (IOException e) { |
|
throw new IllegalStateException(buttonPin + " bus cannot be opened.", e); |
|
} |
|
|
|
try { |
|
bus.setDirection(Gpio.DIRECTION_IN); |
|
bus.setActiveType(Gpio.ACTIVE_LOW); |
|
} catch (IOException e) { |
|
throw new IllegalStateException(buttonPin + " bus cannot be configured.", e); |
|
} |
|
} |
|
|
|
private void onStart() { |
|
try { |
|
bus.setEdgeTriggerType(Gpio.EDGE_BOTH); |
|
bus.registerGpioCallback(touchButtonACallback); |
|
} catch (IOException e) { |
|
throw new IllegalStateException(buttonPin + " bus cannot be monitored.", e); |
|
} |
|
} |
|
|
|
private final GpioCallback touchButtonACallback = new GpioCallback() { |
|
@Override |
|
public boolean onGpioEdge(Gpio gpio) { |
|
try { |
|
if (gpio.getValue()) { |
|
Log.i("TUT", "ON PRESSED DOWN"); |
|
} else { |
|
runnable.run(); |
|
} |
|
} catch (IOException e) { |
|
throw new IllegalStateException(buttonPin + " cannot be read.", e); |
|
} |
|
return true; |
|
} |
|
}; |
|
|
|
private void onStop() { |
|
bus.unregisterGpioCallback(touchButtonACallback); |
|
} |
|
|
|
private void onDestroy() { |
|
try { |
|
bus.close(); |
|
} catch (IOException e) { |
|
Log.e("TUT", buttonPin + " bus cannot be closed, you may experience errors on next launch.", e); |
|
} |
|
} |
|
|
|
} |