Merge "New API for "immersive" activity windows." into gingerbread
diff --git a/docs/html/guide/topics/resources/index.jd b/docs/html/guide/topics/resources/index.jd
index 2aa697e..84eac73 100644
--- a/docs/html/guide/topics/resources/index.jd
+++ b/docs/html/guide/topics/resources/index.jd
@@ -24,20 +24,21 @@
 resources also allows you to provide alternative resources that support specific device
 configurations such as different languages or screen sizes, which becomes increasingly
 important as more Android-powered devices become available with different configurations. In order
-to provide this functionality, you must organize resources in your project's {@code res/}
-directory, using various sub-directories that group resources by type and configuration.</p>
+to provide compatibility with different configurations, you must organize resources in your
+project's {@code res/} directory, using various sub-directories that group resources by type and
+configuration.</p>
 
 <div class="figure" style="width:421px">
 <img src="{@docRoot}images/resources/resource_devices_diagram1.png" height="137" alt="" />
 <p class="img-caption">
-<strong>Figure 1.</strong> Two device configurations, both using default
+<strong>Figure 1.</strong> Two different devices, both using default
 resources.</p>
 </div>
 
 <div class="figure" style="width:421px">
 <img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" />
 <p class="img-caption">
-<strong>Figure 2.</strong> Two device configurations, one using alternative
+<strong>Figure 2.</strong> Two different devices, one using alternative
 resources.</p>
 </div>
 
@@ -55,10 +56,10 @@
 <p>For example, while your default UI
 layout is saved in the {@code res/layout/} directory, you might specify a different UI layout to
 be used when the screen is in landscape orientation, by saving it in the {@code res/layout-land/}
-directory. The Android system will automatically apply the appropriate resources by matching the
+directory. Android automatically applies the appropriate resources by matching the
 device's current configuration to your resource directory names.</p>
 
-<p>Figure 1 demonstrates how a collection of default resources from an application will be applied
+<p>Figure 1 demonstrates how a collection of default resources from an application are applied
 to two different devices when there are no alternative resources available. Figure 2 shows
 the same application with a set of alternative resources that qualify for one of the device
 configurations, thus, the two devices uses different resources.</p>
diff --git a/docs/html/guide/topics/resources/providing-resources.jd b/docs/html/guide/topics/resources/providing-resources.jd
index 6d98385..66154cb 100644
--- a/docs/html/guide/topics/resources/providing-resources.jd
+++ b/docs/html/guide/topics/resources/providing-resources.jd
@@ -9,7 +9,8 @@
   <ul>
     <li>Different types of resources belong in different subdirectories of {@code res/}</li>
     <li>Alternative resources provide configuration-specific resource files</li>
-    <li>Always include default resources so your app does not depend on specific configurations</li>
+    <li>Always include default resources so your app does not depend on specific
+device configurations</li>
   </ul>
   <h2>In this document</h2>
   <ol>
@@ -20,7 +21,12 @@
         <li><a href="#AliasResources">Creating alias resources</a></li>
       </ol>
     </li>
-    <li><a href="#Compatibility">Providing the Best Device Compatibility with Resources</a></li>
+    <li><a href="#Compatibility">Providing the Best Device Compatibility with Resources</a>
+      <ol>
+        <li><a href="#ScreenCompatibility">Providing screen resource compatibility for Android
+1.5</a></li>
+      </ol>
+    </li>
     <li><a href="#BestMatch">How Android Finds the Best-matching Resource</a></li>
     <li><a href="#KnownIssues">Known Issues</a></li>
   </ol>
@@ -36,10 +42,11 @@
 </div>
 
 <p>You should always externalize application resources such as images and strings from your
-code, so that you can maintain them independently. You can also provide alternative resources for
-specific device configurations, by grouping them in specially-named resource directories. Android
-then applies the appropriate resource based on the current configuration. For
-instance, you might want to provide a different UI layout depending on the screen size.</p>
+code, so that you can maintain them independently. You should also provide alternative resources for
+specific device configurations, by grouping them in specially-named resource directories. At
+runtime, Android uses uses the appropriate resource based on the current configuration. For
+example, you might want to provide a different UI layout depending on the screen size or different
+strings depending on the language setting.</p>
 
 <p>Once you externalize your application resources, you can access them
 using resource IDs that are generated in your project's {@code R} class. How to use
