Update Android java code (#7820)
[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("openal");
14                 System.loadLibrary("ogg");
15                 System.loadLibrary("vorbis");
16                 System.loadLibrary("gmp");
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         public void makeFullScreen() {
41                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
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         }
47
48         @Override
49         public void onWindowFocusChanged(boolean hasFocus) {
50                 super.onWindowFocusChanged(hasFocus);
51                 if (hasFocus) {
52                         makeFullScreen();
53                 }
54         }
55
56         public void copyAssets() {
57                 Intent intent = new Intent(this, MinetestAssetCopy.class);
58                 startActivity(intent);
59         }
60
61         public void showDialog(String acceptButton, String hint, String current,
62                                                    int editType) {
63
64                 Intent intent = new Intent(this, MinetestTextEntry.class);
65                 Bundle params = new Bundle();
66                 params.putString("acceptButton", acceptButton);
67                 params.putString("hint", hint);
68                 params.putString("current", current);
69                 params.putInt("editType", editType);
70                 intent.putExtras(params);
71                 startActivityForResult(intent, 101);
72                 m_MessageReturnValue = "";
73                 m_MessagReturnCode = -1;
74         }
75
76         /* ugly code to workaround putMessageBoxResult not beeing found */
77         public int getDialogState() {
78                 return m_MessagReturnCode;
79         }
80
81         public String getDialogValue() {
82                 m_MessagReturnCode = -1;
83                 return m_MessageReturnValue;
84         }
85
86         public float getDensity() {
87                 return getResources().getDisplayMetrics().density;
88         }
89
90         public int getDisplayWidth() {
91                 return getResources().getDisplayMetrics().widthPixels;
92         }
93
94         public int getDisplayHeight() {
95                 return getResources().getDisplayMetrics().heightPixels;
96         }
97
98         @Override
99         protected void onActivityResult(int requestCode, int resultCode,
100                                                                         Intent data) {
101                 if (requestCode == 101) {
102                         if (resultCode == RESULT_OK) {
103                                 String text = data.getStringExtra("text");
104                                 m_MessagReturnCode = 0;
105                                 m_MessageReturnValue = text;
106                         } else {
107                                 m_MessagReturnCode = 1;
108                         }
109                 }
110         }
111 }