abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/VibratorCompat.java
blob: 2795c71ca7dbc00345399def611ea0df2a572f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package juloo.keyboard2;

import android.content.Context;
import android.os.Build.VERSION;
import android.os.Vibrator;
import android.view.HapticFeedbackConstants;
import android.view.View;

public final class VibratorCompat
{
  public static void vibrate(View v, VibrationBehavior b)
  {
    switch (b)
    {
      case DISABLED:
        break;
      case SYSTEM:
        if (VERSION.SDK_INT >= 8)
          v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP,
              HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
        break;
      case STRONG:
        vibrator_vibrate(v, 90);
        break;
      case MEDIUM:
        vibrator_vibrate(v, 45);
        break;
      case LIGHT:
        vibrator_vibrate(v, 20);
        break;
    }
  }

  /** Use the older [Vibrator] when the newer API is not available or the user
      wants more control. */
  static void vibrator_vibrate(View v, int duration)
  {
    try
    {
      get_vibrator(v).vibrate(duration);
    }
    catch (Exception e) {}
  }

  static Vibrator vibrator_service = null;

  static Vibrator get_vibrator(View v)
  {
    if (vibrator_service == null)
    {
      vibrator_service =
        (Vibrator)v.getContext().getSystemService(Context.VIBRATOR_SERVICE);
    }
    return vibrator_service;
  }

  public static enum VibrationBehavior
  {
    DISABLED,
    SYSTEM,
    STRONG,
    MEDIUM,
    LIGHT;

    VibrationBehavior() {}

    /** Defaults [SYSTEM] for unrecognized strings. */
    public static VibrationBehavior of_string(String s)
    {
      switch (s)
      {
        case "disabled": return DISABLED;
        case "system": return SYSTEM;
        case "strong": return STRONG;
        case "medium": return MEDIUM;
        case "light": return LIGHT;
        default: return SYSTEM;
      }
    }
  }
}