Merge "Modify default regulatory info behavior" into rvc-qpr-dev
diff --git a/res/values/config.xml b/res/values/config.xml
index 6be7743..e231476 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -27,7 +27,7 @@
     <!-- Whether Wi-Fi Mac address should be shown or not. -->
     <bool name="config_show_wifi_mac_address">true</bool>
     <!-- Whether to show regulatory info or not. -->
-    <bool name="config_show_regulatory_info">true</bool>
+    <bool name="config_show_regulatory_info">false</bool>
     <!-- Whether premium SMS should be shown or not. -->
     <bool name="config_show_premium_sms">true</bool>
     <!-- Whether exit button in settings' root action bar should be shown or not -->
diff --git a/res/xml/about_settings_fragment.xml b/res/xml/about_settings_fragment.xml
index 4656315..a67234d 100644
--- a/res/xml/about_settings_fragment.xml
+++ b/res/xml/about_settings_fragment.xml
@@ -48,7 +48,7 @@
     <Preference
         android:key="@string/pk_regulatory_labels"
         android:title="@string/regulatory_labels"
-        settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController">
+        settings:controller="com.android.car.settings.system.RegulatoryInfoPreferenceController">
         <intent android:action="android.settings.SHOW_REGULATORY_INFO"/>
     </Preference>
     <com.android.car.ui.preference.CarUiPreference
diff --git a/src/com/android/car/settings/system/RegulatoryInfoPreferenceController.java b/src/com/android/car/settings/system/RegulatoryInfoPreferenceController.java
new file mode 100644
index 0000000..6d3cd75
--- /dev/null
+++ b/src/com/android/car/settings/system/RegulatoryInfoPreferenceController.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.system;
+
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.provider.Settings;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.PreferenceController;
+
+/** Controls the visibility of the regulatory info preference. */
+public class RegulatoryInfoPreferenceController extends PreferenceController<Preference> {
+
+    private static final Intent INTENT_PROBE = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
+
+    private PackageManager mPm;
+
+    public RegulatoryInfoPreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+        mPm = context.getPackageManager();
+    }
+
+    @Override
+    protected Class<Preference> getPreferenceType() {
+        return Preference.class;
+    }
+
+    @Override
+    protected int getAvailabilityStatus() {
+        return mPm.queryIntentActivities(INTENT_PROBE, /* flags= */ 0).isEmpty()
+                ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
+    }
+
+    @VisibleForTesting
+    void setPackageManager(PackageManager pm) {
+        mPm = pm;
+    }
+}
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 33b0d6f..9e989f5 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -11,6 +11,7 @@
     ],
 
     static_libs: [
+        "android.car",
         "androidx.test.core",
         "androidx.test.rules",
         "androidx.test.ext.junit",
diff --git a/tests/unit/src/com/android/car/settings/system/RegulatoryInfoPreferenceControllerTest.java b/tests/unit/src/com/android/car/settings/system/RegulatoryInfoPreferenceControllerTest.java
new file mode 100644
index 0000000..ed15c44
--- /dev/null
+++ b/tests/unit/src/com/android/car/settings/system/RegulatoryInfoPreferenceControllerTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.system;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.PreferenceController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(AndroidJUnit4.class)
+public class RegulatoryInfoPreferenceControllerTest {
+
+    private Context mContext = ApplicationProvider.getApplicationContext();
+    private RegulatoryInfoPreferenceController mPreferenceController;
+    private CarUxRestrictions mCarUxRestrictions;
+
+    @Mock
+    private FragmentController mFragmentController;
+    @Mock
+    private PackageManager mPm;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mCarUxRestrictions = new CarUxRestrictions.Builder(/* reqOpt= */ true,
+                CarUxRestrictions.UX_RESTRICTIONS_BASELINE, /* timestamp= */ 0).build();
+
+        mPreferenceController = new RegulatoryInfoPreferenceController(mContext,
+                /* preferenceKey= */ "key", mFragmentController, mCarUxRestrictions);
+        mPreferenceController.setPackageManager(mPm);
+    }
+
+    @Test
+    public void hasIntent_isAvailable() {
+        List<ResolveInfo> activities = new ArrayList<>();
+        activities.add(new ResolveInfo());
+        when(mPm.queryIntentActivities(any(Intent.class), eq(0)))
+                .thenReturn(activities);
+
+        assertThat(mPreferenceController.getAvailabilityStatus()).isEqualTo(
+                PreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void hasNoIntent_isNotAvailable() {
+        List<ResolveInfo> activities = new ArrayList<>();
+        when(mPm.queryIntentActivities(any(Intent.class), eq(0)))
+                .thenReturn(activities);
+
+        assertThat(mPreferenceController.getAvailabilityStatus()).isEqualTo(
+                PreferenceController.UNSUPPORTED_ON_DEVICE);
+    }
+}