From 6d938a12f663ca15244c1b43a7bd6dfdb736b76d Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 2 Jun 2011 13:24:47 +0000
Subject: [PATCH] android build fixes

---
 android/README.txt                            |  2 +-
 android/build.properties                      |  1 +
 android/build.xml                             | 14 ++++-
 android/src/net/i2p/router/I2PAndroid.java    |  1 +
 android/src/net/i2p/util/LogWriter.java       | 19 +++++--
 android/src/net/i2p/util/SecureDirectory.java | 22 ++++++++
 android/src/net/i2p/util/SecureFile.java      | 22 ++++++++
 .../net/i2p/util/SecureFileOutputStream.java  | 53 +++++++++++++++++++
 8 files changed, 127 insertions(+), 7 deletions(-)
 create mode 100644 android/build.properties
 create mode 100644 android/src/net/i2p/util/SecureDirectory.java
 create mode 100644 android/src/net/i2p/util/SecureFile.java
 create mode 100644 android/src/net/i2p/util/SecureFileOutputStream.java

diff --git a/android/README.txt b/android/README.txt
index 67b8aa30f3..70774a2797 100644
--- a/android/README.txt
+++ b/android/README.txt
@@ -28,7 +28,7 @@ ant debug
 ../../android-sdk-linux_86/tools/emulator -avd i2p &
 
 #then wait a couple minutes until the emulator is up
-#then install the I2P app
+#then install the I2P app (ONE TIME ONLY)
 ant install
 
 #then run the debugger
diff --git a/android/build.properties b/android/build.properties
new file mode 100644
index 0000000000..181724115d
--- /dev/null
+++ b/android/build.properties
@@ -0,0 +1 @@
+application-package=net.i2p.router
diff --git a/android/build.xml b/android/build.xml
index 94356fcf42..6869bce6bd 100644
--- a/android/build.xml
+++ b/android/build.xml
@@ -76,6 +76,9 @@
         <mkdir dir="tmp" />
         <unjar src="../build/i2p.jar" dest="tmp/" />
         <delete file="tmp/net/i2p/util/LogWriter.class" />
+        <delete file="tmp/net/i2p/util/SecureDirectory.class" />
+        <delete file="tmp/net/i2p/util/SecureFile.class" />
+        <delete file="tmp/net/i2p/util/SecureFileOutputStream.class" />
         <!-- org.bouncycastle.crypto already in android
              but we need a little trickery because our HMac is incompatible...
              and the libs aren't in the SDK to compile against??? -->
@@ -237,6 +240,7 @@
     <target name="compile" depends="buildrouter, resource-src, aidl">
         <javac encoding="ascii" target="1.5" debug="true" extdirs=""
                 destdir="${out-classes}"
+                includeantruntime="false"
                 bootclasspathref="android.target.classpath">
             <src path="${source-folder}" />
             <src path="${gen-folder}" />
@@ -280,6 +284,12 @@
 
     <!-- Package the application and sign it with a debug key.
          This is the default target when building. It is used for debug. -->
