forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoNativeActivity.java
257 lines (224 loc) · 9.22 KB
/
GoNativeActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package org.golang.app;
import android.app.Activity;
import android.app.NativeActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyCharacterMap;
import android.view.View;
import android.view.WindowInsets;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
public class GoNativeActivity extends NativeActivity {
private static GoNativeActivity goNativeActivity;
private static final int FILE_OPEN_CODE = 1;
private static final int DEFAULT_INPUT_TYPE = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; // this is required to force samsung keyboards to not suggest
private static final int DEFAULT_KEYBOARD_CODE = 0;
private static final int SINGLELINE_KEYBOARD_CODE = 1;
private static final int NUMBER_KEYBOARD_CODE = 2;
private native void filePickerReturned(String str);
private native void insetsChanged(int top, int bottom, int left, int right);
private native void keyboardTyped(String str);
private native void keyboardDelete();
private EditText mTextEdit;
private String oldState = "";
public GoNativeActivity() {
super();
goNativeActivity = this;
}
String getTmpdir() {
return getCacheDir().getAbsolutePath();
}
void updateLayout() {
try {
WindowInsets insets = getWindow().getDecorView().getRootWindowInsets();
if (insets == null) {
return;
}
insetsChanged(insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetBottom(),
insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetRight());
} catch (java.lang.NoSuchMethodError e) {
Rect insets = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(insets);
View view = findViewById(android.R.id.content).getRootView();
insetsChanged(insets.top, view.getHeight() - insets.height() - insets.top,
insets.left, view.getWidth() - insets.width() - insets.left);
}
}
static void showKeyboard(int keyboardType) {
goNativeActivity.doShowKeyboard(keyboardType);
}
void doShowKeyboard(final int keyboardType) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION;
int inputType = DEFAULT_INPUT_TYPE;
switch (keyboardType) {
case DEFAULT_KEYBOARD_CODE:
imeOptions = EditorInfo.IME_FLAG_NO_ENTER_ACTION;
inputType = DEFAULT_INPUT_TYPE;
break;
case SINGLELINE_KEYBOARD_CODE:
imeOptions = EditorInfo.IME_ACTION_DONE;
inputType = DEFAULT_INPUT_TYPE;
break;
case NUMBER_KEYBOARD_CODE:
imeOptions = EditorInfo.IME_ACTION_DONE;
inputType = DEFAULT_INPUT_TYPE | InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
break;
default:
Log.e("Fyne", "unknown keyboard type, use default");
}
mTextEdit.setImeOptions(imeOptions);
mTextEdit.setInputType(inputType);
oldState = "";
mTextEdit.setText("");
mTextEdit.setVisibility(View.VISIBLE);
mTextEdit.bringToFront();
mTextEdit.requestFocus();
InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.showSoftInput(mTextEdit, 0);
}
});
}
static void hideKeyboard() {
goNativeActivity.doHideKeyboard();
}
void doHideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = findViewById(android.R.id.content).getRootView();
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextEdit.setVisibility(View.GONE);
}
});
}
static void showFileOpen(String mimes) {
goNativeActivity.doShowFileOpen(mimes);
}
void doShowFileOpen(String mimes) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
if ("application/x-directory".equals(mimes) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); // ask for a directory picker if OS supports it
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else if (mimes.contains("|") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimes.split("\\|"));
intent.addCategory(Intent.CATEGORY_OPENABLE);
} else {
intent.setType(mimes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
startActivityForResult(Intent.createChooser(intent, "Open File"), FILE_OPEN_CODE);
}
static int getRune(int deviceId, int keyCode, int metaState) {
try {
int rune = KeyCharacterMap.load(deviceId).get(keyCode, metaState);
if (rune == 0) {
return -1;
}
return rune;
} catch (KeyCharacterMap.UnavailableException e) {
return -1;
} catch (Exception e) {
Log.e("Fyne", "exception reading KeyCharacterMap", e);
return -1;
}
}
private void load() {
// Interestingly, NativeActivity uses a different method
// to find native code to execute, avoiding
// System.loadLibrary. The result is Java methods
// implemented in C with JNIEXPORT (and JNI_OnLoad) are not
// available unless an explicit call to System.loadLibrary
// is done. So we do it here, borrowing the name of the
// library from the same AndroidManifest.xml metadata used
// by NativeActivity.
try {
ActivityInfo ai = getPackageManager().getActivityInfo(
getIntent().getComponent(), PackageManager.GET_META_DATA);
if (ai.metaData == null) {
Log.e("Fyne", "loadLibrary: no manifest metadata found");
return;
}
String libName = ai.metaData.getString("android.app.lib_name");
System.loadLibrary(libName);
} catch (Exception e) {
Log.e("Fyne", "loadLibrary failed", e);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
load();
super.onCreate(savedInstanceState);
setupEntry();
View view = findViewById(android.R.id.content).getRootView();
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
public void onLayoutChange (View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
GoNativeActivity.this.updateLayout();
}
});
}
private void setupEntry() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextEdit = new EditText(goNativeActivity);
mTextEdit.setVisibility(View.GONE);
mTextEdit.setInputType(DEFAULT_INPUT_TYPE);
FrameLayout.LayoutParams mEditTextLayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
mTextEdit.setLayoutParams(mEditTextLayoutParams);
addContentView(mTextEdit, mEditTextLayoutParams);
mTextEdit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > oldState.length()) {
keyboardTyped(s.subSequence(oldState.length(), s.length()).toString());
} else if (s.length() < oldState.length()) {
// backspace key seems to be sent even for soft content
}
oldState = s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// unhandled request
if (requestCode != FILE_OPEN_CODE) {
return;
}
// dialog was cancelled
if (resultCode != Activity.RESULT_OK) {
filePickerReturned("");
return;
}
Uri uri = data.getData();
filePickerReturned(uri.toString());
}
}