Android: build fixes & compat fixes
[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("iconv");
18                 System.loadLibrary("minetest");
19         }
20
21         private int m_MessagReturnCode;
22         private String m_MessageReturnValue;
23
24         public static native void putMessageBoxResult(String text);
25
26         @Override
27         public void onCreate(Bundle savedInstanceState) {
28                 super.onCreate(savedInstanceState);
29                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
30                 m_MessagReturnCode = -1;
31                 m_MessageReturnValue = "";
32         }
33
34         @Override
35         protected void onResume() {
36                 super.onResume();
37                 makeFullScreen();
38         }
39
40         private void makeFullScreen() {
41                 if (Build.VERSION.SDK_INT >= 19)
42                         this.getWindow().getDecorView().setSystemUiVisibility(
43                                         View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
44         }
45
46         @Override
47         public void onWindowFocusChanged(boolean hasFocus) {
48                 super.onWindowFocusChanged(hasFocus);
49                 if (hasFocus)
50                         makeFullScreen();
51         }
52
53         public void copyAssets() {
54                 Intent intent = new Intent(this, MinetestAssetCopy.class);
55                 startActivity(intent);
56         }
57
58         public void showDialog(String acceptButton, String hint, String current,
59                                                    int editType) {
60
61                 Intent intent = new Intent(this, MinetestTextEntry.class);
62                 Bundle params = new Bundle();
63                 params.putString("acceptButton", acceptButton);
64                 params.putString("hint", hint);
65                 params.putString("current", current);
66                 params.putInt("editType", editType);
67                 intent.putExtras(params);
68                 startActivityForResult(intent, 101);
69                 m_MessageReturnValue = "";
70                 m_MessagReturnCode = -1;
71         }
72
73         /* ugly code to workaround putMessageBoxResult not beeing found */
74         public int getDialogState() {
75                 return m_MessagReturnCode;
76         }
77
78         public String getDialogValue() {
79                 m_MessagReturnCode = -1;
80                 return m_MessageReturnValue;
81         }
82
83         public float getDensity() {
84                 return getResources().getDisplayMetrics().density;
85         }
86
87         public int getDisplayWidth() {
88                 return getResources().getDisplayMetrics().widthPixels;
89         }
90
91         public int getDisplayHeight() {
92                 return getResources().getDisplayMetrics().heightPixels;
93         }
94
95         @Override
96         protected void onActivityResult(int requestCode, int resultCode,
97                                                                         Intent data) {
98                 if (requestCode == 101) {
99                         if (resultCode == RESULT_OK) {
100                                 String text = data.getStringExtra("text");
101                                 m_MessagReturnCode = 0;
102                                 m_MessageReturnValue = text;
103                         } else {
104                                 m_MessagReturnCode = 1;
105                         }
106                 }
107         }
108 }