@@ -185,7 +192,7 @@
 different layout resources that take advantage of the extra screen space. Or, if a device has a
 different language setting, then you should provide different string resources that translate the
 text in your user interface. To provide these different resources for different device
-configurations, you need to provide "alternative" resources, in addition to your default
+configurations, you need to provide alternative resources, in addition to your default
 resources.</p>
 
 
@@ -195,7 +202,7 @@
 <div class="figure" style="width:421px">
 <img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" />
 <p class="img-caption">
-<strong>Figure 1.</strong> Two device configurations, one using alternative resources.</p>
+<strong>Figure 1.</strong> Two different devices, one using alternative resources.</p>
 </div>
 
 <p>Almost every application should provide alternative resources to support specific device
@@ -211,10 +218,10 @@
     <ul>
       <li><em>{@code &lt;resources_name&gt;}</em> is the directory name of the corresponding default
 resources (defined in table 1).</li>
-      <li><em>{@code &lt;config_qualifier&gt;}</em> is a name that specifies a configuration
+      <li><em>{@code &lt;qualifier&gt;}</em> is a name that specifies an individual configuration
 for which these resources are to be used (defined in table 2).</li>
     </ul>
-    <p>You can append more than one <em>{@code &lt;config_qualifier&gt;}</em>. Separate each
+    <p>You can append more than one <em>{@code &lt;qualifier&gt;}</em>. Separate each
 one with a dash.</p>
   </li>
   <li>Save the respective alternative resources in this new directory. The resource files must be
@@ -247,13 +254,14 @@
 qualifiers for one resource directory, they must be added to the directory name in the order they
 are listed in the table.</p>
 
-<p class="note"><strong>Note:</strong> Some resource qualifiers were added after Android 1.0, so not
+<p class="note"><strong>Note:</strong> Some configuration qualifiers were added after Android 1.0,
+so not
 all versions of Android support all the qualifiers listed in table 2. New qualifiers
 indicate the version in which they were added. To avoid any issues, always include a set of default
 resources for resources that your application uses. For more information, see the section about <a
 href="#Compatibility">Providing the Best Device Compatibility with Resources</a>.</p>
 
-<p class="table-caption" id="table2"><strong>Table 2.</strong> Alternative resource qualifier
+<p class="table-caption" id="table2"><strong>Table 2.</strong> Configuration qualifier
 names.</p>
 <table>
     <tr>
@@ -349,8 +357,8 @@
 or large.</p>
       </td>
     </tr>
-    <tr id="ScreenLongQualifier">
-      <td>Wider/taller screens</td>
+    <tr id="ScreenAspectQualifier">
+      <td>Screen aspect</td>
       <td>
         <code>long</code><br/>
         <code>notlong</code>
@@ -602,7 +610,7 @@
 
 <h3 id="QualifierRules">Qualifier name rules</h3>
 
-<p>Here are some rules about using resource qualifier names:</p>
+<p>Here are some rules about using configuration qualifier names:</p>
 
 <ul>
     <li>You can specify multiple qualifiers for a single set of resources, separated by dashes. For
@@ -633,15 +641,19 @@
 these qualifiers, Android automatically applies the resources in your application based on the
 current device configuration. Each time a resource is requested, Android checks for alternative
 resource directories that contain the requested resource file, then <a href="#BestMatch">finds the
-best-matching resource</a> (discussed below).</p>
+best-matching resource</a> (discussed below). If there are no alternative resources that match
+a particular device configuration, then Android uses the corresponding default resources (the
+set of resources for a particular resource type that does not include a configuration
+qualifier).</p>
 
 
 
 <h3 id="AliasResources">Creating alias resources</h3>
 
 <p>When you have a resource that you'd like to use for more than one device
-configuration (but not for all configurations), you do not need to put the same resource in
-each alternative resource directory. Instead, you can (in some cases) create an alternative
+configuration (but do not want to provide as a default resource), you do not need to put the same
+resource in more than one alternative resource directory. Instead, you can (in some cases) create an
+alternative
 resource that acts as an alias for a resource saved in your default resource directory.</p>
 
 <p class="note"><strong>Note:</strong> Not all resources offer a mechanism by which you can
@@ -723,7 +735,6 @@
 
 
 
-
 <h2 id="Compatibility">Providing the Best Device Compatibility with Resources</h2>
 
 <p>In order for your application to support multiple device configurations, it's very important that
