Skip to content

Commit

Permalink
Merge pull request #5 from TheAndroidMaster/develop
Browse files Browse the repository at this point in the history
Version 0.0.2
  • Loading branch information
fennifith authored Jun 22, 2018
2 parents c7a9d33 + 49dbc91 commit 003031a
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 141 deletions.
13 changes: 5 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "0.0.1"
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -20,11 +20,8 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile project(path: ':crasher')
compile 'com.android.support:appcompat-v7:27.0.2'
testCompile 'junit:junit:4.12'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation project(path: ':crasher')
}

This file was deleted.

9 changes: 9 additions & 0 deletions app/src/main/java/james/crashersample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MainActivity extends AppCompatActivity implements Crasher.OnCrashLi

private SwitchCompat stackOverflowSwitch;
private SwitchCompat crashActivitySwitch;
private SwitchCompat backgroundSwitch;
private AppCompatButton colorButton;

private int color;
Expand All @@ -35,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {

stackOverflowSwitch = findViewById(R.id.stackOverflow);
crashActivitySwitch = findViewById(R.id.crashActivity);
backgroundSwitch = findViewById(R.id.background);
colorButton = findViewById(R.id.color);

findViewById(R.id.nullPointer).setOnClickListener(new View.OnClickListener() {
Expand All @@ -58,6 +60,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});

backgroundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
crasher.setBackgroundCrash(b);
}
});

setColor(ContextCompat.getColor(this, R.color.colorPrimary));
colorButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
android:padding="8dp"
android:text="isCrashActivityEnabled" />

<android.support.v7.widget.SwitchCompat
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="isBackground" />

<android.support.v7.widget.AppCompatButton
android:id="@+id/color"
android:layout_width="wrap_content"
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/james/crashersample/ExampleUnitTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.2.0-beta01'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
18 changes: 7 additions & 11 deletions crasher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
ext {
PUBLISH_GROUP_ID = 'me.jfenn'
PUBLISH_ARTIFACT_ID = 'crasher'
PUBLISH_VERSION = '0.0.1'
PUBLISH_VERSION = '0.0.2'
}

android {
Expand All @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "0.0.1"
versionCode 2
versionName "0.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true

Expand All @@ -28,14 +28,10 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:design:27.0.2'
compile 'james.buttons:buttons:0.0.3'
testCompile 'junit:junit:4.12'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'james.buttons:buttons:0.0.3'
}

apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'

This file was deleted.

36 changes: 29 additions & 7 deletions crasher/src/main/java/james/crasher/Crasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand All @@ -17,6 +18,7 @@
import java.util.List;

import james.crasher.activities.CrashActivity;
import james.crasher.utils.CrashUtils;

