lib/android: add onKey* callbacks to Nit_activity
[nit.git] / lib / android / NitActivity.java
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package nit.app;
17
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.view.KeyEvent;
21
22 /*
23 * Entry point to Nit applications on Android, redirect most calls to Nit
24 */
25 public class NitActivity extends Activity {
26
27 // Nit activity associated to `this`
28 protected int nitActivity = 0;
29
30 /*
31 * Calls to Nit or to the C framework
32 */
33
34 static {
35 System.loadLibrary("main");
36 }
37
38 /*
39 * Callbacks to Nit through C
40 */
41 protected native int nitRegisterActivity();
42 protected native void nitOnCreate(int activity, Bundle savedInstanceState);
43 protected native void nitOnStart(int activity);
44 protected native void nitOnRestart(int activity);
45 protected native void nitOnResume(int activity);
46 protected native void nitOnPause(int activity);
47 protected native void nitOnStop(int activity);
48 protected native void nitOnDestroy(int activity);
49 protected native void nitOnSaveInstanceState(int activity, Bundle savedInstanceState);
50 protected native void nitOnRestoreInstanceState(int activity, Bundle savedInstanceState);
51 protected native boolean nitOnKeyDown(int activity, int keyCode, KeyEvent event);
52 protected native boolean nitOnKeyLongPress(int activity, int keyCode, KeyEvent event);
53 protected native boolean nitOnKeyMultiple(int activity, int keyCode, int count, KeyEvent event);
54 protected native boolean nitOnKeyUp(int activity, int keyCode, KeyEvent event);
55
56 /*
57 * Implementation of OS callbacks
58 */
59
60 @Override
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63
64 nitActivity = nitRegisterActivity();
65
66 nitOnCreate(nitActivity, savedInstanceState);
67 }
68
69 @Override
70 protected void onStart() {
71 super.onStart();
72 nitOnStart(nitActivity);
73 }
74
75 @Override
76 protected void onRestart() {
77 super.onRestart();
78 nitOnRestart(nitActivity);
79 }
80
81 @Override
82 protected void onResume() {
83 super.onResume();
84 nitOnResume(nitActivity);
85 }
86
87 @Override
88 protected void onPause() {
89 super.onPause();
90 nitOnPause(nitActivity);
91 }
92
93 @Override
94 protected void onStop() {
95 super.onStop();
96 nitOnStop(nitActivity);
97 }
98
99 @Override
100 protected void onDestroy() {
101 super.onDestroy();
102 nitOnDestroy(nitActivity);
103 }
104
105 @Override
106 public void onSaveInstanceState(Bundle savedInstanceState) {
107 super.onSaveInstanceState(savedInstanceState);
108 nitOnSaveInstanceState(nitActivity, savedInstanceState);
109 }
110
111 @Override
112 public void onRestoreInstanceState(Bundle savedInstanceState) {
113 super.onRestoreInstanceState(savedInstanceState);
114 nitOnRestoreInstanceState(nitActivity, savedInstanceState);
115 }
116
117 @Override
118 public boolean onKeyDown(int keyCode, KeyEvent event) {
119 return nitOnKeyDown(nitActivity, keyCode, event)
120 || super.onKeyDown(keyCode, event);
121 }
122
123 @Override
124 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
125 return nitOnKeyLongPress(nitActivity, keyCode, event)
126 || super.onKeyLongPress(keyCode, event);
127 }
128
129 @Override
130 public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
131 return nitOnKeyMultiple(nitActivity, keyCode, count, event)
132 || super.onKeyMultiple(keyCode, count, event);
133 }
134
135 @Override
136 public boolean onKeyUp(int keyCode, KeyEvent event) {
137 return nitOnKeyUp(nitActivity, keyCode, event)
138 || super.onKeyUp(keyCode, event);
139 }
140 }