@@ -744,7 +755,7 @@
 
 <p>Providing default resources is important not only because your application might run on a
 configuration you had not anticipated, but also because new versions of Android sometimes add
-resource qualifiers that older versions do not support. If you use a new resource qualifier,
+configuration qualifiers that older versions do not support. If you use a new resource qualifier,
 but maintain code compatibility with older versions of Android, then when an older version of
 Android runs your application, it will crash if you do not provide default resources, because it
 cannot use the resources named with the new qualifier. For example, if your <a
@@ -755,9 +766,9 @@
 case, you probably want {@code notnight} to be your default resources, so you should exclude that
 qualifier so your drawable resources are in either {@code drawable/} or {@code drawable-night/}.</p>
 
-<p>The bottom line is: For every type of resource your application uses,
-provide a set of default resources that allow your application to perform well, then create
-variations of those resources for specific device configurations.</p>
+<p>So, in order to provide the best device compatibility, always provide default
+resources for the resources your application needs to perform properly. Then create alternative
+resources for specific device configurations using the configuration qualifiers.</p>
 
 <p>There is one exception to this rule: If your application's <a
 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is 4 or
@@ -765,12 +776,71 @@
 resources with the <a href="#DensityQualifier">screen density</a> qualifier. Even without default
 drawable resources, Android can find the best match among the alternative screen densities and scale
 the bitmaps as necessary. However, for the best experience on all types of devices, you should
-provide alternative drawables for all three types of density. (If your <a
+provide alternative drawables for all three types of density. If your <a
 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is
-<em>less than</em> 4, see the section below about <a href="#KnownIssues">known issues</a> for
-information about how to support multiple screen densities.)</p>
+<em>less than</em> 4 (Android 1.5 or lower), be aware that the screen size, density, and aspect
+qualifiers are not supported on Android 1.5 or lower, so you might need to perform additional
+compatibility for these versions.</p>
 
 
+<h3 id="ScreenCompatibility">Providing screen resource compatibility for Android 1.5</h3>
+
+<p>Android 1.5 (and lower) does not support the following resource qualifers:</p>
+<dl>
+  <dt><a href="#DensityQualifier">Density</a></dt>
+    <dd>{@code ldpi}, {@code mdpi}, {@code ldpi}, and {@code nodpi}</dd>
+  <dt><a href="#ScreenSizeQualifier">Screen size</a></dt>
+    <dd>{@code small}, {@code normal}, and {@code large}</dd>
+  <dt><a href="#ScreenAspectQualifier">Screen aspect</a></dt>
+    <dd>{@code long} and {@code notlong}</dd>
+</dl>
+
+<p>These resource qualifiers were introduced in Android 1.6, so Android 1.5 (API Level 3) and lower
+does not support them. If you use these configuration qualifiers and do not provide
+corresponding default resources, then an Android 1.5 device might use any one of the resource
+directories named with the above screen qualifiers, because it ignores the screen qualifiers and
+uses whichever otherwise-matching drawable resource it finds first.</p>
+
+<p>For example, if your application supports Android 1.5 and includes drawable resources for
+each density type ({@code drawable-ldpi/}, {@code drawable-mdpi/}, and {@code drawable-ldpi/}),
+and does <em>not</em> include default drawable resources ({@code drawable/}), then
+an Android 1.5 will use drawables from any one of the alternative resource directories, which
+can result in a user interface that's less than ideal.<p>
+
+<p>So, to provide compatibility with Android 1.5 (and lower) when using the screen configuration
+qualifiers:</p>
+<ol>
+  <li>Provide default resources that are for medium-density, normal, and notlong screens.
+
+    <p>Because all Android 1.5 devices have medium-density, normal, not-long screens, you can
+place these kinds of resources in the corresponding default resource directory. For example, put all
+medium density drawable resources in {@code drawable/} (instead of {@code drawable-mdpi/}),
+put {@code normal} size resources in the corresponding default resource directory, and {@code
+notlong} resources in the corresponding default resource directory.</p>
+  </li>
+
+  <li>Ensure that your <a href="{@docRoot}sdk/tools-notes.html">SDK Tools</a> version
+is r6 or greater.
+
+    <p>You need SDK Tools, Revision 6 (or greater), because it includes a new packaging tool that
+automatically applies an appropriate <a href="#VersionQualifier">version qualifier</a> to any
+resource directory named with a qualifier that does not exist in Android 1.0. For example, because
+the density qualifier was introduced in Android 1.6 (API Level 4), when the packaging tool
+encounters a resource directory using the density qualifier, it adds {@code v4} to the directory
+name to ensure that older versions do not use those resources (only API Level 4 and higher support
+that qualifier). Thus, by putting your medium-density resources in a directory <em>without</em> the
+{@code mdpi} qualifier, they are still accessible by Android 1.5, and any device that supports the
+density qualifer and has a medium-density screen also uses the default resources (which are mdpi)
+because they are the best match for the device (instead of using the {@code ldpi} or {@code hdpi}
+resources).</p>
+</li>
+</ol>
+
+<p class="note"><strong>Note:</strong> Later versions of Android, such as API Level 8,
+introduce other configuration qualifiers that older version do not support. To provide the best
+compatibility, you should always include a set of default resources for each type of resource
+that your application uses, as discussed above to provide the best device compatibility.</p>
+
 
 
 <h2 id="BestMatch">How Android Finds the Best-matching Resource</h2>
