Skip to content

Commit

Permalink
Use builder pattern for config, use AppCompat and autoinitialization,…
Browse files Browse the repository at this point in the history
… and don't show build date if not present. Fixes #26, #28, #30, #31.
  • Loading branch information
Ereza committed May 26, 2017
1 parent 07e9912 commit fb2bb4e
Show file tree
Hide file tree
Showing 34 changed files with 561 additions and 877 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Custom Activity on Crash library
Copyright (c) 2014-2016 Eduard Ereza, http://www.eduardereza.com/
Copyright (c) 2014-2017 Eduard Ereza, http://www.eduardereza.com/

This product is licensed under the terms of the Apache Software License 2.0. See the LICENSE file for the full license text.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ If you use this, the activity **must** be declared in your `AndroidManifest.xml`
Example:
```xml
<activity
android:name="cat.ereza.sample.customactivityoncrash.activity.CustomErrorActivity"
android:name="cat.ereza.customactivityoncrash.sample.CustomErrorActivity"
android:label="@string/error_title"
android:process=":error_activity" />
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.3.2'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=1.5.0
VERSION_CODE=7
VERSION_NAME=1.6.0
VERSION_CODE=8
GROUP=cat.ereza

POM_DESCRIPTION=Android library that allows launching a custom activity when your app crashes, instead of showing the hated "Unfortunately, X has stopped" dialog.
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Fri May 26 10:00:14 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
34 changes: 11 additions & 23 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
/*
* Copyright 2015 Eduard Ereza Martínez
*
* 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.
*/

apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.3"
resourcePrefix 'customactivityoncrash_'

defaultConfig {
minSdkVersion 4
targetSdkVersion 23
versionCode 7
versionName "1.5.0"
minSdkVersion 14
targetSdkVersion 25
versionCode 8
versionName "2.0.0"
}
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

dependencies {
compile "com.android.support:appcompat-v7:25.3.1"
}
16 changes: 0 additions & 16 deletions library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#
# Copyright 2015 Eduard Ereza Martínez
#
# 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.
#

POM_NAME=CustomActivityOnCrash library
POM_ARTIFACT_ID=customactivityoncrash
POM_PACKAGING=aar
27 changes: 7 additions & 20 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
<!--
~ Copyright 2015 Eduard Ereza Martínez
~
~ 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.
-->

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cat.ereza.customactivityoncrash">

<application>

<activity
android:name="cat.ereza.customactivityoncrash.activity.DefaultErrorActivity"
android:process=":error_activity"
android:theme="@style/CustomActivityOnCrashTheme" />

android:process=":error_activity" />
<provider
android:name="cat.ereza.customactivityoncrash.provider.CaocInitProvider"
android:authorities="${applicationId}.customactivityoncrashinitprovider"
android:exported="false"
android:initOrder="101" />
</application>

</manifest>
206 changes: 206 additions & 0 deletions library/src/main/java/cat/ereza/customactivityoncrash/CaocConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright 2014-2017 Eduard Ereza Martínez
*
* 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;

import android.app.Activity;
import android.support.annotation.DrawableRes;

import java.io.Serializable;
import java.lang.reflect.Modifier;

public class CaocConfig implements Serializable {
private boolean launchWhenInBackground = false;
private boolean showErrorDetails = true;
private boolean showRestartButton = true;
private Integer errorDrawable = null;
private Class<? extends Activity> errorActivityClass = null;
private Class<? extends Activity> restartActivityClass = null;
private CustomActivityOnCrash.EventListener eventListener = null;

public boolean isLaunchWhenInBackground() {
return launchWhenInBackground;
}

public void setLaunchWhenInBackground(boolean launchWhenInBackground) {
this.launchWhenInBackground = launchWhenInBackground;
}

public boolean isShowErrorDetails() {
return showErrorDetails;
}

public void setShowErrorDetails(boolean showErrorDetails) {
this.showErrorDetails = showErrorDetails;
}

public boolean isShowRestartButton() {
return showRestartButton;
}

public void setShowRestartButton(boolean showRestartButton) {
this.showRestartButton = showRestartButton;
}

public Integer getErrorDrawable() {
return errorDrawable;
}

public void setErrorDrawable(Integer errorDrawable) {
this.errorDrawable = errorDrawable;
}

public Class<? extends Activity> getErrorActivityClass() {
return errorActivityClass;
}

public void setErrorActivityClass(Class<? extends Activity> errorActivityClass) {
this.errorActivityClass = errorActivityClass;
}

public Class<? extends Activity> getRestartActivityClass() {
return restartActivityClass;
}

public void setRestartActivityClass(Class<? extends Activity> restartActivityClass) {
this.restartActivityClass = restartActivityClass;
}

public CustomActivityOnCrash.EventListener getEventListener() {
return eventListener;
}

public void setEventListener(CustomActivityOnCrash.EventListener eventListener) {
this.eventListener = eventListener;
}

public static class Builder {
private CaocConfig config;

public static Builder create() {
Builder builder = new Builder();
CaocConfig currentConfig = CustomActivityOnCrash.getConfig();

CaocConfig config = new CaocConfig();

if (currentConfig != null) {
config.launchWhenInBackground = currentConfig.launchWhenInBackground;
config.showErrorDetails = currentConfig.showErrorDetails;
config.showRestartButton = currentConfig.showRestartButton;
config.errorDrawable = currentConfig.errorDrawable;
config.errorActivityClass = currentConfig.errorActivityClass;
config.restartActivityClass = currentConfig.restartActivityClass;
config.eventListener = currentConfig.eventListener;
}

builder.config = config;

return builder;
}

/**
* Defines if the error activity must be launched when the app is on background.
* Set it to true if you want to launch the error activity when the app is in background,
* false if you want it not to launch and crash silently.
* This has no effect in API<14 and the error activity is always launched.
* The default is true (the app will be brought to front when a crash occurs).
*/
public Builder launchWhenInBackground(boolean launchWhenInBackground) {
config.launchWhenInBackground = launchWhenInBackground;
return this;
}

/**
* Defines if the error activity must shown the error details button.
* Set it to true if you want to show the full stack trace and device info,
* false if you want it to be hidden.
* The default is true.
*/
public Builder showErrorDetails(boolean showErrorDetails) {
config.showErrorDetails = showErrorDetails;
return this;
}

/**
* Defines if the error activity should show a restart button.
* Set it to true if you want to show a restart button,
* false if you want to show a close button.
* Note that even if restart is enabled but you app does not have any launcher activities,
* a close button will still be used by the default error activity.
* The default is true.
*/
public Builder showRestartButton(boolean showRestartButton) {
config.showRestartButton = showRestartButton;
return this;
}

/**
* Defines which drawable to use in the default error activity image.
* Set this if you want to use an image other than the default one.
* The default is R.drawable.customactivityoncrash_error_image (a cute upside-down bug).
*/
public Builder errorDrawable(@DrawableRes Integer errorDrawable) {
config.errorDrawable = errorDrawable;
return this;
}

/**
* Sets the error activity class to launch when a crash occurs.
* If null, the default error activity will be used.
*/
public Builder errorActivity(Class<? extends Activity> errorActivityClass) {
config.errorActivityClass = errorActivityClass;
return this;
}

/**
* Sets the main activity class that the error activity must launch when a crash occurs.
* If not set or set to null, the default launch activity will be used.
* If your app has no launch activities and this is not set, the default error activity will close instead.
*/
public Builder restartActivity(Class<? extends Activity> restartActivityClass) {
config.restartActivityClass = restartActivityClass;
return this;
}

/**
* Sets an event listener to be called when events occur, so they can be reported
* by the app as, for example, Google Analytics events.
* If not set or set to null, no events will be reported.
*
* @param eventListener The event listener.
* @throws IllegalArgumentException if the eventListener is an inner or anonymous class
*/
public Builder eventListener(CustomActivityOnCrash.EventListener eventListener) {
if (eventListener != null && eventListener.getClass().getEnclosingClass() != null && !Modifier.isStatic(eventListener.getClass().getModifiers())) {
throw new IllegalArgumentException("The event listener cannot be an inner or anonymous class, because it will need to be serialized. Change it to a class of its own, or make it a static inner class.");
} else {
config.eventListener = eventListener;
}
return this;
}

public CaocConfig get() {
return config;
}

public void apply() {
CustomActivityOnCrash.setConfig(config);
}
}


}
Loading

0 comments on commit fb2bb4e

Please sign in to comment.