From 3a9558052cd549ba4809253e2da2b6ba88e92e92 Mon Sep 17 00:00:00 2001 From: Soonil Nagarkar Date: Tue, 27 Apr 2021 14:58:27 -0700 Subject: [PATCH] Include provider tag in complete attribution tag list Ensure that the list of attribution tags considered "producer" or "source" includes the attribution tag of the location provider itself. This is particularily important if the location provider does not use any attribution tags, as this ensures the null tag is included in the list. Bug: 186572461 Test: manual Change-Id: I9a7b2e9b67cbeb700d5c04730541ad4bf051d0da --- .../provider/LocationProviderManager.java | 14 ++++++++++---- .../provider/LocationProviderManagerTest.java | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/location/provider/LocationProviderManager.java b/services/core/java/com/android/server/location/provider/LocationProviderManager.java index 0be325fb6aa70..4b772f29e250b 100644 --- a/services/core/java/com/android/server/location/provider/LocationProviderManager.java +++ b/services/core/java/com/android/server/location/provider/LocationProviderManager.java @@ -2271,22 +2271,28 @@ public class LocationProviderManager extends } if (mOnLocationTagsChangeListener != null) { - if (!oldState.extraAttributionTags.equals(newState.extraAttributionTags)) { + if (!oldState.extraAttributionTags.equals(newState.extraAttributionTags) + || !Objects.equals(oldState.identity, newState.identity)) { if (oldState.identity != null) { FgThread.getHandler().sendMessage(PooledLambda.obtainMessage( OnProviderLocationTagsChangeListener::onLocationTagsChanged, mOnLocationTagsChangeListener, new LocationTagInfo( oldState.identity.getUid(), oldState.identity.getPackageName(), Collections.emptySet()) - )); + )); } if (newState.identity != null) { + ArraySet attributionTags = new ArraySet<>( + newState.extraAttributionTags.size() + 1); + attributionTags.addAll(newState.extraAttributionTags); + attributionTags.add(newState.identity.getAttributionTag()); + FgThread.getHandler().sendMessage(PooledLambda.obtainMessage( OnProviderLocationTagsChangeListener::onLocationTagsChanged, mOnLocationTagsChangeListener, new LocationTagInfo( newState.identity.getUid(), newState.identity.getPackageName(), - newState.extraAttributionTags) - )); + attributionTags) + )); } } } diff --git a/services/tests/mockingservicestests/src/com/android/server/location/provider/LocationProviderManagerTest.java b/services/tests/mockingservicestests/src/com/android/server/location/provider/LocationProviderManagerTest.java index 24b85f056731b..92e4ec9d2e8b8 100644 --- a/services/tests/mockingservicestests/src/com/android/server/location/provider/LocationProviderManagerTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/location/provider/LocationProviderManagerTest.java @@ -61,6 +61,8 @@ import android.location.ILocationListener; import android.location.LastLocationRequest; import android.location.Location; import android.location.LocationManagerInternal; +import android.location.LocationManagerInternal.LocationTagInfo; +import android.location.LocationManagerInternal.OnProviderLocationTagsChangeListener; import android.location.LocationManagerInternal.ProviderEnabledListener; import android.location.LocationRequest; import android.location.LocationResult; @@ -90,6 +92,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mock; @@ -215,6 +218,21 @@ public class LocationProviderManagerTest { assertThat(mManager.hasProvider()).isFalse(); } + @Test + public void testAttributionTags() { + OnProviderLocationTagsChangeListener listener = mock( + OnProviderLocationTagsChangeListener.class); + mManager.setOnProviderLocationTagsChangeListener(listener); + + mProvider.setExtraAttributionTags(Collections.singleton("extra")); + + ArgumentCaptor captor = ArgumentCaptor.forClass(LocationTagInfo.class); + verify(listener, times(2)).onLocationTagsChanged(captor.capture()); + + assertThat(captor.getAllValues().get(0).getTags()).isEmpty(); + assertThat(captor.getAllValues().get(1).getTags()).containsExactly("extra", "attribution"); + } + @Test public void testRemoveProvider() { mManager.setRealProvider(null);