From e1af1f22e56ef62125c9575e9f76e2d8923f4086 Mon Sep 17 00:00:00 2001 From: Asmita Poddar Date: Tue, 28 Feb 2023 17:16:25 +0000 Subject: [PATCH] Create InputManagerGlobal class InputManager should always have a context associated with it instead of injecting it sometimes by calling InputManager#getInstance(Context) as was done as a part of b/266013036. Uptil now, InputManager was a singleton class and it contained the IInputManager interface (which talks to the system server service). This CL introduces InputManagerGlobal, which will be the singleton class (with the IInputManager interface). Bug: b/267758905 Test: Pre-submit Change-Id: I1d24e2b5692d0eb7d4c23c9b6a8e1a325eaaad83 (cherry picked from commit a786f24d3312ad14ea522ed651b7c5c4f6a41cb8) Merged-In: I1d24e2b5692d0eb7d4c23c9b6a8e1a325eaaad83 --- .../android/hardware/input/InputManager.java | 21 ++--- .../hardware/input/InputManagerGlobal.java | 82 +++++++++++++++++++ 2 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 core/java/android/hardware/input/InputManagerGlobal.java diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java index a33cd9760091b..490589f34411c 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -48,8 +48,6 @@ import android.os.Looper; import android.os.Message; import android.os.Process; import android.os.RemoteException; -import android.os.ServiceManager; -import android.os.ServiceManager.ServiceNotFoundException; import android.os.SystemClock; import android.os.VibrationEffect; import android.os.Vibrator; @@ -305,8 +303,11 @@ public final class InputManager { private static String sVelocityTrackerStrategy; - private InputManager(IInputManager im) { - mIm = im; + private InputManagerGlobal mGlobal; + + private InputManager() { + mGlobal = InputManagerGlobal.getInstance(); + mIm = mGlobal.getInputManagerService(); try { sVelocityTrackerStrategy = mIm.getVelocityTrackerStrategy(); } catch (RemoteException ex) { @@ -324,7 +325,8 @@ public final class InputManager { @VisibleForTesting public static InputManager resetInstance(IInputManager inputManagerService) { synchronized (InputManager.class) { - sInstance = new InputManager(inputManagerService); + InputManagerGlobal.resetInstance(inputManagerService); + sInstance = new InputManager(); return sInstance; } } @@ -337,6 +339,7 @@ public final class InputManager { @VisibleForTesting public static void clearInstance() { synchronized (InputManager.class) { + InputManagerGlobal.clearInstance(); sInstance = null; } } @@ -364,13 +367,7 @@ public final class InputManager { public static InputManager getInstance(Context context) { synchronized (InputManager.class) { if (sInstance == null) { - try { - sInstance = new InputManager(IInputManager.Stub - .asInterface(ServiceManager.getServiceOrThrow(Context.INPUT_SERVICE))); - - } catch (ServiceNotFoundException e) { - throw new IllegalStateException(e); - } + sInstance = new InputManager(); } if (sInstance.mWeakContext == null || sInstance.mWeakContext.get() == null) { sInstance.mWeakContext = new WeakReference(context); diff --git a/core/java/android/hardware/input/InputManagerGlobal.java b/core/java/android/hardware/input/InputManagerGlobal.java new file mode 100644 index 0000000000000..82dddfc8756c8 --- /dev/null +++ b/core/java/android/hardware/input/InputManagerGlobal.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2023 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.hardware.input; + +import android.content.Context; +import android.os.IBinder; +import android.os.ServiceManager; + +/** + * Manages communication with the input manager service on behalf of + * an application process. You're probably looking for {@link InputManager}. + * + * @hide + */ +public final class InputManagerGlobal { + private static final String TAG = "InputManagerGlobal"; + + private static InputManagerGlobal sInstance; + + private final IInputManager mIm; + + public InputManagerGlobal(IInputManager im) { + mIm = im; + } + + /** + * Gets an instance of the input manager global singleton. + * + * @return The display manager instance, may be null early in system startup + * before the display manager has been fully initialized. + */ + public static InputManagerGlobal getInstance() { + synchronized (InputManagerGlobal.class) { + if (sInstance == null) { + IBinder b = ServiceManager.getService(Context.INPUT_SERVICE); + if (b != null) { + sInstance = new InputManagerGlobal(IInputManager.Stub.asInterface(b)); + } + } + return sInstance; + } + } + + public IInputManager getInputManagerService() { + return mIm; + } + + /** + * Gets an instance of the input manager. + * + * @return The input manager instance. + */ + public static InputManagerGlobal resetInstance(IInputManager inputManagerService) { + synchronized (InputManager.class) { + sInstance = new InputManagerGlobal(inputManagerService); + return sInstance; + } + } + + /** + * Clear the instance of the input manager. + */ + public static void clearInstance() { + synchronized (InputManagerGlobal.class) { + sInstance = null; + } + } +}