]> git.lizzy.rs Git - dragonfireclient.git/blob - android/app/src/main/java/net/minetest/minetest/GameActivity.java
eeb90ea7f7e59ebbe59e0a181e1ad578668c0799
[dragonfireclient.git] / android / app / src / main / java / net / minetest / minetest / GameActivity.java
1 /*
2 Minetest
3 Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
4 Copyright (C) 2014-2020 ubulem,  Bektur Mambetov <berkut87@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 package net.minetest.minetest;
22
23 import android.app.NativeActivity;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.text.InputType;
29 import android.view.KeyEvent;
30 import android.view.View;
31 import android.view.WindowManager;
32 import android.view.inputmethod.InputMethodManager;
33 import android.widget.Button;
34 import android.widget.EditText;
35 import android.widget.LinearLayout;
36
37 import androidx.annotation.Keep;
38 import androidx.appcompat.app.AlertDialog;
39
40 import java.util.Objects;
41
42 // Native code finds these methods by name (see porting_android.cpp).
43 // This annotation prevents the minifier/Proguard from mangling them.
44 @Keep
45 public class GameActivity extends NativeActivity {
46         static {
47                 System.loadLibrary("c++_shared");
48                 System.loadLibrary("Minetest");
49         }
50
51         private int messageReturnCode = -1;
52         private String messageReturnValue = "";
53
54         public static native void putMessageBoxResult(String text);
55
56         @Override
57         public void onCreate(Bundle savedInstanceState) {
58                 super.onCreate(savedInstanceState);
59                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
60         }
61
62         private void makeFullScreen() {
63                 if (Build.VERSION.SDK_INT >= 19)
64                         this.getWindow().getDecorView().setSystemUiVisibility(
65                                         View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
66                                         View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
67                                         View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
68         }
69
70         @Override
71         public void onWindowFocusChanged(boolean hasFocus) {
72                 super.onWindowFocusChanged(hasFocus);
73                 if (hasFocus)
74                         makeFullScreen();
75         }
76
77         @Override
78         protected void onResume() {
79                 super.onResume();
80                 makeFullScreen();
81         }
82
83         @Override
84         public void onBackPressed() {
85                 // Ignore the back press so Minetest can handle it
86         }
87
88         public void showDialog(String acceptButton, String hint, String current, int editType) {
89                 runOnUiThread(() -> showDialogUI(hint, current, editType));
90         }
91
92         private void showDialogUI(String hint, String current, int editType) {
93                 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
94                 LinearLayout container = new LinearLayout(this);
95                 container.setOrientation(LinearLayout.VERTICAL);
96                 builder.setView(container);
97                 AlertDialog alertDialog = builder.create();
98                 EditText editText;
99                 // For multi-line, do not close the dialog after pressing back button
100                 if (editType == 1) {
101                         editText = new EditText(this);
102                 } else {
103                         editText = new CustomEditText(this);
104                 }
105                 container.addView(editText);
106                 editText.setMaxLines(8);
107                 editText.requestFocus();
108                 editText.setHint(hint);
109                 editText.setText(current);
110                 final InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
111                 Objects.requireNonNull(imm).toggleSoftInput(InputMethodManager.SHOW_FORCED,
112                                 InputMethodManager.HIDE_IMPLICIT_ONLY);
113                 if (editType == 1)
114                         editText.setInputType(InputType.TYPE_CLASS_TEXT |
115                                         InputType.TYPE_TEXT_FLAG_MULTI_LINE);
116                 else if (editType == 3)
117                         editText.setInputType(InputType.TYPE_CLASS_TEXT |
118                                         InputType.TYPE_TEXT_VARIATION_PASSWORD);
119                 else
120                         editText.setInputType(InputType.TYPE_CLASS_TEXT);
121                 editText.setSelection(editText.getText().length());
122                 editText.setOnKeyListener((view, keyCode, event) -> {
123                         // For multi-line, do not submit the text after pressing Enter key
124                         if (keyCode == KeyEvent.KEYCODE_ENTER && editType != 1) {
125                                 imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
126                                 messageReturnCode = 0;
127                                 messageReturnValue = editText.getText().toString();
128                                 alertDialog.dismiss();
129                                 return true;
130                         }
131                         return false;
132                 });
133                 // For multi-line, add Done button since Enter key does not submit text
134                 if (editType == 1) {
135                         Button doneButton = new Button(this);
136                         container.addView(doneButton);
137                         doneButton.setText(R.string.ime_dialog_done);
138                         doneButton.setOnClickListener((view -> {
139                                 imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
140                                 messageReturnCode = 0;
141                                 messageReturnValue = editText.getText().toString();
142                                 alertDialog.dismiss();
143                         }));
144                 }
145                 alertDialog.show();
146                 alertDialog.setOnCancelListener(dialog -> {
147                         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
148                         messageReturnValue = current;
149                         messageReturnCode = -1;
150                 });
151         }
152
153         public int getDialogState() {
154                 return messageReturnCode;
155         }
156
157         public String getDialogValue() {
158                 messageReturnCode = -1;
159                 return messageReturnValue;
160         }
161
162         public float getDensity() {
163                 return getResources().getDisplayMetrics().density;
164         }
165
166         public int getDisplayHeight() {
167                 return getResources().getDisplayMetrics().heightPixels;
168         }
169
170         public int getDisplayWidth() {
171                 return getResources().getDisplayMetrics().widthPixels;
172         }
173
174         public void openURI(String uri) {
175                 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
176                 startActivity(browserIntent);
177         }
178
179         public String getUserDataPath() {
180                 return Utils.getUserDataDirectory(this).getAbsolutePath();
181         }
182
183         public String getCachePath() {
184                 return Utils.getCacheDirectory(this).getAbsolutePath();
185         }
186 }