Merge "Re-initialize error after attach"
diff --git a/cmds/bootanimation/BootAnimation.cpp b/cmds/bootanimation/BootAnimation.cpp
index 5c265ab..1a2ab81 100644
--- a/cmds/bootanimation/BootAnimation.cpp
+++ b/cmds/bootanimation/BootAnimation.cpp
@@ -52,7 +52,6 @@
 
 #include "BootAnimation.h"
 
-#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
 #define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
 #define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
 #define EXIT_PROP_NAME "service.bootanim.exit"
@@ -285,9 +284,6 @@
             (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
             ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
 
-            ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
-            ((zipFile = ZipFileRO::open(USER_BOOTANIMATION_FILE)) != NULL)) ||
-
             ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
             ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
         mZip = zipFile;
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index e6da288..4607902 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -58,7 +58,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.jar.StrictJarFile;
 import java.util.zip.ZipEntry;
 
 import com.android.internal.util.XmlUtils;
@@ -456,7 +456,7 @@
         return pi;
     }
 
-    private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
+    private Certificate[] loadCertificates(StrictJarFile jarFile, ZipEntry je,
             byte[] readBuffer) {
         try {
             // We must read the stream for the JarEntry to retrieve
@@ -466,13 +466,11 @@
                 // not using
             }
             is.close();
-            return je != null ? je.getCertificates() : null;
+            return je != null ? jarFile.getCertificates(je) : null;
         } catch (IOException e) {
-            Slog.w(TAG, "Exception reading " + je.getName() + " in "
-                    + jarFile.getName(), e);
+            Slog.w(TAG, "Exception reading " + je.getName() + " in " + jarFile, e);
         } catch (RuntimeException e) {
-            Slog.w(TAG, "Exception reading " + je.getName() + " in "
-                    + jarFile.getName(), e);
+            Slog.w(TAG, "Exception reading " + je.getName() + " in " + jarFile, e);
         }
         return null;
     }
