Skip to content

Commit a7f1344

Browse files
committed
Added Builder Pattern for better understanding and also added option to enable/disable error events.
1 parent 1f92f48 commit a7f1344

3 files changed

Lines changed: 99 additions & 61 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public void onCreate() {
3030
//You will get an API KEY when you will register on pingmelive.com
3131

3232
String appName = "MyGreatApp";
33-
String errorGroupTitle = "Error for "+appName;
33+
String errorEventTitle = "Error for "+appName;
3434

35-
pingMeLive.install(getApplicationContext(),errorGroupTitle,"API_KEY");
35+
pingMeLive.install(getApplicationContext(),errorEventTitle,"API_KEY");
3636

3737
//thats it
3838

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pingmelive;
2+
3+
import android.app.Application;
4+
5+
public class application extends Application {
6+
7+
@Override
8+
public void onCreate() {
9+
super.onCreate();
10+
11+
new pingMeLive.Builder(getApplicationContext())
12+
.setErrorEventEnabled(true)
13+
.setErrorEventTitle("ERROR_TITLE")
14+
.setAPI_KEY("YOUR_API_KEY")
15+
.setAPP_ID("YOUR_APP_ID")
16+
.install();
17+
}
18+
}

pingmelive/src/main/java/com/pingmelive/pingMeLive.java

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -58,58 +58,55 @@ protected pingMeLive(Builder builder) {
5858
this.builder = builder;
5959
}
6060

61-
public static void install(@Nullable final Context context,boolean errorEvents, final String errorGroupTitle, String APIKEY,String appId) {
62-
try {
63-
if (context == null) {
64-
Log.e(TAG, "Install failed: context is null!");
65-
} else {
61+
public static void install(@Nullable final Context context,boolean errorEvents, final String errorEventTitle, String APIKEY,String appId) {
62+
63+
if(errorEvents) {
64+
try {
65+
if (context == null) {
66+
Log.e(TAG, "Install failed: context is null!");
67+
} else {
6668

6769

68-
if(errorEvents)
69-
{
70-
if(errorGroupTitle==null || errorGroupTitle.trim().length()<=0) {
71-
Log.e(TAG, "errorGroupTitle needed check your application class");
70+
if (errorEventTitle == null || errorEventTitle.trim().length() <= 0) {
71+
Log.e(TAG, "errorEventTitle needed check your application class");
7272
Log.e(TAG, "pingMeLive not installed.");
7373
return;
7474
}
75-
}
7675

77-
if(APIKEY==null || APIKEY.trim().length()<=0)
78-
{
79-
Log.e(TAG, "API KEY needed check your application class");
80-
Log.e(TAG, "pingMeLive not installed.");
81-
return;
82-
}
76+
if (APIKEY == null || APIKEY.trim().length() <= 0) {
77+
Log.e(TAG, "API KEY needed check your application class");
78+
Log.e(TAG, "pingMeLive not installed.");
79+
return;
80+
}
8381

84-
if(appId==null || appId.trim().length()<=0)
85-
{
86-
Log.e(TAG, "appId needed check your application class");
87-
Log.e(TAG, "pingMeLive not installed.");
88-
return;
89-
}
82+
if (appId == null || appId.trim().length() <= 0) {
83+
Log.e(TAG, "appId needed check your application class");
84+
Log.e(TAG, "pingMeLive not installed.");
85+
return;
86+
}
9087

91-
dbHelper = DBHelper.getInstance(context);
92-
pingMePref = com.pingmelive.pingMePref.getInstance(context);
88+
dbHelper = DBHelper.getInstance(context);
89+
pingMePref = com.pingmelive.pingMePref.getInstance(context);
9390

94-
pingMePref.setAPIKey(APIKEY);
95-
pingMePref.setAppId(appId);
91+
pingMePref.setAPIKey(APIKEY);
92+
pingMePref.setAppId(appId);
9693

97-
//INSTALL!
98-
final Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
94+
//INSTALL!
95+
final Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
9996

100-
if (oldHandler != null && oldHandler.getClass().getName().startsWith(CAOC_HANDLER_PACKAGE_NAME)) {
101-
Log.e(TAG, "pingMeLive was already installed, doing nothing!");
102-
} else {
103-
if (oldHandler != null && !oldHandler.getClass().getName().startsWith(DEFAULT_HANDLER_PACKAGE_NAME)) {
104-
Log.e(TAG, "IMPORTANT WARNING! You already have an UncaughtExceptionHandler, are you sure this is correct? If you use a custom UncaughtExceptionHandler, you must initialize it AFTER pingMeLive! Installing anyway, but your original handler will not be called.");
105-
}
97+
if (oldHandler != null && oldHandler.getClass().getName().startsWith(CAOC_HANDLER_PACKAGE_NAME)) {
98+
Log.e(TAG, "pingMeLive was already installed, doing nothing!");
99+
} else {
100+
if (oldHandler != null && !oldHandler.getClass().getName().startsWith(DEFAULT_HANDLER_PACKAGE_NAME)) {
101+
Log.e(TAG, "IMPORTANT WARNING! You already have an UncaughtExceptionHandler, are you sure this is correct? If you use a custom UncaughtExceptionHandler, you must initialize it AFTER pingMeLive! Installing anyway, but your original handler will not be called.");
102+
}
106103

107-
application = (Application) context.getApplicationContext();
104+
application = (Application) context.getApplicationContext();
108105

109-
//We define a default exception handler that does what we want so it can be called from Crashlytics/ACRA
110-
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
111-
@Override
112-
public void uncaughtException(Thread thread, final Throwable throwable) {
106+
//We define a default exception handler that does what we want so it can be called from Crashlytics/ACRA
107+
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
108+
@Override
109+
public void uncaughtException(Thread thread, final Throwable throwable) {
113110

114111
Log.e(TAG, "App has crashed, executing pingMeLive's UncaughtExceptionHandler", throwable);
115112

@@ -128,26 +125,49 @@ public void uncaughtException(Thread thread, final Throwable throwable) {
128125
PrintWriter pw = new PrintWriter(sw);
129126
throwable.printStackTrace(pw);
130127
String stackTraceString = sw.toString();
131-
detailedEvent(errorGroupTitle,throwable.getMessage(),stackTraceString);
128+
detailedEvent(errorEventTitle, throwable.getMessage(), stackTraceString);
132129

133130
}
134131

135132
killCurrentProcess();
136133

137-
}
138-
});
139-
}
134+
}
135+
});
136+
}
140137

141-
Log.i(TAG, "pingMeLive has been installed.");
142-
dbHelper.sendData();
138+
Log.i(TAG, "pingMeLive has been installed.");
139+
dbHelper.sendData();
140+
}
141+
} catch (Throwable t) {
142+
Log.e(TAG, "An unknown error occurred while installing pingMeLive, it may not have been properly initialized. Please report this as a bug if needed.", t);
143143
}
144-
} catch (Throwable t) {
145-
Log.e(TAG, "An unknown error occurred while installing pingMeLive, it may not have been properly initialized. Please report this as a bug if needed.", t);
146144
}
147-
}
145+
else {
146+
147+
if (APIKEY == null || APIKEY.trim().length() <= 0) {
148+
Log.e(TAG, "API KEY needed check your application class");
149+
Log.e(TAG, "pingMeLive not installed.");
150+
return;
151+
}
152+
153+
if (appId == null || appId.trim().length() <= 0) {
154+
Log.e(TAG, "appId needed check your application class");
155+
Log.e(TAG, "pingMeLive not installed.");
156+
return;
157+
}
158+
148159

160+
Log.i(TAG, "pingMeLive has been installed, But without error events, to activate that setErrorEventEnabled(true) in your application class.");
149161

162+
dbHelper = DBHelper.getInstance(context);
163+
pingMePref = com.pingmelive.pingMePref.getInstance(context);
150164

165+
pingMePref.setAPIKey(APIKEY);
166+
pingMePref.setAppId(appId);
167+
168+
dbHelper.sendData();
169+
}
170+
}
151171

