Skip to content

Commit 68b01cf

Browse files
Merge pull request #341 from F43nd1r/master
Make Dialog easier to extend
2 parents aa8cda5 + 5525812 commit 68b01cf

File tree

2 files changed

+102
-42
lines changed

2 files changed

+102
-42
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
5+
buildToolsVersion "23.0.2"
66

77
lintOptions {
88
abortOnError false

src/main/java/org/acra/CrashReportDialog.java

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,27 @@ public class CrashReportDialog extends BaseCrashReportDialog implements DialogIn
2222

2323
private static final String STATE_EMAIL = "email";
2424
private static final String STATE_COMMENT = "comment";
25+
private LinearLayout scrollable;
2526
private EditText userCommentView;
2627
private EditText userEmailView;
2728

28-
AlertDialog mDialog;
29+
private AlertDialog mDialog;
2930

3031
@Override
3132
protected void onCreate(Bundle savedInstanceState) {
3233
super.onCreate(savedInstanceState);
3334

35+
scrollable = new LinearLayout(this);
36+
scrollable.setOrientation(LinearLayout.VERTICAL);
37+
38+
buildAndShowDialog(savedInstanceState);
39+
}
40+
41+
/**
42+
* Build the dialog from the values in config
43+
* @param savedInstanceState old state to restore
44+
*/
45+
protected void buildAndShowDialog(Bundle savedInstanceState){
3446
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
3547
final int titleResourceId = ACRA.getConfig().resDialogTitle();
3648
if (titleResourceId != 0) {
@@ -60,66 +72,107 @@ protected View buildCustomView(Bundle savedInstanceState) {
6072

6173
final ScrollView scroll = new ScrollView(this);
6274
root.addView(scroll, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
63-
final LinearLayout scrollable = new LinearLayout(this);
64-
scrollable.setOrientation(LinearLayout.VERTICAL);
6575
scroll.addView(scrollable);
6676

67-
final TextView text = new TextView(this);
68-
final int dialogTextId = ACRA.getConfig().resDialogText();
69-
if (dialogTextId != 0) {
70-
text.setText(getText(dialogTextId));
71-
}
72-
scrollable.addView(text);
77+
addViewToDialog(getMainView());
7378

7479
// Add an optional prompt for user comments
7580
final int commentPromptId = ACRA.getConfig().resDialogCommentPrompt();
7681
if (commentPromptId != 0) {
77-
final TextView label = new TextView(this);
78-
label.setText(getText(commentPromptId));
79-
80-
label.setPadding(label.getPaddingLeft(), 10, label.getPaddingRight(), label.getPaddingBottom());
81-
scrollable.addView(label, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
82-
83-
userCommentView = new EditText(this);
84-
userCommentView.setLines(2);
82+
String savedComment = null;
8583
if (savedInstanceState != null) {
86-
String savedValue = savedInstanceState.getString(STATE_COMMENT);
87-
if (savedValue != null) {
88-
userCommentView.setText(savedValue);
89-
}
84+
savedComment = savedInstanceState.getString(STATE_COMMENT);
9085
}
91-
scrollable.addView(userCommentView);
86+
userCommentView = getCommentPrompt(getText(commentPromptId), savedComment);
87+
addViewToDialog(userCommentView);
9288
}
9389

9490
// Add an optional user email field
9591
final int emailPromptId = ACRA.getConfig().resDialogEmailPrompt();
9692
if (emailPromptId != 0) {
97-
final TextView label = new TextView(this);
98-
label.setText(getText(emailPromptId));
99-
100-
label.setPadding(label.getPaddingLeft(), 10, label.getPaddingRight(), label.getPaddingBottom());
101-
scrollable.addView(label);
102-
103-
userEmailView = new EditText(this);
104-
userEmailView.setSingleLine();
105-
userEmailView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
106-
107-
String savedValue = null;
93+
String savedEmail = null;
10894
if (savedInstanceState != null) {
109-
savedValue = savedInstanceState.getString(STATE_EMAIL);
110-
}
111-
if (savedValue != null) {
112-
userEmailView.setText(savedValue);
113-
} else {
114-
final SharedPreferences prefs = ACRA.getACRASharedPreferences();
115-
userEmailView.setText(prefs.getString(ACRA.PREF_USER_EMAIL_ADDRESS, ""));
95+
savedEmail = savedInstanceState.getString(STATE_EMAIL);
11696
}
117-
scrollable.addView(userEmailView);
97+
userEmailView = getEmailPrompt(getText(emailPromptId), savedEmail);
98+
addViewToDialog(userEmailView);
11899
}
119100

120101
return root;
121102
}
122103

104+
/**
105+
* adds a view to the end of the dialog
106+
*
107+
* @param v the view to add
108+
*/
109+
protected final void addViewToDialog(View v) {
110+
scrollable.addView(v);
111+
}
112+
113+
/**
114+
* Creates a main view containing text of resDialogText
115+
*
116+
* @return the main view
117+
*/
118+
protected View getMainView() {
119+
final TextView text = new TextView(this);
120+
final int dialogTextId = ACRA.getConfig().resDialogText();
121+
if (dialogTextId != 0) {
122+
text.setText(getText(dialogTextId));
123+
}
124+
return text;
125+
}
126+
127+
/**
128+
* creates a comment prompt
129+
*
130+
* @param label the label of the prompt
131+
* @param savedComment the content of the prompt (usually from a saved state)
132+
* @return the comment prompt
133+
*/
134+
protected EditText getCommentPrompt(CharSequence label, CharSequence savedComment) {
135+
final TextView labelView = new TextView(this);
136+
labelView.setText(label);
137+
138+
labelView.setPadding(labelView.getPaddingLeft(), 10, labelView.getPaddingRight(), labelView.getPaddingBottom());
139+
scrollable.addView(labelView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
140+
141+
EditText userCommentView = new EditText(this);
142+
userCommentView.setLines(2);
143+
if (savedComment != null) {
144+
userCommentView.setText(savedComment);
145+
}
146+
return userCommentView;
147+
}
148+
149+
/**
150+
* creates an email prompt
151+
*
152+
* @param label the label of the prompt
153+
* @param savedEmail the content of the prompt (usually from a saved state)
154+
* @return the email prompt
155+
*/
156+
protected EditText getEmailPrompt(CharSequence label, CharSequence savedEmail) {
157+
final TextView labelView = new TextView(this);
158+
labelView.setText(label);
159+
160+
labelView.setPadding(labelView.getPaddingLeft(), 10, labelView.getPaddingRight(), labelView.getPaddingBottom());
161+
scrollable.addView(labelView);
162+
163+
EditText userEmailView = new EditText(this);
164+
userEmailView.setSingleLine();
165+
userEmailView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
166+
167+
if (savedEmail != null) {
168+
userEmailView.setText(savedEmail);
169+
} else {
170+
final SharedPreferences prefs = ACRA.getACRASharedPreferences();
171+
userEmailView.setText(prefs.getString(ACRA.PREF_USER_EMAIL_ADDRESS, ""));
172+
}
173+
return userEmailView;
174+
}
175+
123176
@Override
124177
public void onClick(DialogInterface dialog, int which) {
125178
if (which == DialogInterface.BUTTON_POSITIVE) {
@@ -166,4 +219,11 @@ protected void onSaveInstanceState(Bundle outState) {
166219
outState.putString(STATE_EMAIL, userEmailView.getText().toString());
167220
}
168221
}
222+
223+
/**
224+
* @return the AlertDialog displayed by this Activity
225+
*/
226+
protected AlertDialog getDialog() {
227+
return mDialog;
228+
}
169229
}

0 commit comments

Comments
 (0)