@@ -894,62 +964,7 @@
 
 <h2 id="KnownIssues">Known Issues</h2>
 
-<p>The following are known issues in terms of how Android finds the best-matching resource on
-certain versions of Android.</p>
-
-<h3>Android 1.5 (and lower)</h3>
-
-<h4>Density and screen size qualifiers are not supported</h4>
-
-<p>Android 1.5 (and lower) does not support the following resource qualifers:</p>
-<dl>
-  <dt><a href="#DensityQualifier">Density</a></dt>
-    <dd>{@code ldpi}, {@code mdpi}, {@code ldpi}, and {@code nodpi}</dd>
-  <dt><a href="#ScreenSizeQualifier">Screen size</a></dt>
-    <dd>{@code small}, {@code normal}, and {@code large}</dd>
-  <dt><a href="#ScreenLongQualifier">Screen length</a></dt>
-    <dd>{@code long} and {@code notlong}</dd>
-</dl>
-
-<p>These resource qualifiers were introduced in Android 1.6, so Android 1.5 (API Level 3) and lower
-does not support them. If your application supports Android 1.5 and includes drawable resources for
-each density type ({@code drawable-ldpi/}, {@code drawable-mdpi/}, and {@code drawable-ldpi/}), then
-an Android 1.5 device might use any one of them, because it doesn't support the density qualifier,
-will ignore it, and will use which ever otherwise-matching drawable resource it finds first. This
-caveat applies the same to screen size and screen length resources.<p>
-
-<p><b>The fix:</b> Ensure that your <a href="{@docRoot}sdk/tools-notes.html">SDK Tools</a> version
-is r6 or greater and provide default drawable resources. For example, to support Android 1.5
-while providing resources for all screen densities, include a set of drawable resources that does
-not use the screen density qualifier. In fact, because all Android 1.5 devices have a medium-density
-screen, you can remove the {@code mdpi} qualifer and put all medium density images in {@code
-drawable/} (instead of {@code drawable-mdpi/}). For the screen size, put your {@code normal} size
-resources in the default resource directory, and for the screen length, put your {@code notlong}
-resources in the default resource directory (because all Android 1.5 devices have medium-density,
-normal, not-long screens).</p>
-
-<p>You need SDK Tools, Revision 6 (or greater), because it includes a new packaging tool that
-automatically applies an appropriate <a href="#VersionQualifier">version qualifier</a> to any
-resource directory named with a qualifier that did not exist in Android 1.0. For example, because
-the density qualifier was introduced in Android 1.6 (API Level 4), when the packaging tool
-encounters a resource directory using the density qualifier, it adds {@code v4} to the directory
-name to ensure that older versions do not use those resources (only API Level 4 and higher support
-that qualifier). Thus, by putting your medium-density resources in a directory <em>without</em> the
-{@code mdpi} qualifier, they are still accessible by Android 1.5, and any device that supports the
-density qualifer and has a medium-density screen also uses these resources because they are the best
-match for the device (instead of using the {@code ldpi} or {@code hdpi} resources).</p>
-
-<p class="note"><strong>Note:</strong> Later versions of Android, such as API Level 8,
-introduce other resource qualifiers that older version do not support. To provide the best
-compatibility, you should always include a set of default resources for each type of resource
-that your application uses. See the section about <a href="#Compatibility">Providing the Best Device
-Compatibility with Resources</a> for more information.</p>
-
-
-
-<h3>Android 1.5 and 1.6</h3>
-
-<h4>Version qualifier performs exact match, instead of best match</h4>
+<h3>Android 1.5 and 1.6: Version qualifier performs exact match, instead of best match</h3>
 
 <p>The correct behavior is for the system to match resources marked with a <a
 href="#VersionQualifier">version qualifier</a> equal
