Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e69b5f41 authored by Lais Andrade's avatar Lais Andrade
Browse files

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
parent ee605b64
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -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,16 +83,19 @@ public class SystemVibrator extends Vibrator {

    @Override
    public boolean hasVibrator() {
        if (mService == null) {
            Log.w(TAG, "Failed to vibrate; no vibrator service.");
            return false;
        }
        try {
            return mService.hasVibrator();
        } catch (RemoteException e) {
            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;
        }
    }

    /**
     * Check whether the vibrator is vibrating.