DO NOT MERGE Add Regulatory Label Dialog to "settings > About". Enable OEMs to add a
Regulatory Labels for the device either based on text/html or an image.

bug: 127298041
Test: Manual. Robolectric.
Change-Id: I436731cf06197b7270af87b2652adf9af279f8ce
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 7c6dc05..8b599f5 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -272,6 +272,16 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name=".system.RegulatoryInfoDisplayActivity"
+            android:label="@string/regulatory_labels"
+            android:enabled="@bool/config_show_regulatory_info">
+            <intent-filter>
+                <action android:name="android.settings.SHOW_REGULATORY_INFO" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <!-- This logic is copied from phone.-->
         <!-- Ensures there's lightweight fallback activity when no other MAIN/HOME activity is present.-->
         <activity android:name=".FallbackHome"
diff --git a/res/drawable/regulatory_info.png b/res/drawable/regulatory_info.png
new file mode 100644
index 0000000..65de26c
--- /dev/null
+++ b/res/drawable/regulatory_info.png
Binary files differ
diff --git a/res/layout/regulatory_info.xml b/res/layout/regulatory_info.xml
new file mode 100644
index 0000000..b4526c5
--- /dev/null
+++ b/res/layout/regulatory_info.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright 2019 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.
+-->
+<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+  <ImageView
+      android:id="@+id/regulatory_info"
+      android:layout_width="wrap_content"
+      android:layout_height="wrap_content"
+      android:adjustViewBounds="true"
+      android:contentDescription="@string/regulatory_labels"
+      android:scaleType="centerCrop"
+      android:src="@drawable/regulatory_info"/>
+</ScrollView>
diff --git a/res/values/config.xml b/res/values/config.xml
index 47437b0..1bfa3dc 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -22,6 +22,8 @@
     <bool name="config_show_system_update_settings">true</bool>
     <!-- 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>
 
     <!-- The component which listens for the enabling of developer options. -->
     <string name="config_dev_options_module" translatable="false">com.android.car.developeroptions/.Settings$DevelopmentSettingsDashboardActivity</string>
diff --git a/res/values/preference_keys.xml b/res/values/preference_keys.xml
index d06ddab..3efe386 100644
--- a/res/values/preference_keys.xml
+++ b/res/values/preference_keys.xml
@@ -280,6 +280,7 @@
     <string name="pk_security_patch" translatable="false">security_patch</string>
     <string name="pk_kernel_version" translatable="false">kernel_version</string>
     <string name="pk_build_number" translatable="false">build_number</string>
+    <string name="pk_regulatory_labels" translatable="false">regulatory_labels</string>
     <string name="pk_about_bluetooth_mac_address" translatable="false">about_bluetooth_mac_address
     </string>
     <string name="pk_about_wifi_mac_address" translatable="false">about_wifi_mac_address</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a475fcf..508c91e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -503,6 +503,9 @@
     <!-- About phone settings screen, setting option name to see wallpapers attributions values -->
     <string name="wallpaper_attributions_values">Satellite imagery providers:\n©2014 CNES / Astrium, DigitalGlobe, Bluesky</string>
 
+    <!-- Text to display in regulatory info screen (from device overlay). [CHAR LIMIT=NONE] -->
+    <string name="regulatory_info_text"></string>
+
     <!-- Title for actual Settings license activity. --><skip/>
     <!-- About phone settings, Legal information setting option name and title of dialog box holding license info -->
     <string name="settings_license_activity_title">Third-party licenses</string>
diff --git a/res/xml/about_settings_fragment.xml b/res/xml/about_settings_fragment.xml
index 115b50b..c7c2b20 100644
--- a/res/xml/about_settings_fragment.xml
+++ b/res/xml/about_settings_fragment.xml
@@ -40,6 +40,12 @@
         android:title="@string/build_number"
         settings:controller="com.android.car.settings.system.BuildNumberPreferenceController"/>
     <Preference
+        android:key="@string/pk_regulatory_labels"
+        android:title="@string/regulatory_labels"
+        settings:controller="com.android.car.settings.common.DefaultRestrictionsPreferenceController">
+        <intent android:action="android.settings.SHOW_REGULATORY_INFO"/>
+    </Preference>
+    <Preference
         android:key="@string/pk_about_bluetooth_mac_address"
         android:title="@string/bluetooth_mac_address"
         settings:controller="com.android.car.settings.system.BluetoothMacAddressPreferenceController"/>
