Merge "Fix broken assumptions in LocalePicker."
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index a61901b..e8caa0c1 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -391,8 +391,8 @@
         property_get("ro.product.locale.language", propLang, "en");
         property_get("ro.product.locale.region", propRegn, "US");
     }
-    strncat(language, propLang, 2);
-    strncat(region, propRegn, 2);
+    strncat(language, propLang, 3);
+    strncat(region, propRegn, 3);
     //ALOGD("language=%s region=%s\n", language, region);
 }
 
@@ -490,6 +490,8 @@
     char profile_backoff[sizeof("-Xprofile-backoff:") + PROPERTY_VALUE_MAX];
     char profile_top_k_threshold[sizeof("-Xprofile-top-k-threshold:") + PROPERTY_VALUE_MAX];
     char profile_top_k_change_threshold[sizeof("-Xprofile-top-k-change-threshold:") + PROPERTY_VALUE_MAX];
+    char profile_type[sizeof("-Xprofile-type:") + PROPERTY_VALUE_MAX];
+    char profile_max_stack_depth[sizeof("-Xprofile-max-stack-depth:")+PROPERTY_VALUE_MAX];
     char langOption[sizeof("-Duser.language=") + 3];
     char regionOption[sizeof("-Duser.region=") + 3];
     char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
@@ -872,6 +874,20 @@
             opt.optionString = profile_top_k_change_threshold;
             mOptions.add(opt);
         }
+
+        // Type of profile data.
+        strcpy(profile_type, "-Xprofile-type:");
+        if (property_get("dalvik.vm.profiler.type", profile_type+15, NULL) > 0) {
+            opt.optionString = profile_type;
+            mOptions.add(opt);
+        }
+
+        // Depth of bounded stack data
+        strcpy(profile_max_stack_depth, "-Xprofile-max-stack-depth:");
+        if (property_get("dalvik.vm.profile.max-stack-depth", profile_max_stack_depth+26, NULL) > 0) {
+           opt.optionString = profile_max_stack_depth;
+           mOptions.add(opt);
+        }
     }
 
     initArgs.version = JNI_VERSION_1_4;
diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp
index 2c828c1e..2f3b1f7 100644
--- a/libs/androidfw/ResourceTypes.cpp
+++ b/libs/androidfw/ResourceTypes.cpp
@@ -1597,9 +1597,9 @@
       out[0] = in[0];
       out[1] = in[1];
   } else {
-      uint8_t first = (in[0] - base) & 0x00ef;
-      uint8_t second = (in[1] - base) & 0x00ef;
-      uint8_t third = (in[2] - base) & 0x00ef;
+      uint8_t first = (in[0] - base) & 0x007f;
+      uint8_t second = (in[1] - base) & 0x007f;
+      uint8_t third = (in[2] - base) & 0x007f;
 
       out[0] = (0x80 | (third << 2) | (second >> 3));
       out[1] = ((second << 5) | first);
diff --git a/libs/androidfw/tests/ResourceTypes_test.cpp b/libs/androidfw/tests/ResourceTypes_test.cpp
index 4888b4a..139f868 100644
--- a/libs/androidfw/tests/ResourceTypes_test.cpp
+++ b/libs/androidfw/tests/ResourceTypes_test.cpp
@@ -75,6 +75,30 @@
      EXPECT_EQ(0, out[3]);
 }
 
+TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterLanguageAtOffset16) {
+     ResTable_config config;
+     config.packLanguage("tgp");
+
+     // We had a bug where we would accidentally mask
+     // the 5th bit of both bytes
+     //
+     // packed[0] = 1011 1100
+     // packed[1] = 1101 0011
+     //
+     // which is equivalent to:
+     // 1  [0]   [1]   [2]
+     // 1-01111-00110-10011
+     EXPECT_EQ(0xbc, config.language[0]);
+     EXPECT_EQ(0xd3, config.language[1]);
+
+     char out[4] = { 1, 1, 1, 1};
+     config.unpackLanguage(out);
+     EXPECT_EQ('t', out[0]);
+     EXPECT_EQ('g', out[1]);
+     EXPECT_EQ('p', out[2]);
+     EXPECT_EQ(0, out[3]);
+}
+
 TEST(ResourceTypesTest, ResourceConfig_packAndUnpack3LetterRegion) {
      ResTable_config config;
      config.packRegion("419");
diff --git a/media/java/android/media/MediaScanner.java b/media/java/android/media/MediaScanner.java
index 06a8f4c..4599bd6 100644
--- a/media/java/android/media/MediaScanner.java
+++ b/media/java/android/media/MediaScanner.java
@@ -1184,6 +1184,7 @@
         HashSet<String> existingFiles = new HashSet<String>();
         String directory = "/sdcard/DCIM/.thumbnails";
         String [] files = (new File(directory)).list();
+        Cursor c = null;
         if (files == null)
             files = new String[0];
 
@@ -1193,7 +1194,7 @@
         }
 
         try {
-            Cursor c = mMediaProvider.query(
+            c = mMediaProvider.query(
                     mPackageName,
                     mThumbsUri,
                     new String [] { "_data" },
@@ -1218,11 +1219,12 @@
             }
 
             Log.v(TAG, "/pruneDeadThumbnailFiles... " + c);
+        } catch (RemoteException e) {
+            // We will soon be killed...
+        } finally {
             if (c != null) {
                 c.close();
             }
-        } catch (RemoteException e) {
-            // We will soon be killed...
         }
     }
 
diff --git a/services/java/com/android/server/pm/SELinuxMMAC.java b/services/java/com/android/server/pm/SELinuxMMAC.java
index c78249b..81302b9 100644
--- a/services/java/com/android/server/pm/SELinuxMMAC.java
+++ b/services/java/com/android/server/pm/SELinuxMMAC.java
@@ -346,31 +346,21 @@
      */
     public static boolean assignSeinfoValue(PackageParser.Package pkg) {
 
-        /*
-         * Non system installed apps should be treated the same. This
-         * means that any post-loaded apk will be assigned the default
-         * tag, if one exists in the policy, else null, without respect
-         * to the signing key.
-         */
-        if (((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ||
-            ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)) {
+        // We just want one of the signatures to match.
+        for (Signature s : pkg.mSignatures) {
+            if (s == null)
+                continue;
 
-            // We just want one of the signatures to match.
-            for (Signature s : pkg.mSignatures) {
-                if (s == null)
-                    continue;
+            Policy policy = sSigSeinfo.get(s);
+            if (policy != null) {
+                String seinfo = policy.checkPolicy(pkg.packageName);
+                if (seinfo != null) {
+                    pkg.applicationInfo.seinfo = seinfo;
+                    if (DEBUG_POLICY_INSTALL)
+                        Slog.i(TAG, "package (" + pkg.packageName +
+                               ") labeled with seinfo=" + seinfo);
 
-                Policy policy = sSigSeinfo.get(s);
-                if (policy != null) {
-                    String seinfo = policy.checkPolicy(pkg.packageName);
-                    if (seinfo != null) {
-                        pkg.applicationInfo.seinfo = seinfo;
-                        if (DEBUG_POLICY_INSTALL)
-                            Slog.i(TAG, "package (" + pkg.packageName +
-                                   ") labeled with seinfo=" + seinfo);
-
-                        return true;
-                    }
+                    return true;
                 }
             }
         }