Merge "CameraITS: fix wrong fixed focus lens logic" into lmp-sprout-dev
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index 55b4f8d..1e5abd3 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -382,6 +382,7 @@
             <meta-data android:name="test_parent" android:value="com.android.cts.verifier.bluetooth.BleScannerTestActivity" />
         </activity>
 
+        <!-- Comment out until we have a better way to validate the hardware scan filter
         <activity android:name=".bluetooth.BleScannerHardwareScanFilterActivity"
                 android:label="@string/ble_scanner_scan_filter_name"
                 android:configChanges="keyboardHidden|orientation|screenSize">
@@ -392,6 +393,7 @@
             <meta-data android:name="test_category" android:value="@string/bt_le" />
             <meta-data android:name="test_parent" android:value="com.android.cts.verifier.bluetooth.BleScannerTestActivity" />
         </activity>
+        -->
 
         <activity android:name=".bluetooth.BleAdvertiserTestActivity"
                 android:label="@string/ble_advertiser_test_name"
@@ -417,6 +419,7 @@
             <meta-data android:name="test_parent" android:value="com.android.cts.verifier.bluetooth.BleAdvertiserTestActivity" />
         </activity>
 
+        <!-- Comment out until we have a better way to validate the hardware scan filter
         <activity android:name=".bluetooth.BleAdvertiserHardwareScanFilterActivity"
                 android:label="@string/ble_advertiser_scan_filter_name"
                 android:configChanges="keyboardHidden|orientation|screenSize">
@@ -427,6 +430,7 @@
             <meta-data android:name="test_category" android:value="@string/bt_le" />
             <meta-data android:name="test_parent" android:value="com.android.cts.verifier.bluetooth.BleAdvertiserTestActivity" />
         </activity>
+        -->
 
         <activity android:name=".suid.SuidFilesActivity"
                 android:label="@string/suid_files"
@@ -1111,11 +1115,7 @@
             </intent-filter>
             <meta-data android:name="test_category" android:value="@string/test_category_notifications" />
             <meta-data android:name="test_excluded_features"
-                    android:value="android.hardware.type.watch" />
-            <meta-data android:name="test_excluded_features"
-                    android:value="android.hardware.type.television" />
-            <meta-data android:name="test_excluded_features"
-                    android:value="android.software.leanback" />
+                       android:value="android.hardware.type.watch:android.software.leanback" />
         </activity>
 
         <service android:name=".notifications.MockListener"
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index a19bcec..9bff601 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -795,8 +795,8 @@
         No camera manager exists!  This test device is in a bad state.
     </string>
     <string name="all_legacy_devices">
-        All cameras on this device are LEGACY mode only - ITS tests will only be applied to LIMITED
-        or better devices.  \'PASS\' button enabled.
+        All cameras on this device are LEGACY mode only - ITS tests are only required on LIMITED
+        or better devices.  Pass.
     </string>
     <string name="its_test_passed">All Camera ITS tests passed.  Pass button enabled!</string>
     <string name="its_test_failed">Some Camera ITS tests failed.</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/camera/its/ItsTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/camera/its/ItsTestActivity.java
index 17df106..1fdd044 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/camera/its/ItsTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/camera/its/ItsTestActivity.java
@@ -138,6 +138,33 @@
         setContentView(R.layout.its_main);
         setInfoResources(R.string.camera_its_test, R.string.camera_its_test_info, -1);
         setPassFailButtonClickListeners();
+
+        // Hide the test if all camera devices are legacy
+        CameraManager manager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
+        try {
+            String[] cameraIds = manager.getCameraIdList();
+            mNonLegacyCameraIds = new ArrayList<String>();
+            boolean allCamerasAreLegacy = true;
+            for (String id : cameraIds) {
+                CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
+                if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
+                        != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
+                    mNonLegacyCameraIds.add(id);
+                    allCamerasAreLegacy = false;
+                }
+            }
+            if (allCamerasAreLegacy) {
+                showToast(R.string.all_legacy_devices);
+                ItsTestActivity.this.getReportLog().setSummary(
+                        "PASS: all cameras on this device are LEGACY"
+                        , 1.0, ResultType.NEUTRAL, ResultUnit.NONE);
+                setTestResultAndFinish(true);
+            }
+        } catch (CameraAccessException e) {
+            Toast.makeText(ItsTestActivity.this,
+                    "Received error from camera service while checking device capabilities: "
+                            + e, Toast.LENGTH_SHORT).show();
+        }
         getPassButton().setEnabled(false);
     }
 