152172

153173
/**
@@ -232,8 +252,8 @@ public static void detailedEvent(String groupTitle,String message,String detaile
232252

233253
public static class Builder {
234254

235-
boolean setErrorEventEnabled = true;
236-
String setErrorEventTitle = null;
255+
boolean ErrorEventEnabled = true;
256+
String ErrorEventTitle = null;
237257
String API_KEY = null;
238258
String APP_ID = null;
239259
Context context;
@@ -243,21 +263,21 @@ public Builder(Context context)
243263
this.context = context;
244264
}
245265

246-
boolean isSetErrorEventEnabled() {
247-
return setErrorEventEnabled;
266+
boolean isErrorEventEnabled() {
267+
return ErrorEventEnabled;
248268
}
249269

250-
public Builder setSetErrorEventEnabled(boolean setErrorEventEnabled) {
251-
this.setErrorEventEnabled = setErrorEventEnabled;
270+
public Builder setErrorEventEnabled(boolean setErrorEventEnabled) {
271+
this.ErrorEventEnabled = setErrorEventEnabled;
252272
return this;
253273
}
254274

255-
String getSetErrorEventTitle() {
256-
return setErrorEventTitle;
275+
String getErrorEventTitle() {
276+
return ErrorEventTitle;
257277
}
258278

259-
public Builder setSetErrorEventTitle(String setErrorEventTitle) {
260-
this.setErrorEventTitle = setErrorEventTitle;
279+
public Builder setErrorEventTitle(String setErrorEventTitle) {
280+
this.ErrorEventTitle = setErrorEventTitle;
261281
return this;
262282
}
263283

@@ -280,7 +300,7 @@ public Builder setAPP_ID(String APP_ID) {
280300
}
281301

282302
public void install() {
283-
pingMeLive.install(context,isSetErrorEventEnabled(),getSetErrorEventTitle(),getAPI_KEY(),getAPP_ID());
303+
pingMeLive.install(context,isErrorEventEnabled(),getErrorEventTitle(),getAPI_KEY(),getAPP_ID());
284304
}
285305
}
286306
}

0 commit comments

Comments
 (0)