@@ -957,8 +972,8 @@
 there is a bug that causes the system to match resources marked with the version qualifier
 only when it exactly matches the version on the device.</p>
 
-<p><b>The fix:</b> To provide version-specific resources, abide by this behavior. However, because
-this bug is fixed in versions of Android available after 1.6, if
+<p><b>The workaround:</b> To provide version-specific resources, abide by this behavior. However,
+because this bug is fixed in versions of Android available after 1.6, if
 you need to differentiate resources between Android 1.5, 1.6, and later versions, then you only need
 to apply the version qualifier to the 1.6 resources and one to match all later versions. Thus, this
 is effectively a non-issue.</p>
diff --git a/docs/html/resources/dashboard/platform-versions.jd b/docs/html/resources/dashboard/platform-versions.jd
index 5be732f..5e75105 100644
--- a/docs/html/resources/dashboard/platform-versions.jd
+++ b/docs/html/resources/dashboard/platform-versions.jd
@@ -50,7 +50,7 @@
 <div class="dashboard-panel">
 
 <img alt="" width="460" height="250"
-src="http://chart.apis.google.com/chart?&cht=p&chs=460x250&chd=t:0.1,24.7,25.1,0.1,0.3,49.8&chl=
+src="http://chart.apis.google.com/chart?&cht=p&chs=460x250&chd=t:0.1,24.6,25.0,0.1,0.3,50.0&chl=
 Android%201.1|Android%201.5|Android%201.6|Android%202.0|Android%202.0.1|Android%202.1&chco=c4df9b,
 6fad0c" />
 
@@ -60,12 +60,12 @@
   <th>Percent of Devices</th>
 </tr>
 <tr><td>Android 1.1</td><td>0.1%</td></tr> 
-<tr><td>Android 1.5</td><td>24.7%</td></tr> 
-<tr><td>Android 1.6</td><td>25.1%</td></tr> 
+<tr><td>Android 1.5</td><td>24.6%</td></tr> 
+<tr><td>Android 1.6</td><td>25.0%</td></tr> 
 <tr><td>Android 2.0</td><td>0.1%</td></tr> 
 <tr><td>Android 2.0.1</td><td>0.3%</td></tr> 
-<tr><td>Android 2.1</td><td>49.8%</td></tr> 
+<tr><td>Android 2.1</td><td>50.0%</td></tr> 
 </table>
-<p><em>Data collected during two weeks ending on June 15, 2010</em></p>
+<p><em>Data collected during two weeks ending on June 16, 2010</em></p>
 </div>
 
diff --git a/docs/html/resources/dashboard/screens.jd b/docs/html/resources/dashboard/screens.jd
index 7ab1530..f8130ea 100644
--- a/docs/html/resources/dashboard/screens.jd
+++ b/docs/html/resources/dashboard/screens.jd
@@ -49,7 +49,7 @@
 <div class="dashboard-panel">
 
 <img alt="" width="460" height="250"
-src="http://chart.apis.google.com/chart?&cht=p&chs=460x250&chd=t:1.1,58.0,40.8&chl=Small%20/%20ldpi|
+src="http://chart.apis.google.com/chart?&cht=p&chs=460x250&chd=t:1.1,57.8,41.0&chl=Small%20/%20ldpi|
 Normal%20/%20mdpi|Normal%20/%20hdpi&chco=c4df9b,6fad0c" />
 
 <table>
@@ -66,8 +66,8 @@
 </tr> 
 <tr><th scope="row">Normal</th> 
 <td></td> 
-<td class='cent hi'>58.0%</td> 
-<td class='cent hi'>40.8%</td> 
+<td class='cent hi'>57.8%</td> 
+<td class='cent hi'>41.0%</td> 
 </tr> 
 <tr><th scope="row">Large</th> 
 <td></td> 
@@ -76,6 +76,6 @@
 </tr> 
 </table>
 
-<p><em>Data collected during two weeks ending on June 15, 2010</em></p>
+<p><em>Data collected during two weeks ending on June 16, 2010</em></p>
 </div>
 
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index a7ccce4..d49c4e0 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -30,6 +30,7 @@
 #include <media/stagefright/MetaData.h>
 #include <media/stagefright/OMXClient.h>
 #include <media/stagefright/OMXCodec.h>
