/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2013 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brltty.android;

import android.util.Log;

import android.content.Intent;

import android.inputmethodservice.InputMethodService;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;

public class InputService extends InputMethodService {
  private static final String LOG_TAG = InputService.class.getName();

  private static InputService inputService = null;
  private static int startIdentifier = 0;

  public static InputService getInputService () {
    return inputService;
  }

  @Override
  public void onCreate () {
    super.onCreate();
    Log.d(LOG_TAG, "input service started");
    inputService = this;
  }

  @Override
  public void onDestroy () {
    super.onDestroy();
    Log.d(LOG_TAG, "input service stopped");
    inputService = null;
    startIdentifier = 0;
  }

  @Override
  public void onBindInput () {
    Log.d(LOG_TAG, "input service bound");
  }

  @Override
  public void onUnbindInput () {
    Log.d(LOG_TAG, "input service unbound");
  }

  @Override
  public int onStartCommand (Intent intent, int flags, int identifier) {
    startIdentifier = identifier;
    Log.d(LOG_TAG, "input service starting");
    return START_STICKY;
  }

  @Override
  public void onStartInput (EditorInfo info, boolean restarting) {
    Log.d(LOG_TAG, "input service " + (restarting? "reconnected": "connected"));
    if (info.actionLabel != null) Log.d(LOG_TAG, "action label: " + info.actionLabel);
    Log.d(LOG_TAG, "action id: " + info.actionId);
  }

  @Override
  public void onFinishInput () {
    Log.d(LOG_TAG, "input service disconnected");
  }

  public void forwardKeyEvent (int code, boolean press) {
    InputConnection connection = getCurrentInputConnection();

    if (connection != null) {
      int action = press? KeyEvent.ACTION_DOWN: KeyEvent.ACTION_UP;
      connection.sendKeyEvent(new KeyEvent(action, code));
    }
  }

  public native boolean handleKeyEvent (int code, boolean press);

  @Override
  public boolean onKeyDown (int code, KeyEvent event) {
    if (handleKeyEvent(code, true)) return true;
    return super.onKeyDown(code, event);
  }

  @Override
  public boolean onKeyUp (int code, KeyEvent event) {
    if (handleKeyEvent(code, false)) return true;
    return super.onKeyUp(code, event);
  }
}

@@ -666,16 +666,48 @@
 droid;%0A%0A
+import org.a11y.brltty.core.*;%0A%0A
 import a
@@ -2675,16 +2675,291 @@
 ress);%0A%0A
+  public boolean acceptKeyEvent (final int code, final boolean press) %7B%0A    CoreWrapper.runOnCoreThread(new Runnable() %7B%0A      @Override%0A      public void run () %7B%0A        if (!handleKeyEvent(code, press)) forwardKeyEvent(code, press);%0A      %7D%0A    %7D);%0A%0A    return true;%0A  %7D%0A%0A
   @Overr
@@ -2954,32 +2954,32 @@
  %7D%0A%0A  @Override%0A
-
   public boolean
@@ -3018,38 +3018,38 @@
 vent) %7B%0A    if (
-handle
+accept
 KeyEvent(code, t
@@ -3187,22 +3187,22 @@
     if (
-handle
+accept
 KeyEvent

Move key event handling to the core thread from the input service thread). (dm)