// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.mojo.keyboard;

import android.content.Context;
import android.text.InputType;
import android.view.inputmethod.InputMethodManager;

import org.chromium.mojo.system.MojoException;
import org.chromium.mojom.keyboard.KeyboardClient;
import org.chromium.mojom.keyboard.KeyboardService;
import org.chromium.mojom.keyboard.KeyboardType;

/**
 * Android implementation of Keyboard.
 */
public class KeyboardServiceImpl implements KeyboardService {
    // We have a unique ServiceImpl per connection.  However the state
    // for the keyboard instance is per-view.  However we don't have the
    // concept of per-view services, so we currently have a hack by which
    // we set the "active view" and its associated per-view keyboard state.
    private static KeyboardServiceState sViewState;
    private Context mContext;

    public KeyboardServiceImpl(Context context) {
        mContext = context;
    }

    public static void setViewState(KeyboardServiceState state) {
        if (sViewState != null) sViewState.close();
        sViewState = state;
    }

    private static int inputTypeFromKeyboardType(int keyboardType) {
        if (keyboardType == KeyboardType.DATETIME) return InputType.TYPE_CLASS_DATETIME;
        if (keyboardType == KeyboardType.NUMBER) return InputType.TYPE_CLASS_NUMBER;
        if (keyboardType == KeyboardType.PHONE) return InputType.TYPE_CLASS_PHONE;
        return InputType.TYPE_CLASS_TEXT;
    }

    @Override
    public void close() {
        if (sViewState == null) return;
        sViewState.close();
    }

    @Override
    public void onConnectionError(MojoException e) {}

    @Override
    public void show(KeyboardClient client, int keyboardType) {
        if (sViewState == null) return;
        sViewState.setClient(client, inputTypeFromKeyboardType(keyboardType));
        InputMethodManager imm =
                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.restartInput(sViewState.getView());
        imm.showSoftInput(sViewState.getView(), InputMethodManager.SHOW_IMPLICIT);
    }

    @Override
    public void showByRequest() {
        if (sViewState == null) return;
        InputMethodManager imm =
                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(sViewState.getView(), 0);
    }

    @Override
    public void hide() {
        if (sViewState == null) return;
        InputMethodManager imm =
                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(sViewState.getView().getApplicationWindowToken(), 0);
        close();
    }

    @Override
    public void setText(String text) {
        sViewState.setText(text);
        InputMethodManager imm =
                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.restartInput(sViewState.getView());
    }

    @Override
    public void setSelection(int start, int end) {
        sViewState.setSelection(start, end);
    }
}

@@ -629,316 +629,15 @@
     
-// We have a unique ServiceImpl per connection.  However the state%0A    // for the keyboard instance is per-view.  However we don't have the%0A    // concept of per-view services, so we currently have a hack by which%0A    // we set the %22active view%22 and its associated per-view keyboard state.%0A    private static
+private
  Key
@@ -654,17 +654,17 @@
 ceState 
-s
+m
 ViewStat
@@ -739,24 +739,52 @@
 text context
+, KeyboardServiceState state
 ) %7B%0A        
@@ -805,133 +805,8 @@
 text
-;%0A    %7D%0A%0A    public static void setViewState(KeyboardServiceState state) %7B%0A        if (sViewState != null) sViewState.close()
 ;%0A  
@@ -803,33 +803,33 @@
 ontext;%0A        
-s
+m
 ViewState = stat
@@ -1265,49 +1265,9 @@
     
-if (sViewState == null) return;%0A        s
+m
 View
@@ -1447,49 +1447,9 @@
     
-if (sViewState == null) return;%0A        s
+m
 View
@@ -1658,33 +1658,33 @@
 mm.restartInput(
-s
+m
 ViewState.getVie
@@ -1707,33 +1707,33 @@
 m.showSoftInput(
-s
+m
 ViewState.getVie
@@ -1831,48 +1831,8 @@
 ) %7B%0A
-        if (sViewState == null) return;%0A
     
@@ -1976,25 +1976,25 @@
 owSoftInput(
-s
+m
 ViewState.ge
@@ -2056,48 +2056,8 @@
 ) %7B%0A
-        if (sViewState == null) return;%0A
     
@@ -2215,17 +2215,17 @@
 mWindow(
-s
+m
 ViewStat
@@ -2350,42 +2350,8 @@
 ) %7B%0A
-        sViewState.setText(text);%0A
     
@@ -2498,17 +2498,17 @@
 rtInput(
-s
+m
 ViewStat
@@ -2601,17 +2601,17 @@
         
-s
+m
 ViewStat

Revert changes Mojo changes to KeyboardServiceImpl.java