Add support to display regulatory info from *#07#.

Add a new action and Activity so the phone dialer can display
regulatory info via "*#07#".

Regulatory info is supplied per device by a Settings app resource
overlay. This overlay must set the boolean resource
"config_show_regulatory_info" to true, and provide either a drawable
named "regulatory_info.png" with the info in graphical form, or a
string resource named "regulatory_info_text" containing the required
information in HTML format.

Bug: 8259869
Change-Id: I73c1431a1a6d1736f9d3e4bd5be7e3771c85cbcb
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4e8ef57..59b562d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1579,6 +1579,17 @@
                        android:resource="@id/security_settings" />
         </activity>
 
+        <!-- Show regulatory info (from settings item or dialing "*#07#") -->
+        <activity android:name="RegulatoryInfoDisplayActivity"
+                  android:label="@string/regulatory_information"
+                  android:taskAffinity=""
+                  android:excludeFromRecents="true">
+            <intent-filter>
+                <action android:name="android.settings.SHOW_REGULATORY_INFO" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <receiver android:name=".widget.SettingsAppWidgetProvider"
                 android:label="@string/gadget_title"
                 android:exported="false"
diff --git a/res/layout/regulatory_info.xml b/res/layout/regulatory_info.xml
index 5827557..478eaab 100755
--- a/res/layout/regulatory_info.xml
+++ b/res/layout/regulatory_info.xml
@@ -14,8 +14,8 @@
      limitations under the License.
 -->
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content">
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
     <ImageView
         android:adjustViewBounds="true"
         android:layout_width="wrap_content"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index bb77a0c..210728a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4504,4 +4504,6 @@
     <!-- An option in a photo selection dialog, if there is no photo yet [CHAR LIMIT=50] -->
     <string name="user_image_choose_photo" msgid="3746334626214970837">Choose photo from Gallery</string>
 
+    <!-- Text to display in regulatory info screen (from device overlay). -->
+    <string name="regulatory_info_text"></string>
 </resources>
diff --git a/res/xml/device_info_settings.xml b/res/xml/device_info_settings.xml
index 03f1e78..07abe41 100644
--- a/res/xml/device_info_settings.xml
+++ b/res/xml/device_info_settings.xml
@@ -89,8 +89,11 @@
         </PreferenceScreen>
         -->
 
-        <com.android.settings.RegulatoryInfoPreference android:key="regulatory_info"
-                android:title="@string/regulatory_information" />
+        <PreferenceScreen
+                android:key="regulatory_info"
+                android:title="@string/regulatory_information">
+            <intent android:action="android.settings.SHOW_REGULATORY_INFO" />
+        </PreferenceScreen>
 
         <!-- Device hardware model -->
         <Preference android:key="device_model" 
diff --git a/src/com/android/settings/RegulatoryInfoDisplayActivity.java b/src/com/android/settings/RegulatoryInfoDisplayActivity.java
new file mode 100644
index 0000000..8adecf1
--- /dev/null
+++ b/src/com/android/settings/RegulatoryInfoDisplayActivity.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2013 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.settings;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.view.Gravity;
+import android.widget.TextView;
+
+/**
+ * {@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, 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 {
+
+    /**
+     * 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_information)
+                .setOnDismissListener(this);
+
+        boolean regulatoryInfoDrawableExists;
+        try {
+            Drawable d = resources.getDrawable(R.drawable.regulatory_info);
+            // 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) {
+            builder.setView(getLayoutInflater().inflate(R.layout.regulatory_info, null));
+            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();
+        }
+    }
+
+    @Override
+    public void onDismiss(DialogInterface dialog) {
+        finish();   // close the activity
+    }
+}
diff --git a/src/com/android/settings/RegulatoryInfoPreference.java b/src/com/android/settings/RegulatoryInfoPreference.java
deleted file mode 100644
index f1a6b77..0000000
--- a/src/com/android/settings/RegulatoryInfoPreference.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2012 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.settings;
-
-import android.content.Context;
-import android.preference.DialogPreference;
-import android.util.AttributeSet;
-
-/**
- * {@link DialogPreference} that displays regulatory information. "About phone"
- * will show a "Regulatory information" preference if
- * R.bool.config_show_regulatory_info is true.
- */
-public class RegulatoryInfoPreference extends DialogPreference {
-
-    public RegulatoryInfoPreference(Context context, AttributeSet attrs) {
-        super(context, attrs, com.android.internal.R.attr.preferenceStyle);
-        setDialogLayoutResource(R.layout.regulatory_info);
-    }
-}