Merge "Camera: Fix an incorrect frame duration check" into qt-dev
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
index 4368e87..8601d35 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
@@ -1065,7 +1065,8 @@
             // puts the device to sleep, but kept around for clarity.
             getInstrumentation().getUiAutomation().performGlobalAction(
                     AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN);
-            if (mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.SYSTEM.getIdentifier())) {
+            if (mAmbientDisplayConfiguration.alwaysOnEnabled(
+                    android.os.Process.myUserHandle().getIdentifier())) {
                 mAmWmState.waitForAodShowing();
             } else {
                 for (int retry = 1; isDisplayOn(DEFAULT_DISPLAY) && retry <= 5; retry++) {
diff --git a/tests/tests/location/src/android/location/cts/psedorange/PseudorangePositionVelocityFromRealTimeEvents.java b/tests/tests/location/src/android/location/cts/psedorange/PseudorangePositionVelocityFromRealTimeEvents.java
index b042856..3bf39d8 100644
--- a/tests/tests/location/src/android/location/cts/psedorange/PseudorangePositionVelocityFromRealTimeEvents.java
+++ b/tests/tests/location/src/android/location/cts/psedorange/PseudorangePositionVelocityFromRealTimeEvents.java
@@ -19,14 +19,12 @@
 import android.location.GnssClock;
 import android.location.GnssMeasurement;
 import android.location.GnssMeasurementsEvent;
-import android.location.GnssNavigationMessage;
 import android.location.GnssStatus;
 import android.util.Log;
 import android.location.cts.pseudorange.Ecef2EnuConverter.EnuValues;
 import android.location.cts.pseudorange.Ecef2LlaConverter.GeodeticLlaValues;
 import android.location.cts.nano.Ephemeris.GpsEphemerisProto;
 import android.location.cts.nano.Ephemeris.GpsNavMessageProto;
-import android.location.cts.pseudorange.GpsTime;
 import android.location.cts.suplClient.SuplRrlpController;
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -57,6 +55,9 @@
   private static final String SUPL_SERVER_NAME = "supl.google.com";
   private static final int SUPL_SERVER_PORT = 7276;
 
+  private static final double GPS_L5_FREQ_HZ_LOWER_BOUND = 1.164e9;
+  private static final double GPS_L5_FREQ_HZ_UPPER_BOUND = 1.189e9;
+
   private final double[] mPositionSolutionLatLngDeg = {Double.NaN, Double.NaN, Double.NaN};
   private final double[] mVelocitySolutionEnuMps = {Double.NaN, Double.NaN, Double.NaN};
   private final double[] mPositionVelocityUncertaintyEnu = {
@@ -106,6 +107,11 @@
       if (measurement.getConstellationType() != GnssStatus.CONSTELLATION_GPS) {
           continue;
         }
+
+      if (isGpsL5FrequencyHz(measurement.getCarrierFrequencyHz())) {
+        continue;
+      }
+
         // ignore raw data if time is zero, if signal to noise ratio is below threshold or if
         // TOW is not yet decoded
         if (measurement.getCn0DbHz() >= C_TO_N0_THRESHOLD_DB_HZ
@@ -263,6 +269,11 @@
     }
   }
 
+  private static boolean isGpsL5FrequencyHz(float carrierFrequencyHz) {
+    return carrierFrequencyHz >= GPS_L5_FREQ_HZ_LOWER_BOUND
+            && carrierFrequencyHz <= GPS_L5_FREQ_HZ_UPPER_BOUND;
+  }
+
   private boolean isEmptyNavMessage(GpsNavMessageProto navMessageProto) {
     if(navMessageProto.iono == null)return true;
     if(navMessageProto.ephemerids.length ==0)return true;
diff --git a/tests/tests/permission/Android.mk b/tests/tests/permission/Android.mk
index 9d02bfb..aeaee7a 100644
--- a/tests/tests/permission/Android.mk
+++ b/tests/tests/permission/Android.mk
@@ -34,7 +34,8 @@
     compatibility-device-util-axt \
     truth-prebuilt \
     androidx.annotation_annotation \
-    platformprotosnano
+    platformprotosnano \
+    permission-test-util-lib
 
 LOCAL_JNI_SHARED_LIBRARIES := libctspermission_jni libnativehelper_compat_libc++
 
diff --git a/tests/tests/permission/permissionTestUtilLib/Android.bp b/tests/tests/permission/permissionTestUtilLib/Android.bp
new file mode 100644
index 0000000..e593998
--- /dev/null
+++ b/tests/tests/permission/permissionTestUtilLib/Android.bp
@@ -0,0 +1,27 @@
+// Copyright (C) 2019 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.
+
+java_library {
+    name: "permission-test-util-lib",
+
+    srcs: ["src/**/*.java"],
+
+    static_libs: [
+        "ub-uiautomator",
+        "compatibility-device-util-axt",
+        "androidx.test.ext.junit-nodeps"
+   ],
+
+    sdk_version: "test_current",
+}
diff --git a/tests/tests/permission/src/android/permission/cts/PermissionUtils.java b/tests/tests/permission/permissionTestUtilLib/src/android/permission/cts/PermissionUtils.java
similarity index 84%
rename from tests/tests/permission/src/android/permission/cts/PermissionUtils.java
rename to tests/tests/permission/permissionTestUtilLib/src/android/permission/cts/PermissionUtils.java
index 7e0d441..74c76a8 100644
--- a/tests/tests/permission/src/android/permission/cts/PermissionUtils.java
+++ b/tests/tests/permission/permissionTestUtilLib/src/android/permission/cts/PermissionUtils.java
@@ -39,29 +39,36 @@
 import android.app.UiAutomation;
 import android.content.Context;
 import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
 import android.content.pm.PermissionInfo;
 import android.os.Process;
 import android.os.UserHandle;
 
 import androidx.annotation.NonNull;
-import androidx.test.InstrumentationRegistry;
+import androidx.test.platform.app.InstrumentationRegistry;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+/**
+ * Common utils for permission tests
+ */
 public class PermissionUtils {
-    private static long TIMEOUT_MILLIS = 10000;
+    private static final long TIMEOUT_MILLIS = 10000;
 
-    private static int TESTED_FLAGS = FLAG_PERMISSION_USER_SET | FLAG_PERMISSION_USER_FIXED
+    private static final int TESTED_FLAGS = FLAG_PERMISSION_USER_SET | FLAG_PERMISSION_USER_FIXED
             | FLAG_PERMISSION_REVOKE_ON_UPGRADE | FLAG_PERMISSION_REVIEW_REQUIRED
             | FLAG_PERMISSION_REVOKE_WHEN_REQUESTED;
 
-    private static final Context sContext = InstrumentationRegistry.getTargetContext();
+    private static final Context sContext =
+            InstrumentationRegistry.getInstrumentation().getTargetContext();
     private static final UiAutomation sUiAutomation =
             InstrumentationRegistry.getInstrumentation().getUiAutomation();
 
+    private PermissionUtils() {
+        // this class should never be instantiated
+    }
+
     /**
      * Get the state of an app-op.
      *
@@ -70,7 +77,8 @@
      *
      * @return The mode the op is on
      */
-    static int getAppOp(@NonNull String packageName, @NonNull String permission) throws Exception {
+    public static int getAppOp(@NonNull String packageName, @NonNull String permission)
+            throws Exception {
         return callWithShellPermissionIdentity(
                 () -> sContext.getSystemService(AppOpsManager.class).unsafeCheckOpRaw(
                         permissionToOp(permission),
@@ -82,7 +90,7 @@
      *
      * @param apkFile The apk to install
      */
-    static void install(@NonNull String apkFile) {
+    public static void install(@NonNull String apkFile) {
         runShellCommand("pm install -r --force-sdk " + apkFile);
     }
 
@@ -91,7 +99,7 @@
      *
      * @param packageName Name of package to be uninstalled
      */
-    static void uninstallApp(@NonNull String packageName) {
+    public static void uninstallApp(@NonNull String packageName) {
         runShellCommand("pm uninstall " + packageName);
     }
 
@@ -102,7 +110,7 @@
      * @param permission The permission the app-op belongs to
      * @param mode The new mode
      */
-    static void setAppOp(@NonNull String packageName, @NonNull String permission, int mode) {
+    public static void setAppOp(@NonNull String packageName, @NonNull String permission, int mode) {
         setAppOpByName(packageName, permissionToOp(permission), mode);
     }
 
@@ -113,7 +121,7 @@
      * @param op The name of the op
      * @param mode The new mode
      */
-    static void setAppOpByName(@NonNull String packageName, @NonNull String op, int mode) {
+    public static void setAppOpByName(@NonNull String packageName, @NonNull String op, int mode) {
         runWithShellPermissionIdentity(
                 () -> sContext.getSystemService(AppOpsManager.class).setUidMode(op,
                         sContext.getPackageManager().getPackageUid(packageName, 0), mode));
@@ -129,8 +137,8 @@
      *
      * @return {@code true} iff the permission is granted
      */
-    static boolean isPermissionGranted(@NonNull String packageName, @NonNull String permission)
-            throws Exception {
+    public static boolean isPermissionGranted(@NonNull String packageName,
+            @NonNull String permission) throws Exception {
         return sContext.checkPermission(permission, Process.myPid(),
                 sContext.getPackageManager().getPackageUid(packageName, 0))
                 == PERMISSION_GRANTED;
@@ -148,7 +156,7 @@
      *
      * @return {@code true} iff the permission is granted
      */
-    static boolean isGranted(@NonNull String packageName, @NonNull String permission)
+    public static boolean isGranted(@NonNull String packageName, @NonNull String permission)
             throws Exception {
         if (!isPermissionGranted(packageName, permission)) {
             return false;
@@ -173,7 +181,7 @@
      * @param packageName The app that should have the permission granted
      * @param permission The permission to grant
      */
-    static void grantPermission(@NonNull String packageName, @NonNull String permission)
+    public static void grantPermission(@NonNull String packageName, @NonNull String permission)
             throws Exception {
         sUiAutomation.grantRuntimePermission(packageName, permission);
 
@@ -207,7 +215,7 @@
      * @param packageName The app that should have the permission revoked
      * @param permission The permission to revoke
      */
-    static void revokePermission(@NonNull String packageName, @NonNull String permission)
+    public static void revokePermission(@NonNull String packageName, @NonNull String permission)
             throws Exception {
         sUiAutomation.revokeRuntimePermission(packageName, permission);
 
@@ -227,7 +235,7 @@
      *
      * @param packageName Package to clear
      */
-    static void clearAppState(@NonNull String packageName) {
+    public static void clearAppState(@NonNull String packageName) {
         runShellCommand("pm clear " + packageName);
     }
 
@@ -239,7 +247,7 @@
      *
      * @return Permission flags
      */
-    static int getPermissionFlags(@NonNull String packageName, @NonNull String permission)  {
+    public static int getPermissionFlags(@NonNull String packageName, @NonNull String permission) {
         try {
             return callWithShellPermissionIdentity(
                     () -> sContext.getPackageManager().getPermissionFlags(permission, packageName,
@@ -257,7 +265,7 @@
      * @param mask Mask of permissions to set
      * @param flags Permissions to set
      */
-    static void setPermissionFlags(@NonNull String packageName, @NonNull String permission,
+    public static void setPermissionFlags(@NonNull String packageName, @NonNull String permission,
             int mask, int flags) {
         runWithShellPermissionIdentity(
                 () -> sContext.getPackageManager().updatePermissionFlags(permission, packageName,
@@ -286,7 +294,7 @@
      *
      * @return The runtime permissions requested by the app
      */
-    static @NonNull List<String> getRuntimePermissions(@NonNull String packageName)
+    public static @NonNull List<String> getRuntimePermissions(@NonNull String packageName)
             throws Exception {
         ArrayList<String> runtimePermissions = new ArrayList<>();
 
@@ -300,13 +308,26 @@
         return runtimePermissions;
     }
 
+    public interface ThrowingRunnable extends Runnable {
+        void runOrThrow() throws Exception;
+
+        @Override
+        default void run() {
+            try {
+                runOrThrow();
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        }
+    }
+
     /**
      * Make sure that a {@link Runnable} eventually finishes without throwing a {@link
      * Exception}.
      *
      * @param r The {@link Runnable} to run.
      */
-    public static void eventually(@NonNull Runnable r) {
+    public static void eventually(@NonNull ThrowingRunnable r) {
         long start = System.currentTimeMillis();
 
         while (true) {
diff --git a/tests/tests/permission2/Android.bp b/tests/tests/permission2/Android.bp
index 2ad4e7b..ec9366b 100644
--- a/tests/tests/permission2/Android.bp
+++ b/tests/tests/permission2/Android.bp
@@ -34,7 +34,8 @@
         "ctstestrunner-axt",
         "guava",
         "androidx.test.ext.junit-nodeps",
-        "truth-prebuilt"
+        "truth-prebuilt",
+        "permission-test-util-lib"
     ],
 
     srcs: ["src/**/*.java"],
diff --git a/tests/tests/permission2/src/android/permission2/cts/RestrictedPermissionsTest.java b/tests/tests/permission2/src/android/permission2/cts/RestrictedPermissionsTest.java
index 586c74c..1a9295d 100644
--- a/tests/tests/permission2/src/android/permission2/cts/RestrictedPermissionsTest.java
+++ b/tests/tests/permission2/src/android/permission2/cts/RestrictedPermissionsTest.java
@@ -16,7 +16,8 @@
 
 package android.permission2.cts;
 
-import static android.permission2.cts.Utils.eventually;
+import static android.permission.cts.PermissionUtils.eventually;
+import static android.permission.cts.PermissionUtils.isGranted;
 
 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
 
@@ -33,6 +34,7 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PermissionInfo;
+import android.permission.cts.PermissionUtils.ThrowingRunnable;
 import android.platform.test.annotations.AppModeFull;
 import android.util.ArraySet;
 
@@ -209,16 +211,6 @@
 
     @Test
     @AppModeFull
-    public void testStorageTargetingSdk28DefaultNotWhitelistedHasIsolatedAccess() throws Exception {
-        // Install with no whitelisted permissions.
-        installApp(APK_USES_STORAGE_DEFAULT_28, Collections.emptySet());
-
-        // Check expected storage mode
-        assertHasIsolatedStorageAccess();
-    }
-
-    @Test
-    @AppModeFull
     public void testStorageTargetingSdk28OptInWhitelistedHasIsolatedAccess() throws Exception {
         // Install with whitelisted permissions.
         installApp(APK_USES_STORAGE_OPT_IN_28, null /*whitelistedPermissions*/);
@@ -229,16 +221,6 @@
 
     @Test
     @AppModeFull
-    public void testStorageTargetingSdk28OptInNotWhitelistedHasIsolatedAccess() throws Exception {
-        // Install with whitelisted permissions.
-        installApp(APK_USES_STORAGE_OPT_IN_28, null /*whitelistedPermissions*/);
-
-        // Check expected storage mode
-        assertHasIsolatedStorageAccess();
-    }
-
-    @Test
-    @AppModeFull
     public void testStorageTargetingSdk29DefaultWhitelistedHasIsolatedAccess() throws Exception {
         // Install with whitelisted permissions.
         installApp(APK_USES_STORAGE_DEFAULT_29, Collections.emptySet());
@@ -369,6 +351,86 @@
         assertCannotWhitelistStorage();
     }
 
+    @Test
+    @AppModeFull
+    public void cannotGrantStorageTargetingSdk28NotWhitelisted() throws Exception {
+        // Install with no whitelisted permissions.
+        installApp(APK_USES_STORAGE_DEFAULT_28, Collections.emptySet(), Collections.emptySet());
+
+        // Could not grant permission as targetSDK<29 and not whitelisted
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isFalse();
+    }
+
+    @Test
+    @AppModeFull
+    public void cannotGrantStorageTargetingSdk28OptInNotWhitelisted() throws Exception {
+        // Install with no whitelisted permissions.
+        installApp(APK_USES_STORAGE_OPT_IN_28, Collections.emptySet(), Collections.emptySet());
+
+        // Could not grant permission as targetSDK<29 and not whitelisted
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isFalse();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk28Whitelisted() throws Exception {
+        // Install with whitelisted permissions.
+        installApp(APK_USES_STORAGE_DEFAULT_28, null, Collections.emptySet());
+
+        // Could grant permission
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk28OptInWhitelisted() throws Exception {
+        // Install with whitelisted permissions.
+        installApp(APK_USES_STORAGE_OPT_IN_28, null, Collections.emptySet());
+
+        // Could grant permission
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk29NotWhitelisted() throws Exception {
+        // Install with no whitelisted permissions.
+        installApp(APK_USES_STORAGE_DEFAULT_29, Collections.emptySet(), Collections.emptySet());
+
+        // Could grant permission as targetSDK=29 apps can always grant
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk29OptOutNotWhitelisted() throws Exception {
+        // Install with no whitelisted permissions.
+        installApp(APK_USES_STORAGE_OPT_OUT_29, Collections.emptySet(), Collections.emptySet());
+
+        // Could grant permission as targetSDK=29 apps can always grant
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk29Whitelisted() throws Exception {
+        // Install with whitelisted permissions.
+        installApp(APK_USES_STORAGE_DEFAULT_29, null, Collections.emptySet());
+
+        // Could grant permission as targetSDK=29 apps can always grant
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
+    @Test
+    @AppModeFull
+    public void canGrantStorageTargetingSdk29OptOutWhitelisted() throws Exception {
+        // Install with whitelisted permissions.
+        installApp(APK_USES_STORAGE_OPT_OUT_29, null, Collections.emptySet());
+
+        // Could grant permission as targetSDK=29 apps can always grant
+        assertThat(isGranted(PKG, Manifest.permission.READ_EXTERNAL_STORAGE)).isTrue();
+    }
+
     private void assertHasFullStorageAccess() throws Exception {
         runWithShellPermissionIdentity(() -> {
             AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
@@ -720,7 +782,7 @@
         return InstrumentationRegistry.getInstrumentation().getContext();
     }
 
-    private static void runWithShellPermissionIdentity(@NonNull Utils.ThrowingRunnable command)
+    private static void runWithShellPermissionIdentity(@NonNull ThrowingRunnable command)
             throws Exception {
         InstrumentationRegistry.getInstrumentation().getUiAutomation()
                 .adoptShellPermissionIdentity();
diff --git a/tests/tests/permission2/src/android/permission2/cts/Utils.java b/tests/tests/permission2/src/android/permission2/cts/Utils.java
deleted file mode 100644
index 7cf2f4d..0000000
--- a/tests/tests/permission2/src/android/permission2/cts/Utils.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2019 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 andf
- * limitations under the License.
- */
-
-package android.permission2.cts;
-
-import androidx.annotation.NonNull;
-
-public class Utils {
-    private static final long TIMEOUT_MILLIS = 30000;
-
-    public interface ThrowingRunnable extends Runnable {
-        void runOrThrow() throws Exception;
-
-        @Override
-        default void run() {
-            try {
-                runOrThrow();
-            } catch (Exception ex) {
-                throw new RuntimeException(ex);
-            }
-        }
-    }
-
-    /**
-     * Make sure that a {@link ThrowingRunnable} eventually finishes without throwing a {@link
-     * Exception}.
-     *
-     * @param r The {@link ThrowingRunnable} to run.
-     */
-    public static void eventually(@NonNull ThrowingRunnable r) throws Exception {
-        long start = System.currentTimeMillis();
-
-        while (true) {
-            try {
-                r.runOrThrow();
-                return;
-            } catch (Throwable e) {
-                if (System.currentTimeMillis() - start < TIMEOUT_MILLIS) {
-                    try {
-                        Thread.sleep(100);
-                    } catch (InterruptedException ignored) {
-                        throw e;
-                    }
-                } else {
-                    throw e;
-                }
-            }
-        }
-    }
-}