@@ -148,27 +175,6 @@
         if (manager == null) {
             showToast(R.string.no_camera_manager);
         } else {
-            try {
-                String[] cameraIds = manager.getCameraIdList();
-                mNonLegacyCameraIds = new ArrayList<String>();
-                boolean allCamerasAreLegacy = true;
-                for (String id : cameraIds) {
-                    CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
-                    if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
-                            != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
-                        mNonLegacyCameraIds.add(id);
-                        allCamerasAreLegacy = false;
-                    }
-                }
-                if (allCamerasAreLegacy) {
-                    showToast(R.string.all_legacy_devices);
-                    getPassButton().setEnabled(true);
-                }
-            } catch (CameraAccessException e) {
-                Toast.makeText(ItsTestActivity.this,
-                        "Received error from camera service while checking device capabilities: "
-                                + e, Toast.LENGTH_SHORT).show();
-            }
             Log.d(TAG, "register ITS result receiver");
             IntentFilter filter = new IntentFilter(ACTION_ITS_RESULT);
             registerReceiver(mSuccessReceiver, filter);
diff --git a/tests/expectations/knownfailures.txt b/tests/expectations/knownfailures.txt
index 48e5194..3abb1f7 100644
--- a/tests/expectations/knownfailures.txt
+++ b/tests/expectations/knownfailures.txt
@@ -1,5 +1,12 @@
 [
 {
+  description: "testWindowContentFrameStats is flaky without 75b55d0846159543aafc1b7420915497fce9b3f1",
+  names: [
+    "android.app.uiautomation.cts.UiAutomationTest#testWindowContentFrameStats"
+  ],
+  bug: 18039218
+},
+{
   description: "the UsageStats is not yet stable enough",
   names: [
     "android.app.usage.cts.UsageStatsTest"
diff --git a/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java b/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
index d0255f3..1f61b27 100644
--- a/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
+++ b/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
@@ -21,7 +21,11 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.ResolveInfo;
+import android.os.Build;
 import android.os.UserManager;
 import android.provider.Settings;
 import android.test.AndroidTestCase;
@@ -44,6 +48,8 @@
     private ComponentName mComponent;
     private ComponentName mSecondComponent;
     private boolean mDeviceAdmin;
+    private boolean mManagedProfiles;
+    private PackageManager mPackageManager;
 
     private static final String TEST_CA_STRING1 =
             "-----BEGIN CERTIFICATE-----\n" +
@@ -68,9 +74,11 @@
         mDevicePolicyManager = (DevicePolicyManager)
                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
         mComponent = DeviceAdminInfoTest.getReceiverComponent();
+        mPackageManager = mContext.getPackageManager();
         mSecondComponent = DeviceAdminInfoTest.getSecondReceiverComponent();
-        mDeviceAdmin =
-                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
+        mDeviceAdmin = mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
+        mManagedProfiles = mDeviceAdmin
+                && mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS);
         setBlankPassword();
     }
 
@@ -894,6 +902,32 @@
         }
     }
 
