package bugbeechat;

import java.io.IOException;

import com.buglabs.device.ButtonEvent;
import com.buglabs.device.IButtonEventListener;

class KeyboardHandler implements IButtonEventListener {
	/**
	 * Callback when button gets pressed.
	 */
	public void buttonEvent(ButtonEvent event) {
		final int rawValue = event.getRawValue();

		if (event.getAction() == ButtonEvent.KEY_UP) {
			if (rawValue == ButtonEvent.BUTTON_HOTKEY_1) {
				toggleKeyboard();
			}
		}
	}

	/**
	 * display or hide the matchbox virtual keyboard
	 */
	private void toggleKeyboard() {
		try {
			Runtime.getRuntime().exec(new String[]{"/usr/bin/matchbox-keyboard", "-t", "bug"});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void stopKeyboard() {
		try {
			Runtime.getRuntime().exec(new String[]{"/usr/bin/matchbox-keyboard", "-h", "bug"});
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
}