Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.database.ContentObserver; |
Beverly | 512077e | 2018-03-15 13:54:43 -0400 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 22 | import android.os.AsyncTask; |
| 23 | import android.os.Handler; |
| 24 | import android.os.UserHandle; |
| 25 | import android.os.VibrationEffect; |
| 26 | import android.os.Vibrator; |
| 27 | import android.provider.Settings; |
| 28 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | /** |
| 33 | */ |
| 34 | @Singleton |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 35 | public class VibratorHelper { |
| 36 | |
| 37 | private final Vibrator mVibrator; |
| 38 | private final Context mContext; |
| 39 | private boolean mHapticFeedbackEnabled; |
Beverly | 512077e | 2018-03-15 13:54:43 -0400 | [diff] [blame] | 40 | private static final AudioAttributes STATUS_BAR_VIBRATION_ATTRIBUTES = |
| 41 | new AudioAttributes.Builder() |
| 42 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 43 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 44 | .build(); |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 45 | |
| 46 | final private ContentObserver mVibrationObserver = new ContentObserver(Handler.getMain()) { |
| 47 | @Override |
| 48 | public void onChange(boolean selfChange) { |
| 49 | updateHapticFeedBackEnabled(); |
| 50 | } |
| 51 | }; |
| 52 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 53 | /** |
| 54 | */ |
| 55 | @Inject |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 56 | public VibratorHelper(Context context) { |
| 57 | mContext = context; |
| 58 | mVibrator = context.getSystemService(Vibrator.class); |
| 59 | |
| 60 | mContext.getContentResolver().registerContentObserver( |
| 61 | Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED), true, |
| 62 | mVibrationObserver); |
| 63 | mVibrationObserver.onChange(false /* selfChange */); |
| 64 | } |
| 65 | |
| 66 | public void vibrate(final int effectId) { |
| 67 | if (mHapticFeedbackEnabled) { |
| 68 | AsyncTask.execute(() -> |
Beverly | 512077e | 2018-03-15 13:54:43 -0400 | [diff] [blame] | 69 | mVibrator.vibrate(VibrationEffect.get(effectId, false /* fallback */), |
| 70 | STATUS_BAR_VIBRATION_ATTRIBUTES)); |
Matthew Ng | 5c0592e | 2018-03-08 14:51:36 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | private void updateHapticFeedBackEnabled() { |
| 75 | mHapticFeedbackEnabled = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 76 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) != 0; |
| 77 | } |
| 78 | } |