Matt Pietal | b508084 | 2020-04-23 14:02:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.controls.ui |
| 18 | |
| 19 | import android.os.VibrationEffect |
| 20 | import android.os.VibrationEffect.Composition.PRIMITIVE_TICK |
| 21 | |
| 22 | object Vibrations { |
| 23 | private const val TOGGLE_TICK_COUNT = 12 |
| 24 | |
| 25 | val toggleOnEffect = initToggleOnEffect() |
| 26 | val toggleOffEffect = initToggleOffEffect() |
| 27 | val rangeEdgeEffect = initRangeEdgeEffect() |
| 28 | val rangeMiddleEffect = initRangeMiddleEffect() |
| 29 | |
| 30 | private fun initToggleOnEffect(): VibrationEffect { |
| 31 | val composition = VibrationEffect.startComposition() |
| 32 | var i = 0 |
| 33 | while (i++ < TOGGLE_TICK_COUNT) { |
| 34 | composition.addPrimitive(PRIMITIVE_TICK, 0.05f, 0) |
| 35 | } |
| 36 | composition.addPrimitive(PRIMITIVE_TICK, 0.5f, 100) |
| 37 | return composition.compose() |
| 38 | } |
| 39 | |
| 40 | private fun initToggleOffEffect(): VibrationEffect { |
| 41 | val composition = VibrationEffect.startComposition() |
| 42 | composition.addPrimitive(PRIMITIVE_TICK, 0.5f, 0) |
| 43 | composition.addPrimitive(PRIMITIVE_TICK, 0.05f, 100) |
| 44 | var i = 0 |
| 45 | while (i++ < TOGGLE_TICK_COUNT) { |
| 46 | composition?.addPrimitive(PRIMITIVE_TICK, 0.05f, 0) |
| 47 | } |
| 48 | return composition.compose() |
| 49 | } |
| 50 | |
| 51 | private fun initRangeEdgeEffect(): VibrationEffect { |
| 52 | val composition = VibrationEffect.startComposition() |
| 53 | composition.addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, 0.5f) |
| 54 | return composition.compose() |
| 55 | } |
| 56 | |
| 57 | private fun initRangeMiddleEffect(): VibrationEffect { |
| 58 | val composition = VibrationEffect.startComposition() |
| 59 | composition.addPrimitive(VibrationEffect.Composition.PRIMITIVE_TICK, 0.1f) |
| 60 | return composition.compose() |
| 61 | } |
| 62 | } |