Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Dialogs #395

Merged
merged 4 commits into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/java/org/acra/dialog/BaseCrashReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@
* The methods sendCrash(comment, usrEmail) and cancelReports() can be used to send or cancel
* sending of reports respectively.
*
* This Activity will be instantiated with 3 arguments:
* This Activity will be instantiated with 3 (or 4) arguments:
* <ol>
* <li>{@link ACRAConstants#EXTRA_REPORT_FILE_NAME}</li>
* <li>{@link ACRAConstants#EXTRA_REPORT_EXCEPTION}</li>
* <li>{@link ACRAConstants#EXTRA_REPORT_CONFIG}</li>
* <li>{@link ACRAConstants#EXTRA_FORCE_CANCEL} (optional)</li>
* </ol>
*/
public abstract class BaseCrashReportDialog extends Activity {

private File reportFile;
private ACRAConfiguration config;
private Throwable exception;

@CallSuper
@Override
Expand All @@ -49,6 +51,9 @@ protected void onCreate(Bundle savedInstanceState) {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "CrashReportDialog extras=" + getIntent().getExtras());

config = (ACRAConfiguration) getIntent().getSerializableExtra(ACRAConstants.EXTRA_REPORT_CONFIG);
if(config == null) {
throw new IllegalStateException("CrashReportDialog has to be called with extra ACRAConstants#EXTRA_REPORT_CONFIG");
}

final boolean forceCancel = getIntent().getBooleanExtra(ACRAConstants.EXTRA_FORCE_CANCEL, false);
if (forceCancel) {
Expand All @@ -61,8 +66,9 @@ protected void onCreate(Bundle savedInstanceState) {
reportFile = (File) getIntent().getSerializableExtra(ACRAConstants.EXTRA_REPORT_FILE);
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Opening CrashReportDialog for " + reportFile);
if (reportFile == null) {
finish();
throw new IllegalStateException("CrashReportDialog has to be called with extra ACRAConstants#EXTRA_REPORT_FILE");
}
exception = (Throwable) getIntent().getSerializableExtra(ACRAConstants.EXTRA_REPORT_EXCEPTION);
}


Expand Down Expand Up @@ -101,4 +107,12 @@ protected void sendCrash(@Nullable String comment, @Nullable String userEmail) {
ToastSender.sendToast(getApplicationContext(), toastId, Toast.LENGTH_LONG);
}
}

protected final ACRAConfiguration getConfig(){
return config;
}

protected final Throwable getException(){
return exception;
}
}
102 changes: 58 additions & 44 deletions src/main/java/org/acra/dialog/CrashReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.config.ACRAConfiguration;
import org.acra.prefs.SharedPreferencesFactory;


Expand All @@ -27,13 +27,13 @@
**/
public class CrashReportDialog extends BaseCrashReportDialog implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {

private static final int PADDING = 10;
private static final String STATE_EMAIL = "email";
private static final String STATE_COMMENT = "comment";

private LinearLayout scrollable;
private EditText userCommentView;
private EditText userEmailView;
private ACRAConfiguration config;
private SharedPreferencesFactory sharedPreferencesFactory;

private AlertDialog mDialog;
Expand All @@ -45,8 +45,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

scrollable = new LinearLayout(this);
scrollable.setOrientation(LinearLayout.VERTICAL);
config = (ACRAConfiguration) getIntent().getSerializableExtra(ACRAConstants.EXTRA_REPORT_CONFIG);
sharedPreferencesFactory = new SharedPreferencesFactory(getApplicationContext(), config);
sharedPreferencesFactory = new SharedPreferencesFactory(getApplicationContext(), getConfig());

buildAndShowDialog(savedInstanceState);
}
Expand All @@ -57,17 +56,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
*/
protected void buildAndShowDialog(@Nullable Bundle savedInstanceState){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
final int titleResourceId = config.resDialogTitle();
if (titleResourceId != 0) {
final int titleResourceId = getConfig().resDialogTitle();
if (titleResourceId != ACRAConstants.DEFAULT_RES_VALUE) {
dialogBuilder.setTitle(titleResourceId);
}
final int iconResourceId = config.resDialogIcon();
if (iconResourceId != 0) {
final int iconResourceId = getConfig().resDialogIcon();
if (iconResourceId != ACRAConstants.DEFAULT_RES_VALUE) {
dialogBuilder.setIcon(iconResourceId);
}
dialogBuilder.setView(buildCustomView(savedInstanceState));
dialogBuilder.setPositiveButton(getText(config.resDialogPositiveButtonText()), CrashReportDialog.this);
dialogBuilder.setNegativeButton(getText(config.resDialogNegativeButtonText()), CrashReportDialog.this);
dialogBuilder.setPositiveButton(getText(getConfig().resDialogPositiveButtonText()), CrashReportDialog.this);
dialogBuilder.setNegativeButton(getText(getConfig().resDialogNegativeButtonText()), CrashReportDialog.this);

mDialog = dialogBuilder.create();
mDialog.setCanceledOnTouchOutside(false);
Expand All @@ -77,41 +76,40 @@ protected void buildAndShowDialog(@Nullable Bundle savedInstanceState){

@NonNull
protected View buildCustomView(@Nullable Bundle savedInstanceState) {
final LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
root.setPadding(10, 10, 10, 10);
final ScrollView root = new ScrollView(this);
root.setPadding(PADDING, PADDING, PADDING, PADDING);
root.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
root.setFocusable(true);
root.setFocusableInTouchMode(true);

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

addViewToDialog(getMainView());

// Add an optional prompt for user comments
final int commentPromptId = config.resDialogCommentPrompt();
if (commentPromptId != 0) {
View comment = getCommentLabel();
if(comment != null){
comment.setPadding(comment.getPaddingLeft(), PADDING, comment.getPaddingRight(), comment.getPaddingBottom());
addViewToDialog(comment);
String savedComment = null;
if (savedInstanceState != null) {
savedComment = savedInstanceState.getString(STATE_COMMENT);
}
userCommentView = getCommentPrompt(getText(commentPromptId), savedComment);
userCommentView = getCommentPrompt(savedComment);
addViewToDialog(userCommentView);
}

// Add an optional user email field
final int emailPromptId = config.resDialogEmailPrompt();
if (emailPromptId != 0) {
View email = getEmailLabel();
if(email != null){
email.setPadding(email.getPaddingLeft(), PADDING, email.getPaddingRight(), email.getPaddingBottom());
addViewToDialog(email);
String savedEmail = null;
if (savedInstanceState != null) {
savedEmail = savedInstanceState.getString(STATE_EMAIL);
}
userEmailView = getEmailPrompt(getText(emailPromptId), savedEmail);
userEmailView = getEmailPrompt(savedEmail);
addViewToDialog(userEmailView);
}

return root;
}

Expand All @@ -125,35 +123,43 @@ protected final void addViewToDialog(View v) {
}

/**
* Creates a main view containing text of resDialogText
* Creates a main view containing text of resDialogText, or nothing if not found
*
* @return the main view
*/
@NonNull
protected View getMainView() {
final TextView text = new TextView(this);
final int dialogTextId = config.resDialogText();
if (dialogTextId != 0) {
final int dialogTextId = getConfig().resDialogText();
if (dialogTextId != ACRAConstants.DEFAULT_RES_VALUE) {
text.setText(getText(dialogTextId));
}
return text;
}

/**
* creates a comment label view with resDialogCommentPrompt as text
* @return the label or null if there is no resource
*/
@Nullable
protected View getCommentLabel() {
final int commentPromptId = getConfig().resDialogCommentPrompt();
if (commentPromptId != ACRAConstants.DEFAULT_RES_VALUE) {
final TextView labelView = new TextView(this);
labelView.setText(getText(commentPromptId));
return labelView;
}
return null;
}

/**
* 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
*/
@NonNull
protected EditText getCommentPrompt(CharSequence label, @Nullable 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.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

protected EditText getCommentPrompt(@Nullable CharSequence savedComment) {
EditText userCommentView = new EditText(this);
userCommentView.setLines(2);
if (savedComment != null) {
Expand All @@ -162,21 +168,29 @@ protected EditText getCommentPrompt(CharSequence label, @Nullable CharSequence s
return userCommentView;
}

/**
* creates a email label view with resDialogEmailPrompt as text
* @return the label or null if there is no resource
*/
@Nullable
protected View getEmailLabel(){
final int emailPromptId = getConfig().resDialogEmailPrompt();
if (emailPromptId != ACRAConstants.DEFAULT_RES_VALUE) {
final TextView labelView = new TextView(this);
labelView.setText(getText(emailPromptId));
return labelView;
}
return null;
}

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

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

protected EditText getEmailPrompt(@Nullable CharSequence savedEmail) {
EditText userEmailView = new EditText(this);
userEmailView.setSingleLine();
userEmailView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
Expand Down