-package org.altusmetrum.AltosDroid;\r
-\r
- import java.lang.reflect.Array;\r
- import java.lang.reflect.Field;\r
- import java.util.HashMap;\r
-\r
- public class Dumper {\r
- private static Dumper instance = new Dumper();\r
-\r
- protected static Dumper getInstance() {\r
- return instance;\r
- }\r
-\r
- class DumpContext {\r
- int maxDepth = 0;\r
- int maxArrayElements = 0;\r
- int callCount = 0;\r
- HashMap<String, String> ignoreList = new HashMap<String, String>();\r
- HashMap<Object, Integer> visited = new HashMap<Object, Integer>();\r
- }\r
-\r
- public static String dump(Object o) {\r
- return dump(o, 0, 0, null);\r
- }\r
-\r
- public static String dump(Object o, int maxDepth, int maxArrayElements, String[] ignoreList) {\r
- DumpContext ctx = Dumper.getInstance().new DumpContext();\r
- ctx.maxDepth = maxDepth;\r
- ctx.maxArrayElements = maxArrayElements;\r
-\r
- if (ignoreList != null) {\r
- for (int i = 0; i < Array.getLength(ignoreList); i++) {\r
- int colonIdx = ignoreList[i].indexOf(':');\r
- if (colonIdx == -1)\r
- ignoreList[i] = ignoreList[i] + ":";\r
- ctx.ignoreList.put(ignoreList[i], ignoreList[i]);\r
- }\r
- }\r
-\r
- return dump(o, ctx);\r
- }\r
-\r
- protected static String dump(Object o, DumpContext ctx) {\r
- if (o == null) {\r
- return "<null>";\r
- }\r
-\r
- ctx.callCount++;\r
- StringBuffer tabs = new StringBuffer();\r
- for (int k = 0; k < ctx.callCount; k++) {\r
- tabs.append("\t");\r
- }\r
- StringBuffer buffer = new StringBuffer();\r
- @SuppressWarnings("rawtypes")\r
- Class oClass = o.getClass();\r
-\r
- String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass);\r
-\r
- if (ctx.ignoreList.get(oSimpleName + ":") != null)\r
- return "<Ignored>";\r
-\r
- if (oClass.isArray()) {\r
- buffer.append("\n");\r
- buffer.append(tabs.toString().substring(1));\r
- buffer.append("[\n");\r
- int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o) : Math.min(ctx.maxArrayElements, Array.getLength(o));\r
- for (int i = 0; i < rowCount; i++) {\r
- buffer.append(tabs.toString());\r
- try {\r
- Object value = Array.get(o, i);\r
- buffer.append(dumpValue(value, ctx));\r
- } catch (Exception e) {\r
- buffer.append(e.getMessage());\r
- }\r
- if (i < Array.getLength(o) - 1)\r
- buffer.append(",");\r
- buffer.append("\n");\r
- }\r
- if (rowCount < Array.getLength(o)) {\r
- buffer.append(tabs.toString());\r
- buffer.append(Array.getLength(o) - rowCount + " more array elements...");\r
- buffer.append("\n");\r
- }\r
- buffer.append(tabs.toString().substring(1));\r
- buffer.append("]");\r
- } else {\r
- buffer.append("\n");\r
- buffer.append(tabs.toString().substring(1));\r
- buffer.append("{\n");\r
- buffer.append(tabs.toString());\r
- buffer.append("hashCode: " + o.hashCode());\r
- buffer.append("\n");\r
- while (oClass != null && oClass != Object.class) {\r
- Field[] fields = oClass.getDeclaredFields();\r
-\r
- if (ctx.ignoreList.get(oClass.getSimpleName()) == null) {\r
- if (oClass != o.getClass()) {\r
- buffer.append(tabs.toString().substring(1));\r
- buffer.append(" Inherited from superclass " + oSimpleName + ":\n");\r
- }\r
-\r
- for (int i = 0; i < fields.length; i++) {\r
-\r
- String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType());\r
- String fName = fields[i].getName();\r
-\r
- fields[i].setAccessible(true);\r
- buffer.append(tabs.toString());\r
- buffer.append(fName + "(" + fSimpleName + ")");\r
- buffer.append("=");\r
-\r
- if (ctx.ignoreList.get(":" + fName) == null &&\r
- ctx.ignoreList.get(fSimpleName + ":" + fName) == null &&\r
- ctx.ignoreList.get(fSimpleName + ":") == null) {\r
-\r
- try {\r
- Object value = fields[i].get(o);\r
- buffer.append(dumpValue(value, ctx));\r
- } catch (Exception e) {\r
- buffer.append(e.getMessage());\r
- }\r
- buffer.append("\n");\r
- } else {\r
- buffer.append("<Ignored>");\r
- buffer.append("\n");\r
- }\r
- }\r
- oClass = oClass.getSuperclass();\r
- oSimpleName = oClass.getSimpleName();\r
- } else {\r
- oClass = null;\r
- oSimpleName = "";\r
- }\r
- }\r
- buffer.append(tabs.toString().substring(1));\r
- buffer.append("}");\r
- }\r
- ctx.callCount--;\r
- return buffer.toString();\r
- }\r
-\r
- protected static String dumpValue(Object value, DumpContext ctx) {\r
- if (value == null) {\r
- return "<null>";\r
- }\r
- if (value.getClass().isPrimitive() ||\r
- value.getClass() == java.lang.Short.class ||\r
- value.getClass() == java.lang.Long.class ||\r
- value.getClass() == java.lang.String.class ||\r
- value.getClass() == java.lang.Integer.class ||\r
- value.getClass() == java.lang.Float.class ||\r
- value.getClass() == java.lang.Byte.class ||\r
- value.getClass() == java.lang.Character.class ||\r
- value.getClass() == java.lang.Double.class ||\r
- value.getClass() == java.lang.Boolean.class) {\r
-\r
- return value.toString();\r
-\r
- } else {\r
-\r
- Integer visitedIndex = ctx.visited.get(value);\r
- if (visitedIndex == null) {\r
- ctx.visited.put(value, ctx.callCount);\r
- if (ctx.maxDepth == 0 || ctx.callCount < ctx.maxDepth) {\r
- return dump(value, ctx);\r
- } else {\r
- return "<Reached max recursion depth>";\r
- }\r
- } else {\r
- return "<Previously visited - see hashCode " + value.hashCode() + ">";\r
- }\r
- }\r
- }\r
-\r
-\r
- private static String getSimpleNameWithoutArrayQualifier(@SuppressWarnings("rawtypes") Class clazz) {\r
- String simpleName = clazz.getSimpleName();\r
- int indexOfBracket = simpleName.indexOf('['); \r
- if (indexOfBracket != -1)\r
- return simpleName.substring(0, indexOfBracket);\r
- return simpleName;\r
- }\r
-}\r
+package org.altusmetrum.AltosDroid;
+
+ import java.lang.reflect.Array;
+ import java.lang.reflect.Field;
+ import java.util.HashMap;
+
+ public class Dumper {
+ private static Dumper instance = new Dumper();
+
+ protected static Dumper getInstance() {
+ return instance;
+ }
+
+ class DumpContext {
+ int maxDepth = 0;
+ int maxArrayElements = 0;
+ int callCount = 0;
+ HashMap<String, String> ignoreList = new HashMap<String, String>();
+ HashMap<Object, Integer> visited = new HashMap<Object, Integer>();
+ }
+
+ public static String dump(Object o) {
+ return dump(o, 0, 0, null);
+ }
+
+ public static String dump(Object o, int maxDepth, int maxArrayElements, String[] ignoreList) {
+ DumpContext ctx = Dumper.getInstance().new DumpContext();
+ ctx.maxDepth = maxDepth;
+ ctx.maxArrayElements = maxArrayElements;
+
+ if (ignoreList != null) {
+ for (int i = 0; i < Array.getLength(ignoreList); i++) {
+ int colonIdx = ignoreList[i].indexOf(':');
+ if (colonIdx == -1)
+ ignoreList[i] = ignoreList[i] + ":";
+ ctx.ignoreList.put(ignoreList[i], ignoreList[i]);
+ }
+ }
+
+ return dump(o, ctx);
+ }
+
+ protected static String dump(Object o, DumpContext ctx) {
+ if (o == null) {
+ return "<null>";
+ }
+
+ ctx.callCount++;
+ StringBuffer tabs = new StringBuffer();
+ for (int k = 0; k < ctx.callCount; k++) {
+ tabs.append("\t");
+ }
+ StringBuffer buffer = new StringBuffer();
+ @SuppressWarnings("rawtypes")
+ Class oClass = o.getClass();
+
+ String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass);
+
+ if (ctx.ignoreList.get(oSimpleName + ":") != null)
+ return "<Ignored>";
+
+ if (oClass.isArray()) {
+ buffer.append("\n");
+ buffer.append(tabs.toString().substring(1));
+ buffer.append("[\n");
+ int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o) : Math.min(ctx.maxArrayElements, Array.getLength(o));
+ for (int i = 0; i < rowCount; i++) {
+ buffer.append(tabs.toString());
+ try {
+ Object value = Array.get(o, i);
+ buffer.append(dumpValue(value, ctx));
+ } catch (Exception e) {
+ buffer.append(e.getMessage());
+ }
+ if (i < Array.getLength(o) - 1)
+ buffer.append(",");
+ buffer.append("\n");
+ }
+ if (rowCount < Array.getLength(o)) {
+ buffer.append(tabs.toString());
+ buffer.append(Array.getLength(o) - rowCount + " more array elements...");
+ buffer.append("\n");
+ }
+ buffer.append(tabs.toString().substring(1));
+ buffer.append("]");
+ } else {
+ buffer.append("\n");
+ buffer.append(tabs.toString().substring(1));
+ buffer.append("{\n");
+ buffer.append(tabs.toString());
+ buffer.append("hashCode: " + o.hashCode());
+ buffer.append("\n");
+ while (oClass != null && oClass != Object.class) {
+ Field[] fields = oClass.getDeclaredFields();
+
+ if (ctx.ignoreList.get(oClass.getSimpleName()) == null) {
+ if (oClass != o.getClass()) {
+ buffer.append(tabs.toString().substring(1));
+ buffer.append(" Inherited from superclass " + oSimpleName + ":\n");
+ }
+
+ for (int i = 0; i < fields.length; i++) {
+
+ String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType());
+ String fName = fields[i].getName();
+
+ fields[i].setAccessible(true);
+ buffer.append(tabs.toString());
+ buffer.append(fName + "(" + fSimpleName + ")");
+ buffer.append("=");
+
+ if (ctx.ignoreList.get(":" + fName) == null &&
+ ctx.ignoreList.get(fSimpleName + ":" + fName) == null &&
+ ctx.ignoreList.get(fSimpleName + ":") == null) {
+
+ try {
+ Object value = fields[i].get(o);
+ buffer.append(dumpValue(value, ctx));
+ } catch (Exception e) {
+ buffer.append(e.getMessage());
+ }
+ buffer.append("\n");
+ } else {
+ buffer.append("<Ignored>");
+ buffer.append("\n");
+ }
+ }
+ oClass = oClass.getSuperclass();
+ oSimpleName = oClass.getSimpleName();
+ } else {
+ oClass = null;
+ oSimpleName = "";
+ }
+ }
+ buffer.append(tabs.toString().substring(1));
+ buffer.append("}");
+ }
+ ctx.callCount--;
+ return buffer.toString();
+ }
+
+ protected static String dumpValue(Object value, DumpContext ctx) {
+ if (value == null) {
+ return "<null>";
+ }
+ if (value.getClass().isPrimitive() ||
+ value.getClass() == java.lang.Short.class ||
+ value.getClass() == java.lang.Long.class ||
+ value.getClass() == java.lang.String.class ||
+ value.getClass() == java.lang.Integer.class ||
+ value.getClass() == java.lang.Float.class ||
+ value.getClass() == java.lang.Byte.class ||
+ value.getClass() == java.lang.Character.class ||
+ value.getClass() == java.lang.Double.class ||
+ value.getClass() == java.lang.Boolean.class) {
+
+ return value.toString();
+
+ } else {
+
+ Integer visitedIndex = ctx.visited.get(value);
+ if (visitedIndex == null) {
+ ctx.visited.put(value, ctx.callCount);
+ if (ctx.maxDepth == 0 || ctx.callCount < ctx.maxDepth) {
+ return dump(value, ctx);
+ } else {
+ return "<Reached max recursion depth>";
+ }
+ } else {
+ return "<Previously visited - see hashCode " + value.hashCode() + ">";
+ }
+ }
+ }
+
+
+ private static String getSimpleNameWithoutArrayQualifier(@SuppressWarnings("rawtypes") Class clazz) {
+ String simpleName = clazz.getSimpleName();
+ int indexOfBracket = simpleName.indexOf('[');
+ if (indexOfBracket != -1)
+ return simpleName.substring(0, indexOfBracket);
+ return simpleName;
+ }
+}
-/*\r
- * Copyright © 2011 Keith Packard <keithp@keithp.com>\r
- * Copyright © 2012 Mike Beattie <mike@ethernal.org>\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; version 2 of the License.\r
- *\r
- * This program is distributed in the hope that it will be useful, but\r
- * WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License along\r
- * with this program; if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
- */\r
-\r
-\r
-package org.altusmetrum.AltosDroid;\r
-\r
-import java.text.*;\r
-import java.io.*;\r
-import java.util.concurrent.*;\r
-import android.util.Log;\r
-import android.os.Handler;\r
-\r
-import org.altusmetrum.altoslib_2.*;\r
-\r
-\r
-public class TelemetryReader extends Thread {\r
-\r
- private static final String TAG = "TelemetryReader";\r
-\r
- int crc_errors;\r
-\r
- Handler handler;\r
-\r
- AltosLink link;\r
- AltosState state = null;\r
-\r
- LinkedBlockingQueue<AltosLine> telemQueue;\r
-\r
- public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
- AltosLine l = telemQueue.take();\r
- if (l.line == null)\r
- throw new IOException("IO error");\r
- AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);\r
- if (state == null)\r
- state = new AltosState();\r
- else\r
- state = state.clone();\r
- telem.update_state(state);\r
- return state;\r
- }\r
-\r
- public void close() {\r
- state = null;\r
- link.remove_monitor(telemQueue);\r
- link = null;\r
- telemQueue.clear();\r
- telemQueue = null;\r
- }\r
-\r
- public void run() {\r
- AltosState state = null;\r
-\r
- try {\r
- for (;;) {\r
- try {\r
- state = read();\r
- handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();\r
- } catch (ParseException pp) {\r
- Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
- } catch (AltosCRCException ce) {\r
- ++crc_errors;\r
- handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();\r
- }\r
- }\r
- } catch (InterruptedException ee) {\r
- } catch (IOException ie) {\r
- } finally {\r
- close();\r
- }\r
- }\r
-\r
- public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
- link = in_link;\r
- handler = in_handler;\r
-\r
- state = null;\r
- telemQueue = new LinkedBlockingQueue<AltosLine>();\r
- link.add_monitor(telemQueue);\r
- }\r
-}\r
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ * 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.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+import android.util.Log;
+import android.os.Handler;
+
+import org.altusmetrum.altoslib_2.*;
+
+
+public class TelemetryReader extends Thread {
+
+ private static final String TAG = "TelemetryReader";
+
+ int crc_errors;
+
+ Handler handler;
+
+ AltosLink link;
+ AltosState state = null;
+
+ LinkedBlockingQueue<AltosLine> telemQueue;
+
+ public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
+ AltosLine l = telemQueue.take();
+ if (l.line == null)
+ throw new IOException("IO error");
+ AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
+ if (state == null)
+ state = new AltosState();
+ else
+ state = state.clone();
+ telem.update_state(state);
+ return state;
+ }
+
+ public void close() {
+ state = null;
+ link.remove_monitor(telemQueue);
+ link = null;
+ telemQueue.clear();
+ telemQueue = null;
+ }
+
+ public void run() {
+ AltosState state = null;
+
+ try {
+ for (;;) {
+ 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()));
+ } catch (AltosCRCException ce) {
+ ++crc_errors;
+ handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
+ }
+ }
+ } catch (InterruptedException ee) {
+ } catch (IOException ie) {
+ } finally {
+ close();
+ }
+ }
+
+ public TelemetryReader (AltosLink in_link, Handler in_handler) {
+ link = in_link;
+ handler = in_handler;
+
+ state = null;
+ telemQueue = new LinkedBlockingQueue<AltosLine>();
+ link.add_monitor(telemQueue);
+ }
+}