android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / android / src / XCSoar.java
blobe3c535a9632dbf4171ec7f733735149983f043b9
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 package org.xcsoar;
25 import android.app.Activity;
26 import android.app.PendingIntent;
27 import android.os.Bundle;
28 import android.view.MotionEvent;
29 import android.view.KeyEvent;
30 import android.view.Window;
31 import android.view.WindowManager;
32 import android.widget.TextView;
33 import android.os.Build;
34 import android.os.Environment;
35 import android.os.PowerManager;
36 import android.os.Handler;
37 import android.os.Message;
38 import android.os.BatteryManager;
39 import android.os.IBinder;
40 import android.content.Context;
41 import android.content.Intent;
42 import android.content.IntentFilter;
43 import android.content.BroadcastReceiver;
44 import android.content.ServiceConnection;
45 import android.content.ComponentName;
46 import android.util.Log;
47 import android.provider.Settings;
49 public class XCSoar extends Activity {
50 private static final String TAG = "XCSoar";
52 /**
53 * Hack: this is set by onCreate(), to support the "testing"
54 * package.
56 protected static Class serviceClass;
58 private static NativeView nativeView;
60 PowerManager.WakeLock wakeLock;
62 BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
63 @Override public void onReceive(Context context, Intent intent) {
64 if (nativeView == null)
65 return;
67 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
68 int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
69 nativeView.setBatteryPercent(level, plugged);
73 @Override protected void onCreate(Bundle savedInstanceState) {
74 if (serviceClass == null)
75 serviceClass = MyService.class;
77 super.onCreate(savedInstanceState);
79 Log.d(TAG, "ABI=" + Build.CPU_ABI);
80 Log.d(TAG, "PRODUCT=" + Build.PRODUCT);
81 Log.d(TAG, "MANUFACTURER=" + Build.MANUFACTURER);
82 Log.d(TAG, "MODEL=" + Build.MODEL);
83 Log.d(TAG, "DEVICE=" + Build.DEVICE);
84 Log.d(TAG, "BOARD=" + Build.BOARD);
85 Log.d(TAG, "FINGERPRINT=" + Build.FINGERPRINT);
87 if (!Loader.loaded) {
88 TextView tv = new TextView(this);
89 tv.setText("Failed to load the native XCSoar libary.\n" +
90 "Report this problem to us, and include the following information:\n" +
91 "ABI=" + Build.CPU_ABI + "\n" +
92 "PRODUCT=" + Build.PRODUCT + "\n" +
93 "FINGERPRINT=" + Build.FINGERPRINT + "\n" +
94 "error=" + Loader.error);
95 setContentView(tv);
96 return;
99 InternalGPS.Initialize();
100 NonGPSSensors.Initialize();
102 try {
103 BluetoothHelper.Initialize();
104 } catch (VerifyError e) {
105 // Android < 2.0 doesn't have Bluetooth support
108 try {
109 DownloadUtil.Initialise(this);
110 } catch (VerifyError e) {
111 // Android < 2.3 doesn't have the DownloadManager
114 // fullscreen mode
115 requestWindowFeature(Window.FEATURE_NO_TITLE);
116 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN|
117 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
119 TextView tv = new TextView(this);
120 tv.setText("Loading XCSoar...");
121 setContentView(tv);
123 registerReceiver(batteryReceiver,
124 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
127 private void quit() {
128 Log.d(TAG, "in quit()");
130 nativeView = null;
132 Log.d(TAG, "stopping service");
133 stopService(new Intent(this, serviceClass));
135 TextView tv = new TextView(XCSoar.this);
136 tv.setText("Shutting down XCSoar...");
137 setContentView(tv);
139 Log.d(TAG, "finish()");
140 finish();
143 Handler quitHandler = new Handler() {
144 public void handleMessage(Message msg) {
145 quit();
149 public void initSDL() {
150 if (!Loader.loaded)
151 return;
153 /* check if external storage is available; XCSoar doesn't work as
154 long as external storage is being forwarded to a PC */
155 String state = Environment.getExternalStorageState();
156 Log.d(TAG, "getExternalStorageState() = " + state);
157 if (!Environment.MEDIA_MOUNTED.equals(state)) {
158 TextView tv = new TextView(this);
159 tv.setText("External storage is not available (state='" + state
160 + "'). Please turn off USB storage.");
161 setContentView(tv);
162 return;
165 nativeView = new NativeView(this, quitHandler);
166 setContentView(nativeView);
167 // Receive keyboard events
168 nativeView.setFocusableInTouchMode(true);
169 nativeView.setFocusable(true);
170 nativeView.requestFocus();
172 // Obtain an instance of the Android PowerManager class
173 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
175 // Create a WakeLock instance to keep the screen from timing out
176 // Note: FULL_WAKE_LOCK is deprecated in favor of FLAG_KEEP_SCREEN_ON
177 wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|
178 PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
180 // Activate the WakeLock
181 wakeLock.acquire();
184 @Override protected void onPause() {
185 if (nativeView != null)
186 nativeView.onPause();
187 super.onPause();
190 private void getHapticFeedbackSettings() {
191 boolean hapticFeedbackEnabled;
192 try {
193 hapticFeedbackEnabled =
194 (Settings.System.getInt(getContentResolver(),
195 Settings.System.HAPTIC_FEEDBACK_ENABLED)) != 0;
196 } catch (Settings.SettingNotFoundException e) {
197 hapticFeedbackEnabled = false;
200 if (nativeView != null)
201 nativeView.setHapticFeedback(hapticFeedbackEnabled);
204 @Override protected void onResume() {
205 super.onResume();
207 startService(new Intent(this, serviceClass));
209 if (nativeView != null)
210 nativeView.onResume();
211 else
212 initSDL();
213 getHapticFeedbackSettings();
216 @Override protected void onDestroy()
218 Log.d(TAG, "in onDestroy()");
220 if (nativeView != null) {
221 nativeView.exitApp();
222 nativeView = null;
225 // Release the WakeLock instance to re-enable screen timeouts
226 if (wakeLock != null) {
227 wakeLock.release();
228 wakeLock = null;
231 super.onDestroy();
232 Log.d(TAG, "System.exit()");
233 System.exit(0);
236 @Override public boolean onKeyDown(int keyCode, final KeyEvent event) {
237 // Overrides Back key to use in our app
238 if (nativeView != null) {
239 nativeView.onKeyDown(keyCode, event);
240 return true;
241 } else
242 return super.onKeyDown(keyCode, event);
245 @Override public boolean onKeyUp(int keyCode, final KeyEvent event) {
246 if (nativeView != null) {
247 nativeView.onKeyUp(keyCode, event);
248 return true;
249 } else
250 return super.onKeyUp(keyCode, event);
253 @Override public boolean dispatchTouchEvent(final MotionEvent ev) {
254 if (nativeView != null) {
255 nativeView.onTouchEvent(ev);
256 return true;
257 } else
258 return super.dispatchTouchEvent(ev);