+    /**
+     * Test whether the version of the pre-installed launcher is at least L. This is needed for
+     * managed profile support.
+     */
+    public void testLauncherVersionAtLeastL() throws Exception {
+        if (!mManagedProfiles) {
+            return;
+        }
+
+        Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.addCategory(Intent.CATEGORY_HOME);
+        List<ResolveInfo> resolveInfos = mPackageManager.queryIntentActivities(intent,
+                0 /* default flags */);
+        assertFalse("No launcher present", resolveInfos.isEmpty());
+
+        for (ResolveInfo resolveInfo : resolveInfos) {
+            ApplicationInfo launcherAppInfo = mPackageManager.getApplicationInfo(
+                    resolveInfo.activityInfo.packageName, 0 /* default flags */);
+            if ((launcherAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 &&
+                    launcherAppInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
+                return;
+            }
+        }
+        fail("No system launcher with version L+ present present on device.");
+    }
+
     private void assertDeviceOwnerMessage(String message) {
         assertTrue("message is: "+ message, message.contains("does not own the device")
                 || message.contains("can only be called by the device owner"));
diff --git a/tests/tests/admin/src/android/admin/cts/NfcProvisioningSetupWizardConfigTest.java b/tests/tests/admin/src/android/admin/cts/NfcProvisioningSetupWizardConfigTest.java
deleted file mode 100644
index 668c8c0..0000000
--- a/tests/tests/admin/src/android/admin/cts/NfcProvisioningSetupWizardConfigTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.admin.cts;
-
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.res.Resources;
-import android.test.AndroidTestCase;
-
-/**
- * Test whether the resources of com.android.nfc specify that managed provisioning intents can be
- * received in the setup wizard. See go/android-enterprise-oemchecklist.
- */
-public class NfcProvisioningSetupWizardConfigTest extends AndroidTestCase {
-
-    private static final String NFC_PACKAGE_NAME = "com.android.nfc";
-    private static final String MANAGED_PROVISIONING_PACKAGE_NAME =
-            "com.android.managedprovisioning";
-
-    private static final String PROVISIONING_MIME_TYPES = "provisioning_mime_types";
-    private static final String ENABLE_NFC_PROVISIONING = "enable_nfc_provisioning";
-
-    private static final String REQUIRED_MIME_TYPE = "application/com.android.managedprovisioning";
-
-    private boolean mHasFeature;
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        mHasFeature = isPackageInstalledOnSystemImage(NFC_PACKAGE_NAME)
-                && getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
-                && isPackageInstalledOnSystemImage(MANAGED_PROVISIONING_PACKAGE_NAME)
-                && getContext().getPackageManager().hasSystemFeature(
-                        PackageManager.FEATURE_DEVICE_ADMIN);
-    }
-
-    public void testNfcEnabledDuringSetupWizard() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-
-        assertTrue("Boolean " + ENABLE_NFC_PROVISIONING + " must be true in resources of "
-                + NFC_PACKAGE_NAME, getBooleanByName(ENABLE_NFC_PROVISIONING));
-    }
-
-    public void testManagedProvisioningMimeTypeAccepted() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-
-        String[] provisioningMimeTypes = getStringArrayByName(PROVISIONING_MIME_TYPES);
-        for (String mimeType : provisioningMimeTypes) {
-            if (mimeType.equals(REQUIRED_MIME_TYPE)) {
-                return;
-            }
-        }
-
-        fail("Mime type " + REQUIRED_MIME_TYPE + " was not present in the list "
-                + PROVISIONING_MIME_TYPES + " in resources of " + NFC_PACKAGE_NAME);
-    }
-
-    private String[] getStringArrayByName(String name) throws Exception {
-        Resources resources = getNfcResources();
-        int arrayId = resources.getIdentifier(name, "array", NFC_PACKAGE_NAME);
-        return resources.getStringArray(arrayId);
-    }
-
-    private boolean getBooleanByName(String name) throws Exception {
-        Resources resources = getNfcResources();
-        int arrayId = resources.getIdentifier(name, "bool", NFC_PACKAGE_NAME);
-        return resources.getBoolean(arrayId);
-    }
-
-    private Resources getNfcResources() throws Exception {
-        return getContext().getPackageManager().getResourcesForApplication(NFC_PACKAGE_NAME);
-    }
-
-    private boolean isPackageInstalledOnSystemImage(String packagename) {
-        try {
-            ApplicationInfo info = getContext().getPackageManager().getApplicationInfo(packagename,
-                    0 /* default flags */);
-            return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
-        } catch (NameNotFoundException e) {
-            return false;
-        }
-    }
-}
diff --git a/tests/tests/hardware/AndroidManifest.xml b/tests/tests/hardware/AndroidManifest.xml
index 1a02d0a..ab81162 100644
--- a/tests/tests/hardware/AndroidManifest.xml
+++ b/tests/tests/hardware/AndroidManifest.xml
@@ -24,6 +24,7 @@
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.BODY_SENSORS" />
+    <uses-permission android:name="android.permission.TRANSMIT_IR" />
 
     <application>
         <uses-library android:name="android.test.runner" />
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/StillCaptureTest.java b/tests/tests/hardware/src/android/hardware/camera2/cts/StillCaptureTest.java
index e816659..37eff10 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/StillCaptureTest.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/StillCaptureTest.java
@@ -440,6 +440,10 @@
         waitForNumResults(resultListener, NUM_FRAMES_WAITED);
 
         stopPreview();
+
+        // Free image resources
+        image.close();
+        closeImageReader();
         return;
     }
 
@@ -609,6 +613,9 @@
         Image image = imageListener.getImage(CAPTURE_IMAGE_TIMEOUT_MS);
         validateJpegCapture(image, maxStillSz);
 
+        // Free image resources
+        image.close();
+
         stopPreview();
     }
 
@@ -654,6 +661,10 @@
                 mSession.capture(stillRequest.build(), resultListener, mHandler);
                 Image image = imageListener.getImage(CAPTURE_IMAGE_TIMEOUT_MS);
                 validateJpegCapture(image, stillSz);
+
+                // Free image resources
+                image.close();
+
                 // stopPreview must be called here to make sure next time a preview stream
                 // is created with new size.
                 stopPreview();
@@ -704,6 +715,9 @@
             dumpFile(rawFileName, rawBuffer);
         }
 
+        // Free image resources
+        image.close();
+
         stopPreview();
     }
 
