LayoutLib: updated fake accessbility manager and ensure there's an InputMethodManager

We had replaced the accessibility Manager but it lacked some new API. Obvisouly
this is fragile and should be fixed, but this works for now.

After fixing this there was another issue with the lack of InputMethodManager.
To fix this I had to create an implementation of IInputMethodManager which
normally comes from a binder object.

I may want to do a similar trick with the accessibility manager later.

Change-Id: I28c6494e333f39072f348d0199124efac93256a5
diff --git a/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java b/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java
index 251c053..1fd7836 100644
--- a/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java
+++ b/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java
@@ -16,8 +16,11 @@
 
 package android.view.accessibility;
 
+import android.accessibilityservice.AccessibilityServiceInfo;
 import android.content.Context;
 import android.content.pm.ServiceInfo;
+import android.view.IWindow;
+import android.view.View;
 
 import java.util.Collections;
 import java.util.List;
@@ -38,6 +41,19 @@
     private static AccessibilityManager sInstance = new AccessibilityManager();
 
     /**
+     * Listener for the accessibility state.
+     */
+    public interface AccessibilityStateChangeListener {
+
+        /**
+         * Called back on change in the accessibility state.
+         *
+         * @param enabled Whether accessibility is enabled.
+         */
+        public void onAccessibilityStateChanged(boolean enabled);
+    }
+
+    /**
      * Get an AccessibilityManager instance (create one if necessary).
      *
      * @hide
@@ -92,4 +108,30 @@
         List<ServiceInfo> services = null;
         return Collections.unmodifiableList(services);
     }
+
+    public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList() {
+        // normal implementation does this in some case, so let's do the same
+        // (unmodifiableList wrapped around null).
+        List<AccessibilityServiceInfo> services = null;
+        return Collections.unmodifiableList(services);
+    }
+
+    public boolean addAccessibilityStateChangeListener(
+            AccessibilityStateChangeListener listener) {
+        return true;
+    }
+
+    public boolean removeAccessibilityStateChangeListener(
+            AccessibilityStateChangeListener listener) {
+        return true;
+    }
+
+    public int addAccessibilityInteractionConnection(IWindow windowToken,
+            IAccessibilityInteractionConnection connection) {
+        return View.NO_ID;
+    }
+
+    public void removeAccessibilityInteractionConnection(IWindow windowToken) {
+    }
+
 }
diff --git a/tools/layoutlib/bridge/src/android/view/inputmethod/InputMethodManager_Delegate.java b/tools/layoutlib/bridge/src/android/view/inputmethod/InputMethodManager_Delegate.java
new file mode 100644
index 0000000..ec7a67e
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/view/inputmethod/InputMethodManager_Delegate.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.view.inputmethod;
+
+import com.android.layoutlib.bridge.android.BridgeIInputMethodManager;
+import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
+
+import android.os.Looper;
+
+
+/**
+ * Delegate used to provide new implementation of a select few methods of {@link InputMethodManager}
+ *
+ * Through the layoutlib_create tool, the original  methods of InputMethodManager have been replaced
+ * by calls to methods of the same name in this delegate class.
+ *
+ */
+public class InputMethodManager_Delegate {
+
+    // ---- Overridden methods ----
+
+    @LayoutlibDelegate
+    /*package*/ static InputMethodManager getInstance(Looper mainLooper) {
+        synchronized (InputMethodManager.mInstanceSync) {
+            if (InputMethodManager.mInstance != null) {
+                return InputMethodManager.mInstance;
+            }
+
+            InputMethodManager.mInstance = new InputMethodManager(new BridgeIInputMethodManager(),
+                    mainLooper);
+        }
+        return InputMethodManager.mInstance;
+
+    }
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java
new file mode 100644
index 0000000..1394c32
--- /dev/null
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge.android;
+
+import com.android.internal.view.IInputContext;
+import com.android.internal.view.IInputMethodClient;
+import com.android.internal.view.IInputMethodManager;
+import com.android.internal.view.InputBindResult;
+
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.os.ResultReceiver;
+import android.text.style.SuggestionSpan;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodSubtype;
+
+import java.util.List;
+
+/**
+ * Basic implementation of IInputMethodManager that does nothing.
+ *
+ */
+public class BridgeIInputMethodManager implements IInputMethodManager {
+
+    public void addClient(IInputMethodClient arg0, IInputContext arg1, int arg2, int arg3)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void finishInput(IInputMethodClient arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public InputMethodSubtype getCurrentInputMethodSubtype() throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public List<InputMethodInfo> getEnabledInputMethodList() throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo arg0,
+            boolean arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public List<InputMethodInfo> getInputMethodList() throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public InputMethodSubtype getLastInputMethodSubtype() throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public List getShortcutInputMethodsAndSubtypes() throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public void hideMySoftInput(IBinder arg0, int arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public boolean hideSoftInput(IInputMethodClient arg0, int arg1, ResultReceiver arg2)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public boolean notifySuggestionPicked(SuggestionSpan arg0, String arg1, int arg2)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void registerSuggestionSpansForNotification(SuggestionSpan[] arg0)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void removeClient(IInputMethodClient arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public boolean setAdditionalInputMethodSubtypes(IBinder arg0, InputMethodSubtype[] arg1)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public boolean setCurrentInputMethodSubtype(InputMethodSubtype arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void setImeWindowStatus(IBinder arg0, int arg1, int arg2) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void setInputMethod(IBinder arg0, String arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void setInputMethodAndSubtype(IBinder arg0, String arg1, InputMethodSubtype arg2)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public boolean setInputMethodEnabled(String arg0, boolean arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void showInputMethodAndSubtypeEnablerFromClient(IInputMethodClient arg0, String arg1)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void showInputMethodPickerFromClient(IInputMethodClient arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void showMySoftInput(IBinder arg0, int arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public boolean showSoftInput(IInputMethodClient arg0, int arg1, ResultReceiver arg2)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public InputBindResult startInput(IInputMethodClient arg0, IInputContext arg1, EditorInfo arg2,
+            boolean arg3, boolean arg4) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public boolean switchToLastInputMethod(IBinder arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void updateStatusIcon(IBinder arg0, String arg1, int arg2) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void windowGainedFocus(IInputMethodClient arg0, IBinder arg1, boolean arg2,
+            boolean arg3, int arg4, boolean arg5, int arg6) throws RemoteException {
+        // TODO Auto-generated method stub
+
+    }
+
+    public IBinder asBinder() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java
index 20f7195..d40222f 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java
@@ -33,8 +33,10 @@
 
 import android.content.res.Configuration;
 import android.os.HandlerThread_Delegate;
+import android.os.Looper;
 import android.util.DisplayMetrics;
 import android.view.ViewConfiguration;
+import android.view.inputmethod.InputMethodManager;
 
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
@@ -224,6 +226,9 @@
         mContext.initResources();
         sCurrentContext = mContext;
 
+        // create an InputMethodManager
+        InputMethodManager.getInstance(Looper.myLooper());
+
         LayoutLog currentLog = mParams.getLog();
         Bridge.setLog(currentLog);
         mContext.getRenderResources().setFrameworkResourceIdProvider(this);
@@ -245,6 +250,9 @@
         // clear the stored ViewConfiguration since the map is per density and not per context.
         ViewConfiguration.sConfigurations.clear();
 
+        // remove the InputMethodManager
+        InputMethodManager.mInstance = null;
+
         sCurrentContext = null;
 
         Bridge.setLog(null);
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index f385805..df7e04f 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -107,6 +107,7 @@
         "android.view.LayoutInflater#rInflate",
         "android.view.LayoutInflater#parseInclude",
         "android.view.View#isInEditMode",
+        "android.view.inputmethod.InputMethodManager#getInstance",
         "com.android.internal.util.XmlUtils#convertValueToInt",
         // TODO: comment out once DelegateClass is working
     };