<uses-sdk android:targetSdkVersion="10" android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="@string/app_name"
android:icon="@drawable/app_icon" >
SRC=\
$(SRC_DIR)/AltosDroid.java \
+ $(SRC_DIR)/AltosDroidPreferences.java \
+ $(SRC_DIR)/AltosVoice.java \
$(SRC_DIR)/TelemetryService.java \
$(SRC_DIR)/TelemetryReader.java \
+ $(SRC_DIR)/TelemetryLogger.java \
$(SRC_DIR)/AltosBluetooth.java \
$(SRC_DIR)/DeviceListActivity.java \
$(SRC_DIR)/Dumper.java
private Messenger mService = null;
final Messenger mMessenger = new Messenger(new IncomingHandler(this));
+ // Preferences
+ private AltosDroidPreferences prefs = null;
+
// TeleBT Config data
private AltosConfigData mConfigData = null;
// Local Bluetooth adapter
return;
}
+ // Initialise preferences
+ prefs = new AltosDroidPreferences(this);
+ AltosPreferences.init(prefs);
+
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//setContentView(R.layout.main);
--- /dev/null
+/*
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosDroid;
+
+import java.io.File;
+import java.util.Map;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.Environment;
+
+import org.altusmetrum.AltosLib.*;
+
+public class AltosDroidPreferences implements AltosPreferencesBackend {
+ public final static String NAME = "org.altusmetrum.AltosDroid";
+ private Context context = null;
+ private SharedPreferences prefs = null;
+ private SharedPreferences.Editor editor = null;
+
+ public AltosDroidPreferences(Context in_context) {
+ this(in_context, NAME);
+ }
+
+ public AltosDroidPreferences(Context in_context, String in_prefs) {
+ context = in_context;
+ prefs = context.getSharedPreferences(in_prefs, 0);
+ editor = prefs.edit();
+ }
+
+ public String[] keys() {
+ Map<String, ?> all = prefs.getAll();
+ return (String[])all.keySet().toArray();
+ }
+
+ public AltosPreferencesBackend node(String key) {
+ return new AltosDroidPreferences(context, key);
+ }
+
+ public boolean nodeExists(String key) {
+ return prefs.contains(key);
+ }
+
+ public boolean getBoolean(String key, boolean def) {
+ return prefs.getBoolean(key, def);
+ }
+
+ public double getDouble(String key, double def) {
+ Float f = Float.valueOf(prefs.getFloat(key, (float)def));
+ return f.doubleValue();
+ }
+
+ public int getInt(String key, int def) {
+ return prefs.getInt(key, def);
+ }
+
+ public String getString(String key, String def) {
+ return prefs.getString(key, def);
+ }
+
+ public void putBoolean(String key, boolean value) {
+ editor.putBoolean(key, value);
+ }
+
+ public void putDouble(String key, double value) {
+ editor.putFloat(key, (float)value);
+ }
+
+ public void putInt(String key, int value) {
+ editor.putInt(key, value);
+ }
+
+ public void putString(String key, String value) {
+ editor.putString(key, value);
+ }
+
+ public void remove(String key) {
+ editor.remove(key);
+ }
+
+ public void flush() {
+ editor.apply();
+ }
+
+ public File homeDirectory() {
+ return Environment.getExternalStorageDirectory();
+ }
+}
--- /dev/null
+package org.altusmetrum.AltosDroid;
+
+import org.altusmetrum.AltosLib.*;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Environment;
+import android.util.Log;
+
+public class TelemetryLogger {
+ private static final String TAG = "TelemetryLogger";
+ private static final boolean D = true;
+
+ private Context context = null;
+ private AltosLink link = null;
+ private AltosLog logger = null;
+
+ private BroadcastReceiver mExternalStorageReceiver;
+
+ public TelemetryLogger(Context in_context, AltosLink in_link) {
+ context = in_context;
+ link = in_link;
+
+ startWatchingExternalStorage();
+ }
+
+ public void stop() {
+ stopWatchingExternalStorage();
+ close();
+ }
+
+ private void close() {
+ if (logger != null) {
+ if (D) Log.d(TAG, "Shutting down Telemetry Logging");
+ logger.close();
+ logger = null;
+ }
+ }
+
+ void handleExternalStorageState() {
+ String state = Environment.getExternalStorageState();
+ if (Environment.MEDIA_MOUNTED.equals(state)) {
+ if (logger == null) {
+ if (D) Log.d(TAG, "Starting up Telemetry Logging");
+ logger = new AltosLog(link);
+ }
+ } else {
+ if (D) Log.d(TAG, "External Storage not present - stopping");
+ close();
+ }
+ }
+
+ void startWatchingExternalStorage() {
+ mExternalStorageReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ handleExternalStorageState();
+ }
+ };
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
+ filter.addAction(Intent.ACTION_MEDIA_REMOVED);
+ context.registerReceiver(mExternalStorageReceiver, filter);
+ handleExternalStorageState();
+ }
+
+ void stopWatchingExternalStorage() {
+ context.unregisterReceiver(mExternalStorageReceiver);
+ }
+
+}
private AltosBluetooth mAltosBluetooth = null;
private AltosConfigData mConfigData = null;
private TelemetryReader mTelemetryReader = null;
+ private TelemetryLogger mTelemetryLogger = null;
// internally track state of bluetooth connection
private int state = STATE_NONE;
}
mTelemetryReader = null;
}
+ if (mTelemetryLogger != null) {
+ if (D) Log.d(TAG, "stopAltosBluetooth(): stopping TelemetryLogger");
+ mTelemetryLogger.stop();
+ mTelemetryLogger = null;
+ }
if (mAltosBluetooth != null) {
if (D) Log.d(TAG, "stopAltosBluetooth(): stopping AltosBluetooth");
mAltosBluetooth.close();
mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler);
mTelemetryReader.start();
+
+ mTelemetryLogger = new TelemetryLogger(this, mAltosBluetooth);
}
* This creates a thread to capture telemetry data and write it to
* a log file
*/
-class AltosLog implements Runnable {
+public class AltosLog implements Runnable {
LinkedBlockingQueue<AltosLine> input_queue;
LinkedBlockingQueue<String> pending_queue;
}
}
- void close() {
+ public void close() {
close_log_file();
if (log_thread != null) {
log_thread.interrupt();
}
}
- File file() {
+ public File file() {
return file;
}
import java.io.*;
import java.util.*;
-import javax.swing.filechooser.FileSystemView;
public class AltosPreferences {
public static AltosPreferencesBackend backend = null;
if (logdir_string != null)
logdir = new File(logdir_string);
else {
- /* Use the file system view default directory */
- logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
+ logdir = new File(backend.homeDirectory(), logdirName);
if (!logdir.exists())
logdir.mkdirs();
}
package org.altusmetrum.AltosLib;
+import java.io.File;
+
public interface AltosPreferencesBackend {
public String getString(String key, String def);
public void remove(String key);
public void flush();
+
+ public File homeDirectory();
}
package altosui;
+import java.io.File;
import java.util.prefs.*;
import org.altusmetrum.AltosLib.*;
+import javax.swing.filechooser.FileSystemView;
public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
}
}
+ public File homeDirectory() {
+ /* Use the file system view default directory */
+ return FileSystemView.getFileSystemView().getDefaultDirectory();
+ }
}