android: Get the port up and running again

The build system needed fixes because the tools paths changed and one tool that
we used (apkbuilder) was removed entirely. Recent NDKs don't ship gcc 4.4.3
anymore, therefore switch to 4.6. The code itself needed a fix for a jni
reference bug that was uncovered by KitKat.

The port now builds with latest sdk (r22) and ndk (r9d).

Change-Id: Id74fa54ba93bbb0ee30373fbe79e92c5ff03201d
diff --git a/android/.classpath b/android/.classpath
index 0ca188f..dec02b3 100644
--- a/android/.classpath
+++ b/android/.classpath
@@ -4,5 +4,6 @@
 	<classpathentry kind="src" path="src"/>
 	<classpathentry kind="src" path="gen"/>
 	<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+	<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
 	<classpathentry kind="output" path="bin/classes"/>
 </classpath>
diff --git a/android/android.make b/android/android.make
index 6d10121..4c93aa7 100644
--- a/android/android.make
+++ b/android/android.make
@@ -28,16 +28,17 @@
 .PHONY: apk classes clean dex dirs libs jar
 
 # API version
-ANDROID_PLATFORM_VERSION=16
+ANDROID_PLATFORM_VERSION=19
 ANDROID_PLATFORM=$(ANDROID_SDK_PATH)/platforms/android-$(ANDROID_PLATFORM_VERSION)
 
 # android tools
-AAPT=$(ANDROID_SDK_PATH)/platform-tools/aapt
-DX=$(ANDROID_SDK_PATH)/platform-tools/dx
-APKBUILDER=$(ANDROID_SDK_PATH)/tools/apkbuilder
+BUILD_TOOLS_VERSION=19.0.3
+AAPT=$(ANDROID_SDK_PATH)/build-tools/$(BUILD_TOOLS_VERSION)/aapt
+DX=$(ANDROID_SDK_PATH)/build-tools/$(BUILD_TOOLS_VERSION)/dx
 ZIPALIGN=$(ANDROID_SDK_PATH)/tools/zipalign
 KEYSTORE=$(HOME)/.android/debug.keystore
 ADB=$(ANDROID_SDK_PATH)/platform-tools/adb
+BUILDAPK=$(ANDROID_DIR)/buildapk.sh
 
 CLASSPATH   := $(BUILDDIR)/bin/classes
 
@@ -130,8 +131,7 @@
 libs: $(DIRS) $(LIBS)
 
 $(TEMP_APK): $(AP_) $(LIBS) $(DEX) | $(DIRS)
-	$(call PRINTS,APK $(subst $(BUILDDIR)/,,$@))$(APKBUILDER) $@ \
-	-u -z $(AP_) -f $(DEX) -nf $(BUILDDIR)/libs
+	$(call PRINTS,APK $(subst $(BUILDDIR)/,,$@))$(BUILDAPK) $(BUILDDIR) $(notdir $@) $(BUILD_TOOLS_VERSION)
 
 $(KEYSTORE):
 	$(SILENT)mkdir -p $(HOME)/.android
diff --git a/android/buildapk.sh b/android/buildapk.sh
new file mode 100755
index 0000000..3404387
--- /dev/null
+++ b/android/buildapk.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+BUILDDIR=$1
+APK=$2
+SDKV=$3
+
+[ -z $ANDROID_SDK_PATH ] && exit 1
+[ -z $BUILDDIR ] && exit 1
+[ -d $BUILDDIR ] || exit 1
+
+# need to cd into the bin dir and create a symlink to the libraries
+# so that aapt puts the libraries with the correct prefix into the apk
+cd $BUILDDIR/bin
+ln -nfs $BUILDDIR/libs lib
+cp resources.ap_ $APK
+$ANDROID_SDK_PATH/build-tools/$SDKV/aapt add $APK classes.dex > /dev/null
+$ANDROID_SDK_PATH/build-tools/$SDKV/aapt add $APK lib/*/* > /dev/null
+
+exit 0
diff --git a/android/project.properties b/android/project.properties
index 895c9ce..a5578ba 100644
--- a/android/project.properties
+++ b/android/project.properties
@@ -8,4 +8,4 @@
 # project structure.
 
 # Project target.
-target=android-16
+target=android-19
diff --git a/android/src/org/rockbox/RockboxFramebuffer.java b/android/src/org/rockbox/RockboxFramebuffer.java
index e1fb99f..405a771 100644
--- a/android/src/org/rockbox/RockboxFramebuffer.java
+++ b/android/src/org/rockbox/RockboxFramebuffer.java
@@ -57,14 +57,6 @@
         setEnabled(false);
     }
 
-    /* second stage init; called from Rockbox with information about the 
-     * display framebuffer */
-    private void initialize(int lcd_width, int lcd_height)
-    {
-        btm = Bitmap.createBitmap(lcd_width, lcd_height, Bitmap.Config.RGB_565);
-        setEnabled(true);
-    }
-
     private void update(ByteBuffer framebuffer)
     {
         SurfaceHolder holder = getHolder();                            
@@ -138,5 +130,7 @@
     public native void surfaceDestroyed(SurfaceHolder holder);
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
     {
+        btm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
+        setEnabled(true);
     }
 }
diff --git a/firmware/target/hosted/android/lcd-android.c b/firmware/target/hosted/android/lcd-android.c
index 2b38374..a161406 100644
--- a/firmware/target/hosted/android/lcd-android.c
+++ b/firmware/target/hosted/android/lcd-android.c
@@ -34,7 +34,6 @@
 static jobject RockboxFramebuffer_instance;
 static jmethodID java_lcd_update;
 static jmethodID java_lcd_update_rect;
