Device state auto-rotation settings: log device state specific state changes

Bug: 221254073
Test: manually + unit tests
Change-Id: Ie8c0d1489d63fd2d19ad604816150b51ede4bb3b
diff --git a/src/com/android/settings/display/DeviceStateAutoRotateSettingController.java b/src/com/android/settings/display/DeviceStateAutoRotateSettingController.java
index c8f6280..beefc05 100644
--- a/src/com/android/settings/display/DeviceStateAutoRotateSettingController.java
+++ b/src/com/android/settings/display/DeviceStateAutoRotateSettingController.java
@@ -22,6 +22,7 @@
 import android.app.settings.SettingsEnums;
 import android.content.Context;
 
+import androidx.annotation.VisibleForTesting;
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleObserver;
 import androidx.lifecycle.OnLifecycleEvent;
@@ -51,16 +52,24 @@
     private final String mDeviceStateDescription;
     private final MetricsFeatureProvider mMetricsFeatureProvider;
 
-    public DeviceStateAutoRotateSettingController(Context context, int deviceState,
-            String deviceStateDescription, int order) {
+    @VisibleForTesting
+    DeviceStateAutoRotateSettingController(Context context, int deviceState,
+            String deviceStateDescription, int order,
+            MetricsFeatureProvider metricsFeatureProvider) {
         super(context, getPreferenceKeyForDeviceState(deviceState));
-        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+        mMetricsFeatureProvider = metricsFeatureProvider;
         mDeviceState = deviceState;
         mDeviceStateDescription = deviceStateDescription;
         mAutoRotateSettingsManager = DeviceStateRotationLockSettingsManager.getInstance(context);
         mOrder = order;
     }
 
+    public DeviceStateAutoRotateSettingController(Context context, int deviceState,
+            String deviceStateDescription, int order) {
+        this(context, deviceState, deviceStateDescription, order,
+                FeatureFactory.getFactory(context).getMetricsFeatureProvider());
+    }
+
     void init(Lifecycle lifecycle) {
         lifecycle.addObserver(this);
     }
@@ -108,12 +117,22 @@
     @Override
     public boolean setChecked(boolean isChecked) {
         boolean isRotationLocked = !isChecked;
-        mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK,
-                isRotationLocked);
+        logSettingChanged(isChecked);
         mAutoRotateSettingsManager.updateSetting(mDeviceState, isRotationLocked);
         return true;
     }
 
+    private void logSettingChanged(boolean isChecked) {
+        boolean isRotationLocked = !isChecked;
+        mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK,
+                isRotationLocked);
+
+        int actionCategory = isChecked
+                ? SettingsEnums.ACTION_ENABLE_AUTO_ROTATION_DEVICE_STATE
+                : SettingsEnums.ACTION_DISABLE_AUTO_ROTATION_DEVICE_STATE;
+        mMetricsFeatureProvider.action(mContext, actionCategory, /* value= */ mDeviceState);
+    }
+
     @Override
     public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
         SearchIndexableRaw indexable = new SearchIndexableRaw(mContext);
diff --git a/tests/robotests/src/com/android/settings/display/DeviceStateAutoRotateSettingControllerTest.java b/tests/robotests/src/com/android/settings/display/DeviceStateAutoRotateSettingControllerTest.java
index 28a071a..d364a3b 100644
--- a/tests/robotests/src/com/android/settings/display/DeviceStateAutoRotateSettingControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/DeviceStateAutoRotateSettingControllerTest.java
@@ -21,6 +21,9 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.Mockito.verify;
+
+import android.app.settings.SettingsEnums;
 import android.content.Context;
 
 import androidx.preference.Preference;
@@ -30,11 +33,15 @@
 import com.android.settings.R;
 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager;
 import com.android.settings.testutils.shadow.ShadowRotationPolicy;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;
 import com.android.settingslib.search.SearchIndexableRaw;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
@@ -54,12 +61,26 @@
     private static final int DEFAULT_ORDER = -10;
 
     private final Context mContext = RuntimeEnvironment.application;
-    private final DeviceStateAutoRotateSettingController mController =
-            new DeviceStateAutoRotateSettingController(mContext, DEFAULT_DEVICE_STATE,
-                    DEFAULT_DEVICE_STATE_DESCRIPTION, DEFAULT_ORDER);
     private final DeviceStateRotationLockSettingsManager mAutoRotateSettingsManager =
             DeviceStateRotationLockSettingsManager.getInstance(mContext);
 
+    @Mock private MetricsFeatureProvider mMetricsFeatureProvider;
+
+    private DeviceStateAutoRotateSettingController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mController = new DeviceStateAutoRotateSettingController(
+                mContext,
+                DEFAULT_DEVICE_STATE,
+                DEFAULT_DEVICE_STATE_DESCRIPTION,
+                DEFAULT_ORDER,
+                mMetricsFeatureProvider
+        );
+    }
+
     @Test
     public void displayPreference_addsPreferenceToPreferenceScreen() {
         PreferenceScreen screen = new PreferenceManager(mContext).createPreferenceScreen(mContext);
@@ -144,6 +165,22 @@
     }
 
     @Test
+    public void setChecked_true_logsDeviceStateBasedSettingOn() {
+        mController.setChecked(true);
+
+        verify(mMetricsFeatureProvider).action(mContext,
+                SettingsEnums.ACTION_ENABLE_AUTO_ROTATION_DEVICE_STATE, DEFAULT_DEVICE_STATE);
+    }
+
+    @Test
+    public void setChecked_false_logsDeviceStateBasedSettingOff() {
+        mController.setChecked(false);
+
+        verify(mMetricsFeatureProvider).action(mContext,
+                SettingsEnums.ACTION_DISABLE_AUTO_ROTATION_DEVICE_STATE, DEFAULT_DEVICE_STATE);
+    }
+
+    @Test
     public void updateRawDataToIndex_addsItemToList() {
         List<SearchIndexableRaw> rawData = new ArrayList<>();