Bug 870049 - Added SharedPreferences to persistent Crash Report client settings. r=bnicholson
authorCJ Barker <cjbarker@gmail.com>
Mon, 02 Dec 2013 22:46:06 -0500
changeset 174169 cd60f45d5165d7af0833433e5d913034a7cf1dd7
parent 174168 4723a2e8f88747853999fd757a89de6040c360ca
child 174170 de5cfdf73a3c7801c24bb96b34b7e7885f1885d4
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs870049
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 870049 - Added SharedPreferences to persistent Crash Report client settings. r=bnicholson
mobile/android/base/CrashReporter.java
--- a/mobile/android/base/CrashReporter.java
+++ b/mobile/android/base/CrashReporter.java
@@ -46,16 +46,21 @@ public class CrashReporter extends Activ
     private static final String PAGE_URL_KEY = "URL";
     private static final String NOTES_KEY = "Notes";
     private static final String SERVER_URL_KEY = "ServerURL";
 
     private static final String CRASH_REPORT_SUFFIX = "/mozilla/Crash Reports/";
     private static final String PENDING_SUFFIX = CRASH_REPORT_SUFFIX + "pending";
     private static final String SUBMITTED_SUFFIX = CRASH_REPORT_SUFFIX + "submitted";
 
+    private static final String PREFS_SEND_REPORT   = "sendReport";
+    private static final String PREFS_INCLUDE_URL   = "includeUrl";
+    private static final String PREFS_ALLOW_CONTACT = "allowContact";
+    private static final String PREFS_CONTACT_EMAIL = "contactEmail";
+
     private Handler mHandler;
     private ProgressDialog mProgressDialog;
     private File mPendingMinidumpFile;
     private File mPendingExtrasFile;
     private HashMap<String, String> mExtrasStringMap;
 
     private boolean moveFile(File inFile, File outFile) {
         Log.i(LOGTAG, "moving " + inFile + " to " + outFile);
@@ -135,16 +140,27 @@ public class CrashReporter extends Activ
         editor.commit();
 
         final CheckBox allowContactCheckBox = (CheckBox) findViewById(R.id.allow_contact);
         final CheckBox includeUrlCheckBox = (CheckBox) findViewById(R.id.include_url);
         final CheckBox sendReportCheckBox = (CheckBox) findViewById(R.id.send_report);
         final EditText commentsEditText = (EditText) findViewById(R.id.comment);
         final EditText emailEditText = (EditText) findViewById(R.id.email);
 
+        // Load CrashReporter preferences to avoid redundant user input.
+        final boolean sendReport   = prefs.getBoolean(PREFS_SEND_REPORT, true);
+        final boolean includeUrl   = prefs.getBoolean(PREFS_INCLUDE_URL, false);
+        final boolean allowContact = prefs.getBoolean(PREFS_ALLOW_CONTACT, false);
+        final String contactEmail  = prefs.getString(PREFS_CONTACT_EMAIL, "");
+
+        allowContactCheckBox.setChecked(allowContact);
+        includeUrlCheckBox.setChecked(includeUrl);
+        sendReportCheckBox.setChecked(sendReport);
+        emailEditText.setText(contactEmail);
+
         sendReportCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton checkbox, boolean isChecked) {
                 commentsEditText.setEnabled(isChecked);
                 commentsEditText.requestFocus();
 
                 includeUrlCheckBox.setEnabled(isChecked);
                 allowContactCheckBox.setEnabled(isChecked);
@@ -197,25 +213,50 @@ public class CrashReporter extends Activ
 
     private void backgroundSendReport() {
         final CheckBox sendReportCheckbox = (CheckBox) findViewById(R.id.send_report);
         if (!sendReportCheckbox.isChecked()) {
             doFinish();
             return;
         }
 
+        // Persist settings to avoid redundant user input.
+        savePrefs();
+
         mProgressDialog.show();
         new Thread(new Runnable() {
             @Override
             public void run() {
                 sendReport(mPendingMinidumpFile, mExtrasStringMap, mPendingExtrasFile);
             }
         }, "CrashReporter Thread").start();
     }
 
+    private void savePrefs() {
+        SharedPreferences prefs = getSharedPreferences(GeckoApp.PREFS_NAME, 0);
+        SharedPreferences.Editor editor = prefs.edit();
+                  
+        final boolean allowContact = ((CheckBox) findViewById(R.id.allow_contact)).isChecked();
+        final boolean includeUrl   = ((CheckBox) findViewById(R.id.include_url)).isChecked();
+        final boolean sendReport   = ((CheckBox) findViewById(R.id.send_report)).isChecked();
+        final String contactEmail  = ((EditText) findViewById(R.id.email)).getText().toString();
+                   
+        editor.putBoolean(PREFS_ALLOW_CONTACT, allowContact);
+        editor.putBoolean(PREFS_INCLUDE_URL, includeUrl);
+        editor.putBoolean(PREFS_SEND_REPORT, sendReport);
+        editor.putString(PREFS_CONTACT_EMAIL, contactEmail);
+                    
+        // A slight performance improvement via async apply() vs. blocking on commit().
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
+            editor.commit();
+        } else { 
+            editor.apply();
+        }
+    }
+
     public void onCloseClick(View v) {  // bound via crash_reporter.xml
         backgroundSendReport();
     }
 
     public void onRestartClick(View v) {  // bound via crash_reporter.xml
         doRestart();
         backgroundSendReport();
     }