From: <kro...@us...> - 2010-12-16 10:58:31
|
Revision: 4035 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=4035&view=rev Author: kroko_koenig Date: 2010-12-16 10:58:24 +0000 (Thu, 16 Dec 2010) Log Message: ----------- add exception handler and send report via email Modified Paths: -------------- trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/main.java Added Paths: ----------- trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/TopExceptionHandler.java Added: trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/TopExceptionHandler.java =================================================================== --- trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/TopExceptionHandler.java (rev 0) +++ trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/TopExceptionHandler.java 2010-12-16 10:58:24 UTC (rev 4035) @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005-2010 Team MediaPortal + * http://www.team-mediaportal.com + * + * This Program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This Program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +package mediaportal.remote; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import java.io.*; + +public class TopExceptionHandler implements Thread.UncaughtExceptionHandler { + + private Thread.UncaughtExceptionHandler defaultUEH; + + private Activity app = null; + + public TopExceptionHandler(Activity app) { + this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); + this.app = app; + } + + public void uncaughtException(Thread t, Throwable e) { + StackTraceElement[] arr = e.getStackTrace(); + String report = e.toString() + "\n\n"; + report += "--------- Stack trace ---------\n\n"; + for (int i = 0; i < arr.length; i++) { + report += " " + arr[i].toString() + "\n"; + } + report += "-------------------------------\n\n"; + + // If the exception was thrown in a background thread inside + // AsyncTask, then the actual exception can be found with getCause + report += "--------- Cause ---------\n\n"; + Throwable cause = e.getCause(); + if (cause != null) { + report += cause.toString() + "\n\n"; + arr = cause.getStackTrace(); + for (int i = 0; i < arr.length; i++) { + report += " " + arr[i].toString() + "\n"; + } + } + + report += "-------------------------------\n\n"; + report += "--------- Device ---------\n\n"; + report += "Brand: " + Build.BRAND + "\n"; + report += "Device: " + Build.DEVICE + "\n"; + report += "Model: " + Build.MODEL + "\n"; + report += "Id: " + Build.ID + "\n"; + report += "Product: " + Build.PRODUCT + "\n"; + report += "-------------------------------\n\n"; + report += "--------- Firmware ---------\n\n"; + report += "SDK: " + Build.VERSION.SDK + "\n"; + report += "Release: " + Build.VERSION.RELEASE + "\n"; + report += "Incremental: " + Build.VERSION.INCREMENTAL + "\n"; + report += "-------------------------------\n\n"; + + try { + FileOutputStream trace = app.openFileOutput("stack.trace", + Context.MODE_PRIVATE); + trace.write(report.getBytes()); + trace.close(); + } catch (IOException ioe) { + // ... + } + + defaultUEH.uncaughtException(t, e); + } + +} Modified: trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/main.java =================================================================== --- trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/main.java 2010-12-15 21:33:12 UTC (rev 4034) +++ trunk/plugins/AndroidRemote/Android/MediaPortalRemote/src/mediaportal/remote/main.java 2010-12-16 10:58:24 UTC (rev 4035) @@ -21,11 +21,16 @@ package mediaportal.remote; +import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import mediaportal.remote.R; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.AssetManager; @@ -42,17 +47,39 @@ private static final String PREFS_PRIVATE = "PREFS_MP_REMOTE"; private Handler mHandler = new Handler(); private static String display = "nothing playing"; - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); + if (reportExist()) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("Do you want to send a crash report ?") + .setTitle("Sorry .. remote has crashed") + .setPositiveButton("Yes", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int item) { + sendReport(); + } + }) + .setNegativeButton("No", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int item) { + deleteReport(); + } + }).show(); + + } + + Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this)); chkStatus(); - + mHandler.removeCallbacks(mUpdateTimeTask); mHandler.postDelayed(mUpdateTimeTask, 1000); - + SharedPreferences settings = getSharedPreferences(PREFS_PRIVATE, MODE_PRIVATE); @@ -86,6 +113,11 @@ Toast.makeText(view.getContext(), "not implemented yet. Sorry !", Toast.LENGTH_LONG) .show(); + + int x = 0; + int y = 1; + + int z = y / x; } }); @@ -142,6 +174,57 @@ }); } + private boolean reportExist() { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader( + this.openFileInput("stack.trace"))); + return true; + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } + + private void sendReport() { + try { + String trace = ""; + String line = ""; + + BufferedReader reader = new BufferedReader(new InputStreamReader( + this.openFileInput("stack.trace"))); + while ((line = reader.readLine()) != null) { + trace += line + "\n"; + } + + Intent sendIntent = new Intent(Intent.ACTION_SEND); + String subject = "Error report"; + String body = "Mail this to mar...@go...: " + + "\n\n" + trace + "\n\n"; + + sendIntent.putExtra(Intent.EXTRA_EMAIL, + new String[] { "mar...@go..." }); + sendIntent.putExtra(Intent.EXTRA_TEXT, body); + sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject); + sendIntent.setType("message/rfc822"); + + startActivity(Intent + .createChooser(sendIntent, "Send crash report:")); + this.deleteFile("stack.trace"); + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private void deleteReport() { + this.deleteFile("stack.trace"); + } + void chkStatus() { final ConnectivityManager connMgr = (ConnectivityManager) this .getSystemService(Context.CONNECTIVITY_SERVICE); @@ -153,16 +236,15 @@ .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (!wifi.isAvailable()) { - Toast.makeText(this, "Wifi is not available !", Toast.LENGTH_SHORT).show(); + Toast.makeText(this, "Warning : Wifi is not available !", + Toast.LENGTH_SHORT).show(); } - + /* - else if (mobile.isAvailable()) { - Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show(); - } - */ + * else if (mobile.isAvailable()) { Toast.makeText(this, "Mobile 3G ", + * Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, + * "No Network ", Toast.LENGTH_LONG).show(); } + */ } public void PostCommand(String button) { @@ -211,7 +293,7 @@ private class update extends AsyncTask<String, Void, Void> { // can use UI thread here - protected void onPreExecute() { + protected void onPreExecute() { } // automatically done on worker thread (separate from UI thread) @@ -227,21 +309,23 @@ handler.fetchdata(); if ((nowplayinghandler.PlayerState != null) - && (!nowplayinghandler.PlayerState.equals("no player active"))) { + && (!nowplayinghandler.PlayerState + .equals("no player active"))) { display = nowplayinghandler.Artist + " - " + nowplayinghandler.Title; } - + return null; } // can use UI thread here protected void onPostExecute(final Void unused) { - - TextView txt0 = (TextView) findViewById(R.id.main_now_playing); + + TextView txt0 = (TextView) findViewById(R.id.main_now_playing); txt0.setText(display); - + mHandler.postDelayed(mUpdateTimeTask, 1000); } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |