2020-04-15 16:27:40 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
|
|
|
|
Copyright (C) 2014-2020 ubulem, Bektur Mambetov <berkut87@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2015-03-14 18:52:31 +01:00
|
|
|
package net.minetest.minetest;
|
2014-04-21 14:10:59 +02:00
|
|
|
|
|
|
|
import android.app.NativeActivity;
|
|
|
|
import android.content.Intent;
|
2020-05-17 20:09:10 +02:00
|
|
|
import android.net.Uri;
|
2014-04-21 14:10:59 +02:00
|
|
|
import android.os.Bundle;
|
2020-09-26 18:42:22 +02:00
|
|
|
import android.text.InputType;
|
2022-06-05 18:42:09 +02:00
|
|
|
import android.util.Log;
|
2020-09-26 18:42:22 +02:00
|
|
|
import android.view.KeyEvent;
|
2018-12-03 00:39:35 +01:00
|
|
|
import android.view.View;
|
2014-04-21 14:10:59 +02:00
|
|
|
import android.view.WindowManager;
|
2020-09-26 18:42:22 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2021-06-04 21:22:53 +02:00
|
|
|
import android.widget.Button;
|
2020-09-26 18:42:22 +02:00
|
|
|
import android.widget.EditText;
|
2021-06-04 21:22:53 +02:00
|
|
|
import android.widget.LinearLayout;
|
2020-09-26 18:42:22 +02:00
|
|
|
|
2022-05-21 17:48:40 +02:00
|
|
|
import androidx.annotation.Keep;
|
2020-09-26 18:42:22 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2022-06-05 18:42:09 +02:00
|
|
|
import androidx.core.content.FileProvider;
|
2020-09-26 18:42:22 +02:00
|
|
|
|
2022-06-05 18:42:09 +02:00
|
|
|
import java.io.File;
|
2020-09-26 18:42:22 +02:00
|
|
|
import java.util.Objects;
|
2014-04-21 14:10:59 +02:00
|
|
|
|
2022-05-21 17:48:40 +02:00
|
|
|
// Native code finds these methods by name (see porting_android.cpp).
|
|
|
|
// This annotation prevents the minifier/Proguard from mangling them.
|
|
|
|
@Keep
|
2022-11-30 16:50:06 +01:00
|
|
|
@SuppressWarnings("unused")
|
2020-04-15 16:27:40 +02:00
|
|
|
public class GameActivity extends NativeActivity {
|
2018-12-03 00:39:35 +01:00
|
|
|
static {
|
2018-12-22 19:02:00 +01:00
|
|
|
System.loadLibrary("c++_shared");
|
2023-05-26 15:21:23 +02:00
|
|
|
System.loadLibrary("minetest");
|
2018-12-03 00:39:35 +01:00
|
|
|
}
|
|
|
|
|
2020-09-26 18:42:22 +02:00
|
|
|
private int messageReturnCode = -1;
|
|
|
|
private String messageReturnValue = "";
|
2018-12-03 00:39:35 +01:00
|
|
|
|
|
|
|
public static native void putMessageBoxResult(String text);
|
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-01-04 21:18:18 +01:00
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
2018-12-03 00:39:35 +01:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2019-11-07 00:39:39 +01:00
|
|
|
private void makeFullScreen() {
|
2023-04-11 00:05:01 +02:00
|
|
|
this.getWindow().getDecorView().setSystemUiVisibility(
|
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
|
|
|
|
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
|
|
|
|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-03 00:39:35 +01:00
|
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
|
|
super.onWindowFocusChanged(hasFocus);
|
2019-11-07 00:39:39 +01:00
|
|
|
if (hasFocus)
|
2018-12-03 00:39:35 +01:00
|
|
|
makeFullScreen();
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2020-04-15 16:27:40 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
makeFullScreen();
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2020-04-15 16:27:40 +02:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
// Ignore the back press so Minetest can handle it
|
|
|
|
}
|
|
|
|
|
2020-09-26 18:42:22 +02:00
|
|
|
public void showDialog(String acceptButton, String hint, String current, int editType) {
|
|
|
|
runOnUiThread(() -> showDialogUI(hint, current, editType));
|
2020-04-15 16:27:40 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2020-09-26 18:42:22 +02:00
|
|
|
private void showDialogUI(String hint, String current, int editType) {
|
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
2021-06-04 21:22:53 +02:00
|
|
|
LinearLayout container = new LinearLayout(this);
|
|
|
|
container.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
builder.setView(container);
|
2020-09-26 18:42:22 +02:00
|
|
|
AlertDialog alertDialog = builder.create();
|
2021-06-04 21:22:53 +02:00
|
|
|
EditText editText;
|
|
|
|
// For multi-line, do not close the dialog after pressing back button
|
|
|
|
if (editType == 1) {
|
|
|
|
editText = new EditText(this);
|
|
|
|
} else {
|
|
|
|
editText = new CustomEditText(this);
|
|
|
|
}
|
|
|
|
container.addView(editText);
|
|
|
|
editText.setMaxLines(8);
|
2020-09-26 18:42:22 +02:00
|
|
|
editText.requestFocus();
|
|
|
|
editText.setHint(hint);
|
|
|
|
editText.setText(current);
|
|
|
|
final InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
|
|
|
|
Objects.requireNonNull(imm).toggleSoftInput(InputMethodManager.SHOW_FORCED,
|
|
|
|
InputMethodManager.HIDE_IMPLICIT_ONLY);
|
|
|
|
if (editType == 1)
|
|
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT |
|
|
|
|
InputType.TYPE_TEXT_FLAG_MULTI_LINE);
|
|
|
|
else if (editType == 3)
|
|
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT |
|
|
|
|
InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
|
|
else
|
|
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT);
|
|
|
|
editText.setSelection(editText.getText().length());
|
2021-06-04 21:22:53 +02:00
|
|
|
editText.setOnKeyListener((view, keyCode, event) -> {
|
|
|
|
// For multi-line, do not submit the text after pressing Enter key
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_ENTER && editType != 1) {
|
2020-09-26 18:42:22 +02:00
|
|
|
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
|
|
|
messageReturnCode = 0;
|
|
|
|
messageReturnValue = editText.getText().toString();
|
|
|
|
alertDialog.dismiss();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2021-06-04 21:22:53 +02:00
|
|
|
// For multi-line, add Done button since Enter key does not submit text
|
|
|
|
if (editType == 1) {
|
|
|
|
Button doneButton = new Button(this);
|
|
|
|
container.addView(doneButton);
|
|
|
|
doneButton.setText(R.string.ime_dialog_done);
|
|
|
|
doneButton.setOnClickListener((view -> {
|
|
|
|
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
|
|
|
|
messageReturnCode = 0;
|
|
|
|
messageReturnValue = editText.getText().toString();
|
|
|
|
alertDialog.dismiss();
|
|
|
|
}));
|
|
|
|
}
|
2020-09-26 18:42:22 +02:00
|
|
|
alertDialog.show();
|
|
|
|
alertDialog.setOnCancelListener(dialog -> {
|
|
|
|
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
|
|
|
|
messageReturnValue = current;
|
|
|
|
messageReturnCode = -1;
|
|
|
|
});
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
public int getDialogState() {
|
2020-04-15 16:27:40 +02:00
|
|
|
return messageReturnCode;
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
public String getDialogValue() {
|
2020-04-15 16:27:40 +02:00
|
|
|
messageReturnCode = -1;
|
|
|
|
return messageReturnValue;
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
public float getDensity() {
|
|
|
|
return getResources().getDisplayMetrics().density;
|
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
public int getDisplayHeight() {
|
|
|
|
return getResources().getDisplayMetrics().heightPixels;
|
|
|
|
}
|
2015-10-24 20:44:07 +02:00
|
|
|
|
2020-04-15 16:27:40 +02:00
|
|
|
public int getDisplayWidth() {
|
|
|
|
return getResources().getDisplayMetrics().widthPixels;
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|
2020-05-17 20:09:10 +02:00
|
|
|
|
2020-12-19 14:27:15 +01:00
|
|
|
public void openURI(String uri) {
|
|
|
|
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
|
2020-05-17 20:09:10 +02:00
|
|
|
startActivity(browserIntent);
|
|
|
|
}
|
2021-10-15 18:14:48 +02:00
|
|
|
|
|
|
|
public String getUserDataPath() {
|
|
|
|
return Utils.getUserDataDirectory(this).getAbsolutePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getCachePath() {
|
|
|
|
return Utils.getCacheDirectory(this).getAbsolutePath();
|
|
|
|
}
|
2022-06-05 18:42:09 +02:00
|
|
|
|
|
|
|
public void shareFile(String path) {
|
|
|
|
File file = new File(path);
|
|
|
|
if (!file.exists()) {
|
|
|
|
Log.e("GameActivity", "File " + file.getAbsolutePath() + " doesn't exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uri fileUri = FileProvider.getUriForFile(this, "net.minetest.minetest.fileprovider", file);
|
|
|
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_SEND, fileUri);
|
|
|
|
intent.setDataAndType(fileUri, getContentResolver().getType(fileUri));
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
|
|
|
|
|
|
|
|
Intent shareIntent = Intent.createChooser(intent, null);
|
|
|
|
startActivity(shareIntent);
|
|
|
|
}
|
2014-04-21 14:10:59 +02:00
|
|
|
}
|