blob: 442416fd3beaf6b835b0b46f0d672094e0cea16f [file] [log] [blame]
Matthew Ng5c0592e2018-03-08 14:51:36 -08001/*
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
17package com.android.systemui.statusbar;
18
19import android.content.Context;
20import android.database.ContentObserver;
Beverly512077e2018-03-15 13:54:43 -040021import android.media.AudioAttributes;
Matthew Ng5c0592e2018-03-08 14:51:36 -080022import android.os.AsyncTask;
23import android.os.Handler;
24import android.os.UserHandle;
25import android.os.VibrationEffect;
26import android.os.Vibrator;
27import android.provider.Settings;
28
Jason Monk196d6392018-12-20 13:25:34 -050029import javax.inject.Inject;
30import javax.inject.Singleton;
31
32/**
33 */
34@Singleton
Matthew Ng5c0592e2018-03-08 14:51:36 -080035public class VibratorHelper {
36
37 private final Vibrator mVibrator;
38 private final Context mContext;
39 private boolean mHapticFeedbackEnabled;
Beverly512077e2018-03-15 13:54:43 -040040 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 Ng5c0592e2018-03-08 14:51:36 -080045
46 final private ContentObserver mVibrationObserver = new ContentObserver(Handler.getMain()) {
47 @Override
48 public void onChange(boolean selfChange) {
49 updateHapticFeedBackEnabled();
50 }
51 };
52
Jason Monk196d6392018-12-20 13:25:34 -050053 /**
54 */
55 @Inject
Matthew Ng5c0592e2018-03-08 14:51:36 -080056 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(() ->
Beverly512077e2018-03-15 13:54:43 -040069 mVibrator.vibrate(VibrationEffect.get(effectId, false /* fallback */),
70 STATUS_BAR_VIBRATION_ATTRIBUTES));
Matthew Ng5c0592e2018-03-08 14:51:36 -080071 }
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}