3 Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
4 Copyright (C) 2014-2020 ubulem, Bektur Mambetov <berkut87@gmail.com>
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.
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.
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.
21 package net.minetest.minetest;
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;
37 import androidx.annotation.Keep;
38 import androidx.appcompat.app.AlertDialog;
40 import java.util.Objects;
42 // Native code finds these methods by name (see porting_android.cpp).
43 // This annotation prevents the minifier/Proguard from mangling them.
45 public class GameActivity extends NativeActivity {
47 System.loadLibrary("c++_shared");
48 System.loadLibrary("Minetest");
51 private int messageReturnCode = -1;
52 private String messageReturnValue = "";
54 public static native void putMessageBoxResult(String text);
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
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);
71 public void onWindowFocusChanged(boolean hasFocus) {
72 super.onWindowFocusChanged(hasFocus);
78 protected void onResume() {
84 public void onBackPressed() {
85 // Ignore the back press so Minetest can handle it
88 public void showDialog(String acceptButton, String hint, String current, int editType) {
89 runOnUiThread(() -> showDialogUI(hint, current, editType));
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();
99 // For multi-line, do not close the dialog after pressing back button
101 editText = new EditText(this);
103 editText = new CustomEditText(this);
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);
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);
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();
133 // For multi-line, add Done button since Enter key does not submit text
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();
146 alertDialog.setOnCancelListener(dialog -> {
147 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
148 messageReturnValue = current;
149 messageReturnCode = -1;
153 public int getDialogState() {
154 return messageReturnCode;
157 public String getDialogValue() {
158 messageReturnCode = -1;
159 return messageReturnValue;
162 public float getDensity() {
163 return getResources().getDisplayMetrics().density;
166 public int getDisplayHeight() {
167 return getResources().getDisplayMetrics().heightPixels;
170 public int getDisplayWidth() {
171 return getResources().getDisplayMetrics().widthPixels;
174 public void openURI(String uri) {
175 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
176 startActivity(browserIntent);
179 public String getUserDataPath() {
180 return Utils.getUserDataDirectory(this).getAbsolutePath();
183 public String getCachePath() {
184 return Utils.getCacheDirectory(this).getAbsolutePath();