Merge remote-tracking branch 'mjb/altosdroid'
authorKeith Packard <keithp@keithp.com>
Thu, 20 Sep 2012 09:30:19 +0000 (11:30 +0200)
committerKeith Packard <keithp@keithp.com>
Thu, 20 Sep 2012 09:30:19 +0000 (11:30 +0200)
altosdroid/AndroidManifest.xml
altosdroid/Makefile.am
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java [new file with mode: 0644]
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryLogger.java [new file with mode: 0644]
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java
altoslib/AltosLog.java
altoslib/AltosPreferences.java
altoslib/AltosPreferencesBackend.java
altosui/AltosUIPreferencesBackend.java

index 1239175968fb334ed0d44a400d727ef37c61fe17..7147c0b1ef919da0507c3cd515118eab9118dab8 100644 (file)
@@ -22,6 +22,7 @@
     <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" >
index 36d28ca216ad1a0ef17c76bd4d3e90e4ca55fbe5..96831b722ba7955a87242bf562e5823df8965f37 100644 (file)
@@ -23,8 +23,11 @@ ALTOSLIB=$(EXT_LIBDIR)/$(ALTOSLIB_JAR)
 
 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
index 006896842a338fc2b2b330c85de4d12c48241eed..3396f77e40f9df866d94bd5ef87e355cfa1a3299 100644 (file)
@@ -87,6 +87,9 @@ public class AltosDroid extends Activity {
        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
@@ -228,6 +231,10 @@ public class AltosDroid extends Activity {
                        return;
                }
 
+               // Initialise preferences
+               prefs = new AltosDroidPreferences(this);
+               AltosPreferences.init(prefs);
+
                // Set up the window layout
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
                //setContentView(R.layout.main);
diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java
new file mode 100644 (file)
index 0000000..3b4bdcf
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * 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();
+       }
+}
diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryLogger.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryLogger.java
new file mode 100644 (file)
index 0000000..b2dcdb4
--- /dev/null
@@ -0,0 +1,73 @@
+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);
+       }
+
+}
index 393fd2f6f03609856bcb63195360f2e87c0f2387..3cb498e8a1995c05ddc2edc954846016dfe83c79 100644 (file)
@@ -76,6 +76,7 @@ public class TelemetryService extends Service {
        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;
@@ -164,6 +165,11 @@ public class TelemetryService extends Service {
                        }
                        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();
@@ -216,6 +222,8 @@ public class TelemetryService extends Service {
 
                mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler);
                mTelemetryReader.start();
+               
+               mTelemetryLogger = new TelemetryLogger(this, mAltosBluetooth);
        }
 
 
index 3c12470045935abe9c9297db3c73d3569224e51f..1c7069ce8521fd66209ed34a3f2837de9a62b199 100644 (file)
@@ -25,7 +25,7 @@ import java.util.concurrent.LinkedBlockingQueue;
  * 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;
@@ -45,7 +45,7 @@ class AltosLog implements Runnable {
                }
        }
 
-       void close() {
+       public void close() {
                close_log_file();
                if (log_thread != null) {
                        log_thread.interrupt();
@@ -53,7 +53,7 @@ class AltosLog implements Runnable {
                }
        }
 
-       File file() {
+       public File file() {
                return file;
        }
 
index a82ea3f67f0e4ba5229a5dc6d2abb0314c9ab663..47196d6e70b89342ef3f876207bef574bcb941f4 100644 (file)
@@ -19,7 +19,6 @@ package org.altusmetrum.AltosLib;
 
 import java.io.*;
 import java.util.*;
-import javax.swing.filechooser.FileSystemView;
 
 public class AltosPreferences {
        public static AltosPreferencesBackend backend = null;
@@ -145,8 +144,7 @@ public class AltosPreferences {
                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();
                }
index 3fc4b0aab93ad3d4dee1662c3c217beb9329e48b..a1184c0bc54c54dde9ca6ffcfeb592b5d72144eb 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.altusmetrum.AltosLib;
 
+import java.io.File;
+
 public interface AltosPreferencesBackend {
 
        public String  getString(String key, String def);
@@ -38,4 +40,6 @@ public interface AltosPreferencesBackend {
        public void    remove(String key);
 
        public void    flush();
+
+       public File homeDirectory();
 }
index 210dcb8bb14cdebb536ce18fb29637ade64f038c..3131fd3286ce917de9f3cf5c7eee1e23d5cc716f 100644 (file)
 
 package altosui;
 
+import java.io.File;
 import java.util.prefs.*;
 import org.altusmetrum.AltosLib.*;
+import javax.swing.filechooser.FileSystemView;
 
 public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
 
@@ -92,4 +94,8 @@ public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
                }
        }
 
+       public File homeDirectory() {
+               /* Use the file system view default directory */
+               return FileSystemView.getFileSystemView().getDefaultDirectory();
+       }
 }