@@ -591,9 +589,9 @@
      */
     public boolean collectManifestDigest(Package pkg) {
         try {
-            final JarFile jarFile = new JarFile(mArchiveSourcePath);
+            final StrictJarFile jarFile = new StrictJarFile(mArchiveSourcePath);
             try {
-                final ZipEntry je = jarFile.getEntry(ANDROID_MANIFEST_FILENAME);
+                final ZipEntry je = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
                 if (je != null) {
                     pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
                 }
@@ -624,7 +622,7 @@
         }
 
         try {
-            JarFile jarFile = new JarFile(mArchiveSourcePath);
+            StrictJarFile jarFile = new StrictJarFile(mArchiveSourcePath);
 
             Certificate[] certs = null;
 
@@ -633,7 +631,7 @@
                 // can trust it...  we'll just use the AndroidManifest.xml
                 // to retrieve its signatures, not validating all of the
                 // files.
-                JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
+                ZipEntry jarEntry = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
                 certs = loadCertificates(jarFile, jarEntry, readBuffer);
                 if (certs == null) {
                     Slog.e(TAG, "Package " + pkg.packageName
@@ -656,9 +654,9 @@
                     }
                 }
             } else {
-                Enumeration<JarEntry> entries = jarFile.entries();
-                while (entries.hasMoreElements()) {
-                    final JarEntry je = entries.nextElement();
+                Iterator<ZipEntry> entries = jarFile.iterator();
+                while (entries.hasNext()) {
+                    final ZipEntry je = entries.next();
                     if (je.isDirectory()) continue;
 
                     final String name = je.getName();
@@ -744,6 +742,10 @@
             Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
             mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
             return false;
+        } catch (SecurityException e) {
+            Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
+            mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
+            return false;
         } catch (RuntimeException e) {
             Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
             mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
diff --git a/core/jni/android_os_SystemClock.cpp b/core/jni/android_os_SystemClock.cpp
index d20b800..5f4d570 100644
--- a/core/jni/android_os_SystemClock.cpp
+++ b/core/jni/android_os_SystemClock.cpp
@@ -43,16 +43,77 @@
 
 namespace android {
 
+static int setCurrentTimeMillisAlarmDriver(struct timeval *tv)
+{
+    struct timespec ts;
+    int fd;
+    int res;
+
+    fd = open("/dev/alarm", O_RDWR);
+    if(fd < 0) {
+        ALOGV("Unable to open alarm driver: %s\n", strerror(errno));
+        return -1;
+    }
+    ts.tv_sec = tv->tv_sec;
+    ts.tv_nsec = tv->tv_usec * 1000;
+    res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
+    if (res < 0)
+        ALOGV("ANDROID_ALARM_SET_RTC ioctl failed: %s\n", strerror(errno));
+    close(fd);
+    return res;
+}
+
+static int setCurrentTimeMillisRtc(struct timeval *tv)
+{
+    struct rtc_time rtc;
+    struct tm tm, *gmtime_res;
+    int fd;
+    int res;
+
+    fd = open("/dev/rtc0", O_RDWR);
+    if (fd < 0) {
+        ALOGV("Unable to open RTC driver: %s\n", strerror(errno));
+        return -1;
+    }
+
+    res = settimeofday(tv, NULL);
+    if (res < 0) {
+        ALOGV("settimeofday() failed: %s\n", strerror(errno));
+        goto done;
+    }
+
+    gmtime_res = gmtime_r(&tv->tv_sec, &tm);
+    if (!gmtime_res) {
+        ALOGV("gmtime_r() failed: %s\n", strerror(errno));
+        res = -1;
+        goto done;
+    }
+
+    memset(&rtc, 0, sizeof(rtc));
+    rtc.tm_sec = tm.tm_sec;
+    rtc.tm_min = tm.tm_min;
+    rtc.tm_hour = tm.tm_hour;
+    rtc.tm_mday = tm.tm_mday;
+    rtc.tm_mon = tm.tm_mon;
+    rtc.tm_year = tm.tm_year;
+    rtc.tm_wday = tm.tm_wday;
+    rtc.tm_yday = tm.tm_yday;
+    rtc.tm_isdst = tm.tm_isdst;
+    res = ioctl(fd, RTC_SET_TIME, &rtc);
+    if (res < 0)
+        ALOGV("RTC_SET_TIME ioctl failed: %s\n", strerror(errno));
+done:
+    close(fd);
+    return res;
+}
+
 /*
  * Set the current time.  This only works when running as root.
  */
 static int setCurrentTimeMillis(int64_t millis)
 {
     struct timeval tv;
-    struct timespec ts;
-    int fd;
-    int res;
-    int ret = 0;
+    int ret;
 
     if (millis <= 0 || millis / 1000LL >= INT_MAX) {
         return -1;
@@ -63,19 +124,14 @@
 
     ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec);
 
-    fd = open("/dev/alarm", O_RDWR);
-    if(fd < 0) {
-        ALOGW("Unable to open alarm driver: %s\n", strerror(errno));
-        return -1;
-    }
-    ts.tv_sec = tv.tv_sec;
-    ts.tv_nsec = tv.tv_usec * 1000;
-    res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
-    if(res < 0) {
+    ret = setCurrentTimeMillisAlarmDriver(&tv);
+    if (ret < 0)
+        ret = setCurrentTimeMillisRtc(&tv);
+
+    if(ret < 0) {
         ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno));
         ret = -1;
     }
-    close(fd);
     return ret;
 }
 
diff --git a/core/jni/android_view_DisplayEventReceiver.cpp b/core/jni/android_view_DisplayEventReceiver.cpp
index 64fb27b..01ab2ff 100644
--- a/core/jni/android_view_DisplayEventReceiver.cpp
+++ b/core/jni/android_view_DisplayEventReceiver.cpp
@@ -87,7 +87,7 @@
         return result;
     }
 
-    int rc = mMessageQueue->getLooper()->addFd(mReceiver.getFd(), 0, ALOOPER_EVENT_INPUT,
+    int rc = mMessageQueue->getLooper()->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT,
             this, NULL);
     if (rc < 0) {
         return UNKNOWN_ERROR;
@@ -125,13 +125,13 @@
 }
 
 int NativeDisplayEventReceiver::handleEvent(int receiveFd, int events, void* data) {
-    if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
+    if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
         ALOGE("Display event receiver pipe was closed or an error occurred.  "
                 "events=0x%x", events);
         return 0; // remove the callback
     }
 
-    if (!(events & ALOOPER_EVENT_INPUT)) {
+    if (!(events & Looper::EVENT_INPUT)) {
         ALOGW("Received spurious callback for unhandled poll event.  "
                 "events=0x%x", events);
         return 1; // keep the callback
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index cbc4e5a..a4221dc 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -335,7 +335,7 @@
     jint len = 0;
     if (data) {
         len = _env->GetArrayLength(data);
-        jint *ptr = _env->GetIntArrayElements(data, NULL);
+        ptr = _env->GetIntArrayElements(data, NULL);
     }
     LOG_API("nContextSendMessage, con(%p), id(%i), len(%i)", con, id, len);
     rsContextSendMessage(con, id, (const uint8_t *)ptr, len * sizeof(int));
diff --git a/include/androidfw/ResourceTypes.h b/include/androidfw/ResourceTypes.h
index 97afa59..5151b06 100644
--- a/include/androidfw/ResourceTypes.h
+++ b/include/androidfw/ResourceTypes.h
@@ -1020,7 +1020,7 @@
     // attrs_manifest.xml.
     enum {
         CONFIG_MCC = ACONFIGURATION_MCC,
-        CONFIG_MNC = ACONFIGURATION_MCC,
+        CONFIG_MNC = ACONFIGURATION_MNC,
         CONFIG_LOCALE = ACONFIGURATION_LOCALE,
         CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN,
         CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD,
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 00e7870..0be17ff 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -732,7 +732,9 @@
 
         if (mRs == 0) {
             mRs = new RSC::RS();
-            if (!mRs->init(RSC::RS_INIT_LOW_LATENCY | RSC::RS_INIT_SYNCHRONOUS)) {
+            // a null path is OK because there are no custom kernels used
+            // hence nothing gets cached by RS
+            if (!mRs->init("", RSC::RS_INIT_LOW_LATENCY | RSC::RS_INIT_SYNCHRONOUS)) {
                 ALOGE("blur RS failed to init");
             }
 
diff --git a/media/java/android/media/MediaDrm.java b/media/java/android/media/MediaDrm.java
index 6b278d4..f5a703b 100644
--- a/media/java/android/media/MediaDrm.java
+++ b/media/java/android/media/MediaDrm.java
@@ -100,7 +100,7 @@
     private EventHandler mEventHandler;
     private OnEventListener mOnEventListener;
 
-    private int mNativeContext;
+    private long mNativeContext;
 
     /**
      * Query if the given scheme identified by its UUID is supported on
diff --git a/media/java/android/media/MediaMuxer.java b/media/java/android/media/MediaMuxer.java
index 65a9308..e5c97e7 100644
--- a/media/java/android/media/MediaMuxer.java
+++ b/media/java/android/media/MediaMuxer.java
@@ -65,8 +65,6 @@
 
 final public class MediaMuxer {
 
-    private int mNativeContext;
-
     static {
         System.loadLibrary("media_jni");
     }
@@ -84,16 +82,16 @@
     };
 
     // All the native functions are listed here.
-    private static native int nativeSetup(FileDescriptor fd, int format);
-    private static native void nativeRelease(int nativeObject);
-    private static native void nativeStart(int nativeObject);
-    private static native void nativeStop(int nativeObject);
-    private static native int nativeAddTrack(int nativeObject, String[] keys,
+    private static native long nativeSetup(FileDescriptor fd, int format);
+    private static native void nativeRelease(long nativeObject);
+    private static native void nativeStart(long nativeObject);
+    private static native void nativeStop(long nativeObject);
+    private static native int nativeAddTrack(long nativeObject, String[] keys,
             Object[] values);
-    private static native void nativeSetOrientationHint(int nativeObject,
+    private static native void nativeSetOrientationHint(long nativeObject,
             int degrees);
-    private static native void nativeSetLocation(int nativeObject, int latitude, int longitude);
-    private static native void nativeWriteSampleData(int nativeObject,
+    private static native void nativeSetLocation(long nativeObject, int latitude, int longitude);
+    private static native void nativeWriteSampleData(long nativeObject,
             int trackIndex, ByteBuffer byteBuf,
             int offset, int size, long presentationTimeUs, int flags);
 
@@ -108,7 +106,7 @@
     private final CloseGuard mCloseGuard = CloseGuard.get();
     private int mLastTrackIndex = -1;
 
-    private int mNativeObject;
+    private long mNativeObject;
 
     /**
      * Constructor.
diff --git a/media/jni/android_media_MediaDrm.cpp b/media/jni/android_media_MediaDrm.cpp
index bbb74d25b..eb7d51c 100644
--- a/media/jni/android_media_MediaDrm.cpp
+++ b/media/jni/android_media_MediaDrm.cpp
@@ -267,7 +267,7 @@
 }
 
 static sp<IDrm> GetDrm(JNIEnv *env, jobject thiz) {
-    JDrm *jdrm = (JDrm *)env->GetIntField(thiz, gFields.context);
+    JDrm *jdrm = (JDrm *)env->GetLongField(thiz, gFields.context);
     return jdrm ? jdrm->getDrm() : NULL;
 }
 
@@ -484,14 +484,14 @@
 
 static sp<JDrm> setDrm(
         JNIEnv *env, jobject thiz, const sp<JDrm> &drm) {
-    sp<JDrm> old = (JDrm *)env->GetIntField(thiz, gFields.context);
+    sp<JDrm> old = (JDrm *)env->GetLongField(thiz, gFields.context);
     if (drm != NULL) {
         drm->incStrong(thiz);
     }
     if (old != NULL) {
         old->decStrong(thiz);
     }
-    env->SetIntField(thiz, gFields.context, (int)drm.get());
+    env->SetLongField(thiz, gFields.context, reinterpret_cast<jlong>(drm.get()));
 
     return old;
 }
@@ -520,7 +520,7 @@
 static void android_media_MediaDrm_native_init(JNIEnv *env) {
     jclass clazz;
     FIND_CLASS(clazz, "android/media/MediaDrm");
-    GET_FIELD_ID(gFields.context, clazz, "mNativeContext", "I");
+    GET_FIELD_ID(gFields.context, clazz, "mNativeContext", "J");
     GET_STATIC_METHOD_ID(gFields.post_event, clazz, "postEventFromNative",
                          "(Ljava/lang/Object;IILjava/lang/Object;)V");
 
diff --git a/media/jni/android_media_MediaMuxer.cpp b/media/jni/android_media_MediaMuxer.cpp
index 457b956..2c16a05 100644
--- a/media/jni/android_media_MediaMuxer.cpp
+++ b/media/jni/android_media_MediaMuxer.cpp
@@ -31,7 +31,6 @@
 namespace android {
 
 struct fields_t {
-    jfieldID context;
     jmethodID arrayID;
 };
 
@@ -42,7 +41,7 @@
 using namespace android;
 
 static jint android_media_MediaMuxer_addTrack(
-        JNIEnv *env, jclass clazz, jint nativeObject, jobjectArray keys,
+        JNIEnv *env, jclass clazz, jlong nativeObject, jobjectArray keys,
         jobjectArray values) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer == NULL) {
@@ -72,7 +71,7 @@
 }
 
 static void android_media_MediaMuxer_writeSampleData(
-        JNIEnv *env, jclass clazz, jint nativeObject, jint trackIndex,
+        JNIEnv *env, jclass clazz, jlong nativeObject, jint trackIndex,
         jobject byteBuf, jint offset, jint size, jlong timeUs, jint flags) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer == NULL) {
@@ -147,7 +146,7 @@
 }
 
 static void android_media_MediaMuxer_setOrientationHint(
-        JNIEnv *env, jclass clazz, jint nativeObject, jint degrees) {
+        JNIEnv *env, jclass clazz, jlong nativeObject, jint degrees) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer == NULL) {
         jniThrowException(env, "java/lang/IllegalStateException",
@@ -177,7 +176,7 @@
 }
 
 static void android_media_MediaMuxer_start(JNIEnv *env, jclass clazz,
-                                           jint nativeObject) {
+                                           jlong nativeObject) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer == NULL) {
         jniThrowException(env, "java/lang/IllegalStateException",
@@ -195,7 +194,7 @@
 }
 
 static void android_media_MediaMuxer_stop(JNIEnv *env, jclass clazz,
-                                          jint nativeObject) {
+                                          jlong nativeObject) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer == NULL) {
         jniThrowException(env, "java/lang/IllegalStateException",
@@ -213,7 +212,7 @@
 }
 
 static void android_media_MediaMuxer_native_release(
-        JNIEnv *env, jclass clazz, jint nativeObject) {
+        JNIEnv *env, jclass clazz, jlong nativeObject) {
     sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
     if (muxer != NULL) {
         muxer->decStrong(clazz);
@@ -222,26 +221,26 @@
 
 static JNINativeMethod gMethods[] = {
 
-    { "nativeAddTrack", "(I[Ljava/lang/String;[Ljava/lang/Object;)I",
+    { "nativeAddTrack", "(J[Ljava/lang/String;[Ljava/lang/Object;)I",
         (void *)android_media_MediaMuxer_addTrack },
 
-    { "nativeSetOrientationHint", "(II)V",
+    { "nativeSetOrientationHint", "(JI)V",
         (void *)android_media_MediaMuxer_setOrientationHint},
 
-    { "nativeSetLocation", "(III)V",
+    { "nativeSetLocation", "(JII)V",
         (void *)android_media_MediaMuxer_setLocation},
 
-    { "nativeStart", "(I)V", (void *)android_media_MediaMuxer_start},
+    { "nativeStart", "(J)V", (void *)android_media_MediaMuxer_start},
 
-    { "nativeWriteSampleData", "(IILjava/nio/ByteBuffer;IIJI)V",
+    { "nativeWriteSampleData", "(JILjava/nio/ByteBuffer;IIJI)V",
         (void *)android_media_MediaMuxer_writeSampleData },
 
-    { "nativeStop", "(I)V", (void *)android_media_MediaMuxer_stop},
+    { "nativeStop", "(J)V", (void *)android_media_MediaMuxer_stop},
 
-    { "nativeSetup", "(Ljava/io/FileDescriptor;I)I",
+    { "nativeSetup", "(Ljava/io/FileDescriptor;I)J",
         (void *)android_media_MediaMuxer_native_setup },
 
-    { "nativeRelease", "(I)V",
+    { "nativeRelease", "(J)V",
         (void *)android_media_MediaMuxer_native_release },
 
 };
@@ -252,12 +251,6 @@
     int err = AndroidRuntime::registerNativeMethods(env,
                 "android/media/MediaMuxer", gMethods, NELEM(gMethods));
 
-    jclass clazz = env->FindClass("android/media/MediaMuxer");
-    CHECK(clazz != NULL);
-
-    gFields.context = env->GetFieldID(clazz, "mNativeContext", "I");
-    CHECK(gFields.context != NULL);
-
     jclass byteBufClass = env->FindClass("java/nio/ByteBuffer");
     CHECK(byteBufClass != NULL);
 
diff --git a/native/android/input.cpp b/native/android/input.cpp
index e9d08b4..fc52138 100644
--- a/native/android/input.cpp
+++ b/native/android/input.cpp
@@ -273,7 +273,7 @@
 void AInputQueue_attachLooper(AInputQueue* queue, ALooper* looper,
         int ident, ALooper_callbackFunc callback, void* data) {
     InputQueue* iq = static_cast<InputQueue*>(queue);
-    Looper* l = static_cast<Looper*>(looper);
+    Looper* l = reinterpret_cast<Looper*>(looper);
     iq->attachLooper(l, ident, callback, data);
 }
 
diff --git a/native/android/looper.cpp b/native/android/looper.cpp
index 455e950..24cb234 100644
--- a/native/android/looper.cpp
+++ b/native/android/looper.cpp
@@ -25,20 +25,28 @@
 using android::sp;
 using android::IPCThreadState;
 
+static inline Looper* ALooper_to_Looper(ALooper* alooper) {
+    return reinterpret_cast<Looper*>(alooper);
+}
+
+static inline ALooper* Looper_to_ALooper(Looper* looper) {
+    return reinterpret_cast<ALooper*>(looper);
+}
+
 ALooper* ALooper_forThread() {
-    return Looper::getForThread().get();
+    return Looper_to_ALooper(Looper::getForThread().get());
 }
 
 ALooper* ALooper_prepare(int opts) {
-    return Looper::prepare(opts).get();
+    return Looper_to_ALooper(Looper::prepare(opts).get());
 }
 
 void ALooper_acquire(ALooper* looper) {
-    static_cast<Looper*>(looper)->incStrong((void*)ALooper_acquire);
+    ALooper_to_Looper(looper)->incStrong((void*)ALooper_acquire);
 }
 
 void ALooper_release(ALooper* looper) {
-    static_cast<Looper*>(looper)->decStrong((void*)ALooper_acquire);
+    ALooper_to_Looper(looper)->decStrong((void*)ALooper_acquire);
 }
 
 int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
@@ -64,14 +72,14 @@
 }
 
 void ALooper_wake(ALooper* looper) {
-    static_cast<Looper*>(looper)->wake();
+    ALooper_to_Looper(looper)->wake();
 }
 
 int ALooper_addFd(ALooper* looper, int fd, int ident, int events,
         ALooper_callbackFunc callback, void* data) {
-    return static_cast<Looper*>(looper)->addFd(fd, ident, events, callback, data);
+    return ALooper_to_Looper(looper)->addFd(fd, ident, events, callback, data);
 }
 
 int ALooper_removeFd(ALooper* looper, int fd) {
-    return static_cast<Looper*>(looper)->removeFd(fd);
+    return ALooper_to_Looper(looper)->removeFd(fd);
 }
diff --git a/services/java/com/android/server/AlarmManagerService.java b/services/java/com/android/server/AlarmManagerService.java
index 5ae9a6d..2e1b0af 100644
--- a/services/java/com/android/server/AlarmManagerService.java
+++ b/services/java/com/android/server/AlarmManagerService.java
@@ -99,7 +99,7 @@
 
     private Object mLock = new Object();
 
-    private int mDescriptor;
+    private long mNativeData;
     private long mNextWakeup;
     private long mNextNonWakeup;
     private int mBroadcastRefCount = 0;
@@ -464,7 +464,7 @@
     
     public AlarmManagerService(Context context) {
         mContext = context;
-        mDescriptor = init();
+        mNativeData = init();
         mNextWakeup = mNextNonWakeup = 0;
 
         // We have to set current TimeZone info to kernel
@@ -493,7 +493,7 @@
         mClockReceiver.scheduleDateChangedEvent();
         mUninstallReceiver = new UninstallReceiver();
         
-        if (mDescriptor != -1) {
+        if (mNativeData != 0) {
             mWaitThread.start();
         } else {
             Slog.w(TAG, "Failed to open alarm driver. Falling back to a handler.");
@@ -502,7 +502,7 @@
     
     protected void finalize() throws Throwable {
         try {
-            close(mDescriptor);
+            close(mNativeData);
         } finally {
             super.finalize();
         }
@@ -702,7 +702,7 @@
                 // Update the kernel timezone information
                 // Kernel tracks time offsets as 'minutes west of GMT'
                 int gmtOffset = zone.getOffset(System.currentTimeMillis());
-                setKernelTimezone(mDescriptor, -(gmtOffset / 60000));
+                setKernelTimezone(mNativeData, -(gmtOffset / 60000));
             }
 
             TimeZone.setDefault(null);
@@ -796,7 +796,7 @@
 
     private void setLocked(int type, long when)
     {
-        if (mDescriptor != -1)
+        if (mNativeData != 0)
         {
             // The kernel never triggers alarms with negative wakeup times
             // so we ensure they are positive.
@@ -809,7 +809,7 @@
                 alarmNanoseconds = (when % 1000) * 1000 * 1000;
             }
             
-            set(mDescriptor, type, alarmSeconds, alarmNanoseconds);
+            set(mNativeData, type, alarmSeconds, alarmNanoseconds);
         }
         else
         {
@@ -1014,11 +1014,11 @@
         }
     }
 
-    private native int init();
-    private native void close(int fd);
-    private native void set(int fd, int type, long seconds, long nanoseconds);
-    private native int waitForAlarm(int fd);
-    private native int setKernelTimezone(int fd, int minuteswest);
+    private native long init();
+    private native void close(long nativeData);
+    private native void set(long nativeData, int type, long seconds, long nanoseconds);
+    private native int waitForAlarm(long nativeData);
+    private native int setKernelTimezone(long nativeData, int minuteswest);
 
     private void triggerAlarmsLocked(ArrayList<Alarm> triggerList, long nowELAPSED, long nowRTC) {
         // batches are temporally sorted, so we need only pull from the
@@ -1158,7 +1158,7 @@
 
             while (true)
             {
-                int result = waitForAlarm(mDescriptor);
+                int result = waitForAlarm(mNativeData);
 
                 triggerList.clear();
 
@@ -1340,7 +1340,7 @@
                 // daylight savings information.
                 TimeZone zone = TimeZone.getTimeZone(SystemProperties.get(TIMEZONE_PROPERTY));
                 int gmtOffset = zone.getOffset(System.currentTimeMillis());
-                setKernelTimezone(mDescriptor, -(gmtOffset / 60000));
+                setKernelTimezone(mNativeData, -(gmtOffset / 60000));
                 scheduleDateChangedEvent();
             }
         }
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index 84cedb8..37f4cd7 100644
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -2526,6 +2526,7 @@
         // activity into the stopped state and then finish it.
         if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
         mStackSupervisor.mFinishingActivities.add(r);
+        r.resumeKeyDispatchingLocked();
         mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
         return r;
     }
@@ -3160,9 +3161,7 @@
 
         final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
         if (task == tr && task.mOnTopOfHome || numTasks <= 1) {
-            if (task != null) {
-                task.mOnTopOfHome = false;
-            }
+            tr.mOnTopOfHome = false;
             return mStackSupervisor.resumeHomeActivity(null);
         }
 
diff --git a/services/jni/com_android_server_AlarmManagerService.cpp b/services/jni/com_android_server_AlarmManagerService.cpp
index c2f6151..342515b 100644
--- a/services/jni/com_android_server_AlarmManagerService.cpp
+++ b/services/jni/com_android_server_AlarmManagerService.cpp
@@ -25,6 +25,8 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/epoll.h>
+#include <sys/timerfd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -37,7 +39,136 @@
 
 namespace android {
 
-static jint android_server_AlarmManagerService_setKernelTimezone(JNIEnv* env, jobject obj, jint fd, jint minswest)
+static const size_t N_ANDROID_TIMERFDS = ANDROID_ALARM_TYPE_COUNT + 1;
+static const clockid_t android_alarm_to_clockid[N_ANDROID_TIMERFDS] = {
+    CLOCK_REALTIME_ALARM,
+    CLOCK_REALTIME,
+    CLOCK_BOOTTIME_ALARM,
+    CLOCK_BOOTTIME,
+    CLOCK_MONOTONIC,
+    CLOCK_REALTIME,
+};
+/* to match the legacy alarm driver implementation, we need an extra
+   CLOCK_REALTIME fd which exists specifically to be canceled on RTC changes */
+
+class AlarmImpl
+{
+public:
+    AlarmImpl(int *fds, size_t n_fds);
+    virtual ~AlarmImpl();
+
+    virtual int set(int type, struct timespec *ts) = 0;
+    virtual int waitForAlarm() = 0;
+
+protected:
+    int *fds;
+    size_t n_fds;
+};
+
+class AlarmImplAlarmDriver : public AlarmImpl
+{
+public:
+    AlarmImplAlarmDriver(int fd) : AlarmImpl(&fd, 1) { }
+
+    int set(int type, struct timespec *ts);
+    int waitForAlarm();
+};
+
+class AlarmImplTimerFd : public AlarmImpl
+{
+public:
+    AlarmImplTimerFd(int fds[N_ANDROID_TIMERFDS], int epollfd) :
+        AlarmImpl(fds, N_ANDROID_TIMERFDS), epollfd(epollfd) { }
+    ~AlarmImplTimerFd();
+
+    int set(int type, struct timespec *ts);
+    int waitForAlarm();
+
+private:
+    int epollfd;
+};
+
+AlarmImpl::AlarmImpl(int *fds_, size_t n_fds) : fds(new int[n_fds]),
+        n_fds(n_fds)
+{
+    memcpy(fds, fds_, n_fds * sizeof(fds[0]));
+}
+
+AlarmImpl::~AlarmImpl()
+{
+    for (size_t i = 0; i < n_fds; i++) {
+        close(fds[i]);
+    }
+    delete [] fds;
+}
+
+int AlarmImplAlarmDriver::set(int type, struct timespec *ts)
+{
+    return ioctl(fds[0], ANDROID_ALARM_SET(type), ts);
+}
+
+int AlarmImplAlarmDriver::waitForAlarm()
+{
+    return ioctl(fds[0], ANDROID_ALARM_WAIT);
+}
+
+AlarmImplTimerFd::~AlarmImplTimerFd()
+{
+    for (size_t i = 0; i < N_ANDROID_TIMERFDS; i++) {
+        epoll_ctl(epollfd, EPOLL_CTL_DEL, fds[i], NULL);
+    }
+    close(epollfd);
+}
+
+int AlarmImplTimerFd::set(int type, struct timespec *ts)
+{
+    if (type > ANDROID_ALARM_TYPE_COUNT) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    if (!ts->tv_nsec && !ts->tv_sec) {
+        ts->tv_nsec = 1;
+    }
+    /* timerfd interprets 0 = disarm, so replace with a practically
+       equivalent deadline of 1 ns */
+
+    struct itimerspec spec;
+    memset(&spec, 0, sizeof(spec));
+    memcpy(&spec.it_value, ts, sizeof(spec.it_value));
+
+    return timerfd_settime(fds[type], TFD_TIMER_ABSTIME, &spec, NULL);
+}
+
+int AlarmImplTimerFd::waitForAlarm()
+{
+    epoll_event events[N_ANDROID_TIMERFDS];
+
+    int nevents = epoll_wait(epollfd, events, N_ANDROID_TIMERFDS, -1);
+    if (nevents < 0) {
+        return nevents;
+    }
+
+    int result = 0;
+    for (int i = 0; i < nevents; i++) {
+        uint32_t alarm_idx = events[i].data.u32;
+        uint64_t unused;
+        ssize_t err = read(fds[alarm_idx], &unused, sizeof(unused));
+        if (err < 0) {
+            if (alarm_idx == ANDROID_ALARM_TYPE_COUNT && errno == ECANCELED) {
+                result |= ANDROID_ALARM_TIME_CHANGE_MASK;
+            } else {
+                return err;
+            }
+        } else {
+            result |= (1 << alarm_idx);
+        }
+    }
+
+    return result;
+}
+
+static jint android_server_AlarmManagerService_setKernelTimezone(JNIEnv*, jobject, jlong, jint minswest)
 {
     struct timezone tz;
 
@@ -55,40 +186,116 @@
     return 0;
 }
 
-static jint android_server_AlarmManagerService_init(JNIEnv* env, jobject obj)
+static jlong init_alarm_driver()
 {
-    return open("/dev/alarm", O_RDWR);
+    int fd = open("/dev/alarm", O_RDWR);
+    if (fd < 0) {
+        ALOGV("opening alarm driver failed: %s", strerror(errno));
+        return 0;
+    }
+
+    AlarmImpl *ret = new AlarmImplAlarmDriver(fd);
+    return reinterpret_cast<jlong>(ret);
 }
 
-static void android_server_AlarmManagerService_close(JNIEnv* env, jobject obj, jint fd)
+static jlong init_timerfd()
 {
-	close(fd);
+    int epollfd;
+    int fds[N_ANDROID_TIMERFDS];
+
+    epollfd = epoll_create(N_ANDROID_TIMERFDS);
+    if (epollfd < 0) {
+        ALOGV("epoll_create(%u) failed: %s", N_ANDROID_TIMERFDS,
+                strerror(errno));
+        return 0;
+    }
+
+    for (size_t i = 0; i < N_ANDROID_TIMERFDS; i++) {
+        fds[i] = timerfd_create(android_alarm_to_clockid[i], 0);
+        if (fds[i] < 0) {
+            ALOGV("timerfd_create(%u) failed: %s",  android_alarm_to_clockid[i],
+                    strerror(errno));
+            close(epollfd);
+            for (size_t j = 0; j < i; j++) {
+                close(fds[j]);
+            }
+            return 0;
+        }
+    }
+
+    AlarmImpl *ret = new AlarmImplTimerFd(fds, epollfd);
+
+    for (size_t i = 0; i < N_ANDROID_TIMERFDS; i++) {
+        epoll_event event;
+        event.events = EPOLLIN | EPOLLWAKEUP;
+        event.data.u32 = i;
+
+        int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
+        if (err < 0) {
+            ALOGV("epoll_ctl(EPOLL_CTL_ADD) failed: %s", strerror(errno));
+            delete ret;
+            return 0;
+        }
+    }
+
+    struct itimerspec spec;
+    memset(&spec, 0, sizeof(spec));
+    /* 0 = disarmed; the timerfd doesn't need to be armed to get
+       RTC change notifications, just set up as cancelable */
+
+    int err = timerfd_settime(fds[ANDROID_ALARM_TYPE_COUNT],
+            TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, NULL);
+    if (err < 0) {
+        ALOGV("timerfd_settime() failed: %s", strerror(errno));
+        delete ret;
+        return 0;
+    }
+
+    return reinterpret_cast<jlong>(ret);
 }
 
-static void android_server_AlarmManagerService_set(JNIEnv* env, jobject obj, jint fd, jint type, jlong seconds, jlong nanoseconds)
+static jlong android_server_AlarmManagerService_init(JNIEnv*, jobject)
 {
+    jlong ret = init_alarm_driver();
+    if (ret) {
+        return ret;
+    }
+
+    return init_timerfd();
+}
+
+static void android_server_AlarmManagerService_close(JNIEnv*, jobject, jlong nativeData)
+{
+    AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
+    delete impl;
+}
+
+static void android_server_AlarmManagerService_set(JNIEnv*, jobject, jlong nativeData, jint type, jlong seconds, jlong nanoseconds)
+{
+    AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
     struct timespec ts;
     ts.tv_sec = seconds;
     ts.tv_nsec = nanoseconds;
 
-	int result = ioctl(fd, ANDROID_ALARM_SET(type), &ts);
-	if (result < 0)
-	{
+    int result = impl->set(type, &ts);
+    if (result < 0)
+    {
         ALOGE("Unable to set alarm to %lld.%09lld: %s\n", seconds, nanoseconds, strerror(errno));
     }
 }
 
-static jint android_server_AlarmManagerService_waitForAlarm(JNIEnv* env, jobject obj, jint fd)
+static jint android_server_AlarmManagerService_waitForAlarm(JNIEnv*, jobject, jlong nativeData)
 {
-	int result = 0;
+    AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
+    int result = 0;
 
-	do
-	{
-		result = ioctl(fd, ANDROID_ALARM_WAIT);
-	} while (result < 0 && errno == EINTR);
+    do
+    {
+        result = impl->waitForAlarm();
+    } while (result < 0 && errno == EINTR);
 
-	if (result < 0)
-	{
+    if (result < 0)
+    {
         ALOGE("Unable to wait on alarm: %s\n", strerror(errno));
         return 0;
     }
@@ -98,11 +305,11 @@
 
 static JNINativeMethod sMethods[] = {
      /* name, signature, funcPtr */
-	{"init", "()I", (void*)android_server_AlarmManagerService_init},
-	{"close", "(I)V", (void*)android_server_AlarmManagerService_close},
-	{"set", "(IIJJ)V", (void*)android_server_AlarmManagerService_set},
-    {"waitForAlarm", "(I)I", (void*)android_server_AlarmManagerService_waitForAlarm},
-    {"setKernelTimezone", "(II)I", (void*)android_server_AlarmManagerService_setKernelTimezone},
+    {"init", "()J", (void*)android_server_AlarmManagerService_init},
+    {"close", "(J)V", (void*)android_server_AlarmManagerService_close},
+    {"set", "(JIJJ)V", (void*)android_server_AlarmManagerService_set},
+    {"waitForAlarm", "(J)I", (void*)android_server_AlarmManagerService_waitForAlarm},
+    {"setKernelTimezone", "(JI)I", (void*)android_server_AlarmManagerService_setKernelTimezone},
 };
 
 int register_android_server_AlarmManagerService(JNIEnv* env)
diff --git a/tools/aapt/ResourceTable.cpp b/tools/aapt/ResourceTable.cpp
index f2e5254..6ced8b3 100644
--- a/tools/aapt/ResourceTable.cpp
+++ b/tools/aapt/ResourceTable.cpp
@@ -1342,7 +1342,7 @@
                 curType = string16;
                 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
                 curIsStyled = true;
-                curIsPseudolocalizable = true;
+                curIsPseudolocalizable = (translatable != false16);
             } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) {
                 curTag = &drawable16;
                 curType = drawable16;
@@ -1408,15 +1408,24 @@
                 // Check whether these strings need valid formats.
                 // (simplified form of what string16 does above)
                 size_t n = block.getAttributeCount();
+
+                // Pseudolocalizable by default, unless this string array isn't
+                // translatable.
+                curIsPseudolocalizable = true;
                 for (size_t i = 0; i < n; i++) {
                     size_t length;
                     const uint16_t* attr = block.getAttributeName(i, &length);
-                    if (strcmp16(attr, translatable16.string()) == 0
-                            || strcmp16(attr, formatted16.string()) == 0) {
+                    if (strcmp16(attr, translatable16.string()) == 0) {
+                        const uint16_t* value = block.getAttributeStringValue(i, &length);
+                        if (strcmp16(value, false16.string()) == 0) {
+                            curIsPseudolocalizable = false;
+                        }
+                    }
+
+                    if (strcmp16(attr, formatted16.string()) == 0) {
                         const uint16_t* value = block.getAttributeStringValue(i, &length);
                         if (strcmp16(value, false16.string()) == 0) {
                             curIsFormatted = false;
-                            break;
                         }
                     }
                 }
@@ -1426,7 +1435,6 @@
                 curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING;
                 curIsBag = true;
                 curIsBagReplaceOnOverwrite = true;
-                curIsPseudolocalizable = true;
             } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) {
                 curTag = &integer_array16;
                 curType = array16;