Skip to content

Commit

Permalink
Android: replace InputDialogActivity on simple dialog window (#10034)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim committed Sep 26, 2020
1 parent 65c15e1 commit 4298d95
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 137 deletions.
7 changes: 1 addition & 6 deletions build/android/app/src/main/AndroidManifest.xml
Expand Up @@ -17,8 +17,8 @@
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/label"
android:resizeableActivity="false"
android:requestLegacyExternalStorage="true"
android:resizeableActivity="false"
tools:ignore="UnusedAttribute">

<meta-data
Expand Down Expand Up @@ -53,11 +53,6 @@
android:value="Minetest" />
</activity>

<activity
android:name=".InputDialogActivity"
android:maxAspectRatio="3.0"
android:theme="@style/InputTheme" />

<service
android:name=".UnzipService"
android:enabled="true"
Expand Down
@@ -0,0 +1,45 @@
/*
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.
*/

package net.minetest.minetest;

import android.content.Context;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.widget.AppCompatEditText;

import java.util.Objects;

public class CustomEditText extends AppCompatEditText {
public CustomEditText(Context context) {
super(context);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
InputMethodManager mgr = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Objects.requireNonNull(mgr).hideSoftInputFromWindow(this.getWindowToken(), 0);
}
return false;
}
}
Expand Up @@ -25,26 +25,32 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;

import java.util.Objects;

public class GameActivity extends NativeActivity {
static {
System.loadLibrary("c++_shared");
System.loadLibrary("Minetest");
}

private int messageReturnCode;
private String messageReturnValue;
private int messageReturnCode = -1;
private String messageReturnValue = "";

public static native void putMessageBoxResult(String text);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
messageReturnCode = -1;
messageReturnValue = "";
}

private void makeFullScreen() {
Expand Down Expand Up @@ -73,29 +79,46 @@ public void onBackPressed() {
// Ignore the back press so Minetest can handle it
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 101) {
if (resultCode == RESULT_OK) {
String text = data.getStringExtra("text");
messageReturnCode = 0;
messageReturnValue = text;
} else
messageReturnCode = 1;
}
public void showDialog(String acceptButton, String hint, String current, int editType) {
runOnUiThread(() -> showDialogUI(hint, current, editType));
}

public void showDialog(String acceptButton, String hint, String current, int editType) {
Intent intent = new Intent(this, InputDialogActivity.class);
Bundle params = new Bundle();
params.putString("acceptButton", acceptButton);
params.putString("hint", hint);
params.putString("current", current);
params.putInt("editType", editType);
intent.putExtras(params);
startActivityForResult(intent, 101);
messageReturnValue = "";
messageReturnCode = -1;
private void showDialogUI(String hint, String current, int editType) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
EditText editText = new CustomEditText(this);
builder.setView(editText);
AlertDialog alertDialog = builder.create();
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());
editText.setOnKeyListener((view, KeyCode, event) -> {
if (KeyCode == KeyEvent.KEYCODE_ENTER) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
messageReturnCode = 0;
messageReturnValue = editText.getText().toString();
alertDialog.dismiss();
return true;
}
return false;
});
alertDialog.show();
alertDialog.setOnCancelListener(dialog -> {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
messageReturnValue = current;
messageReturnCode = -1;
});
}

public int getDialogState() {
Expand Down

This file was deleted.

9 changes: 1 addition & 8 deletions build/android/app/src/main/res/values/styles.xml
Expand Up @@ -8,15 +8,8 @@
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="p">shortEdges</item>
</style>

<style name="InputTheme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>

<style name="CustomProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
<style name="CustomProgressBar" parent="Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">20dip</item>
</style>

</resources>

0 comments on commit 4298d95

Please sign in to comment.