Search in sources :

Example 1 with CaocConfig

use of cat.ereza.customactivityoncrash.config.CaocConfig in project CustomActivityOnCrash by Ereza.

the class DefaultErrorActivity method onCreate.

@SuppressLint("PrivateResource")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This is needed to avoid a crash if the developer has not specified
    // an app-level theme that extends Theme.AppCompat
    TypedArray a = obtainStyledAttributes(R.styleable.AppCompatTheme);
    if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
        setTheme(R.style.Theme_AppCompat_Light_DarkActionBar);
    }
    a.recycle();
    setContentView(R.layout.customactivityoncrash_default_error_activity);
    // Close/restart button logic:
    // If a class if set, use restart.
    // Else, use close and just finish the app.
    // It is recommended that you follow this logic if implementing a custom error activity.
    Button restartButton = findViewById(R.id.customactivityoncrash_error_activity_restart_button);
    final CaocConfig config = CustomActivityOnCrash.getConfigFromIntent(getIntent());
    if (config == null) {
        // This should never happen - Just finish the activity to avoid a recursive crash.
        finish();
        return;
    }
    if (config.isShowRestartButton() && config.getRestartActivityClass() != null) {
        restartButton.setText(R.string.customactivityoncrash_error_activity_restart_app);
        restartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.restartApplication(DefaultErrorActivity.this, config);
            }
        });
    } else {
        restartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.closeApplication(DefaultErrorActivity.this, config);
            }
        });
    }
    Button moreInfoButton = findViewById(R.id.customactivityoncrash_error_activity_more_info_button);
    if (config.isShowErrorDetails()) {
        moreInfoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // We retrieve all the error data and show it
                AlertDialog dialog = new AlertDialog.Builder(DefaultErrorActivity.this).setTitle(R.string.customactivityoncrash_error_activity_error_details_title).setMessage(CustomActivityOnCrash.getAllErrorDetailsFromIntent(DefaultErrorActivity.this, getIntent())).setPositiveButton(R.string.customactivityoncrash_error_activity_error_details_close, null).setNeutralButton(R.string.customactivityoncrash_error_activity_error_details_copy, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        copyErrorToClipboard();
                    }
                }).show();
                TextView textView = dialog.findViewById(android.R.id.message);
                if (textView != null) {
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.customactivityoncrash_error_activity_error_details_text_size));
                }
            }
        });
    } else {
        moreInfoButton.setVisibility(View.GONE);
    }
    Integer defaultErrorActivityDrawableId = config.getErrorDrawable();
    ImageView errorImageView = findViewById(R.id.customactivityoncrash_error_activity_image);
    if (defaultErrorActivityDrawableId != null) {
        errorImageView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), defaultErrorActivityDrawableId, getTheme()));
    }
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) CaocConfig(cat.ereza.customactivityoncrash.config.CaocConfig) DialogInterface(android.content.DialogInterface) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) SuppressLint(android.annotation.SuppressLint) Button(android.widget.Button) TypedArray(android.content.res.TypedArray) TextView(android.widget.TextView) ImageView(android.widget.ImageView) SuppressLint(android.annotation.SuppressLint)

Example 2 with CaocConfig

use of cat.ereza.customactivityoncrash.config.CaocConfig in project CustomActivityOnCrash by Ereza.

the class CustomErrorActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_error);
    // **IMPORTANT**
    // The custom error activity in this sample is uglier than the default one and just
    // for demonstration purposes, please don't copy it to your project!
    // We recommend taking the original library's DefaultErrorActivity as a basis.
    // Of course, you are free to implement it as you wish in your application.
    // These four methods are available for you to use:
    // CustomActivityOnCrash.getStackTraceFromIntent(getIntent()): gets the stack trace as a string
    // CustomActivityOnCrash.getActivityLogFromIntent(getIntent()): gets the activity log as a string
    // CustomActivityOnCrash.getAllErrorDetailsFromIntent(context, getIntent()): returns all error details including stacktrace as a string
    // CustomActivityOnCrash.getConfigFromIntent(getIntent()): returns the config of the library when the error happened
    // Now, treat here the error as you wish. If you allow the user to restart or close the app,
    // don't forget to call the appropriate methods.
    // Otherwise, if you don't finish the activity, you will get the CustomErrorActivity on the activity stack and it will be visible again under some circumstances.
    // Also, you will get multiprocess problems in API<17.
    TextView errorDetailsText = findViewById(R.id.error_details);
    errorDetailsText.setText(CustomActivityOnCrash.getStackTraceFromIntent(getIntent()));
    Button restartButton = findViewById(R.id.restart_button);
    final CaocConfig config = CustomActivityOnCrash.getConfigFromIntent(getIntent());
    if (config == null) {
        // This should never happen - Just finish the activity to avoid a recursive crash.
        finish();
        return;
    }
    if (config.isShowRestartButton() && config.getRestartActivityClass() != null) {
        restartButton.setText(R.string.restart_app);
        restartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.restartApplication(CustomErrorActivity.this, config);
            }
        });
    } else {
        restartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.closeApplication(CustomErrorActivity.this, config);
            }
        });
    }
}
Also used : CaocConfig(cat.ereza.customactivityoncrash.config.CaocConfig) Button(android.widget.Button) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View)

Aggregations

View (android.view.View)2 Button (android.widget.Button)2 TextView (android.widget.TextView)2 CaocConfig (cat.ereza.customactivityoncrash.config.CaocConfig)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 TypedArray (android.content.res.TypedArray)1 ImageView (android.widget.ImageView)1 AlertDialog (androidx.appcompat.app.AlertDialog)1