altosdroid: Centralize debug printf code
authorKeith Packard <keithp@keithp.com>
Wed, 27 May 2015 06:05:11 +0000 (23:05 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 27 May 2015 06:05:11 +0000 (23:05 -0700)
Create AltosDebug to hold the debug code, use it everywhere.

Signed-off-by: Keith Packard <keithp@keithp.com>
12 files changed:
altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosDebug.java [new file with mode: 0644]
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidLink.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidTab.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosUsb.java
altosdroid/src/org/altusmetrum/AltosDroid/DeviceListActivity.java
altosdroid/src/org/altusmetrum/AltosDroid/TabMapOffline.java
altosdroid/src/org/altusmetrum/AltosDroid/TabsAdapter.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryLogger.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java

index d506ff5..03ae5cb 100644 (file)
@@ -29,16 +29,11 @@ import android.bluetooth.BluetoothSocket;
 //import android.os.Bundle;
 import android.os.Handler;
 //import android.os.Message;
-import android.util.Log;
 
 import org.altusmetrum.altoslib_7.*;
 
 public class AltosBluetooth extends AltosDroidLink {
 
-       // Debugging
-       private static final String TAG = "AltosBluetooth";
-       private static final boolean D = true;
-
        private ConnectThread    connect_thread = null;
 
        private BluetoothDevice  device;
@@ -60,7 +55,7 @@ public class AltosBluetooth extends AltosDroidLink {
 
        void connected() {
                if (closed()) {
-                       if (D) Log.d(TAG, "connected after closed");
+                       AltosDebug.debug("connected after closed");
                        return;
                }
 
@@ -82,7 +77,7 @@ public class AltosBluetooth extends AltosDroidLink {
 
        private void connect_failed() {
                if (closed()) {
-                       if (D) Log.d(TAG, "connect_failed after closed");
+                       AltosDebug.debug("connect_failed after closed");
                        return;
                }
 
@@ -90,7 +85,7 @@ public class AltosBluetooth extends AltosDroidLink {
                input = null;
                output = null;
                handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
-               if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
+               AltosDebug.error("ConnectThread: Failed to establish connection");
        }
 
        void close_device() {
@@ -105,7 +100,7 @@ public class AltosBluetooth extends AltosDroidLink {
                        try {
                                tmp_socket.close();
                        } catch (IOException e) {
-                               if (D) Log.e(TAG, "close_socket failed");
+                               AltosDebug.error("close_socket failed");
                        }
                }
        }
@@ -122,13 +117,14 @@ public class AltosBluetooth extends AltosDroidLink {
 
                BluetoothSocket tmp_socket = null;
 
+               AltosDebug.check_ui("create_socket\n");
                try {
                        tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
                } catch (IOException e) {
                        e.printStackTrace();
                }
                if (socket != null) {
-                       if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString()));
+                       AltosDebug.debug("Socket already allocated %s", socket.toString());
                        close_device();
                }
                synchronized (this) {
@@ -139,7 +135,7 @@ public class AltosBluetooth extends AltosDroidLink {
        private class ConnectThread extends Thread {
 
                public void run() {
-                       if (D) Log.d(TAG, "ConnectThread: BEGIN");
+                       AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause);
                        setName("ConnectThread");
 
                        if (pause) {
@@ -154,7 +150,7 @@ public class AltosBluetooth extends AltosDroidLink {
                        try {
                                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                        } catch (Exception e) {
-                               if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString()));
+                               AltosDebug.debug("cancelDiscovery exception %s", e.toString());
                        }
 
                        BluetoothSocket local_socket = null;
@@ -170,8 +166,13 @@ public class AltosBluetooth extends AltosDroidLink {
                                        // This is a blocking call and will only return on a
                                        // successful connection or an exception
                                        local_socket.connect();
-                               } catch (IOException e) {
-                                       if (D) Log.d(TAG, String.format("Connect exception %s", e.toString()));
+                               } catch (Exception e) {
+                                       AltosDebug.debug("Connect exception %s", e.toString());
+                                       try {
+                                               local_socket.close();
+                                       } catch (Exception ce) {
+                                               AltosDebug.debug("Close exception %s", ce.toString());
+                                       }
                                        local_socket = null;
                                }
                        }
@@ -182,15 +183,16 @@ public class AltosBluetooth extends AltosDroidLink {
                                connect_failed();
                        }
 
-                       if (D) Log.d(TAG, "ConnectThread: completed");
+                       AltosDebug.debug("ConnectThread: completed");
                }
        }
 
        private synchronized void wait_connected() throws InterruptedException, IOException {
+               AltosDebug.check_ui("wait_connected\n");
                if (input == null && socket != null) {
-                       if (D) Log.d(TAG, "wait_connected...");
+                       AltosDebug.debug("wait_connected...");
                        wait();
-                       if (D) Log.d(TAG, "wait_connected done");
+                       AltosDebug.debug("wait_connected done");
                }
                if (socket == null)
                        throw new IOException();
diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDebug.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDebug.java
new file mode 100644 (file)
index 0000000..80f1861
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * 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.util.Arrays;
+import java.io.*;
+import java.lang.*;
+
+import org.altusmetrum.altoslib_7.*;
+
+import android.app.Activity;
+import android.graphics.*;
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentTransaction;
+import android.view.*;
+import android.widget.*;
+import android.location.Location;
+import android.content.*;
+import android.util.Log;
+import android.os.Looper;
+
+public class AltosDebug {
+       // Debugging
+       static final String TAG = "AltosDroid";
+       public static final boolean D = true;
+
+       static void info(String format, Object ... arguments) {
+               Log.i(TAG, String.format(format, arguments));
+       }
+
+       static void debug(String format, Object ... arguments) {
+               if (D)
+                       Log.d(TAG, String.format(format, arguments));
+       }
+
+       static void error(String format, Object ... arguments) {
+               Log.e(TAG, String.format(format, arguments));
+       }
+
+       static void check_ui(String format, Object ... arguments) {
+               if (Looper.myLooper() == Looper.getMainLooper()) {
+                       Log.e(TAG, String.format("ON UI THREAD " + format, arguments));
+                       for (StackTraceElement el : Thread.currentThread().getStackTrace())
+                               Log.e(TAG, "\t" + el.toString() + "\n");
+               }
+       }
+}
index dd87614..6561864 100644 (file)
@@ -42,7 +42,6 @@ import android.content.res.Resources;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.app.FragmentManager;
 import android.util.DisplayMetrics;
-import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -60,9 +59,6 @@ import android.hardware.usb.*;
 import org.altusmetrum.altoslib_7.*;
 
 public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
-       // Debugging
-       static final String TAG = "AltosDroid";
-       static final boolean D = true;
 
        // Actions sent to the telemetry server at startup time
 
@@ -133,17 +129,17 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
 
                        switch (msg.what) {
                        case MSG_STATE:
-                               if(D) Log.d(TAG, "MSG_STATE");
+                               AltosDebug.debug("MSG_STATE");
                                TelemetryState telemetry_state = (TelemetryState) msg.obj;
                                if (telemetry_state == null) {
-                                       Log.d(TAG, "telemetry_state null!");
+                                       AltosDebug.debug("telemetry_state null!");
                                        return;
                                }
 
                                ad.update_state(telemetry_state);
                                break;
                        case MSG_UPDATE_AGE:
-                               if(D) Log.d(TAG, "MSG_UPDATE_AGE");
+                               AltosDebug.debug("MSG_UPDATE_AGE");
                                ad.update_age();
                                break;
                        }
@@ -421,7 +417,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
-               if(D) Log.e(TAG, "+++ ON CREATE +++");
+               AltosDebug.debug("+++ ON CREATE +++");
 
                fm = getSupportFragmentManager();
 
@@ -526,13 +522,13 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
 
-               if (D) Log.e(TAG, "intent " + intent + " device " + device + " granted " + granted);
+               AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
 
                if (!granted)
                        device = null;
 
                if (device != null) {
-                       if (D) Log.d(TAG, "intent has usb device " + device.toString());
+                       AltosDebug.debug("intent has usb device " + device.toString());
                        connectUsb(device);
                } else {
 
@@ -542,11 +538,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                         * don't want to loop forever...
                         */
                        if (granted) {
-                               if (D) Log.d(TAG, "check for a USB device at startup");
+                               AltosDebug.debug("check for a USB device at startup");
                                if (check_usb())
                                        return;
                        }
-                       if (D) Log.d(TAG, "Starting by looking for bluetooth devices");
+                       AltosDebug.debug("Starting by looking for bluetooth devices");
                        if (ensureBluetooth())
                                return;
                        finish();
@@ -556,7 +552,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        @Override
        public void onStart() {
                super.onStart();
-               if(D) Log.e(TAG, "++ ON START ++");
+               AltosDebug.debug("++ ON START ++");
 
                noticeIntent(getIntent());
 
@@ -575,26 +571,26 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        @Override
        public void onNewIntent(Intent intent) {
                super.onNewIntent(intent);
-               if(D) Log.d(TAG, "onNewIntent");
+               AltosDebug.debug("onNewIntent");
                noticeIntent(intent);
        }
 
        @Override
        public void onResume() {
                super.onResume();
-               if(D) Log.e(TAG, "+ ON RESUME +");
+               AltosDebug.debug("+ ON RESUME +");
        }
 
        @Override
        public void onPause() {
                super.onPause();
-               if(D) Log.e(TAG, "- ON PAUSE -");
+               AltosDebug.debug("- ON PAUSE -");
        }
 
        @Override
        public void onStop() {
                super.onStop();
-               if(D) Log.e(TAG, "-- ON STOP --");
+               AltosDebug.debug("-- ON STOP --");
 
                doUnbindService();
                if (mAltosVoice != null) {
@@ -606,14 +602,14 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        @Override
        public void onDestroy() {
                super.onDestroy();
-               if(D) Log.e(TAG, "--- ON DESTROY ---");
+               AltosDebug.debug("--- ON DESTROY ---");
 
                if (mAltosVoice != null) mAltosVoice.stop();
                stop_timer();
        }
 
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-               if(D) Log.d(TAG, "onActivityResult " + resultCode);
+               AltosDebug.debug("onActivityResult " + resultCode);
                switch (requestCode) {
                case REQUEST_CONNECT_DEVICE:
                        // When DeviceListActivity returns with a device to connect to
@@ -628,7 +624,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                                //setupChat();
                        } else {
                                // User did not enable Bluetooth or an error occured
-                               Log.e(TAG, "BT not enabled");
+                               AltosDebug.error("BT not enabled");
                                stopService(new Intent(AltosDroid.this, TelemetryService.class));
                                Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
                                finish();
@@ -644,9 +640,9 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                        // Attempt to connect to the device
                        try {
                                mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
-                               if (D) Log.d(TAG, "Sent OPEN_USB message");
+                               AltosDebug.debug("Sent OPEN_USB message");
                        } catch (RemoteException e) {
-                               if (D) Log.e(TAG, "connect device message failed");
+                               AltosDebug.debug("connect device message failed");
                        }
                }
        }
@@ -657,12 +653,12 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                        String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                        String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
 
-                       if (D) Log.d(TAG, "Connecting to " + address + " " + name);
+                       AltosDebug.debug("Connecting to " + address + " " + name);
                        DeviceAddress   a = new DeviceAddress(address, name);
                        mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
-                       if (D) Log.d(TAG, "Sent connecting message");
+                       AltosDebug.debug("Sent connecting message");
                } catch (RemoteException e) {
-                       if (D) Log.e(TAG, "connect device message failed");
+                       AltosDebug.debug("connect device message failed");
                }
        }
 
@@ -738,7 +734,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                        disconnectDevice();
                        return true;
                case R.id.quit:
-                       Log.d(TAG, "R.id.quit");
+                       AltosDebug.debug("R.id.quit");
                        disconnectDevice();
                        finish();
                        return true;
index 6209077..c723051 100644 (file)
@@ -23,16 +23,11 @@ import java.io.OutputStream;
 import java.util.UUID;
 
 import android.os.Handler;
-import android.util.Log;
 
 import org.altusmetrum.altoslib_7.*;
 
 public abstract class AltosDroidLink extends AltosLink {
 
-       // Debugging
-       private static final String TAG = "AltosDroidLink";
-       private static final boolean D = true;
-
        Handler         handler;
 
        Thread          input_thread   = null;
@@ -70,7 +65,7 @@ public abstract class AltosDroidLink extends AltosLink {
                // Configure the newly connected device for telemetry
                print("~\nE 0\n");
                set_monitor(false);
-               if (D) Log.d(TAG, "ConnectThread: connected");
+               AltosDebug.debug("ConnectThread: connected");
 
                /* Let TelemetryService know we're connected
                 */
@@ -83,7 +78,7 @@ public abstract class AltosDroidLink extends AltosLink {
 
        public void closing() {
                synchronized(closed_lock) {
-                       if (D) Log.d(TAG, "Marked closing true");
+                       AltosDebug.debug("Marked closing true");
                        closing = true;
                }
        }
@@ -97,14 +92,14 @@ public abstract class AltosDroidLink extends AltosLink {
        abstract void close_device();
 
        public void close() {
-               if (D) Log.d(TAG, "close(): begin");
+               AltosDebug.debug("close(): begin");
 
                closing();
 
                flush_output();
 
                synchronized (closed_lock) {
-                       if (D) Log.d(TAG, "Marked closed true");
+                       AltosDebug.debug("Marked closed true");
                        closed = true;
                }
 
@@ -113,11 +108,11 @@ public abstract class AltosDroidLink extends AltosLink {
                synchronized(this) {
 
                        if (input_thread != null) {
-                               if (D) Log.d(TAG, "close(): stopping input_thread");
+                               AltosDebug.debug("close(): stopping input_thread");
                                try {
-                                       if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
+                                       AltosDebug.debug("close(): input_thread.interrupt().....");
                                        input_thread.interrupt();
-                                       if (D) Log.d(TAG, "close(): input_thread.join().....");
+                                       AltosDebug.debug("close(): input_thread.join().....");
                                        input_thread.join();
                                } catch (Exception e) {}
                                input_thread = null;
@@ -143,7 +138,7 @@ public abstract class AltosDroidLink extends AltosLink {
 
        private void debug_input(byte b) {
                if (b == '\n') {
-                       Log.d(TAG, "            " + new String(debug_chars, 0, debug_off));
+                       AltosDebug.debug("            " + new String(debug_chars, 0, debug_off));
                        debug_off = 0;
                } else {
                        if (debug_off < buffer_size)
@@ -153,11 +148,11 @@ public abstract class AltosDroidLink extends AltosLink {
 
        private void disconnected() {
                if (closed()) {
-                       if (D) Log.d(TAG, "disconnected after closed");
+                       AltosDebug.debug("disconnected after closed");
                        return;
                }
 
-               if (D) Log.d(TAG, "Sending disconnected message");
+               AltosDebug.debug("Sending disconnected message");
                handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
        }
 
@@ -169,13 +164,13 @@ public abstract class AltosDroidLink extends AltosLink {
                while (buffer_off == buffer_len) {
                        buffer_len = read(in_buffer, buffer_size);
                        if (buffer_len < 0) {
-                               Log.d(TAG, "ERROR returned from getchar()");
+                               AltosDebug.debug("ERROR returned from getchar()");
                                disconnected();
                                return ERROR;
                        }
                        buffer_off = 0;
                }
-               if (D)
+               if (AltosDebug.D)
                        debug_input(in_buffer[buffer_off]);
                return in_buffer[buffer_off++];
        }
@@ -192,7 +187,7 @@ public abstract class AltosDroidLink extends AltosLink {
                        int     sent = write(out_buffer, out_buffer_off);
 
                        if (sent <= 0) {
-                               Log.d(TAG, "flush_output() failed");
+                               AltosDebug.debug("flush_output() failed");
                                out_buffer_off = 0;
                                break;
                        }
@@ -212,10 +207,10 @@ public abstract class AltosDroidLink extends AltosLink {
 
        public void print(String data) {
                byte[] bytes = data.getBytes();
-               if (D) Log.d(TAG, "print(): begin");
+               AltosDebug.debug("print(): begin");
                for (byte b : bytes)
                        putchar(b);
-               if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
+               AltosDebug.debug("print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
        }
 
        public AltosDroidLink(Handler handler) {
index f91ddf5..d33ad05 100644 (file)
@@ -26,7 +26,6 @@ import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v4.app.FragmentManager;
 import android.location.Location;
-import android.util.Log;
 import android.widget.TextView;
 
 public abstract class AltosDroidTab extends Fragment implements AltosUnitsListener {
index 4685a82..e559f81 100644 (file)
@@ -27,16 +27,11 @@ import android.content.Context;
 import android.hardware.usb.*;
 import android.app.*;
 import android.os.Handler;
-import android.util.Log;
 
 import org.altusmetrum.altoslib_7.*;
 
 public class AltosUsb extends AltosDroidLink {
 
-       // Debugging
-       private static final String TAG = "AltosUsb";
-       private static final boolean D = true;
-
        private Thread           input_thread   = null;
 
        private Handler          handler;
@@ -91,19 +86,19 @@ public class AltosUsb extends AltosDroidLink {
                }
 
                if (in != null && out != null) {
-                       Log.d(TAG, String.format("\tin %s out %s\n", in.toString(), out.toString()));
+                       AltosDebug.debug("\tin %s out %s\n", in.toString(), out.toString());
 
                        manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
 
                        if (manager == null) {
-                               Log.d(TAG, "USB_SERVICE failed");
+                               AltosDebug.debug("USB_SERVICE failed");
                                return;
                        }
 
                        connection = manager.openDevice(device);
 
                        if (connection == null) {
-                               Log.d(TAG, "openDevice failed");
+                               AltosDebug.debug("openDevice failed");
                                return;
                        }
 
@@ -167,7 +162,7 @@ public class AltosUsb extends AltosDroidLink {
 //             if (manager.hasPermission(device))
 //                     return true;
 
-               Log.d(TAG, "request permission for USB device " + device.toString());
+               AltosDebug.debug("request permission for USB device " + device.toString());
 
                manager.requestPermission(device, pi);
                return false;
@@ -183,7 +178,7 @@ public class AltosUsb extends AltosDroidLink {
                        int     product = device.getProductId();
 
                        if (matchProduct(match_product, device)) {
-                               Log.d(TAG, "found USB device " + device.toString());
+                               AltosDebug.debug("found USB device " + device.toString());
                                return device;
                        }
                }
@@ -193,11 +188,11 @@ public class AltosUsb extends AltosDroidLink {
 
        private void disconnected() {
                if (closed()) {
-                       if (D) Log.d(TAG, "disconnected after closed");
+                       AltosDebug.debug("disconnected after closed");
                        return;
                }
 
-               if (D) Log.d(TAG, "Sending disconnected message");
+               AltosDebug.debug("Sending disconnected message");
                handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
        }
 
@@ -210,20 +205,20 @@ public class AltosUsb extends AltosDroidLink {
                }
 
                if (tmp_connection != null) {
-                       if (D) Log.d(TAG, "Closing USB device");
+                       AltosDebug.debug("Closing USB device");
                        tmp_connection.close();
                }
        }
 
        int read(byte[] buffer, int len) {
                int ret = connection.bulkTransfer(in, buffer, len, -1);
-               if (D) Log.d(TAG, String.format("read(%d) = %d\n", len, ret));
+               AltosDebug.debug("read(%d) = %d\n", len, ret);
                return ret;
        }
 
        int write(byte[] buffer, int len) {
                int ret = connection.bulkTransfer(out, buffer, len, -1);
-               if (D) Log.d(TAG, String.format("write(%d) = %d\n", len, ret));
+               AltosDebug.debug("write(%d) = %d\n", len, ret);
                return ret;
        }
 
index fd6abe0..4af117a 100644 (file)
@@ -27,7 +27,6 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
-import android.util.Log;
 import android.view.View;
 import android.view.Window;
 import android.view.View.OnClickListener;
@@ -45,9 +44,6 @@ import android.widget.AdapterView.OnItemClickListener;
  * Activity in the result Intent.
  */
 public class DeviceListActivity extends Activity {
-       // Debugging
-       private static final String TAG = "DeviceListActivity";
-       private static final boolean D = true;
 
        // Return Intent extra
        public static final String EXTRA_DEVICE_ADDRESS = "device_address";
@@ -137,7 +133,7 @@ public class DeviceListActivity extends Activity {
        * Start device discover with the BluetoothAdapter
        */
        private void doDiscovery() {
-               if (D) Log.d(TAG, "doDiscovery()");
+               AltosDebug.debug("doDiscovery()");
 
                // Indicate scanning in the title
                setProgressBarIndeterminateVisibility(true);
@@ -173,7 +169,7 @@ public class DeviceListActivity extends Activity {
                        else
                                name = info;
 
-                       if (D) Log.d(TAG, String.format("******* selected item '%s'", info));
+                       AltosDebug.debug("******* selected item '%s'", info);
 
                        // Create the result Intent and include the MAC address
                        Intent intent = new Intent();
index 42d80ad..ceabe7b 100644 (file)
@@ -31,12 +31,8 @@ import android.view.*;
 import android.widget.*;
 import android.location.Location;
 import android.content.*;
-import android.util.Log;
 
 public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
-       // Debugging
-       static final String TAG = "AltosDroid";
-       static final boolean D = true;
 
        AltosDroid mAltosDroid;
 
@@ -75,7 +71,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
                public boolean onScale(ScaleGestureDetector detector) {
                        float   f = detector.getScaleFactor();
-                       if (D) Log.d(TAG, String.format("onScale %f\n", f));
+                       AltosDebug.debug("onScale %f\n", f);
                        if (f <= 0.8) {
                                map.set_zoom(map.get_zoom() - 1);
                                return true;
@@ -88,12 +84,12 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                }
 
                public boolean onScaleBegin(ScaleGestureDetector detector) {
-                       if (D) Log.d(TAG, String.format("onScaleBegin %f\n", detector.getScaleFactor()));
+                       AltosDebug.debug("onScaleBegin %f\n", detector.getScaleFactor());
                        return true;
                }
 
                public void onScaleEnd(ScaleGestureDetector detector) {
-                       if (D) Log.d(TAG, String.format("onScaleEnd %f\n", detector.getScaleFactor()));
+                       AltosDebug.debug("onScaleEnd %f\n", detector.getScaleFactor());
                }
 
                @Override
@@ -105,19 +101,19 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                        }
 
                        if (scaling) {
-                               if(D) Log.d(TAG, "scale in progress\n");
+                               if(AltosDebug.D) AltosDebug.debug("scale in progress\n");
                                if (event.getAction() == MotionEvent.ACTION_UP) {
-                                       if (D) Log.d(TAG, "scale finished\n");
+                                       AltosDebug.debug("scale finished\n");
                                        scaling = false;
                                }
                                return true;
                        }
 
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
-                               if(D) Log.d(TAG, String.format("down event %g %g\n", event.getX(), event.getY()));
+                               AltosDebug.debug("down event %g %g\n", event.getX(), event.getY());
                                map.touch_start((int) event.getX(), (int) event.getY(), true);
                        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
-                               if(D) Log.d(TAG, String.format("continue event %g %g\n", event.getX(), event.getY()));
+                               AltosDebug.debug("continue event %g %g\n", event.getX(), event.getY());
                                map.touch_continue((int) event.getX(), (int) event.getY(), true);
                        }
                        return true;
index 1ac34f9..b34a25b 100644 (file)
@@ -29,7 +29,6 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TabHost;
 import android.widget.TabWidget;
-import android.util.Log;
 
 /**
  * This is a helper class that implements the management of tabs and all
@@ -106,7 +105,7 @@ public class TabsAdapter extends FragmentPagerAdapter
        @Override
        public Fragment getItem(int position) {
                TabInfo info = mTabs.get(position);
-               Log.d(AltosDroid.TAG, String.format("TabsAdapter.getItem(%d)", position));
+               AltosDebug.debug("TabsAdapter.getItem(%d)", position);
                info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
                return info.fragment;
        }
@@ -131,7 +130,7 @@ public class TabsAdapter extends FragmentPagerAdapter
                if (cur_frag != null) {
                        cur_frag.set_visible(true);
                }
-               Log.d(AltosDroid.TAG, String.format("TabsAdapter.onTabChanged(%s) = %d", tabId, position));
+               AltosDebug.debug("TabsAdapter.onTabChanged(%s) = %d", tabId, position);
                mViewPager.setCurrentItem(position);
        }
 
index 0cd9b2c..7c3c226 100644 (file)
@@ -7,12 +7,8 @@ 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;
@@ -33,7 +29,7 @@ public class TelemetryLogger {
 
        private void close() {
                if (logger != null) {
-                       if (D) Log.d(TAG, "Shutting down Telemetry Logging");
+                       AltosDebug.debug("Shutting down Telemetry Logging");
                        logger.close();
                        logger = null;
                }
@@ -43,11 +39,11 @@ public class TelemetryLogger {
                String state = Environment.getExternalStorageState();
                if (Environment.MEDIA_MOUNTED.equals(state)) {
                        if (logger == null) {
-                               if (D) Log.d(TAG, "Starting up Telemetry Logging");
+                               AltosDebug.debug("Starting up Telemetry Logging");
                                logger = new AltosLog(link);
                        }
                } else {
-                       if (D) Log.d(TAG, "External Storage not present - stopping");
+                       AltosDebug.debug("External Storage not present - stopping");
                        close();
                }
        }
index 3f31fa6..7b29fe4 100644 (file)
@@ -22,7 +22,6 @@ package org.altusmetrum.AltosDroid;
 import java.text.*;
 import java.io.*;
 import java.util.concurrent.*;
-import android.util.Log;
 import android.os.Handler;
 
 import org.altusmetrum.altoslib_7.*;
@@ -30,9 +29,6 @@ import org.altusmetrum.altoslib_7.*;
 
 public class TelemetryReader extends Thread {
 
-       private static final String TAG = "TelemetryReader";
-       private static final boolean D = true;
-
        int         crc_errors;
 
        Handler     handler;
@@ -67,13 +63,13 @@ public class TelemetryReader extends Thread {
                AltosState  state = null;
 
                try {
-                       if (D) Log.d(TAG, "starting loop");
+                       AltosDebug.debug("starting loop");
                        while (telemQueue != null) {
                                try {
                                        state = read();
                                        handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
                                } catch (ParseException pp) {
-                                       Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
+                                       AltosDebug.error("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage());
                                } catch (AltosCRCException ce) {
                                        ++crc_errors;
                                        handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
@@ -81,7 +77,7 @@ public class TelemetryReader extends Thread {
                        }
                } catch (InterruptedException ee) {
                } catch (IOException ie) {
-                       Log.e(TAG, "IO exception in telemetry reader");
+                       AltosDebug.error("IO exception in telemetry reader");
                        handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, link).sendToTarget();
                } finally {
                        close();
@@ -89,7 +85,7 @@ public class TelemetryReader extends Thread {
        }
 
        public TelemetryReader (AltosLink in_link, Handler in_handler, AltosState in_state) {
-               if (D) Log.d(TAG, "connected TelemetryReader create started");
+               AltosDebug.debug("connected TelemetryReader create started");
                link    = in_link;
                handler = in_handler;
 
@@ -98,6 +94,6 @@ public class TelemetryReader extends Thread {
                link.add_monitor(telemQueue);
                link.set_telemetry(AltosLib.ao_telemetry_standard);
 
-               if (D) Log.d(TAG, "connected TelemetryReader created");
+               AltosDebug.debug("connected TelemetryReader created");
        }
 }
index ed7b75a..627f595 100644 (file)
@@ -39,7 +39,6 @@ import android.os.Message;
 import android.os.Messenger;
 import android.os.RemoteException;
 import android.os.Looper;
-import android.util.Log;
 import android.widget.Toast;
 import android.location.Location;
 import android.location.LocationManager;
@@ -51,9 +50,6 @@ import org.altusmetrum.altoslib_7.*;
 
 public class TelemetryService extends Service implements LocationListener {
 
-       private static final String TAG = "TelemetryService";
-       private static final boolean D = true;
-
        static final int MSG_REGISTER_CLIENT   = 1;
        static final int MSG_UNREGISTER_CLIENT = 2;
        static final int MSG_CONNECT           = 3;
@@ -109,23 +105,23 @@ public class TelemetryService extends Service implements LocationListener {
                                s.remove_client(msg.replyTo);
                                break;
                        case MSG_CONNECT:
-                               if (D) Log.d(TAG, "Connect command received");
+                               AltosDebug.debug("Connect command received");
                                DeviceAddress address = (DeviceAddress) msg.obj;
                                AltosDroidPreferences.set_active_device(address);
                                s.start_altos_bluetooth(address, false);
                                break;
                        case MSG_OPEN_USB:
-                               if (D) Log.d(TAG, "Open USB command received");
+                               AltosDebug.debug("Open USB command received");
                                UsbDevice device = (UsbDevice) msg.obj;
                                s.start_usb(device);
                                break;
                        case MSG_DISCONNECT:
-                               if (D) Log.d(TAG, "Disconnect command received");
+                               AltosDebug.debug("Disconnect command received");
                                s.address = null;
                                s.disconnect(true);
                                break;
                        case MSG_SETFREQUENCY:
-                               if (D) Log.d(TAG, "MSG_SETFREQUENCY");
+                               AltosDebug.debug("MSG_SETFREQUENCY");
                                s.telemetry_state.frequency = (Double) msg.obj;
                                if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
                                        try {
@@ -138,7 +134,7 @@ public class TelemetryService extends Service implements LocationListener {
                                s.send_to_clients();
                                break;
                        case MSG_SETBAUD:
-                               if (D) Log.d(TAG, "MSG_SETBAUD");
+                               AltosDebug.debug("MSG_SETBAUD");
                                s.telemetry_state.telemetry_rate = (Integer) msg.obj;
                                if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
                                        s.altos_link.set_telemetry_rate(s.telemetry_state.telemetry_rate);
@@ -151,29 +147,29 @@ public class TelemetryService extends Service implements LocationListener {
                                 *Messages from AltosBluetooth
                                 */
                        case MSG_CONNECTED:
-                               Log.d(TAG, "MSG_CONNECTED");
+                               AltosDebug.debug("MSG_CONNECTED");
                                bt = (AltosDroidLink) msg.obj;
 
                                if (bt != s.altos_link) {
-                                       if (D) Log.d(TAG, "Stale message");
+                                       AltosDebug.debug("Stale message");
                                        break;
                                }
-                               if (D) Log.d(TAG, "Connected to device");
+                               AltosDebug.debug("Connected to device");
                                try {
                                        s.connected();
                                } catch (InterruptedException ie) {
                                }
                                break;
                        case MSG_CONNECT_FAILED:
-                               Log.d(TAG, "MSG_CONNECT_FAILED");
+                               AltosDebug.debug("MSG_CONNECT_FAILED");
                                bt = (AltosDroidLink) msg.obj;
 
                                if (bt != s.altos_link) {
-                                       if (D) Log.d(TAG, "Stale message");
+                                       AltosDebug.debug("Stale message");
                                        break;
                                }
                                if (s.address != null) {
-                                       if (D) Log.d(TAG, "Connection failed... retrying");
+                                       AltosDebug.debug("Connection failed... retrying");
                                        s.start_altos_bluetooth(s.address, true);
                                } else {
                                        s.disconnect(true);
@@ -182,15 +178,15 @@ public class TelemetryService extends Service implements LocationListener {
                        case MSG_DISCONNECTED:
 
                                /* This can be sent by either AltosDroidLink or TelemetryReader */
-                               Log.d(TAG, "MSG_DISCONNECTED");
+                               AltosDebug.debug("MSG_DISCONNECTED");
                                bt = (AltosDroidLink) msg.obj;
 
                                if (bt != s.altos_link) {
-                                       if (D) Log.d(TAG, "Stale message");
+                                       AltosDebug.debug("Stale message");
                                        break;
                                }
                                if (s.address != null) {
-                                       if (D) Log.d(TAG, "Connection lost... retrying");
+                                       AltosDebug.debug("Connection lost... retrying");
                                        s.start_altos_bluetooth(s.address, true);
                                } else {
                                        s.disconnect(true);
@@ -203,16 +199,16 @@ public class TelemetryService extends Service implements LocationListener {
                        case MSG_TELEMETRY:
                                s.telemetry_state.state = (AltosState) msg.obj;
                                if (s.telemetry_state.state != null) {
-                                       if (D) Log.d(TAG, "Save state");
+                                       AltosDebug.debug("Save state");
                                        AltosPreferences.set_state(0, s.telemetry_state.state, null);
                                }
-                               if (D) Log.d(TAG, "MSG_TELEMETRY");
+                               AltosDebug.debug("MSG_TELEMETRY");
                                s.send_to_clients();
                                break;
                        case MSG_CRC_ERROR:
                                // forward crc error messages
                                s.telemetry_state.crc_errors = (Integer) msg.obj;
-                               if (D) Log.d(TAG, "MSG_CRC_ERROR");
+                               AltosDebug.debug("MSG_CRC_ERROR");
                                s.send_to_clients();
                                break;
                        default:
@@ -225,9 +221,9 @@ public class TelemetryService extends Service implements LocationListener {
         */
        private Message message() {
                if (telemetry_state == null)
-                       Log.d(TAG, "telemetry_state null!");
+                       AltosDebug.debug("telemetry_state null!");
                if (telemetry_state.state == null)
-                       Log.d(TAG, "telemetry_state.state null!");
+                       AltosDebug.debug("telemetry_state.state null!");
                return Message.obtain(null, AltosDroid.MSG_STATE, telemetry_state);
        }
 
@@ -236,7 +232,7 @@ public class TelemetryService extends Service implements LocationListener {
        private void add_client(Messenger client) {
 
                clients.add(client);
-               if (D) Log.d(TAG, "Client bound to service");
+               AltosDebug.debug("Client bound to service");
 
                /* On connect, send the current state to the new client
                 */
@@ -246,7 +242,7 @@ public class TelemetryService extends Service implements LocationListener {
                 * go ahead and try to reconnect to the device
                 */
                if (address != null && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
-                       if (D) Log.d(TAG, "Reconnecting now...");
+                       AltosDebug.debug("Reconnecting now...");
                        start_altos_bluetooth(address, false);
                }
        }
@@ -255,37 +251,37 @@ public class TelemetryService extends Service implements LocationListener {
         */
        private void remove_client(Messenger client) {
                clients.remove(client);
-               if (D) Log.d(TAG, "Client unbound from service");
+               AltosDebug.debug("Client unbound from service");
 
                /* When the list of clients is empty, stop the service if
                 * we have no current telemetry source
                 */
 
                 if (clients.isEmpty() && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
-                        if (!D) Log.d(TAG, "No clients, no connection. Stopping\n");
+                        AltosDebug.debug("No clients, no connection. Stopping\n");
                         stopSelf();
                 }
        }
 
        private void send_to_client(Messenger client, Message m) {
                try {
-                       if (D) Log.d(TAG, String.format("Send message to client %s", client.toString()));
+                       AltosDebug.debug("Send message to client %s", client.toString());
                        client.send(m);
                } catch (RemoteException e) {
-                       if (D) Log.e(TAG, String.format("Client %s disappeared", client.toString()));
+                       AltosDebug.error("Client %s disappeared", client.toString());
                        remove_client(client);
                }
        }
 
        private void send_to_clients() {
                Message m = message();
-               if (D) Log.d(TAG, String.format("Send message to %d clients", clients.size()));
+               AltosDebug.debug("Send message to %d clients", clients.size());
                for (Messenger client : clients)
                        send_to_client(client, m);
        }
 
        private void disconnect(boolean notify) {
-               if (D) Log.d(TAG, "disconnect(): begin");
+               AltosDebug.debug("disconnect(): begin");
 
                telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
                telemetry_state.address = null;
@@ -294,7 +290,7 @@ public class TelemetryService extends Service implements LocationListener {
                        altos_link.closing();
 
                if (telemetry_reader != null) {
-                       if (D) Log.d(TAG, "disconnect(): stopping TelemetryReader");
+                       AltosDebug.debug("disconnect(): stopping TelemetryReader");
                        telemetry_reader.interrupt();
                        try {
                                telemetry_reader.join();
@@ -303,21 +299,21 @@ public class TelemetryService extends Service implements LocationListener {
                        telemetry_reader = null;
                }
                if (telemetry_logger != null) {
-                       if (D) Log.d(TAG, "disconnect(): stopping TelemetryLogger");
+                       AltosDebug.debug("disconnect(): stopping TelemetryLogger");
                        telemetry_logger.stop();
                        telemetry_logger = null;
                }
                if (altos_link != null) {
-                       if (D) Log.d(TAG, "disconnect(): stopping AltosDroidLink");
+                       AltosDebug.debug("disconnect(): stopping AltosDroidLink");
                        altos_link.close();
                        altos_link = null;
                }
                telemetry_state.config = null;
                if (notify) {
-                       if (D) Log.d(TAG, "disconnect(): send message to clients");
+                       AltosDebug.debug("disconnect(): send message to clients");
                        send_to_clients();
                        if (clients.isEmpty()) {
-                               if (D) Log.d(TAG, "disconnect(): no clients, terminating");
+                               AltosDebug.debug("disconnect(): no clients, terminating");
                                stopSelf();
                        }
                }
@@ -338,19 +334,21 @@ public class TelemetryService extends Service implements LocationListener {
 
        private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
                // Get the BLuetoothDevice object
+               AltosDebug.check_ui("start_altos_bluetooth\n");
                BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
 
                disconnect(false);
                this.address = address;
-               if (D) Log.d(TAG, String.format("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
-               altos_link = new AltosBluetooth(device, handler);
+               AltosDebug.debug("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress());
+               altos_link = new AltosBluetooth(device, handler, pause);
                telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
                telemetry_state.address = address;
                send_to_clients();
        }
 
        private void connected() throws InterruptedException {
-               if (D) Log.d(TAG, "connected top");
+               AltosDebug.debug("connected top");
+               AltosDebug.check_ui("connected\n");
                try {
                        if (altos_link == null)
                                throw new InterruptedException("no bluetooth");
@@ -360,9 +358,9 @@ public class TelemetryService extends Service implements LocationListener {
                } catch (TimeoutException e) {
                        // If this timed out, then we really want to retry it, but
                        // probably safer to just retry the connection from scratch.
-                       if (D) Log.d(TAG, "connected timeout");
+                       AltosDebug.debug("connected timeout");
                        if (address != null) {
-                               if (D) Log.d(TAG, "connected timeout, retrying");
+                               AltosDebug.debug("connected timeout, retrying");
                                start_altos_bluetooth(address, true);
                        } else {
                                handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
@@ -371,18 +369,18 @@ public class TelemetryService extends Service implements LocationListener {
                        return;
                }
 
-               if (D) Log.d(TAG, "connected bluetooth configured");
+               AltosDebug.debug("connected bluetooth configured");
                telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
                telemetry_state.address = address;
 
                telemetry_reader = new TelemetryReader(altos_link, handler, telemetry_state.state);
                telemetry_reader.start();
 
-               if (D) Log.d(TAG, "connected TelemetryReader started");
+               AltosDebug.debug("connected TelemetryReader started");
 
                telemetry_logger = new TelemetryLogger(this, altos_link);
 
-               if (D) Log.d(TAG, "Notify UI of connection");
+               AltosDebug.debug("Notify UI of connection");
 
                send_to_clients();
        }
@@ -412,7 +410,7 @@ public class TelemetryService extends Service implements LocationListener {
                AltosSavedState saved_state = AltosPreferences.state(0);
 
                if (saved_state != null) {
-                       if (D) Log.d(TAG, String.format("recovered old state flight %d\n", saved_state.state.flight));
+                       AltosDebug.debug("recovered old state flight %d\n", saved_state.state.flight);
                        telemetry_state.state = saved_state.state;
                }
 
@@ -424,7 +422,7 @@ public class TelemetryService extends Service implements LocationListener {
 
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
-               Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
+               AltosDebug.debug("Received start id %d: %s", startId, intent);
 
                CharSequence text = getText(R.string.telemetry_service_started);
 
@@ -483,7 +481,7 @@ public class TelemetryService extends Service implements LocationListener {
 
        public void onLocationChanged(Location location) {
                telemetry_state.location = location;
-               if (D) Log.d(TAG, "location changed");
+               AltosDebug.debug("location changed");
                send_to_clients();
        }