public class Crasher implements Thread.UncaughtExceptionHandler {

Expand All @@ -29,6 +31,7 @@ public class Crasher implements Thread.UncaughtExceptionHandler {
private boolean isBackground;

private String email;
private String debugMessage;
private Integer color;

public Crasher(Context context) {
Expand Down Expand Up @@ -93,6 +96,16 @@ public String getEmail() {
return email;
}

public Crasher setDebugMessage(String debugMessage) {
this.debugMessage = debugMessage;
return this;
}

@Nullable
public String getDebugMessage() {
return debugMessage;
}

public Crasher setColor(@ColorInt int color) {
this.color = color;
return this;
Expand All @@ -107,25 +120,30 @@ public Integer getColor() {
@Override
public void uncaughtException(Thread t, final Throwable e) {
Intent intent = null;

StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
String stack = writer.toString();

if ((BuildConfig.DEBUG && isStackOverflow) || isForceStackOverflow) {
e.printStackTrace();
Log.d("Crasher", "Exception thrown: " + e.getClass().getName() + ". Opening StackOverflow query for \"" + e.getMessage() + "\".");

intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/search?q=[java][android]" + e.getMessage()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (isCrashActivity) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));

intent = new Intent(context, CrashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CrashActivity.EXTRA_NAME, e.getClass().getName());
intent.putExtra(CrashActivity.EXTRA_MESSAGE, e.getLocalizedMessage());
intent.putExtra(CrashActivity.EXTRA_STACK_TRACE, writer.toString());
intent.putExtra(CrashActivity.EXTRA_STACK_TRACE, stack);

if (email != null)
intent.putExtra(CrashActivity.EXTRA_EMAIL, email);

if (debugMessage != null)
intent.putExtra(CrashActivity.EXTRA_DEBUG_MESSAGE, debugMessage);

if (color != null)
intent.putExtra(CrashActivity.EXTRA_COLOR, color);
} else e.printStackTrace();
Expand All @@ -137,12 +155,16 @@ public void uncaughtException(Thread t, final Throwable e) {
NotificationCompat.Builder builder;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(new NotificationChannel("crashNotifications", context.getString(R.string.title_crash_notifications), NotificationManager.IMPORTANCE_DEFAULT));
manager.createNotificationChannel(new NotificationChannel("crashNotifications", context.getString(R.string.title_crasher_crash_notifications), NotificationManager.IMPORTANCE_DEFAULT));
builder = new NotificationCompat.Builder(context, "crashNotifications");
} else builder = new NotificationCompat.Builder(context);

manager.notify(0, builder.setContentTitle(String.format(context.getString(R.string.title_crash_notifications), context.getString(R.string.app_name)))
.setContentText(String.format(context.getString(R.string.title_email), e.getClass().getName(), context.getString(R.string.app_name)))
manager.notify(0, builder.setContentTitle(String.format(context.getString(R.string.title_crasher_crash_notification), context.getString(R.string.app_name), e.getClass().getName(), CrashUtils.getCause(context, stack)))
.setContentText(context.getString(R.string.msg_crasher_crash_notification))
.setSmallIcon(R.drawable.ic_crasher_bug)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build());
}
} else context.startActivity(intent);
Expand Down
45 changes: 32 additions & 13 deletions crasher/src/main/java/james/crasher/activities/CrashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import james.crasher.BuildConfig;
import james.crasher.R;
import james.crasher.utils.ColorUtils;
import james.crasher.utils.CrashUtils;
import james.crasher.utils.ImageUtils;

public class CrashActivity extends AppCompatActivity implements View.OnClickListener {
Expand All @@ -31,6 +32,7 @@ public class CrashActivity extends AppCompatActivity implements View.OnClickList
public static final String EXTRA_STACK_TRACE = "james.crasher.EXTRA_STACK_TRACE";

public static final String EXTRA_EMAIL = "james.crasher.EXTRA_EMAIL";
public static final String EXTRA_DEBUG_MESSAGE = "james.crasher.EXTRA_DEBUG_MESSAGE";
public static final String EXTRA_COLOR = "james.crasher.EXTRA_COLOR";

private Toolbar toolbar;
Expand All @@ -45,6 +47,8 @@ public class CrashActivity extends AppCompatActivity implements View.OnClickList
private ImageView stackTraceArrow;
private TextView stackTrace;

private String body;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -87,23 +91,38 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(String.format(Locale.getDefault(), getString(R.string.title_crashed), getString(R.string.app_name)));
actionBar.setHomeAsUpIndicator(ImageUtils.getVectorDrawable(this, R.drawable.ic_back, isColorDark ? Color.WHITE : Color.BLACK));
actionBar.setTitle(String.format(Locale.getDefault(), getString(R.string.title_crasher_crashed), getString(R.string.app_name)));
actionBar.setHomeAsUpIndicator(ImageUtils.getVectorDrawable(this, R.drawable.ic_crasher_back, isColorDark ? Color.WHITE : Color.BLACK));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(colorDark);
getWindow().setNavigationBarColor(colorDark);
}

name.setText(getIntent().getStringExtra(EXTRA_NAME));
message.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
description.setText(String.format(Locale.getDefault(), getString(R.string.msg_crashed), getString(R.string.app_name)));
String stack = getIntent().getStringExtra(EXTRA_STACK_TRACE);
String stackCause = CrashUtils.getCause(this, stack);

String nameString = getIntent().getStringExtra(EXTRA_NAME) + (stackCause != null ? " at " + stackCause : "");
String messageString = getIntent().getStringExtra(EXTRA_NAME);

name.setText(nameString);
if (messageString != null && messageString.length() > 0)
message.setText(messageString);
else message.setVisibility(View.GONE);

stackTrace.setText(getIntent().getStringExtra(EXTRA_STACK_TRACE));
description.setText(String.format(Locale.getDefault(), getString(R.string.msg_crasher_crashed), getString(R.string.app_name)));

stackTrace.setText(stack);
stackTraceHeader.setOnClickListener(this);
if (BuildConfig.DEBUG)
stackTraceHeader.callOnClick();

body = nameString + "\n" + (messageString != null ? messageString : "") + "\n\n" + stack
+ "\n\nAndroid Version: " + Build.VERSION.SDK_INT
+ "\nDevice Manufacturer: " + Build.MANUFACTURER
+ "\nDevice Model: " + Build.MODEL
+ "\n\n" + (getIntent().hasExtra(EXTRA_DEBUG_MESSAGE) ? getIntent().getStringExtra(EXTRA_DEBUG_MESSAGE) : "");
}

@Override
Expand All @@ -126,25 +145,25 @@ else if (service instanceof android.text.ClipboardManager)
} else if (v.getId() == R.id.share) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, stackTrace.getText().toString()); //TODO: include device info, app name, build, version, etc
intent.putExtra(Intent.EXTRA_TEXT, body);

startActivity(Intent.createChooser(intent, getString(R.string.action_share)));
startActivity(Intent.createChooser(intent, getString(R.string.title_crasher_share)));
} else if (v.getId() == R.id.email) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.setData(Uri.parse("mailto:" + getIntent().getStringExtra(EXTRA_EMAIL)));
intent.putExtra(Intent.EXTRA_EMAIL, getIntent().getStringExtra(EXTRA_EMAIL));
intent.putExtra(Intent.EXTRA_SUBJECT, String.format(Locale.getDefault(), getString(R.string.title_email), name.getText().toString(), getString(R.string.app_name)));
intent.putExtra(Intent.EXTRA_TEXT, stackTrace.getText().toString()); //TODO: include device info, app name, build, version, etc
intent.putExtra(Intent.EXTRA_SUBJECT, String.format(Locale.getDefault(), getString(R.string.title_crasher_exception), name.getText().toString(), getString(R.string.app_name)));
intent.putExtra(Intent.EXTRA_TEXT, body);

startActivity(Intent.createChooser(intent, getString(R.string.action_send_email)));
startActivity(Intent.createChooser(intent, getString(R.string.title_crasher_send_email)));
} else if (v.getId() == R.id.stackTraceHeader) {
if (stackTrace.getVisibility() == View.GONE) {
stackTrace.setVisibility(View.VISIBLE);
stackTraceArrow.animate().rotation(180).start();
stackTraceArrow.animate().scaleY(-1).start();
} else {
stackTrace.setVisibility(View.GONE);
stackTraceArrow.animate().rotation(0).start();
stackTraceArrow.animate().scaleY(1).start();
}
}
}
Expand Down
Loading

0 comments on commit 003031a

Please sign in to comment.