Check intent support during grant time init.

Do not assign grant time if any health permission was
autogranted in test setup, but the intent is not
supported.
Add grant time unit tests.

Bug: 272239201
Test: atest HealthFitnessUnitTests
Change-Id: I21188aaaf2d2e1a69737c0fd32685792a99f8316
diff --git a/service/java/com/android/server/healthconnect/permission/FirstGrantTimeManager.java b/service/java/com/android/server/healthconnect/permission/FirstGrantTimeManager.java
index 06a6ea0..e487b99 100644
--- a/service/java/com/android/server/healthconnect/permission/FirstGrantTimeManager.java
+++ b/service/java/com/android/server/healthconnect/permission/FirstGrantTimeManager.java
@@ -287,9 +287,14 @@
         boolean stateChanged = false;
         logIfInDebugMode("Valid apps for " + user + ": ", healthPackagesInfos);
 
-        // If package holds health permissions but doesn't have recorded grant
-        // time (e.g. because of permissions rollback), set current time as the first grant time.
+        // If package holds health permissions and supports health permission intent
+        // but doesn't have recorded grant time (e.g. because of permissions rollback),
+        // set current time as the first grant time.
         for (PackageInfo info : healthPackagesInfos) {
+            if (!mTracker.supportsPermissionUsageIntent(info.packageName, user)) {
+                continue;
+            }
+
             if (info.sharedUserId == null) {
                 stateChanged |= setPackageGrantTimeIfNotRecorded(recordedState, info.packageName);
                 validPackagesPerUser.add(info.packageName);
diff --git a/tests/unittests/AndroidManifest.xml b/tests/unittests/AndroidManifest.xml
index 1e4af0d..6f892c1 100644
--- a/tests/unittests/AndroidManifest.xml
+++ b/tests/unittests/AndroidManifest.xml
@@ -26,7 +26,7 @@
     </application>
 
     <!-- To get visibility of the app with health permissions definitions,
-     required for testing defined permissions. -->
+     required for testing defined permissions; for testing first grant time. -->
     <uses-permission android:name="android.permission.health.READ_STEPS"/>
 
     <instrumentation
diff --git a/tests/unittests/src/com/android/server/healthconnect/permission/FirstGrantTimeUnitTest.java b/tests/unittests/src/com/android/server/healthconnect/permission/FirstGrantTimeUnitTest.java
new file mode 100644
index 0000000..548ab9e
--- /dev/null
+++ b/tests/unittests/src/com/android/server/healthconnect/permission/FirstGrantTimeUnitTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2023 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 com.android.server.healthconnect.permission;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.UiAutomation;
+import android.content.Context;
+import android.os.Process;
+import android.os.UserHandle;
+
+import androidx.test.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.time.Instant;
+
+public class FirstGrantTimeUnitTest {
+
+    private static final String SELF_PACKAGE_NAME = "com.android.healthconnect.unittests";
+    private static final UserHandle CURRENT_USER = Process.myUserHandle();
+
+    @Mock private HealthPermissionIntentAppsTracker mTracker;
+
+    private FirstGrantTimeManager mGrantTimeManager;
+
+    private final UiAutomation mUiAutomation =
+            InstrumentationRegistry.getInstrumentation().getUiAutomation();
+
+    @Mock private FirstGrantTimeDatastore mDatastore;
+
+    @Before
+    public void setUp() {
+        Context context = InstrumentationRegistry.getContext();
+        MockitoAnnotations.initMocks(this);
+        when(mDatastore.readForUser(CURRENT_USER)).thenReturn(new UserGrantTimeState(1));
+
+        mUiAutomation.adoptShellPermissionIdentity(
+                "android.permission.OBSERVE_GRANT_REVOKE_PERMISSIONS");
+        mGrantTimeManager = new FirstGrantTimeManager(context, mTracker, mDatastore);
+        mUiAutomation.dropShellPermissionIdentity();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testUnknownPackage_throwsException() {
+        mGrantTimeManager.getFirstGrantTime("android.unknown_package", CURRENT_USER);
+    }
+
+    @Test
+    public void testCurrentPackage_intentNotSupported_grantTimeIsNull() {
+        when(mTracker.supportsPermissionUsageIntent(SELF_PACKAGE_NAME, CURRENT_USER))
+                .thenReturn(false);
+        assertThat(mGrantTimeManager.getFirstGrantTime(SELF_PACKAGE_NAME, CURRENT_USER)).isNull();
+    }
+
+    @Test
+    public void testCurrentPackage_intentSupported_grantTimeIsNotNull() {
+        when(mTracker.supportsPermissionUsageIntent(SELF_PACKAGE_NAME, CURRENT_USER))
+                .thenReturn(true);
+        assertThat(mGrantTimeManager.getFirstGrantTime(SELF_PACKAGE_NAME, CURRENT_USER))
+                .isNotNull();
+        assertThat(mGrantTimeManager.getFirstGrantTime(SELF_PACKAGE_NAME, CURRENT_USER))
+                .isGreaterThan(Instant.now().minusSeconds((long) 1e3));
+        assertThat(mGrantTimeManager.getFirstGrantTime(SELF_PACKAGE_NAME, CURRENT_USER))
+                .isLessThan(Instant.now().plusSeconds((long) 1e3));
+        verify(mDatastore).writeForUser(ArgumentMatchers.any(), ArgumentMatchers.eq(CURRENT_USER));
+        verify(mDatastore).readForUser(CURRENT_USER);
+    }
+}