diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java new file mode 100644 index 0000000000000..5c21fd1167e40 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2018 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.systemui.statusbar; + +import android.content.Context; +import android.database.ContentObserver; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.UserHandle; +import android.os.VibrationEffect; +import android.os.Vibrator; +import android.provider.Settings; + +public class VibratorHelper { + + private final Vibrator mVibrator; + private final Context mContext; + private boolean mHapticFeedbackEnabled; + + final private ContentObserver mVibrationObserver = new ContentObserver(Handler.getMain()) { + @Override + public void onChange(boolean selfChange) { + updateHapticFeedBackEnabled(); + } + }; + + public VibratorHelper(Context context) { + mContext = context; + mVibrator = context.getSystemService(Vibrator.class); + + mContext.getContentResolver().registerContentObserver( + Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED), true, + mVibrationObserver); + mVibrationObserver.onChange(false /* selfChange */); + } + + public void vibrate(final int effectId) { + if (mHapticFeedbackEnabled) { + AsyncTask.execute(() -> + mVibrator.vibrate(VibrationEffect.get(effectId, false /* fallback */))); + } + } + + private void updateHapticFeedBackEnabled() { + mHapticFeedbackEnabled = Settings.System.getIntForUser(mContext.getContentResolver(), + Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) != 0; + } +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java index 056874fd1dcb1..ca5a3503b5482 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java @@ -39,6 +39,7 @@ import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.os.SystemProperties; +import android.os.VibrationEffect; import android.support.annotation.ColorInt; import android.util.AttributeSet; import android.util.Log; @@ -67,6 +68,7 @@ import com.android.systemui.recents.RecentsOnboarding; import com.android.systemui.shared.system.ActivityManagerWrapper; import com.android.systemui.shared.system.NavigationBarCompat; import com.android.systemui.stackdivider.Divider; +import com.android.systemui.statusbar.VibratorHelper; import com.android.systemui.statusbar.policy.DeadZone; import com.android.systemui.statusbar.policy.KeyButtonDrawable; import com.android.systemui.statusbar.policy.TintedKeyButtonDrawable; @@ -148,6 +150,7 @@ public class NavigationBarView extends FrameLayout implements PluginListener