diff --git a/src/com/android/car/settings/system/RegulatoryInfoDisplayActivity.java b/src/com/android/car/settings/system/RegulatoryInfoDisplayActivity.java
new file mode 100644
index 0000000..a7deb12
--- /dev/null
+++ b/src/com/android/car/settings/system/RegulatoryInfoDisplayActivity.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2019 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.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.os.SystemProperties;
+import android.text.TextUtils;
+import android.view.Gravity;
+import android.view.View;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import androidx.annotation.VisibleForTesting;
+
+import com.android.car.settings.R;
+
+import java.util.Locale;
+
+/**
+ * Code drop from {@link com.android.settings.RegulatoryInfoDisplayActivity}.
+ *
+ * {@link Activity} that displays regulatory information for the "Regulatory information"
+ * preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
+ * set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
+ * same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
+ * of the required regulatory info (If ro.bootloader.hardware.sku property is set use
+ * "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
+ * or add a string resource named "regulatory_info_text" with an HTML version of the required
+ * information (text will be centered in the dialog).
+ */
+public class RegulatoryInfoDisplayActivity extends Activity implements
+        DialogInterface.OnDismissListener {
+
+    private static final String DEFAULT_REGULATORY_INFO_FILEPATH =
+            "/data/misc/elabel/regulatory_info.png";
+    private static final String REGULATORY_INFO_FILEPATH_TEMPLATE =
+            "/data/misc/elabel/regulatory_info_%s.png";
+
+    private final String mRegulatoryInfoResource = "regulatory_info";
+
+    /**
+     * Display the regulatory info graphic in a dialog window.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Resources resources = getResources();
+
+        if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
+            finish();   // No regulatory info to display for this device.
+        }
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(this)
+                .setTitle(R.string.regulatory_labels)
+                .setOnDismissListener(this);
+
+        boolean regulatoryInfoDrawableExists = false;
+
+        String regulatoryInfoFile = getRegulatoryInfoImageFileName();
+        Bitmap regulatoryInfoBitmap = BitmapFactory.decodeFile(regulatoryInfoFile);
+
+        if (regulatoryInfoBitmap != null) {
+            regulatoryInfoDrawableExists = true;
+        }
+
+        int resId = 0;
+        if (!regulatoryInfoDrawableExists) {
+            resId = getImageResourceId();
+        }
+        if (resId != 0) {
+            try {
+                Drawable d = getDrawable(resId);
+                // Set to false if the width or height is <= 2.
+                // (missing PNG can return an empty 2x2 pixel Drawable)
+                regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
+                        && d.getIntrinsicHeight() > 2);
+            } catch (Resources.NotFoundException ignored) {
+                regulatoryInfoDrawableExists = false;
+            }
+        }
+
+        CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
+
+        if (regulatoryInfoDrawableExists) {
+            View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
+            ImageView image = view.findViewById(R.id.regulatory_info);
+            if (regulatoryInfoBitmap != null) {
+                image.setImageBitmap(regulatoryInfoBitmap);
+            } else {
+                image.setImageResource(resId);
+            }
+            builder.setView(view);
+            builder.show();
+        } else if (regulatoryText.length() > 0) {
+            builder.setMessage(regulatoryText);
+            AlertDialog dialog = builder.show();
+            // We have to show the dialog first, or the setGravity() call will throw a NPE.
+            TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
+            messageText.setGravity(Gravity.CENTER);
+        } else {
+            // Neither drawable nor text resource exists, finish activity.
+            finish();
+        }
+    }
+
+    private int getImageResourceId() {
+        // Use regulatory_info by default.
+        int resId = getResources().getIdentifier(
+                mRegulatoryInfoResource, "drawable", getPackageName());
+
+        // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
+        String sku = getSku();
+        if (!TextUtils.isEmpty(sku)) {
+            String regulatory_info_res = mRegulatoryInfoResource + "_" + sku.toLowerCase();
+            int id = getResources().getIdentifier(
+                    regulatory_info_res, "drawable", getPackageName());
+            if (id != 0) {
+                resId = id;
+            }
+        }
+        return resId;
+    }
+
+    @Override
+    public void onDismiss(DialogInterface dialog) {
+        finish();
+    }
+
+    @VisibleForTesting
+    static String getSku() {
+        return SystemProperties.get("ro.boot.hardware.sku", "");
+    }
+
+    @VisibleForTesting
+    static String getRegulatoryInfoImageFileName() {
+        String sku = getSku();
+        if (TextUtils.isEmpty(sku)) {
+            return DEFAULT_REGULATORY_INFO_FILEPATH;
+        } else {
+            return String.format(Locale.US, REGULATORY_INFO_FILEPATH_TEMPLATE, sku.toLowerCase());
+        }
+    }
+}
diff --git a/tests/robotests/src/com/android/car/settings/system/RegulatoryInfoDisplayActivityTest.java b/tests/robotests/src/com/android/car/settings/system/RegulatoryInfoDisplayActivityTest.java
new file mode 100644
index 0000000..34ecb69
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/system/RegulatoryInfoDisplayActivityTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2019 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 android.os.SystemProperties;
+
+import com.android.car.settings.CarSettingsRobolectricTestRunner;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.android.controller.ActivityController;
+import org.robolectric.shadows.ShadowAlertDialog;
+
+/** Unit test for {@link RegulatoryInfoDisplayActivity}. */
+@RunWith(CarSettingsRobolectricTestRunner.class)
+public class RegulatoryInfoDisplayActivityTest {
+
+    private ActivityController<RegulatoryInfoDisplayActivity> mActivityController;
+    private RegulatoryInfoDisplayActivity mActivity;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mActivityController = ActivityController.of(new RegulatoryInfoDisplayActivity());
+        mActivity = mActivityController.get();
+        mActivityController.create();
+    }
+
+    @After
+    public void tearDown() {
+        ShadowAlertDialog.reset();
+    }
+
+    @Test
+    public void getRegulatoryInfoImageFileName_skuIsNotEmpty() {
+        SystemProperties.set("ro.boot.hardware.sku", "test");
+
+        assertThat(mActivity.getRegulatoryInfoImageFileName())
+                .isEqualTo("/data/misc/elabel/regulatory_info_test.png");
+    }
+
+    @Test
+    public void getRegulatoryInfoImageFileName_skuIsEmpty() {
+        SystemProperties.set("ro.boot.hardware.sku", "");
+
+        assertThat(mActivity.getRegulatoryInfoImageFileName())
+                .isEqualTo("/data/misc/elabel/regulatory_info.png");
+    }
+
+    @Test
+    public void getSku_shouldReturnSystemProperty() {
+        String testSku = "test";
+        SystemProperties.set("ro.boot.hardware.sku", testSku);
+
+        assertThat(mActivity.getSku()).isEqualTo(testSku);
+    }
+}