@@ -1021,6 +1035,9 @@
             if (!mStaticInfo.isHardwareLevelLegacy()) {
                 jpegTestExifExtraTags(exif, maxStillSz, stillResult);
             }
+
+            // Free image resources
+            image.close();
         }
     }
 
diff --git a/tests/tests/text/src/android/text/format/cts/TimeTest.java b/tests/tests/text/src/android/text/format/cts/TimeTest.java
index 2d623e3..cc73272 100644
--- a/tests/tests/text/src/android/text/format/cts/TimeTest.java
+++ b/tests/tests/text/src/android/text/format/cts/TimeTest.java
@@ -36,16 +36,20 @@
 
     private Locale originalLocale;
 
+    private static List<Locale> sSystemLocales;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         originalLocale = Locale.getDefault();
+
+        maybeInitializeSystemLocales();
     }
 
     @Override
     public void tearDown() throws Exception {
         // The Locale may be changed by tests. Revert to the original.
-        changeJavaAndAndroidLocale(originalLocale);
+        changeJavaAndAndroidLocale(originalLocale, true /* force */);
         super.tearDown();
     }
 
@@ -771,7 +775,10 @@
     }
 
     public void testFormat_tokensUkLocale() throws Exception {
-        changeJavaAndAndroidLocale(Locale.UK);
+        if (!changeJavaAndAndroidLocale(Locale.UK, false /* force */)) {
+            Log.w(TAG, "Skipping testFormat_tokensUkLocale: no assets found");
+            return;
+        }
 
         Time t = new Time("Europe/London");
         Fields.setDateTime(t, 2005, 5, 1, 12, 30, 15);
@@ -863,7 +870,10 @@
     }
 
     public void testFormat_tokensUsLocale() throws Exception {
-        changeJavaAndAndroidLocale(Locale.US);
+        if (!changeJavaAndAndroidLocale(Locale.US, false /* force */)) {
+            Log.w(TAG, "Skipping testFormat_tokensUSLocale: no assets found");
+            return;
+        }
 
         Time t = new Time("America/New_York");
         Fields.setDateTime(t, 2005, 5, 1, 12, 30, 15);
@@ -955,7 +965,10 @@
     }
 
     public void testFormat_tokensFranceLocale() throws Exception {
-        changeJavaAndAndroidLocale(Locale.FRANCE);
+        if (!changeJavaAndAndroidLocale(Locale.FRANCE, false /* force */)) {
+            Log.w(TAG, "Skipping testFormat_tokensFranceLocale: no assets found");
+            return;
+        }
 
         Time t = new Time("Europe/Paris");
         Fields.setDateTime(t, 2005, 5, 1, 12, 30, 15);
@@ -1047,7 +1060,10 @@
     }
 
     public void testFormat_tokensJapanLocale() throws Exception {
-        changeJavaAndAndroidLocale(Locale.JAPAN);
+        if (!changeJavaAndAndroidLocale(Locale.JAPAN, false /* force */)) {
+            Log.w(TAG, "Skipping testFormat_tokensJapanLocale: no assets found");
+            return;
+        }
 
         Time t = new Time("Asia/Tokyo");
         Fields.setDateTime(t, 2005, 5, 1, 12, 30, 15);
@@ -2843,7 +2859,19 @@
         }
     }
 
-    private static void changeJavaAndAndroidLocale(Locale locale) {
+    private static void maybeInitializeSystemLocales() {
+        if (sSystemLocales == null) {
+            String[] locales = Resources.getSystem().getAssets().getLocales();
+            List<Locale> systemLocales = new ArrayList<Locale>(locales.length);
+            for (String localeStr : locales) {
+                systemLocales.add(Locale.forLanguageTag(localeStr));
+            }
+
+            sSystemLocales = systemLocales;
+        }
+    }
+
+    private static boolean changeJavaAndAndroidLocale(Locale locale, boolean force) {
         // The Time class uses the Android-managed locale for string resources containing format
         // patterns and the Java-managed locale for other things (e.g. month names, week days names)
         // that are placed into those patterns. For example the format "%c" expands to include
@@ -2856,6 +2884,10 @@
         // locales agree), when the Java-managed locale is changed during this test the locale in
         // the runtime-local copy of the system resources is modified as well.
 
+        if (!force && !sSystemLocales.contains(locale)) {
+            return false;
+        }
+
         // Change the Java-managed locale.
         Locale.setDefault(locale);
 
@@ -2864,5 +2896,6 @@
         Configuration configuration = Resources.getSystem().getConfiguration();
         configuration.locale = locale;
         Resources.getSystem().updateConfiguration(configuration, null);
+        return true;
     }
 }