Java tutorial
/* * Copyright 2014-2017 Eduard Ereza Martnez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cat.ereza.customactivityoncrash.activity; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.DialogInterface; import android.content.res.TypedArray; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AppCompatActivity; import android.util.TypedValue; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import cat.ereza.customactivityoncrash.CustomActivityOnCrash; import cat.ereza.customactivityoncrash.R; import cat.ereza.customactivityoncrash.config.CaocConfig; public final class DefaultErrorReportActivity extends AppCompatActivity { @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 = (Button) findViewById(R.id.customactivityoncrash_error_activity_restart_button); final CaocConfig config = CustomActivityOnCrash.getConfigFromIntent(getIntent()); 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(DefaultErrorReportActivity.this, config); } }); } else { restartButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomActivityOnCrash.closeApplication(DefaultErrorReportActivity.this, config); } }); } Button moreInfoButton = (Button) 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(DefaultErrorReportActivity.this) .setTitle(R.string.customactivityoncrash_error_activity_error_details_title) .setMessage(CustomActivityOnCrash .getAllErrorDetailsFromIntent(DefaultErrorReportActivity.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(); Toast.makeText(DefaultErrorReportActivity.this, R.string.customactivityoncrash_error_activity_error_details_copied, Toast.LENGTH_SHORT).show(); } }) .show(); TextView textView = (TextView) dialog.findViewById(android.R.id.message); 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 = ((ImageView) findViewById(R.id.customactivityoncrash_error_activity_image)); if (defaultErrorActivityDrawableId != null) { errorImageView.setImageDrawable( ResourcesCompat.getDrawable(getResources(), defaultErrorActivityDrawableId, getTheme())); } } private void copyErrorToClipboard() { String errorInformation = CustomActivityOnCrash .getAllErrorDetailsFromIntent(DefaultErrorReportActivity.this, getIntent()); ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText( getString(R.string.customactivityoncrash_error_activity_error_details_clipboard_label), errorInformation); clipboard.setPrimaryClip(clip); } }