+    <!--
+         I2P when this fails 365 days later because the key expired, delete ~/.android/debug.keystore
+         Then do 'ant uninstall' (since the new key doesn't match the old key)
+         Then do 'ant install'
+         See http://developer.android.com/guide/publishing/app-signing.html for more info
+      -->
     <target name="debug" depends="dex, package-resources">
         <apkbuilder
                 outfolder="${out-folder}"
@@ -327,12 +337,12 @@
         </exec>
     </target>
 
-    <!-- Uinstall the package from the default emulator -->
+    <!-- Uninstall the package from the default emulator -->
     <target name="uninstall">
         <echo>Uninstalling ${application-package} from the default emulator...</echo>
         <exec executable="${adb}" failonerror="true">
             <arg value="uninstall" />
-            <arg path="${application-package}" />
+            <arg value="${application-package}" />
         </exec>
     </target>
     
diff --git a/android/src/net/i2p/router/I2PAndroid.java b/android/src/net/i2p/router/I2PAndroid.java
index 88d522dd63..262493ec36 100644
--- a/android/src/net/i2p/router/I2PAndroid.java
+++ b/android/src/net/i2p/router/I2PAndroid.java
@@ -46,6 +46,7 @@ public class I2PAndroid extends Activity
     {
         System.err.println("onStart called");
         super.onStart();
+//      net.i2p.crypto.DSAEngine.main(null);
         RouterLaunch.main(null);
         System.err.println("Router.main finished");
     }
diff --git a/android/src/net/i2p/util/LogWriter.java b/android/src/net/i2p/util/LogWriter.java
index 0babfab37b..18ba54c8ed 100644
--- a/android/src/net/i2p/util/LogWriter.java
+++ b/android/src/net/i2p/util/LogWriter.java
@@ -10,6 +10,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.List;
+import java.util.Queue;
 
 /**
  * bridge to android logging
@@ -56,11 +57,21 @@ class LogWriter implements Runnable {
     public void flushRecords() { flushRecords(true); }
     public void flushRecords(boolean shouldWait) {
         try {
-            List records = _manager._removeAll();
+            // zero copy, drain the manager queue directly
+            Queue<LogRecord> records = _manager.getQueue();
             if (records == null) return;
-            for (int i = 0; i < records.size(); i++) {
-                LogRecord rec = (LogRecord) records.get(i);
-                writeRecord(rec);
+            if (!records.isEmpty()) {
+                LogRecord rec;
+                while ((rec = records.poll()) != null) {
+                    writeRecord(rec);
+                }
+                try {
+                    if (_currentOut != null)
+                        _currentOut.flush();
+                } catch (IOException ioe) {
+                    //if (++_diskFullMessageCount < MAX_DISKFULL_MESSAGES)
+                        System.err.println("Error writing the router log - disk full? " + ioe);
+                }
             }
         } catch (Throwable t) {
             t.printStackTrace();
diff --git a/android/src/net/i2p/util/SecureDirectory.java b/android/src/net/i2p/util/SecureDirectory.java
new file mode 100644
index 0000000000..0c34c91c05
--- /dev/null
+++ b/android/src/net/i2p/util/SecureDirectory.java
@@ -0,0 +1,22 @@
+package net.i2p.util;
+
+import java.io.File;
+
+/**
+ *  setXXX() not available until API level 9 (Platform Version 2.3)
+ *  @since 0.8.7
+ */
+public class SecureDirectory extends File {
+
+    public SecureDirectory(String pathname) {
+        super(pathname);
+    }
+
+    public SecureDirectory(String parent, String child) {
+        super(parent, child);
+    }
+
+    public SecureDirectory(File parent, String child) {
+        super(parent, child);
+    }
+}
diff --git a/android/src/net/i2p/util/SecureFile.java b/android/src/net/i2p/util/SecureFile.java
new file mode 100644
index 0000000000..e9362ef946
--- /dev/null
+++ b/android/src/net/i2p/util/SecureFile.java
@@ -0,0 +1,22 @@
+package net.i2p.util;
+
+import java.io.File;
+
+/**
+ *  setXXX() not available until API level 9 (Platform Version 2.3)
+ *  @since 0.8.7
+ */
+public class SecureFile extends SecureDirectory {
+
+    public SecureFile(String pathname) {
+        super(pathname);
+    }
+
+    public SecureFile(String parent, String child) {
+        super(parent, child);
+    }
+
+    public SecureFile(File parent, String child) {
+        super(parent, child);
+    }
+}
diff --git a/android/src/net/i2p/util/SecureFileOutputStream.java b/android/src/net/i2p/util/SecureFileOutputStream.java
new file mode 100644
index 0000000000..e45798cf98
--- /dev/null
+++ b/android/src/net/i2p/util/SecureFileOutputStream.java
@@ -0,0 +1,53 @@
+package net.i2p.util;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+
+import net.i2p.I2PAppContext;
+
+/**
+ *  setXXX() not available until API level 9 (Platform Version 2.3)
+ *  @since 0.8.7
+ */
+public class SecureFileOutputStream extends FileOutputStream {
+
+    /**
+     *  super()
+     */
+    public SecureFileOutputStream(String file) throws FileNotFoundException {
+        super(file);
+    }
+
+    /**
+     *  super()
+     */
+    public SecureFileOutputStream(String file, boolean append) throws FileNotFoundException {
+        super(file, append);
+    }
+
+    /**
+     *  super()
+     */
+    public SecureFileOutputStream(File file) throws FileNotFoundException {
+        super(file);
+    }
+
+    /**
+     *  super()
+     */
+    public SecureFileOutputStream(File file, boolean append) throws FileNotFoundException {
+        super(file, append);
+    }
+
+    /** @return false */
+    static boolean canSetPerms() {
+        return false;
+    }
+
+    /**
+     *  noop
+     */
+    public static void setPerms(File f) {
+    }
+}
-- 
GitLab