Merge "Update Managed Device Info text for a financed device" into sc-dev
diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceController.java b/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceController.java
index b07eb91..8353d20 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceController.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceController.java
@@ -13,6 +13,9 @@
  */
 package com.android.settings.enterprise;
 
+import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
+
+import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 
 import androidx.preference.Preference;
@@ -39,6 +42,10 @@
         if (preference == null) {
             return;
         }
+        if (isFinancedDevice()) {
+            preference.setTitle(R.string.financed_privacy_settings);
+        }
+
         final String organizationName = mFeatureProvider.getDeviceOwnerOrganizationName();
         if (organizationName == null) {
             preference.setSummary(R.string.enterprise_privacy_settings_summary_generic);
@@ -57,4 +64,10 @@
     public String getPreferenceKey() {
         return KEY_ENTERPRISE_PRIVACY;
     }
+
+    private boolean isFinancedDevice() {
+        final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
+        return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
+                dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceControllerTest.java
index 6858dd3..8052357 100644
--- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyPreferenceControllerTest.java
@@ -16,10 +16,15 @@
 
 package com.android.settings.enterprise;
 
+import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
+import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.Mockito.when;
 
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
 import android.content.Context;
 
 import androidx.preference.Preference;
@@ -42,9 +47,14 @@
     private static final String MANAGED_WITH_NAME = "managed by Foo, Inc.";
     private static final String MANAGING_ORGANIZATION = "Foo, Inc.";
     private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
+    private static final String FINANCED_PREFERENCE_TITLE = "Financed device info";
+    private static final ComponentName DEVICE_OWNER_COMPONENT =
+            new ComponentName("com.android.foo", "bar");
 
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private Context mContext;
+    @Mock
+    private DevicePolicyManager mDevicePolicyManager;
     private FakeFeatureFactory mFeatureFactory;
 
     private EnterprisePrivacyPreferenceController mController;
@@ -54,6 +64,14 @@
         MockitoAnnotations.initMocks(this);
         mFeatureFactory = FakeFeatureFactory.setupForTest();
         mController = new EnterprisePrivacyPreferenceController(mContext);
+
+        when((Object) mContext.getSystemService(DevicePolicyManager.class))
+                .thenReturn(mDevicePolicyManager);
+        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
+        when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
+                .thenReturn(DEVICE_OWNER_COMPONENT);
+        when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
+                .thenReturn(DEVICE_OWNER_TYPE_DEFAULT);
     }
 
     @Test
@@ -77,6 +95,25 @@
     }
 
     @Test
+    public void testUpdateState_verifyPreferenceTitleIsUpdatedForFinancedDevice() {
+        final Preference preference = new Preference(mContext, null, 0, 0);
+        when(mContext.getResources().getString(
+                R.string.enterprise_privacy_settings_summary_with_name, MANAGING_ORGANIZATION))
+                .thenReturn(MANAGED_WITH_NAME);
+        when(mContext.getString(R.string.financed_privacy_settings))
+                .thenReturn(FINANCED_PREFERENCE_TITLE);
+        when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
+                .thenReturn(MANAGING_ORGANIZATION);
+        when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
+                .thenReturn(DEVICE_OWNER_TYPE_FINANCED);
+
+        mController.updateState(preference);
+
+        assertThat(preference.getTitle()).isEqualTo(FINANCED_PREFERENCE_TITLE);
+        assertThat(preference.getSummary()).isEqualTo(MANAGED_WITH_NAME);
+    }
+
+    @Test
     public void testIsAvailable() {
         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(false);
         assertThat(mController.isAvailable()).isFalse();