Merge "Catch read error from AudioRecord and do not assert"
diff --git a/api/11.xml b/api/11.xml
index 4a40c36..61472a3 100644
--- a/api/11.xml
+++ b/api/11.xml
@@ -90761,6 +90761,8 @@
 >
 <parameter name="surfaceTexture" type="android.graphics.SurfaceTexture">
 </parameter>
+<exception name="IOException" type="java.io.IOException">
+</exception>
 </method>
 <method name="setZoomChangeListener"
  return="void"
diff --git a/api/current.xml b/api/current.xml
index ea86e3b..08227e3 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -90906,6 +90906,8 @@
 >
 <parameter name="surfaceTexture" type="android.graphics.SurfaceTexture">
 </parameter>
+<exception name="IOException" type="java.io.IOException">
+</exception>
 </method>
 <method name="setZoomChangeListener"
  return="void"
diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp
index dcea968..7a599e9 100644
--- a/cmds/screencap/screencap.cpp
+++ b/cmds/screencap/screencap.cpp
@@ -19,6 +19,10 @@
 #include <stdio.h>
 #include <fcntl.h>
 
+#include <linux/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
 #include <binder/IMemory.h>
 #include <surfaceflinger/SurfaceComposerClient.h>
 
@@ -28,14 +32,15 @@
 
 using namespace android;
 
-static void usage()
+static void usage(const char* pname)
 {
     fprintf(stderr,
-            "usage: screenshot [-hp] [FILENAME]\n"
+            "usage: %s [-hp] [FILENAME]\n"
             "   -h: this message\n"
             "   -p: save the file as a png.\n"
             "If FILENAME ends with .png it will be saved as a png.\n"
-            "If FILENAME is not given, the results will be printed to stdout.\n"
+            "If FILENAME is not given, the results will be printed to stdout.\n",
+            pname
     );
 }
 
@@ -54,8 +59,33 @@
     }
 }
 
+static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
+        uint32_t* bytespp, uint32_t* f)
+{
+
+    switch (vinfo.bits_per_pixel) {
+        case 16:
+            *f = PIXEL_FORMAT_RGB_565;
+            *bytespp = 2;
+            break;
+        case 24:
+            *f = PIXEL_FORMAT_RGB_888;
+            *bytespp = 3;
+            break;
+        case 32:
+            // TODO: do better decoding of vinfo here
+            *f = PIXEL_FORMAT_RGBX_8888;
+            *bytespp = 4;
+            break;
+        default:
+            return BAD_VALUE;
+    }
+    return NO_ERROR;
+}
+
 int main(int argc, char** argv)
 {
+    const char* pname = argv[0];
     bool png = false;
     int c;
     while ((c = getopt(argc, argv, "ph")) != -1) {
@@ -65,7 +95,7 @@
                 break;
             case '?':
             case 'h':
-                usage();
+                usage(pname);
                 return 1;
         }
     }
@@ -79,7 +109,7 @@
         const char* fn = argv[0];
         fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
         if (fd == -1) {
-            fprintf(stderr, "Error opening file: (%d) %s\n", errno, strerror(errno));
+            fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
             return 1;
         }
         const int len = strlen(fn);
@@ -89,34 +119,66 @@
     }
     
     if (fd == -1) {
-        usage();
+        usage(pname);
         return 1;
     }
 
+    void const* mapbase = MAP_FAILED;
+    ssize_t mapsize = -1;
+
+    void const* base = 0;
+    uint32_t w, h, f;
+    size_t size = 0;
+
     ScreenshotClient screenshot;
-    if (screenshot.update() != NO_ERROR) {
-        return 0;
+    if (screenshot.update() == NO_ERROR) {
+        base = screenshot.getPixels();
+        w = screenshot.getWidth();
+        h = screenshot.getHeight();
+        f = screenshot.getFormat();
+        size = screenshot.getSize();
+    } else {
+        const char* fbpath = "/dev/graphics/fb0";
+        int fb = open(fbpath, O_RDONLY);
+        if (fb >= 0) {
+            struct fb_var_screeninfo vinfo;
+            if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
+                uint32_t bytespp;
+                if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
+                    size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp;
+                    w = vinfo.xres;
+                    h = vinfo.yres;
+                    size = w*h*bytespp;
+                    mapsize = offset + size;
+                    mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
+                    if (mapbase != MAP_FAILED) {
+                        base = (void const *)((char const *)mapbase + offset);
+                    }
+                }
+            }
+            close(fb);
+        }
     }
 
