Bug 713921 - Updating master password should not reattach textbox listeners. r=mfinkle
authorWes Johnston <wjohnston@mozilla.com>
Tue, 03 Jan 2012 15:53:29 -0800
changeset 84932 8fba04927c44b914d46ce8cb046a6b8dc5e43e36
parent 84931 e251ac6325b49356548223f73f24624018d094d8
child 84933 9ca7f53b97753c65d0394862eeaad7c22d66ba8e
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs713921
milestone12.0a1
Bug 713921 - Updating master password should not reattach textbox listeners. r=mfinkle
mobile/android/base/GeckoPreferences.java
--- a/mobile/android/base/GeckoPreferences.java
+++ b/mobile/android/base/GeckoPreferences.java
@@ -165,51 +165,49 @@ public class GeckoPreferences
         inputtype |= InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
         input.setInputType(inputtype);
 
         String hint = getResources().getString(aHintText);
         input.setHint(aHintText);
         return input;
     }
 
-    private AlertDialog mDialog = null;
-
     private class PasswordTextWatcher implements TextWatcher {
         EditText input1 = null;
         EditText input2 = null;
+        AlertDialog dialog = null;
 
-        PasswordTextWatcher(EditText aInput1, EditText aInput2) {
+        PasswordTextWatcher(EditText aInput1, EditText aInput2, AlertDialog aDialog) {
             input1 = aInput1;
             input2 = aInput2;
+            dialog = aDialog;
         }
 
         public void afterTextChanged(Editable s) {
-            if (mDialog == null)
+            if (dialog == null)
                 return;
 
             String text1 = input1.getText().toString();
             String text2 = input2.getText().toString();
-            mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(text1.equals(text2));
+            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(text1.equals(text2));
         }
 
         public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
         public void onTextChanged(CharSequence s, int start, int before, int count) { }
     }
 
     protected Dialog onCreateDialog(int id) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         LinearLayout linearLayout = new LinearLayout(this);
         linearLayout.setOrientation(LinearLayout.VERTICAL);
+        AlertDialog dialog = null;
         switch(id) {
             case DIALOG_CREATE_MASTER_PASSWORD:
                 final EditText input1 = getTextBox(R.string.masterpassword_password);
                 final EditText input2 = getTextBox(R.string.masterpassword_confirm);
-                PasswordTextWatcher watcher = new PasswordTextWatcher(input1, input2);
-                input1.addTextChangedListener((TextWatcher)watcher);
-                input2.addTextChangedListener((TextWatcher)watcher);
                 linearLayout.addView(input1);
                 linearLayout.addView(input2);
 
                 builder.setTitle(R.string.masterpassword_create_title)
                        .setView((View)linearLayout)
                        .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {  
                             public void onClick(DialogInterface dialog, int which) {
                                 JSONObject jsonPref = new JSONObject();
@@ -218,30 +216,37 @@ public class GeckoPreferences
                                     jsonPref.put("type", "string");
                                     jsonPref.put("value", input1.getText().toString());
                     
                                     GeckoEvent event = new GeckoEvent("Preferences:Set", jsonPref.toString());
                                     GeckoAppShell.sendEventToGecko(event);
                                 } catch(Exception ex) {
                                     Log.e(LOGTAG, "Error setting masterpassword", ex);
                                 }
-                                mDialog = null;
-                                input1.setText("");
-                                input2.setText("");
                                 return;
                             }
                         })
                         .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {  
                             public void onClick(DialogInterface dialog, int which) {
-                                mDialog = null;
-                                input1.setText("");
-                                input2.setText("");
                                 return;
                             }
                         });
+                        dialog = builder.create();
+                        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+                            public void onDismiss(DialogInterface dialog) {
+                                input1.setText("");
+                                input2.setText("");
+                                input1.requestFocus();
+                            }
+                        });
+
+                        PasswordTextWatcher watcher = new PasswordTextWatcher(input1, input2, dialog);
+                        input1.addTextChangedListener((TextWatcher)watcher);
+                        input2.addTextChangedListener((TextWatcher)watcher);
+
                 break;
             case DIALOG_REMOVE_MASTER_PASSWORD:
                 final EditText input = getTextBox(R.string.masterpassword_password);
                 linearLayout.addView(input);
 
                 builder.setTitle(R.string.masterpassword_remove_title)
                        .setView((View)linearLayout)
                        .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {  
@@ -252,33 +257,36 @@ public class GeckoPreferences
                                     jsonPref.put("type", "string");
                                     jsonPref.put("value", input.getText().toString());
                         
                                     GeckoEvent event = new GeckoEvent("Preferences:Set", jsonPref.toString());
                                     GeckoAppShell.sendEventToGecko(event);
                                 } catch(Exception ex) {
                                     Log.e(LOGTAG, "Error setting masterpassword", ex);
                                 }
-                                input.setText("");
-                                mDialog = null;
                                 return;
                             }
                         })
-                       .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {  
+                        .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {  
                             public void onClick(DialogInterface dialog, int which) {
-                                mDialog = null;
+                                return;
+                            }
+                        });
+                        dialog = builder.create();
+                        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+                            public void onDismiss(DialogInterface dialog) {
                                 input.setText("");
-                                return;
                             }
                         });
                 break;
             default:
                 return null;
         }
-        return mDialog = builder.create();
+
+        return dialog;
     }
 
     private void refresh(JSONArray jsonPrefs) {
         // enable all preferences once we have them from gecko
         GeckoAppShell.getMainHandler().post(new Runnable() {
             public void run() {
                 mPreferenceScreen.setEnabled(true);
             }