Fix null pointer crash in BT renaming dialog

The dialog may become null after onDestroy has been invoked, so
we need to catch this case.

This CL also moves the listener outside to make it easy to test.

Change-Id: I4ce640c5bdaf1f201f9fecb14b3e5e38e10d4b79
Fixes: 115679393
Test: RunSettingsRoboTests
This commit is contained in:
jackqdyulei
2018-09-17 13:25:36 -07:00
parent e5791f3c99
commit 5b7fc8f3a5
2 changed files with 105 additions and 18 deletions

View File

@@ -34,6 +34,7 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
@@ -43,8 +44,14 @@ import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
* Dialog fragment for renaming a Bluetooth device.
*/
abstract class BluetoothNameDialogFragment extends InstrumentedDialogFragment
implements TextWatcher {
private AlertDialog mAlertDialog;
implements TextWatcher, TextView.OnEditorActionListener {
// Key to save the edited name and edit status for restoring after rotation
private static final String KEY_NAME = "device_name";
private static final String KEY_NAME_EDITED = "device_name_edited";
@VisibleForTesting
AlertDialog mAlertDialog;
private Button mOkButton;
EditText mDeviceNameView;
@@ -55,10 +62,6 @@ abstract class BluetoothNameDialogFragment extends InstrumentedDialogFragment
// This flag is set when the user edits the name (preserved on rotation)
private boolean mDeviceNameEdited;
// Key to save the edited name and edit status for restoring after rotation
private static final String KEY_NAME = "device_name";
private static final String KEY_NAME_EDITED = "device_name_edited";
/**
* @return the title to use for the dialog.
*/
@@ -123,21 +126,23 @@ abstract class BluetoothNameDialogFragment extends InstrumentedDialogFragment
}
mDeviceNameView.addTextChangedListener(this);
com.android.settings.Utils.setEditTextCursorPosition(mDeviceNameView);
mDeviceNameView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
setDeviceName(v.getText().toString());
mAlertDialog.dismiss();
return true; // action handled
} else {
return false; // not handled
}
}
});
mDeviceNameView.setOnEditorActionListener(this);
return view;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
setDeviceName(v.getText().toString());
if (mAlertDialog != null && mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
return true; // action handled
} else {
return false; // not handled
}
}
@Override
public void onDestroy() {
super.onDestroy();