-    void const* base = screenshot.getPixels();
-    uint32_t w = screenshot.getWidth();
-    uint32_t h = screenshot.getHeight();
-    uint32_t f = screenshot.getFormat();
-
-    if (png) {
-        SkBitmap b;
-        b.setConfig(flinger2skia(f), w, h);
-        b.setPixels((void*)base);
-        SkDynamicMemoryWStream stream;
-        SkImageEncoder::EncodeStream(&stream, b,
-                SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
-        write(fd, stream.getStream(), stream.getOffset());
-    } else {
-        write(fd, &w, 4);
-        write(fd, &h, 4);
-        write(fd, &f, 4);
-        write(fd, base, w*h*4);
+    if (base) {
+        if (png) {
+            SkBitmap b;
+            b.setConfig(flinger2skia(f), w, h);
+            b.setPixels((void*)base);
+            SkDynamicMemoryWStream stream;
+            SkImageEncoder::EncodeStream(&stream, b,
+                    SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
+            write(fd, stream.getStream(), stream.getOffset());
+        } else {
+            write(fd, &w, 4);
+            write(fd, &h, 4);
+            write(fd, &f, 4);
+            write(fd, base, size);
+        }
     }
     close(fd);
+    if (mapbase != MAP_FAILED) {
+        munmap((void *)mapbase, mapsize);
+    }
     return 0;
 }
diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java
index 965f7dc..390e542 100644
--- a/core/java/android/database/sqlite/SQLiteDatabase.java
+++ b/core/java/android/database/sqlite/SQLiteDatabase.java
@@ -2386,37 +2386,39 @@
      * @return true if write-ahead-logging is set. false otherwise
      */
     public boolean enableWriteAheadLogging() {
-        // make sure the database is not READONLY. WAL doesn't make sense for readonly-databases.
-        if (isReadOnly()) {
-            return false;
-        }
-        // acquire lock - no that no other thread is enabling WAL at the same time
-        lock();
-        try {
-            if (mConnectionPool != null) {
-                // already enabled
-                return true;
-            }
-            if (mPath.equalsIgnoreCase(MEMORY_DB_PATH)) {
-                Log.i(TAG, "can't enable WAL for memory databases.");
-                return false;
-            }
-
-            // make sure this database has NO attached databases because sqlite's write-ahead-logging
-            // doesn't work for databases with attached databases
-            if (mHasAttachedDbs) {
-                if (Log.isLoggable(TAG, Log.DEBUG)) {
-                    Log.d(TAG,
-                            "this database: " + mPath + " has attached databases. can't  enable WAL.");
-                }
-                return false;
-            }
-            mConnectionPool = new DatabaseConnectionPool(this);
-            setJournalMode(mPath, "WAL");
-            return true;
-        } finally {
-            unlock();
-        }
+        // turn off WAL until lockingprotocolerror bug and diskIO bug are fixed
+        return false;
+//        // make sure the database is not READONLY. WAL doesn't make sense for readonly-databases.
+//        if (isReadOnly()) {
+//            return false;
+//        }
+//        // acquire lock - no that no other thread is enabling WAL at the same time
+//        lock();
+//        try {
+//            if (mConnectionPool != null) {
+//                // already enabled
+//                return true;
+//            }
+//            if (mPath.equalsIgnoreCase(MEMORY_DB_PATH)) {
+//                Log.i(TAG, "can't enable WAL for memory databases.");
+//                return false;
+//            }
+//
+//            // make sure this database has NO attached databases because sqlite's write-ahead-logging
+//            // doesn't work for databases with attached databases
+//            if (mHasAttachedDbs) {
+//                if (Log.isLoggable(TAG, Log.DEBUG)) {
+//                    Log.d(TAG,
+//                            "this database: " + mPath + " has attached databases. can't  enable WAL.");
+//                }
+//                return false;
+//            }
+//            mConnectionPool = new DatabaseConnectionPool(this);
+//            setJournalMode(mPath, "WAL");
+//            return true;
+//        } finally {
+//            unlock();
+//        }
     }
 
     /**
@@ -2424,19 +2426,20 @@
      * @hide
      */
     public void disableWriteAheadLogging() {
-        // grab database lock so that writeAheadLogging is not disabled from 2 different threads
-        // at the same time
-        lock();
-        try {
-            if (mConnectionPool == null) {
-                return; // already disabled
-            }
-            mConnectionPool.close();
-            setJournalMode(mPath, "TRUNCATE");
-            mConnectionPool = null;
-        } finally {
-            unlock();
-        }
+        return;
+//        // grab database lock so that writeAheadLogging is not disabled from 2 different threads
+//        // at the same time
+//        lock();
+//        try {
+//            if (mConnectionPool == null) {
+//                return; // already disabled
+//            }
+//            mConnectionPool.close();
+//            setJournalMode(mPath, "TRUNCATE");
+//            mConnectionPool = null;
+//        } finally {
+//            unlock();
+//        }
     }
 
     /* package */ SQLiteDatabase getDatabaseHandle(String sql) {
diff --git a/core/java/android/hardware/Camera.java b/core/java/android/hardware/Camera.java
index 207785c..c2f3ae7 100644
--- a/core/java/android/hardware/Camera.java
+++ b/core/java/android/hardware/Camera.java
@@ -357,7 +357,7 @@
         }
     }
 
-    private native final void setPreviewDisplay(Surface surface);
+    private native final void setPreviewDisplay(Surface surface) throws IOException;
 
     /**
      * Sets the {@link SurfaceTexture} to be used for live preview.
@@ -380,7 +380,7 @@
      * @throws IOException if the method fails (for example, if the surface
      *     texture is unavailable or unsuitable).
      */
-    public native final void setPreviewTexture(SurfaceTexture surfaceTexture);
+    public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;
 
     /**
      * Callback interface used to deliver copies of preview frames as