diff options
| author | 2024-10-28 19:31:17 +0000 | |
|---|---|---|
| committer | 2024-10-28 19:31:17 +0000 | |
| commit | 234ea361175a8fb652690ec4474986bb101a1ac3 (patch) | |
| tree | b27490c452a2c2294f3df461e4dec929dde18520 | |
| parent | 7ace523bbc80e1d710c655cc698ab5941b27023d (diff) | |
| parent | 600daf1de84848998e2f21849c38fe555690faf7 (diff) | |
Always show all approved apps am: e81fa0c19c am: 2719ef089a am: 600daf1de8
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/30024287
Change-Id: Icf2b2a234c909921c5ed328f5b288e714f6f69f0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2 files changed, 88 insertions, 10 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/ServiceListing.java b/packages/SettingsLib/src/com/android/settingslib/applications/ServiceListing.java index c8bcabff1094..261c722e517c 100644 --- a/packages/SettingsLib/src/com/android/settingslib/applications/ServiceListing.java +++ b/packages/SettingsLib/src/com/android/settingslib/applications/ServiceListing.java @@ -138,23 +138,37 @@ public class ServiceListing {          }          final PackageManager pmWrapper = mContext.getPackageManager(); +        // Add requesting apps, with full validation          List<ResolveInfo> installedServices = pmWrapper.queryIntentServicesAsUser(                  new Intent(mIntentAction), flags, user);          for (ResolveInfo resolveInfo : installedServices) {              ServiceInfo info = resolveInfo.serviceInfo; -            if (!mPermission.equals(info.permission)) { -                Slog.w(mTag, "Skipping " + mNoun + " service " -                        + info.packageName + "/" + info.name -                        + ": it does not require the permission " -                        + mPermission); -                continue; +            if (!mEnabledServices.contains(info.getComponentName())) { +                if (!mPermission.equals(info.permission)) { +                    Slog.w(mTag, "Skipping " + mNoun + " service " +                            + info.packageName + "/" + info.name +                            + ": it does not require the permission " +                            + mPermission); +                    continue; +                } +                if (mValidator != null && !mValidator.test(info)) { +                    continue; +                } +                mServices.add(info);              } -            if (mValidator != null && !mValidator.test(info)) { -                continue; +        } + +        // Add all apps with access, in case prior approval was granted without full validation +        for (ComponentName cn : mEnabledServices) { +            List<ResolveInfo> enabledServices = pmWrapper.queryIntentServicesAsUser( +                    new Intent().setComponent(cn), flags, user); +            for (ResolveInfo resolveInfo : enabledServices) { +                ServiceInfo info = resolveInfo.serviceInfo; +                mServices.add(info);              } -            mServices.add(info);          } +          for (Callback callback : mCallbacks) {              callback.onServicesReloaded(mServices);          } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ServiceListingTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ServiceListingTest.java index 7ff0988c494d..feef559dfe26 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ServiceListingTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ServiceListingTest.java @@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;  import static org.mockito.ArgumentMatchers.any;  import static org.mockito.ArgumentMatchers.anyInt;  import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.argThat;  import static org.mockito.Mockito.mock;  import static org.mockito.Mockito.spy;  import static org.mockito.Mockito.times; @@ -29,6 +30,7 @@ import static org.mockito.Mockito.when;  import android.content.ComponentName;  import android.content.Context; +import android.content.Intent;  import android.content.pm.PackageManager;  import android.content.pm.ResolveInfo;  import android.content.pm.ServiceInfo; @@ -42,6 +44,7 @@ import org.junit.Before;  import org.junit.Test;  import org.junit.runner.RunWith;  import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher;  import org.robolectric.RobolectricTestRunner;  import org.robolectric.RuntimeEnvironment; @@ -72,19 +75,26 @@ public class ServiceListingTest {                  .build();      } +    private ArgumentMatcher<Intent> filterEquals(Intent intent) { +        return (test) -> { +            return intent.filterEquals(test); +        }; +    } +      @Test      public void testValidator() {          ServiceInfo s1 = new ServiceInfo();          s1.permission = "testPermission";          s1.packageName = "pkg"; +        s1.name = "Service1";          ServiceInfo s2 = new ServiceInfo();          s2.permission = "testPermission";          s2.packageName = "pkg2"; +        s2.name = "service2";          ResolveInfo r1 = new ResolveInfo();          r1.serviceInfo = s1;          ResolveInfo r2 = new ResolveInfo();          r2.serviceInfo = s2; -          when(mPm.queryIntentServicesAsUser(any(), anyInt(), anyInt())).thenReturn(                  ImmutableList.of(r1, r2)); @@ -118,9 +128,11 @@ public class ServiceListingTest {          ServiceInfo s1 = new ServiceInfo();          s1.permission = "testPermission";          s1.packageName = "pkg"; +        s1.name = "Service1";          ServiceInfo s2 = new ServiceInfo();          s2.permission = "testPermission";          s2.packageName = "pkg2"; +        s2.name = "service2";          ResolveInfo r1 = new ResolveInfo();          r1.serviceInfo = s1;          ResolveInfo r2 = new ResolveInfo(); @@ -193,4 +205,56 @@ public class ServiceListingTest {          assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(),                  TEST_SETTING)).contains(testComponent2.flattenToString());      } + +    @Test +    public void testHasPermissionWithoutMeetingCurrentRegs() { +        ServiceInfo s1 = new ServiceInfo(); +        s1.permission = "testPermission"; +        s1.packageName = "pkg"; +        s1.name = "Service1"; +        ServiceInfo s2 = new ServiceInfo(); +        s2.permission = "testPermission"; +        s2.packageName = "pkg2"; +        s2.name = "service2"; +        ResolveInfo r1 = new ResolveInfo(); +        r1.serviceInfo = s1; +        ResolveInfo r2 = new ResolveInfo(); +        r2.serviceInfo = s2; + +        ComponentName approvedComponent = new ComponentName(s2.packageName, s2.name); + +        Settings.Secure.putString( +                mContext.getContentResolver(), TEST_SETTING, approvedComponent.flattenToString()); + +        when(mPm.queryIntentServicesAsUser(argThat( +                filterEquals(new Intent(TEST_INTENT))), anyInt(), anyInt())) +                .thenReturn(ImmutableList.of(r1)); +        when(mPm.queryIntentServicesAsUser(argThat( +                filterEquals(new Intent().setComponent(approvedComponent))), +                anyInt(), anyInt())) +                .thenReturn(ImmutableList.of(r2)); + +        mServiceListing = new ServiceListing.Builder(mContext) +                .setTag("testTag") +                .setSetting(TEST_SETTING) +                .setNoun("testNoun") +                .setIntentAction(TEST_INTENT) +                .setValidator(info -> { +                    if (info.packageName.equals("pkg")) { +                        return true; +                    } +                    return false; +                }) +                .setPermission("testPermission") +                .build(); +        ServiceListing.Callback callback = mock(ServiceListing.Callback.class); +        mServiceListing.addCallback(callback); +        mServiceListing.reload(); + +        verify(mPm, times(2)).queryIntentServicesAsUser(any(), anyInt(), anyInt()); +        ArgumentCaptor<List<ServiceInfo>> captor = ArgumentCaptor.forClass(List.class); +        verify(callback, times(1)).onServicesReloaded(captor.capture()); + +        assertThat(captor.getValue()).containsExactlyElementsIn(ImmutableList.of(s2, s1)); +    }  }  |