Skip to content

Commit

Permalink
Merge pull request #341 from F43nd1r/master
Browse files Browse the repository at this point in the history
Make Dialog easier to extend
  • Loading branch information
william-ferguson-au committed Jan 3, 2016
2 parents aa8cda5 + 5525812 commit 68b01cf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 42 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"

lintOptions {
abortOnError false
Expand Down
142 changes: 101 additions & 41 deletions src/main/java/org/acra/CrashReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@ public class CrashReportDialog extends BaseCrashReportDialog implements DialogIn

private static final String STATE_EMAIL = "email";
private static final String STATE_COMMENT = "comment";
private LinearLayout scrollable;
private EditText userCommentView;
private EditText userEmailView;

AlertDialog mDialog;
private AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

scrollable = new LinearLayout(this);
scrollable.setOrientation(LinearLayout.VERTICAL);

buildAndShowDialog(savedInstanceState);
}

/**
* Build the dialog from the values in config
* @param savedInstanceState old state to restore
*/
protected void buildAndShowDialog(Bundle savedInstanceState){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
final int titleResourceId = ACRA.getConfig().resDialogTitle();
if (titleResourceId != 0) {
Expand Down Expand Up @@ -60,66 +72,107 @@ protected View buildCustomView(Bundle savedInstanceState) {

final ScrollView scroll = new ScrollView(this);
root.addView(scroll, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
final LinearLayout scrollable = new LinearLayout(this);
scrollable.setOrientation(LinearLayout.VERTICAL);
scroll.addView(scrollable);

final TextView text = new TextView(this);
final int dialogTextId = ACRA.getConfig().resDialogText();
if (dialogTextId != 0) {
text.setText(getText(dialogTextId));
}
scrollable.addView(text);
addViewToDialog(getMainView());

// Add an optional prompt for user comments
final int commentPromptId = ACRA.getConfig().resDialogCommentPrompt();
if (commentPromptId != 0) {
final TextView label = new TextView(this);
label.setText(getText(commentPromptId));

label.setPadding(label.getPaddingLeft(), 10, label.getPaddingRight(), label.getPaddingBottom());
scrollable.addView(label, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

userCommentView = new EditText(this);
userCommentView.setLines(2);
String savedComment = null;
if (savedInstanceState != null) {
String savedValue = savedInstanceState.getString(STATE_COMMENT);
if (savedValue != null) {
userCommentView.setText(savedValue);
}
savedComment = savedInstanceState.getString(STATE_COMMENT);
}
scrollable.addView(userCommentView);
userCommentView = getCommentPrompt(getText(commentPromptId), savedComment);
addViewToDialog(userCommentView);
}

// Add an optional user email field
final int emailPromptId = ACRA.getConfig().resDialogEmailPrompt();
if (emailPromptId != 0) {
final TextView label = new TextView(this);
label.setText(getText(emailPromptId));

label.setPadding(label.getPaddingLeft(), 10, label.getPaddingRight(), label.getPaddingBottom());
scrollable.addView(label);

userEmailView = new EditText(this);
userEmailView.setSingleLine();
userEmailView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

String savedValue = null;
String savedEmail = null;
if (savedInstanceState != null) {
savedValue = savedInstanceState.getString(STATE_EMAIL);
}
if (savedValue != null) {
userEmailView.setText(savedValue);
} else {
final SharedPreferences prefs = ACRA.getACRASharedPreferences();
userEmailView.setText(prefs.getString(ACRA.PREF_USER_EMAIL_ADDRESS, ""));
savedEmail = savedInstanceState.getString(STATE_EMAIL);
}
scrollable.addView(userEmailView);
userEmailView = getEmailPrompt(getText(emailPromptId), savedEmail);
addViewToDialog(userEmailView);
}

return root;
}

/**
* adds a view to the end of the dialog
*
* @param v the view to add
*/
protected final void addViewToDialog(View v) {
scrollable.addView(v);
}

/**
* Creates a main view containing text of resDialogText
*
* @return the main view
*/
protected View getMainView() {
final TextView text = new TextView(this);
final int dialogTextId = ACRA.getConfig().resDialogText();
if (dialogTextId != 0) {
text.setText(getText(dialogTextId));
}
return text;
}

/**
* creates a comment prompt
*
* @param label the label of the prompt
* @param savedComment the content of the prompt (usually from a saved state)
* @return the comment prompt
*/
protected EditText getCommentPrompt(CharSequence label, CharSequence savedComment) {
final TextView labelView = new TextView(this);
labelView.setText(label);

labelView.setPadding(labelView.getPaddingLeft(), 10, labelView.getPaddingRight(), labelView.getPaddingBottom());
scrollable.addView(labelView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

EditText userCommentView = new EditText(this);
userCommentView.setLines(2);
if (savedComment != null) {
userCommentView.setText(savedComment);
}
return userCommentView;
}

/**
* creates an email prompt
*
* @param label the label of the prompt
* @param savedEmail the content of the prompt (usually from a saved state)
* @return the email prompt
*/
protected EditText getEmailPrompt(CharSequence label, CharSequence savedEmail) {
final TextView labelView = new TextView(this);
labelView.setText(label);

labelView.setPadding(labelView.getPaddingLeft(), 10, labelView.getPaddingRight(), labelView.getPaddingBottom());
scrollable.addView(labelView);

EditText userEmailView = new EditText(this);
userEmailView.setSingleLine();
userEmailView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

if (savedEmail != null) {
userEmailView.setText(savedEmail);
} else {
final SharedPreferences prefs = ACRA.getACRASharedPreferences();
userEmailView.setText(prefs.getString(ACRA.PREF_USER_EMAIL_ADDRESS, ""));
}
return userEmailView;
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
Expand Down Expand Up @@ -166,4 +219,11 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putString(STATE_EMAIL, userEmailView.getText().toString());
}
}

/**
* @return the AlertDialog displayed by this Activity
*/
protected AlertDialog getDialog() {
return mDialog;
}
}

0 comments on commit 68b01cf

Please sign in to comment.