Debian Codegraph @

samples/demo/haptic-assessment/src/com/example/android/hapticassessment/MainActivity.kt

raw
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.example.android.hapticassessment
18
19 import android.graphics.Color
20 import android.os.Bundle
21 import android.os.VibrationEffect
22 import android.os.VibrationEffect.EFFECT_CLICK
23 import android.os.Vibrator
24 import android.view.View
25 import android.widget.Button
26
27 import androidx.appcompat.app.AppCompatActivity
28
29 /** App main screen. */
30 class MainActivity : AppCompatActivity() {
31
32 companion object {
33
34 private const val ONE_SHOT_TIMING = 20L
35 private const val ONE_SHOT_AMPLITUDE = 255
36
37 private val WAVEFORM_TIMINGS = longArrayOf(500, 500)
38 private val WAVEFORM_AMPLITUDES = intArrayOf(128, 255)
39 }
40
41 override fun onCreate(savedInstanceState: Bundle?) {
42 super.onCreate(savedInstanceState)
43 setContentView(R.layout.activity_main)
44
45 val vibrator = getSystemService(Vibrator::class.java)
46
47 findViewById<Button>(R.id.click_effect_button).setOnClickListener {
48 vibrator.vibrate(VibrationEffect.createPredefined(EFFECT_CLICK))
49 }
50
51 findViewById<Button>(R.id.oneshot_effect_button).setOnClickListener {
52 vibrator.vibrate(VibrationEffect.createOneShot(ONE_SHOT_TIMING, ONE_SHOT_AMPLITUDE))
53 }
54
55 findViewById<Button>(R.id.waveform_effect_button).setOnClickListener { view: View ->
56 vibrator.vibrate(
57 VibrationEffect.createWaveform(WAVEFORM_TIMINGS, WAVEFORM_AMPLITUDES, -1))
58
59 val button = view as Button
60 if (vibrator.hasAmplitudeControl()) {
61 button.text = getString(R.string.button_3_pass)
62 button.setBackgroundColor(Color.GREEN)
63 button.setTextColor(Color.BLACK)
64 } else {
65 button.text = getString(R.string.button_3_fail)
66 button.setBackgroundColor(Color.RED)
67 button.setTextColor(Color.WHITE)
68 }
69 }
70 }
71 }