-static jmethodID java_lcd_init;
 
 static jclass    AndroidRect_class;
 static jmethodID AndroidRect_constructor;
@@ -42,6 +41,7 @@
 static int dpi;
 static int scroll_threshold;
 static bool display_on;
+static bool connected;
 
 /* this might actually be called before lcd_init_device() or even main(), so
  * be sure to only access static storage initalized at library loading,
@@ -49,38 +49,26 @@
 static void connect_with_java(JNIEnv* env, jobject fb_instance)
 {
     JNIEnv e = *env;
-    static bool have_class;
 
-    if (!have_class)
-    {
-        jclass fb_class = e->GetObjectClass(env, fb_instance);
-        /* cache update functions */
-        java_lcd_update      = e->GetMethodID(env, fb_class,
-                                             "update",
-                                             "(Ljava/nio/ByteBuffer;)V");
-        java_lcd_update_rect = e->GetMethodID(env, fb_class,
-                                             "update",
-                                             "(Ljava/nio/ByteBuffer;"
-                                              "Landroid/graphics/Rect;)V");
-        jmethodID get_dpi    = e->GetMethodID(env, fb_class,
-                                             "getDpi", "()I");
-        jmethodID thresh     = e->GetMethodID(env, fb_class,
-                                             "getScrollThreshold", "()I");
-        /* these don't change with new instances so call them now */
-        dpi                  = e->CallIntMethod(env, fb_instance, get_dpi);
-        scroll_threshold     = e->CallIntMethod(env, fb_instance, thresh);
+    jclass fb_class = e->GetObjectClass(env, fb_instance);
+    /* cache update functions */
+    java_lcd_update      = e->GetMethodID(env, fb_class,
+                                         "update",
+                                         "(Ljava/nio/ByteBuffer;)V");
+    java_lcd_update_rect = e->GetMethodID(env, fb_class,
+                                         "update",
+                                         "(Ljava/nio/ByteBuffer;"
+                                          "Landroid/graphics/Rect;)V");
+    jmethodID get_dpi    = e->GetMethodID(env, fb_class,
+                                         "getDpi", "()I");
+    jmethodID thresh     = e->GetMethodID(env, fb_class,
+                                         "getScrollThreshold", "()I");
+    /* these don't change with new instances so call them now */
+    dpi                  = e->CallIntMethod(env, fb_instance, get_dpi);
+    scroll_threshold     = e->CallIntMethod(env, fb_instance, thresh);
 
-        java_lcd_init        = e->GetMethodID(env, fb_class,
-                                             "initialize", "(II)V");
-        AndroidRect_class    = e->FindClass(env, "android/graphics/Rect");
-        AndroidRect_constructor = e->GetMethodID(env, AndroidRect_class,
-                                             "<init>", "(IIII)V");
-        have_class           = true;
-    }
-
-    /* we need to setup parts for the java object every time */
-    (*env)->CallVoidMethod(env, fb_instance, java_lcd_init,
-                          (jint)LCD_WIDTH, (jint)LCD_HEIGHT);
+    AndroidRect_constructor = e->GetMethodID(env, AndroidRect_class,
+                                         "<init>", "(IIII)V");
 }
 
 /*
@@ -132,12 +120,18 @@
                                                      jobject surfaceholder)
 {
     (void)surfaceholder;
+    jclass rect;
 
     /* Update RockboxFramebuffer_instance */
     RockboxFramebuffer_instance = (*env)->NewGlobalRef(env, this);
-
+    rect = (*env)->FindClass(env, "android/graphics/Rect");
+    AndroidRect_class = (*env)->NewGlobalRef(env, rect);
     /* possibly a new instance - reconnect */
-    connect_with_java(env, this);
+    if (!connected)
+    {
+        connect_with_java(env, this);
+        connected = true;
+    }
     display_on = true;
 
     /* need to wait for button_queue to be valid to post to */
@@ -163,6 +157,8 @@
 
     (*e)->DeleteGlobalRef(e, RockboxFramebuffer_instance);
     RockboxFramebuffer_instance = NULL;
+    (*e)->DeleteGlobalRef(e, AndroidRect_class);
+    AndroidRect_class = NULL;
 }
 
 bool lcd_active(void)
diff --git a/tools/configure b/tools/configure
index 73a8cae..5b3419d 100755
--- a/tools/configure
+++ b/tools/configure
@@ -673,11 +673,11 @@
     LDOPTS="$LDOPTS -Wl,-soname,librockbox.so -shared -ldl -llog"
     GLOBAL_LDOPTS="-Wl,-z,defs -Wl,-z,noexecstack -shared"
     ANDROID_ARCH=$1 # for android.make too
+    gccchoice="4.6"
     # arch dependant stuff
     case $ANDROID_ARCH in
         armeabi)
             endian="little"
-            gccchoice="4.4.3"
             gcctarget="arm-linux-androideabi-"
             # sigaltstack is not available in pre-android-9, however asm
             # threads work fine so far
@@ -688,7 +688,6 @@
             ;;
         mips)
             endian="little"
-            gccchoice="4.4.3"
             gcctarget="mipsel-linux-android-"
             thread_support="HAVE_SIGALTSTACK_THREADS"
             GCCOPTS="$GCCOPTS -march=mips32 -mtune=r4600 -mno-mips16 -mno-long-calls -fomit-frame-pointer \
@@ -696,8 +695,7 @@
             LDOPTS="$LDOPTS --sysroot=$ANDROID_NDK_PATH/platforms/android-14/arch-mips"
             ;;
         x86)
-            endian=little
-            gccchoice="4.4.3"
+            endian="little"
             gcctarget="i686-linux-android-"
             gccdir=x86-$gccchoice
             thread_support="HAVE_SIGALTSTACK_THREADS"