abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/Utils.java
blob: 88c865c3f715ed750b0ef4f42bd6c28a63791b99 (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
package juloo.keyboard2;

import android.app.AlertDialog;
import android.content.res.Resources;
import android.graphics.Insets;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;

public final class Utils
{
  /** Turn the first letter of a string uppercase. */
  public static String capitalize_string(String s)
  {
    if (s.length() < 1)
      return s;
    // Make sure not to cut a code point in half
    int i = s.offsetByCodePoints(0, 1);
    return s.substring(0, i).toUpperCase(Locale.getDefault()) + s.substring(i);
  }

  /** Like [dialog.show()] but properly configure layout params when called
      from an IME. [token] is the input view's [getWindowToken()]. */
  public static void show_dialog_on_ime(AlertDialog dialog, IBinder token)
  {
    Window win = dialog.getWindow();
    WindowManager.LayoutParams lp = win.getAttributes();
    lp.token = token;
    lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    win.setAttributes(lp);
    win.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    dialog.show();
  }

  public static String read_all_utf8(InputStream inp) throws Exception
  {
    InputStreamReader reader = new InputStreamReader(inp, "UTF-8");
    StringBuilder out = new StringBuilder();
    int buff_length = 8000;
    char[] buff = new char[buff_length];
    int l;
    while ((l = reader.read(buff, 0, buff_length)) != -1)
      out.append(buff, 0, l);
    return out.toString();
  }

  /** Whether the thin gesture-navigation bar is used.
      https://stackoverflow.com/questions/36514167/how-to-really-get-the-navigation-bar-height-in-android
       */
  public static boolean is_navigation_bar_gestural(Resources res)
  {
    // core/java/android/view/WindowManagerPolicyConstants.java
    int res_id = res.getIdentifier("config_navBarInteractionMode", "integer", "android");
    return (res_id > 0 && res.getInteger(res_id) == 2);
  }
}