Adding code to invoke feedback for the device.

Please also see http://ag/457938

Change-Id: I842e7211b339ece7066523c503db6e7b0eb12f40
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1eae3ae..0c73893 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5206,4 +5206,10 @@
 
     <!-- [CHAR LIMIT=20] Zen mode settings: End time option: Summary text value format -->
     <string name="zen_mode_end_time_summary_format"><xliff:g id="formatted_time">%s</xliff:g> next day</string>
+
+   <!-- [CHAR LIMIT=35] Feedback on the device -->
+   <string name="device_feedback">Send feedback about this device</string>
+
+    <!-- Full package name of OEM preferred device feedback reporter [DO NOT TRANSLATE] -->
+    <string name="oem_preferred_feedback_reporter" translatable="false"></string>
 </resources>
diff --git a/res/xml/device_info_settings.xml b/res/xml/device_info_settings.xml
index 07abe41..0261b67 100644
--- a/res/xml/device_info_settings.xml
+++ b/res/xml/device_info_settings.xml
@@ -95,6 +95,11 @@
             <intent android:action="android.settings.SHOW_REGULATORY_INFO" />
         </PreferenceScreen>
 
+        <!-- Feedback on the device -->
+        <PreferenceScreen android:key="device_feedback"
+                android:title="@string/device_feedback">
+        </PreferenceScreen>
+
         <!-- Device hardware model -->
         <Preference android:key="device_model" 
                 style="?android:preferenceInformationStyle"
diff --git a/src/com/android/settings/DeviceInfoSettings.java b/src/com/android/settings/DeviceInfoSettings.java
index 1e3b9a8..bf303df 100644
--- a/src/com/android/settings/DeviceInfoSettings.java
+++ b/src/com/android/settings/DeviceInfoSettings.java
@@ -19,8 +19,14 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Binder;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Parcel;
+import android.os.RemoteException;
 import android.os.SELinux;
 import android.os.SystemClock;
 import android.os.SystemProperties;
@@ -29,19 +35,20 @@
 import android.preference.PreferenceGroup;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
+import android.text.TextUtils;
 import android.util.Log;
 import android.widget.Toast;
 
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class DeviceInfoSettings extends RestrictedSettingsFragment {
 
     private static final String LOG_TAG = "DeviceInfoSettings";
-
     private static final String FILENAME_PROC_VERSION = "/proc/version";
     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
 
@@ -64,6 +71,7 @@
     private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
     private static final String KEY_EQUIPMENT_ID = "fcc_equipment_id";
     private static final String PROPERTY_EQUIPMENT_ID = "ro.ril.fccid";
+    private static final String KEY_DEVICE_FEEDBACK = "device_feedback";
 
     static final int TAPS_TO_BE_A_DEVELOPER = 7;
 
@@ -120,6 +128,11 @@
             getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
         }
 
+        // Dont show feedback option if there is no reporter.
+        if (TextUtils.isEmpty(getFeedbackReporterPackage(getActivity()))) {
+            getPreferenceScreen().removePreference(findPreference(KEY_DEVICE_FEEDBACK));
+        }
+
         /*
          * Settings is a generic app and should not contain any device-specific
          * info.
@@ -226,6 +239,8 @@
                         Toast.LENGTH_LONG);
                 mDevHitToast.show();
             }
+        } else if (preference.getKey().equals(KEY_DEVICE_FEEDBACK)) {
+            sendFeedback();
         }
         return super.onPreferenceTreeClick(preferenceScreen, preference);
     }
@@ -271,6 +286,16 @@
         }
     }
 
+    private void sendFeedback() {
+        String reporterPackage = getFeedbackReporterPackage(getActivity());
+        if (TextUtils.isEmpty(reporterPackage)) {
+            return;
+        }
+        Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
+        intent.setPackage(reporterPackage);
+        startActivityForResult(intent, 0);
+    }
+
     /**
      * Reads a line from the specified file.
      * @param filename the file to read from
@@ -347,4 +372,40 @@
         }
         return "";
     }
+
+    private static String getFeedbackReporterPackage(Context context) {
+        String feedbackReporter =
+                context.getResources().getString(R.string.oem_preferred_feedback_reporter);
+        if (TextUtils.isEmpty(feedbackReporter)) {
+            // Reporter not configured. Return.
+            return feedbackReporter;
+        }
+        // Additional checks to ensure the reporter is on system image, and reporter is
+        // configured to listen to the intent. Otherwise, dont show the "send feedback" option.
+        Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
+
+        PackageManager pm = context.getPackageManager();
+        List<ResolveInfo> resolvedPackages =
+                pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
+        for (ResolveInfo info : resolvedPackages) {
+            if (info.activityInfo != null) {
+                if (!TextUtils.isEmpty(info.activityInfo.packageName)) {
+                    try {
+                        ApplicationInfo ai = pm.getApplicationInfo(info.activityInfo.packageName, 0);
+                        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
+                            // Package is on the system image
+                            if (TextUtils.equals(
+                                        info.activityInfo.packageName, feedbackReporter)) {
+                                return feedbackReporter;
+                            }
+                        }
+                    } catch (PackageManager.NameNotFoundException e) {
+                         // No need to do anything here.
+                    }
+                }
+            }
+        }
+        return null;
+    }
 }
+