+#include <media/MediaProfiles.h>
 #include <camera/ICamera.h>
 #include <camera/Camera.h>
 #include <camera/CameraParameters.h>
@@ -440,7 +441,7 @@
         new AudioSource(
                 mAudioSource,
                 mSampleRate,
-                AudioSystem::CHANNEL_IN_MONO);
+                mAudioChannels);
 
     status_t err = audioSource->initCheck();
 
@@ -561,6 +562,74 @@
     return OK;
 }
 
+void StagefrightRecorder::clipVideoFrameRate() {
+    LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder);
+    int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.fps.min", mVideoEncoder);
+    int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.fps.max", mVideoEncoder);
+    if (mFrameRate < minFrameRate) {
+        LOGW("Intended video encoding frame rate (%d fps) is too small"
+             " and will be set to (%d fps)", mFrameRate, minFrameRate);
+        mFrameRate = minFrameRate;
+    } else if (mFrameRate > maxFrameRate) {
+        LOGW("Intended video encoding frame rate (%d fps) is too large"
+             " and will be set to (%d fps)", mFrameRate, maxFrameRate);
+        mFrameRate = maxFrameRate;
+    }
+}
+
+void StagefrightRecorder::clipVideoBitRate() {
+    LOGV("clipVideoBitRate: encoder %d", mVideoEncoder);
+    int minBitRate = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.bps.min", mVideoEncoder);
+    int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.bps.max", mVideoEncoder);
+    if (mVideoBitRate < minBitRate) {
+        LOGW("Intended video encoding bit rate (%d bps) is too small"
+             " and will be set to (%d bps)", mVideoBitRate, minBitRate);
+        mVideoBitRate = minBitRate;
+    } else if (mVideoBitRate > maxBitRate) {
+        LOGW("Intended video encoding bit rate (%d bps) is too large"
+             " and will be set to (%d bps)", mVideoBitRate, maxBitRate);
+        mVideoBitRate = maxBitRate;
+    }
+}
+
+void StagefrightRecorder::clipVideoFrameWidth() {
+    LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder);
+    int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.width.min", mVideoEncoder);
+    int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.width.max", mVideoEncoder);
+    if (mVideoWidth < minFrameWidth) {
+        LOGW("Intended video encoding frame width (%d) is too small"
+             " and will be set to (%d)", mVideoWidth, minFrameWidth);
+        mVideoWidth = minFrameWidth;
+    } else if (mVideoWidth > maxFrameWidth) {
+        LOGW("Intended video encoding frame width (%d) is too large"
+             " and will be set to (%d)", mVideoWidth, maxFrameWidth);
+        mVideoWidth = maxFrameWidth;
+    }
+}
+
+void StagefrightRecorder::clipVideoFrameHeight() {
+    LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder);
+    int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.height.min", mVideoEncoder);
+    int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
+                        "enc.vid.height.max", mVideoEncoder);
+    if (mVideoHeight < minFrameHeight) {
+        LOGW("Intended video encoding frame height (%d) is too small"
+             " and will be set to (%d)", mVideoHeight, minFrameHeight);
+        mVideoHeight = minFrameHeight;
+    } else if (mVideoHeight > maxFrameHeight) {
+        LOGW("Intended video encoding frame height (%d) is too large"
+             " and will be set to (%d)", mVideoHeight, maxFrameHeight);
+        mVideoHeight = maxFrameHeight;
+    }
+}
+
 status_t StagefrightRecorder::startMPEG4Recording() {
     mWriter = new MPEG4Writer(dup(mOutputFd));
 
@@ -587,6 +656,11 @@
     if (mVideoSource == VIDEO_SOURCE_DEFAULT
             || mVideoSource == VIDEO_SOURCE_CAMERA) {
 
+        clipVideoBitRate();
+        clipVideoFrameRate();
+        clipVideoFrameWidth();
+        clipVideoFrameHeight();
+
         int64_t token = IPCThreadState::self()->clearCallingIdentity();
         if (mCamera == 0) {
             mCamera = Camera::connect(0);
@@ -748,6 +822,7 @@
     mAudioBitRate  = 12200;
     mInterleaveDurationUs = 0;
     mIFramesInterval = 1;
+    mEncoderProfiles = MediaProfiles::getInstance();
 
     mOutputFd = -1;
     mFlags = 0;
diff --git a/media/libmediaplayerservice/StagefrightRecorder.h b/media/libmediaplayerservice/StagefrightRecorder.h
index baf33cf..7de96f6 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.h
+++ b/media/libmediaplayerservice/StagefrightRecorder.h
@@ -26,6 +26,7 @@
 class Camera;
 struct MediaSource;
 struct MediaWriter;
+class MediaProfiles;
 
 struct StagefrightRecorder : public MediaRecorderBase {
     StagefrightRecorder();
@@ -84,6 +85,8 @@
     int mOutputFd;
     int32_t mFlags;
 
+    MediaProfiles *mEncoderProfiles;
+
     status_t startMPEG4Recording();
     status_t startAMRRecording();
     status_t startAACRecording();
@@ -96,6 +99,10 @@
     status_t setParamInterleaveDuration(int32_t durationUs);
     status_t setParamIFramesInterval(int32_t interval);
     status_t setParamMaxDurationOrFileSize(int64_t limit, bool limit_is_duration);
+    void clipVideoBitRate();
+    void clipVideoFrameRate();
+    void clipVideoFrameWidth();
+    void clipVideoFrameHeight();
 
     StagefrightRecorder(const StagefrightRecorder &);
     StagefrightRecorder &operator=(const StagefrightRecorder &);
diff --git a/media/libstagefright/AudioSource.cpp b/media/libstagefright/AudioSource.cpp
index 9717aa6..d6020a6 100644
--- a/media/libstagefright/AudioSource.cpp
+++ b/media/libstagefright/AudioSource.cpp
@@ -33,15 +33,25 @@
 
 AudioSource::AudioSource(
         int inputSource, uint32_t sampleRate, uint32_t channels)
-    : mRecord(new AudioRecord(
-                inputSource, sampleRate, AudioSystem::PCM_16_BIT, channels)),
-      mInitCheck(mRecord->initCheck()),
-      mStarted(false),
+    : mStarted(false),
       mCollectStats(false),
       mTotalReadTimeUs(0),
       mTotalReadBytes(0),
       mTotalReads(0),
       mGroup(NULL) {
+
+    LOGV("sampleRate: %d, channels: %d", sampleRate, channels);
+    uint32_t flags = AudioRecord::RECORD_AGC_ENABLE |
+                     AudioRecord::RECORD_NS_ENABLE  |
+                     AudioRecord::RECORD_IIR_ENABLE;
+
+    mRecord = new AudioRecord(
+                inputSource, sampleRate, AudioSystem::PCM_16_BIT,
+                channels > 1? AudioSystem::CHANNEL_IN_STEREO: AudioSystem::CHANNEL_IN_MONO,
+                4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */
+                flags);
+
+    mInitCheck = mRecord->initCheck();
 }
 
 AudioSource::~AudioSource() {
diff --git a/media/libstagefright/TimedEventQueue.cpp b/media/libstagefright/TimedEventQueue.cpp
index 3de8c1d..0dacb53 100644
--- a/media/libstagefright/TimedEventQueue.cpp
+++ b/media/libstagefright/TimedEventQueue.cpp
@@ -22,11 +22,13 @@
 //#define LOG_NDEBUG 0
 #define LOG_TAG "TimedEventQueue"
 #include <utils/Log.h>
+#include <utils/threads.h>
 
 #include "include/TimedEventQueue.h"
 
 #include <sys/prctl.h>
 #include <sys/time.h>
+#include <sys/resource.h>
 
 #include <media/stagefright/MediaDebug.h>
 
@@ -206,6 +208,7 @@
     vm->AttachCurrentThread(&env, NULL);
 #endif
 
+    setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_FOREGROUND);
     static_cast<TimedEventQueue *>(me)->threadEntry();
 
 #ifdef ANDROID_SIMULATOR
diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp
index 1e8b395..83057b8 100644
--- a/tools/aapt/Command.cpp
+++ b/tools/aapt/Command.cpp
@@ -1000,7 +1000,7 @@
             }
 
             // Bluetooth-related compatibility logic
-            if (!specBluetoothFeature && hasBluetoothPermission) {
+            if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
                 // if app takes a Bluetooth permission but does not request the Bluetooth
                 // feature, we infer that it meant to
                 printf("uses-feature:'android.hardware.bluetooth'\n");