Add Safety information to Settings > About Phone

The Safety information link is stored in a system property, ro.url.safetylegal

If the phone has data connection, select "Safety information" menu will open the
Safety information link.  If there is no data connection for the phone, a message will
pop up which ask user to open Safety information link from PC.

Change-Id: Ia46e2e868ebcda93138cb9f00c978d8ac827022a
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 9393057..0cb88f1 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -249,6 +249,15 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="SettingsSafetyLegalActivity"
+                android:label="@string/settings_safetylegal_activity_title"
+                android:theme="@*android:style/Theme.Dialog.Alert">
+            <intent-filter>
+                <action android:name="android.settings.SAFETY" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="ApplicationSettings" android:label="@string/applications_settings_header"
                 >
             <intent-filter>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3b55972..2ad8fed 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1234,6 +1234,15 @@
     <!-- About phone settings screen, Open source license dialog title until license is fully loaded -->
     <string name="settings_license_activity_loading">Loading\u2026</string>
 
+    <!-- About phone settings, Safety Legal information setting option name and title of dialog box holding safety legal info -->
+    <string name="settings_safetylegal_title">Safety information</string>
+    <!-- About phone settings, Safety Legal information setting option name and title of dialog box holding safety legal info -->
+    <string name="settings_safetylegal_activity_title">Safety information</string>
+    <!-- About phone settings screen, Safety legal dialog message when data network is not connected -->
+    <string name="settings_safetylegal_activity_unreachable">Your phone is not connected to a data service. To view this information now, go to %s from any computer connected to the Internet.</string>
+    <!-- About phone settings screen, Safety Legal dialog title until the link is fully loaded -->
+    <string name="settings_safetylegal_activity_loading">Loading\u2026</string>
+
     <!-- Lock Pattern settings -->
     <!-- Security & location settings screen, header -->
     <string name="lock_settings_title">Screen unlock pattern</string>
diff --git a/res/xml/device_info_settings.xml b/res/xml/device_info_settings.xml
index 56810b4..860c2ea 100644
--- a/res/xml/device_info_settings.xml
+++ b/res/xml/device_info_settings.xml
@@ -75,6 +75,12 @@
 
         </PreferenceScreen>
         
+        <PreferenceScreen
+                android:key="safetylegal"
+                android:title="@string/settings_safetylegal_title">
+            <intent android:action="android.settings.SAFETY" />
+        </PreferenceScreen>
+
         <!-- Contributors -->
         <!-- 
         <PreferenceScreen
diff --git a/src/com/android/settings/DeviceInfoSettings.java b/src/com/android/settings/DeviceInfoSettings.java
index 7768b7d..82b7f28 100644
--- a/src/com/android/settings/DeviceInfoSettings.java
+++ b/src/com/android/settings/DeviceInfoSettings.java
@@ -48,6 +48,7 @@
     private static final String KEY_LICENSE = "license";
     private static final String KEY_COPYRIGHT = "copyright";
     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
+    private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
     
     @Override
     protected void onCreate(Bundle icicle) {
@@ -61,6 +62,10 @@
         setStringSummary("build_number", Build.DISPLAY);
         findPreference("kernel_version").setSummary(getFormattedKernelVersion());
 
+        // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
+        removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
+                PROPERTY_URL_SAFETYLEGAL);
+
         /*
          * Settings is a generic app and should not contain any device-specific
          * info.
@@ -85,7 +90,21 @@
         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_CONTRIBUTORS,
                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
     }
-    
+
+    private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
+            String preference, String property ) {
+        if (SystemProperties.get(property).equals(""))
+        {
+            // Property is missing so remove preference from group
+            try {
+                preferenceGroup.removePreference(findPreference(preference));
+            } catch (RuntimeException e) {
+                Log.d(TAG, "Property '" + property + "' missing and no '"
+                        + preference + "' preference");
+            }
+        }
+    }
+
     private void setStringSummary(String preference, String value) {
         try {
             findPreference(preference).setSummary(value);
diff --git a/src/com/android/settings/SettingsSafetyLegalActivity.java b/src/com/android/settings/SettingsSafetyLegalActivity.java
new file mode 100644
index 0000000..83daa05
--- /dev/null
+++ b/src/com/android/settings/SettingsSafetyLegalActivity.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2009 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.AlertDialog;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.SystemProperties;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+import com.android.internal.app.AlertActivity;
+import com.android.internal.app.AlertController;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+
+/**
+ * The "dialog" that shows from "Safety information" in the Settings app.
+ */
+public class SettingsSafetyLegalActivity extends AlertActivity {
+    private static final String PROPERTY_LSAFETYLEGAL_URL = "ro.url.safetylegal";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        String userSafetylegalUrl = SystemProperties.get(PROPERTY_LSAFETYLEGAL_URL);
+
+        final Configuration configuration = getResources().getConfiguration();
+        final String language = configuration.locale.getLanguage();
+        final String country = configuration.locale.getCountry();
+
+        String loc = String.format("locale=%s-%s", language, country);
+
+        userSafetylegalUrl = String.format("%s&%s", userSafetylegalUrl, loc);
+
+        if (!isDataNetworkConnected()) {
+            showErrorAndFinish(userSafetylegalUrl);
+            return;
+        }
+
+        WebView webView = new WebView(this);
+
+        // Begin accessing
+        webView.getSettings().setJavaScriptEnabled(true);
+        webView.loadUrl(userSafetylegalUrl);
+        webView.setWebViewClient(new WebViewClient() {
+            @Override
+            public void onPageFinished(WebView view, String url) {
+                // Change from 'Loading...' to the real title
+                mAlert.setTitle(getString(R.string.settings_safetylegal_activity_title));
+            }
+        });
+
+        final AlertController.AlertParams p = mAlertParams;
+        p.mTitle = getString(R.string.settings_safetylegal_activity_loading);
+        p.mView = webView;
+        p.mForceInverseBackground = true;
+        setupAlert();
+    }
+
+    private void showErrorAndFinish(String url) {
+        new AlertDialog.Builder(this)
+                .setMessage(getResources()
+                        .getString(R.string.settings_safetylegal_activity_unreachable, url))
+                .setTitle(R.string.settings_safetylegal_activity_title)
+                .setPositiveButton(android.R.string.ok, mOkListener)
+                .setCancelable(true)
+                .show();
+    }
+
+    private boolean isDataNetworkConnected() {
+        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
+
+        if (mTelephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    private final OnClickListener mOkListener = new OnClickListener() {
+         public void onClick(DialogInterface dialog, int whichButton) {
+             finish();
+         }
+    };
+}