Cache result from VibratorService.hasVibrator in SystemVibrator

The existing implementation of performHapticFeedback in
PhoneWindowManager uses both hasVibrator and vibrate methods from
Vibrator class. The implementation of VibratorService.hasVibrator is
already constant, returning true if the device has a built in vibrator.

This adds a cache for this value to SystemVibrator, to avoid binder
calls to system_server after the first one.

Bug: 170127981
Test: manual
Change-Id: I68934a41068f4388449321d08a42d8bf890ea91e
This commit is contained in:
Lais Andrade
2021-01-19 17:13:45 +00:00
parent ee605b6465
commit e69b5f4177

View File

@@ -17,6 +17,7 @@
package android.os;
import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
@@ -27,6 +28,8 @@ import android.util.Log;
import com.android.internal.annotations.GuardedBy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
import java.util.concurrent.Executor;
@@ -38,6 +41,14 @@ import java.util.concurrent.Executor;
public class SystemVibrator extends Vibrator {
private static final String TAG = "Vibrator";
private static final int VIBRATOR_PRESENT_UNKNOWN = 0;
private static final int VIBRATOR_PRESENT_YES = 1;
private static final int VIBRATOR_PRESENT_NO = 2;
@Retention(RetentionPolicy.SOURCE)
@IntDef({VIBRATOR_PRESENT_UNKNOWN, VIBRATOR_PRESENT_YES, VIBRATOR_PRESENT_NO})
private @interface VibratorPresent {}
private final IVibratorService mService;
private final IVibratorManagerService mManagerService;
private final Object mLock = new Object();
@@ -45,6 +56,9 @@ public class SystemVibrator extends Vibrator {
private final Context mContext;
@GuardedBy("mLock")
private VibratorInfo mVibratorInfo;
@GuardedBy("mLock")
@VibratorPresent
private int mVibratorPresent;
@GuardedBy("mDelegates")
private final ArrayMap<OnVibratorStateChangedListener,
@@ -69,15 +83,18 @@ public class SystemVibrator extends Vibrator {
@Override
public boolean hasVibrator() {
if (mService == null) {
Log.w(TAG, "Failed to vibrate; no vibrator service.");
try {
synchronized (mLock) {
if (mVibratorPresent == VIBRATOR_PRESENT_UNKNOWN && mService != null) {
mVibratorPresent =
mService.hasVibrator() ? VIBRATOR_PRESENT_YES : VIBRATOR_PRESENT_NO;
}
return mVibratorPresent == VIBRATOR_PRESENT_YES;
}
} catch (RemoteException e) {
Log.w(TAG, "Failed to query vibrator presence", e);
return false;
}
try {
return mService.hasVibrator();
} catch (RemoteException e) {
}
return false;
}
/**