Debian Codegraph @

tools/build-tool/data/android/integrated/app/src/main/java/io/castleengine/ServiceMiscellaneous.java

raw symbols SCIP
1 /* -*- tab-width: 4 -*- */
2 package io.castleengine;
3
4 import android.view.View;
5 import android.os.Build;
6 import android.content.Intent;
7 import android.content.Context;
8 import android.content.res.TypedArray;
9 import android.net.Uri;
10 import android.widget.Toast;
11 import android.app.Service;
12 import android.view.inputmethod.InputMethodManager;
13 import android.view.KeyEvent;
14 import android.view.Window;
15
16 import androidx.core.view.ViewCompat;
17 import androidx.core.view.WindowInsetsControllerCompat;
18 import androidx.core.view.WindowInsetsCompat;
19 import androidx.core.graphics.Insets;
20
21 /**
22 * Integration of various Android small stuff with
23 * Castle Game Engine.
24 */
25 public class ServiceMiscellaneous extends ServiceAbstract
26 {
27 private static final String CATEGORY = "ServiceMiscellaneous";
28
29 public ServiceMiscellaneous(MainActivity activity)
30 {
31 super(activity);
32 }
33
34 public String getName()
35 {
36 return "miscellaneous";
37 }
38
39 @Override
40 public void onCreate()
41 {
42 super.onCreate();
43
44 Window window = getActivity().getWindow();
45 View decorView = window.getDecorView();
46
47 /* Use setOnApplyWindowInsetsListener to get insets values,
48 following https://developer.android.com/develop/ui/views/layout/edge-to-edge
49 Note: Trying to get insets later, by various ways (I tried many...)
50 generally results in nothing, as insets have been already consumed.
51 Using setOnApplyWindowInsetsListener is critical to get the insets. */
52
53 ViewCompat.setOnApplyWindowInsetsListener(decorView, (v, windowInsets) -> {
54 Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
55
56 /* Send to Pascal code value of safe borders.
57 As 4D vector, in CSS order (top, right, bottom, left,
58 https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties ). */
59 messageSend(new String[]{"safe-borders",
60 Integer.toString(insets.top),
61 Integer.toString(insets.right),
62 Integer.toString(insets.bottom),
63 Integer.toString(insets.left)
64 });
65
66 // Return CONSUMED if you don't want want the window insets to keep passing
67 // down to descendant views.
68 // It doesn't really matter for CGE usage, most likely,
69 // as we don't have descendant views?
70 return WindowInsetsCompat.CONSUMED;
71 });
72 }
73
74 /** Immersive mode. */
75 private void hideSystemBars(View decorView)
76 {
77 // Implementation follows
78 // https://developer.android.com/develop/ui/views/layout/immersive?hl=pl#java
79 WindowInsetsControllerCompat windowInsetsController =
80 ViewCompat.getWindowInsetsController(decorView);
81 if (windowInsetsController == null) {
82 return;
83 }
84 // Configure the behavior of the hidden system bars
85 windowInsetsController.setSystemBarsBehavior(
86 WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
87 );
88 // Hide both the status bar and the navigation bar
89 windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
90 }
91
92 /** Immersive mode. */
93 @Override
94 public void onWindowFocusChanged(boolean hasFocus)
95 {
96 if (hasFocus) {
97 Window window = getActivity().getWindow();
98 View decorView = window.getDecorView();
99
100 /* This effectively calculates whether our CastleEngineManifest.xml
101 has fullscreen_immersive="true" .
102 The resulting Java bool "fullscreen" should reflect this. */
103 int[] attrs = {android.R.attr.windowFullscreen};
104 TypedArray ta = getActivity().getTheme().obtainStyledAttributes(attrs);
105 boolean fullscreen = ta.getBoolean(0, false);
106
107 if (fullscreen) {
108 hideSystemBars(decorView);
109 }
110 }
111 }
112
113 /* Shares ---------------------------------------------------------------- */
114
115 /**
116 * Share a text with other applications.
117 * See https://developer.android.com/training/sharing/send.html
118 */
119 private void shareText(String title, String subject, String text)
120 {
121 Intent sendIntent = new Intent();
122 sendIntent.setAction(Intent.ACTION_SEND);
123 sendIntent.putExtra(Intent.EXTRA_TEXT, text);
124 sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
125 sendIntent.setType("text/plain");
126 getActivity().startActivity(Intent.createChooser(sendIntent, title));
127 }
128
129 /**
130 * View URL.
131 * See http://stackoverflow.com/questions/4969217/share-application-link-in-android
132 */
133 private void viewUrl(String url)
134 {
135 Intent intent = new Intent(Intent.ACTION_VIEW);
136 intent.setData(Uri.parse(url));
137 getActivity().startActivity(intent);
138 }
139
140 @Override
141 public boolean messageReceived(String[] parts)
142 {
143 if (parts.length == 2 && parts[0].equals("view-url")) {
144 viewUrl(parts[1]);
145 return true;
146 } else
147 if (parts.length == 4 && parts[0].equals("share-text")) {
148 shareText(parts[1], parts[2], parts[3]);
149 return true;
150 } else
151 if (parts.length == 2 && parts[0].equals("on-screen-notification")) {
152 Toast.makeText(getActivity().getApplicationContext(), parts[1], Toast.LENGTH_SHORT).show();
153 return true;
154 } else
155 {
156 return false;
157 }
158 }
159 }