|
1 |
| -package com.example.snippets.ui.haptics; |
| 1 | +package com.example.snippets.ui.haptics |
2 | 2 |
|
3 |
| -import android.Manifest; |
4 |
| -import android.app.Activity; |
5 |
| -import android.os.Build; |
6 |
| -import android.os.VibrationEffect; |
7 |
| -import android.os.Vibrator; |
| 3 | +import android.Manifest |
| 4 | +import android.app.Activity |
| 5 | +import android.os.Build |
| 6 | +import android.os.VibrationEffect |
| 7 | +import android.os.Vibrator |
8 | 8 |
|
9 |
| -import androidx.annotation.RequiresApi; |
10 |
| -import androidx.annotation.RequiresPermission; |
| 9 | +import androidx.annotation.RequiresApi |
| 10 | +import androidx.annotation.RequiresPermission |
11 | 11 |
|
12 | 12 | @RequiresApi(api = Build.VERSION_CODES.M)
|
13 |
| -public class CustomVibrationPatternsJava extends Activity { |
14 |
| - Vibrator vibrator = getApplicationContext().getSystemService(Vibrator.class); |
| 13 | +class CustomVibrationPatternsJava : Activity() { |
| 14 | + val vibrator = applicationContext.getSystemService(Vibrator::class.java) as Vibrator |
15 | 15 |
|
16 | 16 | // [START android_ui_haptics_ramp_up]
|
17 | 17 | @RequiresApi(Build.VERSION_CODES.O)
|
18 | 18 | @RequiresPermission(Manifest.permission.VIBRATE)
|
19 |
| - private void rampUpPattern(Vibrator vibrator) { |
20 |
| - long[] timings = new long[] { |
21 |
| - 50, 50, 50, 50, 50, 100, 350, 25, 25, 25, 25, 200 }; |
22 |
| - int[] amplitudes = new int[] { |
23 |
| - 33, 51, 75, 113, 170, 255, 0, 38, 62, 100, 160, 255 }; |
24 |
| - int repeatIndex = -1; // Don't repeat. |
| 19 | + fun rampUpPattern() { |
| 20 | + val timings = longArrayOf( |
| 21 | + 50, 50, 50, 50, 50, 100, 350, 25, 25, 25, 25, 200) |
| 22 | + val amplitudes = intArrayOf( |
| 23 | + 33, 51, 75, 113, 170, 255, 0, 38, 62, 100, 160, 255) |
| 24 | + val repeatIndex = -1 // Don't repeat. |
25 | 25 |
|
26 | 26 | vibrator.vibrate(VibrationEffect.createWaveform(
|
27 |
| - timings, amplitudes, repeatIndex)); |
| 27 | + timings, amplitudes, repeatIndex)) |
28 | 28 | }
|
29 | 29 | // [END android_ui_haptics_ramp_up]
|
30 | 30 |
|
31 | 31 | @RequiresApi(api = Build.VERSION_CODES.O)
|
32 | 32 | @RequiresPermission(Manifest.permission.VIBRATE)
|
33 | 33 | // [START android_ui_haptics_repeat]
|
34 |
| - private void startVibrating() { |
35 |
| - long[] timings = new long[] { 50, 50, 100, 50, 50 }; |
36 |
| - int[] amplitudes = new int[] { 64, 128, 255, 128, 64 }; |
37 |
| - int repeat = 1; // Repeat from the second entry, index = 1. |
| 34 | + fun startVibrating() { |
| 35 | + val timings = longArrayOf(50, 50, 100, 50, 50) |
| 36 | + val amplitudes = intArrayOf(64, 128, 255, 128, 64) |
| 37 | + val repeat = 1 // Repeat from the second entry, index = 1. |
38 | 38 | VibrationEffect repeatingEffect = VibrationEffect.createWaveform(
|
39 |
| - timings, amplitudes, repeat); |
| 39 | + timings, amplitudes, repeat) |
40 | 40 | // repeatingEffect can be used in multiple places.
|
41 | 41 |
|
42 |
| - vibrator.vibrate(repeatingEffect); |
| 42 | + vibrator.vibrate(repeatingEffect) |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | // [START_EXCLUDE]
|
46 | 46 | @RequiresPermission(Manifest.permission.VIBRATE)
|
47 | 47 | // [END_EXCLUDE]
|
48 |
| - private void stopVibrating() { |
49 |
| - vibrator.cancel(); |
| 48 | + fun stopVibrating() { |
| 49 | + vibrator.cancel() |
50 | 50 | }
|
51 | 51 | // [END android_ui_haptics_repeat]
|
52 | 52 |
|
53 | 53 |
|
54 | 54 | // [START android_ui_haptics_fallback]
|
55 | 55 | @RequiresApi(api = Build.VERSION_CODES.O)
|
56 | 56 | @RequiresPermission(Manifest.permission.VIBRATE)
|
57 |
| - private void patternWithFallback() { |
58 |
| - long[] smoothTimings = new long[] { 50, 50, 100, 50, 50 }; |
59 |
| - long[] onOffTimings = new long[] { 50, 100 }; |
60 |
| - int[] amplitudes = new int[] { 64, 128, 255, 128, 64 }; |
61 |
| - int repeatIndex = -1; // Don't repeat. |
| 57 | + fun patternWithFallback() { |
| 58 | + val smoothTimings = longArrayOf(50, 50, 100, 50, 50) |
| 59 | + val onOffTimings = longArrayOf(50, 100) |
| 60 | + val amplitudes = intArrayOf(64, 128, 255, 128, 64) |
| 61 | + val repeatIndex = -1 // Don't repeat. |
62 | 62 |
|
63 | 63 | if (vibrator.hasAmplitudeControl()) {
|
64 | 64 | vibrator.vibrate(VibrationEffect.createWaveform(
|
65 |
| - smoothTimings, amplitudes, repeatIndex)); |
| 65 | + smoothTimings, amplitudes, repeatIndex)) |
66 | 66 | } else {
|
67 | 67 | vibrator.vibrate(VibrationEffect.createWaveform(
|
68 |
| - onOffTimings, repeatIndex)); |
| 68 | + onOffTimings, repeatIndex)) |
69 | 69 | }
|
70 | 70 | }
|
71 | 71 | // [END android_ui_haptics_fallback]
|
|
0 commit comments