android: NitActivity and NitService use long to hold pointers
[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 long nitActivity = 0;
29
30 /*
31 * Calls to Nit or to the C framework
32 */
33
34 static {
35 System.loadLibrary("nit_app");
36 }
37
38 /*
39 * Callbacks to Nit through C
40 */
41 protected native long nitRegisterActivity();
42 protected native void nitOnCreate(long activity, Bundle savedInstanceState);
43 protected native void nitOnStart(long activity);
44 protected native void nitOnRestart(long activity);
45 protected native void nitOnResume(long activity);
46 protected native void nitOnPause(long activity);
47 protected native void nitOnStop(long activity);
48 protected native void nitOnDestroy(long activity);
49 protected native void nitOnSaveInstanceState(long activity, Bundle savedInstanceState);
50 protected native void nitOnRestoreInstanceState(long activity, Bundle savedInstanceState);
51 protected native boolean nitOnBackPressed(long activity);
52 protected native boolean nitOnKeyDown(long activity, int keyCode, KeyEvent event);
53 protected native boolean nitOnKeyLongPress(long activity, int keyCode, KeyEvent event);
54 protected native boolean nitOnKeyMultiple(long activity, int keyCode, int count, KeyEvent event);
55 protected native boolean nitOnKeyUp(long activity, int keyCode, KeyEvent event);
56
57 /*
58 * Implementation of OS callbacks
59 */
60
61 @Override
62 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64
65 nitActivity = nitRegisterActivity();
66
67 nitOnCreate(nitActivity, savedInstanceState);
68 }
69
70 @Override
71 protected void onStart() {
72 super.onStart();
73 nitOnStart(nitActivity);
74 }
75
76 @Override
77 protected void onRestart() {
78 super.onRestart();
79 nitOnRestart(nitActivity);
80 }
81
82 @Override
83 protected void onResume() {
84 super.onResume();
85 nitOnResume(nitActivity);
86 }
87
88 @Override
89 protected void onPause() {
90 super.onPause();
91 nitOnPause(nitActivity);
92 }
93
94 @Override
95 protected void onStop() {
96 super.onStop();
97 nitOnStop(nitActivity);
98 }
99
100 @Override
101 protected void onDestroy() {
102 super.onDestroy();
103 nitOnDestroy(nitActivity);
104 }
105
106 @Override
107 public void onSaveInstanceState(Bundle savedInstanceState) {
108 super.onSaveInstanceState(savedInstanceState);
109 nitOnSaveInstanceState(nitActivity, savedInstanceState);
110 }
111
112 @Override
113 public void onRestoreInstanceState(Bundle savedInstanceState) {
114 super.onRestoreInstanceState(savedInstanceState);
115 nitOnRestoreInstanceState(nitActivity, savedInstanceState);
116 }
117
118 @Override
119 public void onBackPressed() {
120 if (!nitOnBackPressed(nitActivity))
121 super.onBackPressed();
122 }
123
124 @Override
125 public boolean onKeyDown(int keyCode, KeyEvent event) {
126 return nitOnKeyDown(nitActivity, keyCode, event)
127 || super.onKeyDown(keyCode, event);
128 }
129
130 @Override
131 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
132 return nitOnKeyLongPress(nitActivity, keyCode, event)
133 || super.onKeyLongPress(keyCode, event);
134 }
135
136 @Override
137 public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
138 return nitOnKeyMultiple(nitActivity, keyCode, count, event)
139 || super.onKeyMultiple(keyCode, count, event);
140 }
141
142 @Override
143 public boolean onKeyUp(int keyCode, KeyEvent event) {
144 return nitOnKeyUp(nitActivity, keyCode, event)
145 || super.onKeyUp(keyCode, event);
146 }
147 }