Permalink
Browse files

sending reports on crush, working for notifications

  • Loading branch information...
1 parent f081e82 commit 8f75102a6c03cebe3523e7e210b68f2b6cc213a6 Jon Vlachoyiannis committed Mar 18, 2011
View
@@ -4,7 +4,7 @@ Sfalma Android
How to use it
------
-After you build the project (see Building section), copy sfalma-trace.jar into your project libraries and import it to your project:
+You build the project (see Building section), copy sfalma-trace.jar into your project libraries and import it to your project or you could copy sfalma-trace.jar :
import com.sfalma.trace.SfalmaHandler;
@@ -3,6 +3,12 @@
package="com.sfalma.trace.example"
android:versionCode="1"
android:versionName="1.0">
+
+ <uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".MainActivity"
@@ -12,12 +18,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- <uses-permission android:name="android.permission.INTERNET" />
</application>
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
Binary file not shown.
@@ -55,32 +55,43 @@ public DefaultExceptionHandler(UncaughtExceptionHandler pDefaultExceptionHandler
// Default exception handler
public void uncaughtException(Thread t, Throwable e) {
- // Here you should have a more robust, permanent record of problems
+ Date now = new Date();
+ int sTimeout = 0;
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
+
e.printStackTrace(printWriter);
+
try {
- // Random number to avoid duplicate files
- Random generator = new Random();
- int random = generator.nextInt(99999);
- // Embed version in stacktrace filename
- String filename = G.APP_VERSION+"-"+Integer.toString(random);
- Log.d(G.TAG, "Writing unhandled exception to: " + G.FILES_PATH+"/"+filename+".stacktrace");
- // Write the stacktrace to disk
- BufferedWriter bos = new BufferedWriter(new FileWriter(G.FILES_PATH+"/"+filename+".stacktrace"));
+ Sfalma.submitError(sTimeout, now, result.toString());
+ } catch (Exception ebos) {
- Date now = new Date();
- bos.write(G.ANDROID_VERSION + "\n");
- bos.write(G.PHONE_MODEL + "\n");
- bos.write(now + "\n");
- bos.write(result.toString());
- bos.flush();
- // Close up everything
- bos.close();
- } catch (Exception ebos) {
- // Nothing much we can do about this - the game is over
- Log.e(G.TAG, "Error saving exception stacktrace", e);
+ // If we couldn't send the error, save it
+ Log.e(G.TAG, "Error sending exception stacktrace", e);
+
+ try {
+ // Random number to avoid duplicate files
+ Random generator = new Random();
+ int random = generator.nextInt(99999);
+ // Embed version in stacktrace filename
+ String filename = G.APP_VERSION+"-"+Integer.toString(random);
+ Log.d(G.TAG, "Writing unhandled exception to: " + G.FILES_PATH+"/"+filename+".stacktrace");
+ // Write the stacktrace to disk
+ BufferedWriter bos = new BufferedWriter(new FileWriter(G.FILES_PATH+"/"+filename+".stacktrace"));
+
+ bos.write(G.ANDROID_VERSION + "\n");
+ bos.write(G.PHONE_MODEL + "\n");
+ bos.write(now + "\n");
+ bos.write(result.toString());
+ bos.flush();
+ // Close up everything
+ bos.close();
+ } catch (Exception ebos2) {
+ // Nothing much we can do about this - the game is over
+ Log.e(G.TAG, "Error saving exception stacktrace", e);
+ }
}
+
Log.d(G.TAG, result.toString());
//call original handler
defaultExceptionHandler.uncaughtException(t, e);
@@ -35,11 +35,11 @@
// Since the exception handler doesn't have access to the context,
// or anything really, the library prepares these values for when
// the handler needs them.
- public static String FILES_PATH = null;
+ public static String FILES_PATH = "null";
public static String APP_VERSION = "unknown";
public static String APP_PACKAGE = "unknown";
- public static String URL = "http://10.39.255.101:8080/api/errors";
- //public static String URL = "http://192.168.0.55:8080/api/errors";
+ //public static String URL = "http://10.39.255.101:8080/api/errors";
+ public static String URL = "http://dev.latest.sfalma-engine.appspot.com/api/errors";
public static String TAG = "SfalmaHandler";
public static String ANDROID_VERSION = null;
public static String PHONE_MODEL = null;
@@ -35,11 +35,11 @@ public class G {
// Since the exception handler doesn't have access to the context,
// or anything really, the library prepares these values for when
// the handler needs them.
- public static String FILES_PATH = null;
+ public static String FILES_PATH = "null";
public static String APP_VERSION = "unknown";
public static String APP_PACKAGE = "unknown";
- public static String URL = "http://10.39.255.101:8080/api/errors";
- //public static String URL = "http://192.168.0.55:8080/api/errors";
+ //public static String URL = "http://10.39.255.101:8080/api/errors";
+ public static String URL = "http://dev.latest.sfalma-engine.appspot.com/api/errors";
public static String TAG = "SfalmaHandler";
public static String ANDROID_VERSION = null;
public static String PHONE_MODEL = null;
@@ -33,16 +33,31 @@
import java.math.*;
import java.io.BufferedReader;
import java.io.StringReader;
-
+import java.lang.String;
+import java.util.Date;
+import java.util.ArrayList;
+import java.util.List;
+
import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
+import android.util.Log;
+
+
public class Sfalma {
// FIXME: Use Gson
- public static String createJSON(String app_package, String version, String phoneModel, String android_version, String stackTrace, String wifi_status, String mob_net_status, String gps_status) throws Exception {
+ public static String createJSON(String app_package, String version, String phoneModel, String android_version, String stackTrace, String wifi_status, String mob_net_status, String gps_status, Date occuredAt) throws Exception {
JSONObject json = new JSONObject();
JSONObject request_json = new JSONObject();
@@ -55,10 +70,16 @@ public static String createJSON(String app_package, String version, String phone
// stackTrace contains many info we need to extract
BufferedReader reader = new BufferedReader(new StringReader(stackTrace));
-
- exception_json.put("occured_at", reader.readLine()); // get date
+
+ if (occuredAt == null)
+ exception_json.put("occured_at", reader.readLine());
+ else
+ exception_json.put("occured_at", occuredAt);
exception_json.put("message", reader.readLine()); //get message
- exception_json.put("exception_class", reader.readLine()); // get exception class
+
+ String exception_class = reader.readLine();
+ exception_json.put("exception_class", exception_class.substring(exception_class.lastIndexOf("(") + 1, exception_class.lastIndexOf(")"))); // where
+
exception_json.put("backtrace", stackTrace);
json.put("exception", exception_json);
@@ -67,7 +88,7 @@ public static String createJSON(String app_package, String version, String phone
application_json.put("phone_model", phoneModel);
application_json.put("package_version", version);
application_json.put("package_name", app_package);
- application_json.put("version", android_version);
+ application_json.put("version", android_version); //os_ver
application_json.put("wifi_on", wifi_status);
application_json.put("mobile_net_on", mob_net_status);
application_json.put("gps_on", gps_status);
@@ -97,4 +118,44 @@ public static String removeFirstLine(String in) {
}
return out;
}
+
+ public static void submitError(int sTimeout, Date occuredAt, final String stacktrace) throws Exception {
+ // Transmit stack trace with POST request
+
+ try {
+ Log.d(G.TAG, "Transmitting stack trace: " + stacktrace);
+ if (occuredAt != null)
+ SfalmaHandler.ShowToast("Sending old crashes");
+ else
+ SfalmaHandler.ShowToast("Helping the app becoming better!");
+
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+ HttpParams params = httpClient.getParams();
+
+ // Lighty 1.4 has trouble with the expect header
+ // (http://redmine.lighttpd.net/issues/1017), and a
+ // potential workaround is only included in 1.4.21
+ // (http://www.lighttpd.net/2009/2/16/1-4-21-yes-we-can-do-another-release).
+ HttpProtocolParams.setUseExpectContinue(params, false);
+ if (sTimeout != 0) {
+ HttpConnectionParams.setConnectionTimeout(params, sTimeout);
+ HttpConnectionParams.setSoTimeout(params, sTimeout);
+ }
+
+ HttpPost httpPost = new HttpPost(G.URL);
+ httpPost.addHeader("X-Sfalma-Api-Key", G.API_KEY);
+
+ List <NameValuePair> nvps = new ArrayList <NameValuePair>();
+ nvps.add(new BasicNameValuePair("data", createJSON(G.APP_PACKAGE, G.APP_VERSION, G.PHONE_MODEL, G.ANDROID_VERSION, stacktrace, SfalmaHandler.isWifiOn(), SfalmaHandler.isMobileNetworkOn(), SfalmaHandler.isGPSOn(), occuredAt)));
+ nvps.add(new BasicNameValuePair("hash", MD5(removeFirstLine(stacktrace))));
+
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
+ // We don't care about the response, so we just hope it
+ // went well and on with it.
+ httpClient.execute(httpPost);
+ } catch (Exception e) {
+ Log.e(G.TAG, "Error sending exception stacktrace", e);
+ throw e;
+ }
+ }
}
Oops, something went wrong.

0 comments on commit 8f75102

Please sign in to comment.