Merge "Fix emulation of old platform images."
diff --git a/android/avd/info.c b/android/avd/info.c
index a8e1e33..083b823 100644
--- a/android/avd/info.c
+++ b/android/avd/info.c
@@ -487,8 +487,8 @@
     return 0;
 }
 
-int
-avdInfo_getApiLevel( AvdInfo*  i )
+static int
+_avdInfo_getApiLevel( AvdInfo*  i )
 {
     char*       target;
     const char* p;
@@ -557,6 +557,12 @@
     goto EXIT;
 }
 
+
+int
+avdInfo_getApiLevel(AvdInfo* i) {
+    return i->apiLevel;
+}
+
 /* Look for a named file inside the AVD's content directory.
  * Returns NULL if it doesn't exist, or a strdup() copy otherwise.
  */
@@ -735,11 +741,17 @@
         D("Cannot find target CPU ABI, defaulting to '%s'",
           i->targetAbi);
     }
-    i->apiLevel = propertyFile_getApiLevel(i->buildProperties);
-    if (i->apiLevel < 3) {
-        i->apiLevel = 3;
-        D("Cannot find target API level, defaulting to %d",
-          i->apiLevel);
+    if (!i->apiLevel) {
+        // Note: for regular AVDs, the API level is already extracted
+        // from config.ini, besides, for older SDK platform images,
+        // there is no build.prop file and the following function
+        // would always return 1000, making the AVD unbootable!.
+        i->apiLevel = propertyFile_getApiLevel(i->buildProperties);
+        if (i->apiLevel < 3) {
+            i->apiLevel = 3;
+            D("Cannot find target API level, defaulting to %d",
+            i->apiLevel);
+        }
     }
 }
 
@@ -781,7 +793,7 @@
          _avdInfo_getCoreHwIniPath(i, i->contentPath) < 0 )
         goto FAIL;
 
-    i->apiLevel = avdInfo_getApiLevel(i);
+    i->apiLevel = _avdInfo_getApiLevel(i);
 
     /* look for image search paths. handle post 1.1/pre cupcake
      * obsolete SDKs.
@@ -1321,6 +1333,12 @@
 
 int avdInfo_getAdbdCommunicationMode( AvdInfo* i )
 {
+    if (i->apiLevel < 16) {
+        // QEMU pipe for ADB communication was added in android-4.1.1_r1 API 16
+        D("API < 16, forcing ro.adb.qemud==0");
+        return 0;
+    }
+
     return propertyFile_getAdbdCommunicationMode(i->buildProperties);
 }
 
diff --git a/android/avd/util.c b/android/avd/util.c
index c513d54..cbd6e9c 100644
--- a/android/avd/util.c
+++ b/android/avd/util.c
@@ -226,9 +226,8 @@
     int level = propertyFile_getInt(data, "ro.build.version.sdk", kMinLevel,
                                     &searchResult);
     if (searchResult == RESULT_NOT_FOUND) {
-        D("Could not find target API sdkVersion / SDK version in build properties!");
         level = kMaxLevel;
-        D("Default target API sdkVersion: %d", level);
+        D("Could not find SDK version in build.prop, default is: %d", level);
     } else if (searchResult == RESULT_INVALID || level < 0) {
         D("Defaulting to target API sdkVersion %d", level);
     } else {
@@ -239,12 +238,6 @@
 
 int
 propertyFile_getAdbdCommunicationMode(const FileData* data) {
-    if ( propertyFile_getApiLevel(data) < 16 ) {
-        // QEMU pipe for ADB communication was added in android-4.1.1_r1 API 16
-        D("API < 16, forcing ro.adb.qemud==0");
-        return 0;
-    }
-
     SearchResult searchResult;
     int qemud = propertyFile_getInt(data, "ro.adb.qemud", 1, &searchResult);
     if (searchResult == RESULT_FOUND) {
diff --git a/android/avd/util_unittest.cpp b/android/avd/util_unittest.cpp
index 0472bb2..4197b0a 100644
--- a/android/avd/util_unittest.cpp
+++ b/android/avd/util_unittest.cpp
@@ -104,48 +104,27 @@
   const char* emptyFile =
     "\n";
 
-  const char* testFile15 =
-    "ro.build.version.sdk=15\n";
-
-  const char* testFile16 =
-    "ro.build.version.sdk=16\n";
-
-  const char* testFile15_0 =
-    "ro.build.version.sdk=15\n"
+  const char* valueIsZero =
     "ro.adb.qemud=0";
 
-  const char* testFile15_1 =
-    "ro.build.version.sdk=15\n"
+  const char* valueIsOne =
     "ro.adb.qemud=1";
 
-  const char* testFile16_0 =
-    "ro.build.version.sdk=16\n"
-    "ro.adb.qemud=0";
+  const char* valueIsBogus =
+    "ro.adb.qemud=bogus";
 
-  const char* testFile16_1 =
-    "ro.build.version.sdk=16\n"
-    "ro.adb.qemud=1";
+  // Empty file -> assume 1
+  EXPECT_EQ(0, fileData_initFromMemory(&fd, emptyFile, strlen(emptyFile)));
+  EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
 
-  // API unspecified -> API level == 10000
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, emptyFile, strlen(emptyFile)));
-  EXPECT_EQ(1,propertyFile_getAdbdCommunicationMode(&fd));
+  EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsZero, strlen(valueIsZero)));
+  EXPECT_EQ(0, propertyFile_getAdbdCommunicationMode(&fd));
 
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile15, strlen(testFile15)));
-  EXPECT_EQ(0,propertyFile_getAdbdCommunicationMode(&fd));
+  EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsOne, strlen(valueIsOne)));
+  EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
 
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile16, strlen(testFile16)));
-  EXPECT_EQ(1,propertyFile_getAdbdCommunicationMode(&fd));
-
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile15_0, strlen(testFile15_0)));
-  EXPECT_EQ(0,propertyFile_getAdbdCommunicationMode(&fd));
-
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile15_1, strlen(testFile15_1)));
-  EXPECT_EQ(0,propertyFile_getAdbdCommunicationMode(&fd));
-
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile16_0, strlen(testFile16_0)));
-  EXPECT_EQ(0,propertyFile_getAdbdCommunicationMode(&fd));
-
-  EXPECT_EQ(0,fileData_initFromMemory(&fd, testFile16_1, strlen(testFile16_1)));
-  EXPECT_EQ(1,propertyFile_getAdbdCommunicationMode(&fd));
+  // BOGUS -> 1
+  EXPECT_EQ(0, fileData_initFromMemory(&fd, valueIsBogus, strlen(valueIsBogus)));
+  EXPECT_EQ(1, propertyFile_getAdbdCommunicationMode(&fd));
 }