From 76ba215300c3a7543f7d4a7e53f5d2dc07c96593 Mon Sep 17 00:00:00 2001 From: Eghosa Ewansiha-Vlachavas Date: Mon, 30 Oct 2023 14:01:55 +0000 Subject: [PATCH] Log radio button clicks on the user aspect ratio app info page When a user changes an apps aspect ratio, log both the new aspect ratio they select and the previous aspect ratio they are changing from. Fix: 308395282 Bug: 298030311 Test: atest SettingsRoboTests:UserAspectRatioDetailsTest Change-Id: I0ba8f84e1dae94eba0de0301b281506c4eb82f32 --- .../appcompat/UserAspectRatioDetails.java | 69 ++++++++++++++++++- .../appcompat/UserAspectRatioDetailsTest.java | 36 ++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/applications/appcompat/UserAspectRatioDetails.java b/src/com/android/settings/applications/appcompat/UserAspectRatioDetails.java index 076d37c0a8d..02d5c27e5c6 100644 --- a/src/com/android/settings/applications/appcompat/UserAspectRatioDetails.java +++ b/src/com/android/settings/applications/appcompat/UserAspectRatioDetails.java @@ -28,6 +28,7 @@ import static android.content.pm.PackageManager.USER_MIN_ASPECT_RATIO_UNSET; import android.app.ActivityManager; import android.app.IActivityManager; +import android.app.settings.SettingsEnums; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; @@ -44,8 +45,10 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.settings.R; import com.android.settings.Utils; import com.android.settings.applications.AppInfoBase; +import com.android.settings.overlay.FeatureFactory; import com.android.settings.widget.EntityHeaderController; import com.android.settingslib.applications.AppUtils; +import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.widget.ActionButtonsPreference; import java.util.ArrayList; @@ -104,6 +107,7 @@ public class UserAspectRatioDetails extends AppInfoBase implements Log.e(TAG, "Unable to set user min aspect ratio"); return; } + logActionMetrics(selectedKey, mSelectedKey); // Only update to selected aspect ratio if nothing goes wrong mSelectedKey = selectedKey; updateAllPreferences(mSelectedKey); @@ -118,8 +122,7 @@ public class UserAspectRatioDetails extends AppInfoBase implements @Override public int getMetricsCategory() { - // TODO(b/292566895): add metrics for logging - return 0; + return SettingsEnums.USER_ASPECT_RATIO_APP_INFO_SETTINGS; } @Override @@ -244,6 +247,68 @@ public class UserAspectRatioDetails extends AppInfoBase implements } } + private void logActionMetrics(@NonNull String selectedKey, @NonNull String unselectedKey) { + final MetricsFeatureProvider metricsFeatureProvider = + FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); + final int attribution = metricsFeatureProvider.getAttribution(getActivity()); + metricsFeatureProvider.action( + attribution, + getUnselectedAspectRatioAction(unselectedKey), + getMetricsCategory(), + mPackageName, + mUserId + ); + metricsFeatureProvider.action( + attribution, + getSelectedAspectRatioAction(selectedKey), + getMetricsCategory(), + mPackageName, + mUserId + ); + } + + private static int getSelectedAspectRatioAction(@NonNull String selectedKey) { + switch (selectedKey) { + case KEY_PREF_DEFAULT: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_APP_DEFAULT_SELECTED; + case KEY_PREF_FULLSCREEN: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_FULL_SCREEN_SELECTED; + case KEY_PREF_HALF_SCREEN: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_HALF_SCREEN_SELECTED; + case KEY_PREF_4_3: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_4_3_SELECTED; + case KEY_PREF_16_9: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_16_9_SELECTED; + case KEY_PREF_3_2: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_3_2_SELECTED; + case KEY_PREF_DISPLAY_SIZE: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_DISPLAY_SIZE_SELECTED; + default: + return SettingsEnums.ACTION_UNKNOWN; + } + } + + private static int getUnselectedAspectRatioAction(@NonNull String unselectedKey) { + switch (unselectedKey) { + case KEY_PREF_DEFAULT: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_APP_DEFAULT_UNSELECTED; + case KEY_PREF_FULLSCREEN: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_FULL_SCREEN_UNSELECTED; + case KEY_PREF_HALF_SCREEN: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_HALF_SCREEN_UNSELECTED; + case KEY_PREF_4_3: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_4_3_UNSELECTED; + case KEY_PREF_16_9: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_16_9_UNSELECTED; + case KEY_PREF_3_2: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_3_2_UNSELECTED; + case KEY_PREF_DISPLAY_SIZE: + return SettingsEnums.ACTION_USER_ASPECT_RATIO_DISPLAY_SIZE_UNSELECTED; + default: + return SettingsEnums.ACTION_UNKNOWN; + } + } + @VisibleForTesting UserAspectRatioManager getAspectRatioManager() { return mUserAspectRatioManager; diff --git a/tests/robotests/src/com/android/settings/applications/appcompat/UserAspectRatioDetailsTest.java b/tests/robotests/src/com/android/settings/applications/appcompat/UserAspectRatioDetailsTest.java index d98b0e7feed..b615163fbc7 100644 --- a/tests/robotests/src/com/android/settings/applications/appcompat/UserAspectRatioDetailsTest.java +++ b/tests/robotests/src/com/android/settings/applications/appcompat/UserAspectRatioDetailsTest.java @@ -21,21 +21,27 @@ import static com.android.settings.applications.appcompat.UserAspectRatioDetails import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.IActivityManager; +import android.app.settings.SettingsEnums; import android.content.Context; import android.os.RemoteException; import androidx.test.core.app.ApplicationProvider; +import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.shadow.ShadowActivityManager; +import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; @@ -56,6 +62,7 @@ public class UserAspectRatioDetailsTest { private RadioWithImagePreference mRadioButtonPref; private Context mContext; private UserAspectRatioDetails mFragment; + private MetricsFeatureProvider mMetricsFeatureProvider; @Before public void setUp() { @@ -67,6 +74,8 @@ public class UserAspectRatioDetailsTest { when(mFragment.getAspectRatioManager()).thenReturn(mUserAspectRatioManager); ShadowActivityManager.setService(mAm); mRadioButtonPref = new RadioWithImagePreference(mContext); + final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); + mMetricsFeatureProvider = featureFactory.metricsFeatureProvider; } @Test @@ -93,4 +102,31 @@ public class UserAspectRatioDetailsTest { verify(mUserAspectRatioManager).setUserMinAspectRatio( any(), anyInt(), anyInt()); } + + @Test + public void onRadioButtonClicked_prefChange_logMetrics() throws NullPointerException { + // Default was already selected + mRadioButtonPref.setKey(KEY_PREF_DEFAULT); + mFragment.onRadioButtonClicked(mRadioButtonPref); + // Preference changed + mRadioButtonPref.setKey(KEY_PREF_3_2); + mFragment.onRadioButtonClicked(mRadioButtonPref); + InOrder inOrder = inOrder(mMetricsFeatureProvider); + // Check the old aspect ratio value is logged as having been unselected + inOrder.verify(mMetricsFeatureProvider) + .action( + eq(SettingsEnums.PAGE_UNKNOWN), + eq(SettingsEnums.ACTION_USER_ASPECT_RATIO_APP_DEFAULT_UNSELECTED), + eq(SettingsEnums.USER_ASPECT_RATIO_APP_INFO_SETTINGS), + any(), + anyInt()); + // Check the new aspect ratio value is logged as having been selected + inOrder.verify(mMetricsFeatureProvider) + .action( + eq(SettingsEnums.PAGE_UNKNOWN), + eq(SettingsEnums.ACTION_USER_ASPECT_RATIO_3_2_SELECTED), + eq(SettingsEnums.USER_ASPECT_RATIO_APP_INFO_SETTINGS), + any(), + anyInt()); + } }