Android: Fix start-up crashes on version 4.x (#8003)
[oweals/minetest.git] / build / android / src / main / java / net.minetest.minetest / MtNativeActivity.java
1 package net.minetest.minetest;
2
3 import android.app.NativeActivity;
4 import android.content.Intent;
5 import android.os.Build;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.WindowManager;
9
10 public class MtNativeActivity extends NativeActivity {
11
12         static {
13                 System.loadLibrary("c++_shared");
14                 System.loadLibrary("openal");
15                 System.loadLibrary("ogg");
16                 System.loadLibrary("vorbis");
17                 System.loadLibrary("gmp");
18                 System.loadLibrary("iconv");
19                 System.loadLibrary("minetest");
20         }
21
22         private int m_MessagReturnCode;
23         private String m_MessageReturnValue;
24
25         public static native void putMessageBoxResult(String text);
26
27         @Override
28         public void onCreate(Bundle savedInstanceState) {
29                 super.onCreate(savedInstanceState);
30                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
31                 m_MessagReturnCode = -1;
32                 m_MessageReturnValue = "";
33         }
34
35         @Override
36         protected void onResume() {
37                 super.onResume();
38                 makeFullScreen();
39         }
40
41         public void makeFullScreen() {
42                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
43                         this.getWindow().getDecorView().setSystemUiVisibility(
44                                         View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
45                         );
46                 }
47         }
48
49         @Override
50         public void onWindowFocusChanged(boolean hasFocus) {
51                 super.onWindowFocusChanged(hasFocus);
52                 if (hasFocus) {
53                         makeFullScreen();
54                 }
55         }
56
57         public void copyAssets() {
58                 Intent intent = new Intent(this, MinetestAssetCopy.class);
59                 startActivity(intent);
60         }
61
62         public void showDialog(String acceptButton, String hint, String current,
63                                                    int editType) {
64
65                 Intent intent = new Intent(this, MinetestTextEntry.class);
66                 Bundle params = new Bundle();
67                 params.putString("acceptButton", acceptButton);
68                 params.putString("hint", hint);
69                 params.putString("current", current);
70                 params.putInt("editType", editType);
71                 intent.putExtras(params);
72                 startActivityForResult(intent, 101);
73                 m_MessageReturnValue = "";
74                 m_MessagReturnCode = -1;
75         }
76
77         /* ugly code to workaround putMessageBoxResult not beeing found */
78         public int getDialogState() {
79                 return m_MessagReturnCode;
80         }
81
82         public String getDialogValue() {
83                 m_MessagReturnCode = -1;
84                 return m_MessageReturnValue;
85         }
86
87         public float getDensity() {
88                 return getResources().getDisplayMetrics().density;
89         }
90
91         public int getDisplayWidth() {
92                 return getResources().getDisplayMetrics().widthPixels;
93         }
94
95         public int getDisplayHeight() {
96                 return getResources().getDisplayMetrics().heightPixels;
97         }
98
99         @Override
100         protected void onActivityResult(int requestCode, int resultCode,
101                                                                         Intent data) {
102                 if (requestCode == 101) {
103                         if (resultCode == RESULT_OK) {
104                                 String text = data.getStringExtra("text");
105                                 m_MessagReturnCode = 0;
106                                 m_MessageReturnValue = text;
107                         } else {
108                                 m_MessagReturnCode = 1;
109                         }
110                 }
111         }
112 }