diff --git a/ExceptionReportViewer/bin/ExceptionReportViewer.apk b/ExceptionReportViewer/bin/ExceptionReportViewer.apk index 1523bf5..52262df 100644 Binary files a/ExceptionReportViewer/bin/ExceptionReportViewer.apk and b/ExceptionReportViewer/bin/ExceptionReportViewer.apk differ diff --git a/ExceptionReportViewer/bin/classes.dex b/ExceptionReportViewer/bin/classes.dex index 96d94c5..f7d21c3 100644 Binary files a/ExceptionReportViewer/bin/classes.dex and b/ExceptionReportViewer/bin/classes.dex differ diff --git a/ExceptionReportViewer/bin/res/drawable-hdpi/ic_launcher.png b/ExceptionReportViewer/bin/res/drawable-hdpi/ic_launcher.png deleted file mode 100644 index c6d9781..0000000 Binary files a/ExceptionReportViewer/bin/res/drawable-hdpi/ic_launcher.png and /dev/null differ diff --git a/ExceptionReportViewer/bin/res/drawable-ldpi/ic_launcher.png b/ExceptionReportViewer/bin/res/drawable-ldpi/ic_launcher.png deleted file mode 100644 index ef1e59e..0000000 Binary files a/ExceptionReportViewer/bin/res/drawable-ldpi/ic_launcher.png and /dev/null differ diff --git a/ExceptionReportViewer/bin/res/drawable-mdpi/ic_launcher.png b/ExceptionReportViewer/bin/res/drawable-mdpi/ic_launcher.png deleted file mode 100644 index d4f0612..0000000 Binary files a/ExceptionReportViewer/bin/res/drawable-mdpi/ic_launcher.png and /dev/null differ diff --git a/ExceptionReportViewer/bin/res/drawable-xhdpi/ic_launcher.png b/ExceptionReportViewer/bin/res/drawable-xhdpi/ic_launcher.png deleted file mode 100644 index 32f3b89..0000000 Binary files a/ExceptionReportViewer/bin/res/drawable-xhdpi/ic_launcher.png and /dev/null differ diff --git a/ExceptionReportViewer/bin/resources.ap_ b/ExceptionReportViewer/bin/resources.ap_ index 2cc357e..8e0ab3f 100644 Binary files a/ExceptionReportViewer/bin/resources.ap_ and b/ExceptionReportViewer/bin/resources.ap_ differ diff --git a/ExceptionReportViewer/gen/com/TwentyCodes/android/ExceptionReportViewer/R.java b/ExceptionReportViewer/gen/com/TwentyCodes/android/ExceptionReportViewer/R.java index 5b864e0..ca1806b 100644 --- a/ExceptionReportViewer/gen/com/TwentyCodes/android/ExceptionReportViewer/R.java +++ b/ExceptionReportViewer/gen/com/TwentyCodes/android/ExceptionReportViewer/R.java @@ -191,6 +191,7 @@ or to a theme attribute in the form "?[package:][type:]na public static final int send=0x7f040003; public static final int sending=0x7f040002; public static final int sorry=0x7f040001; + public static final int there_was_an_error=0x7f040008; public static final int version=0x7f040005; } public static final class style { diff --git a/ExceptionReportViewer/gen/com/TwentyCodes/android/exception/R.java b/ExceptionReportViewer/gen/com/TwentyCodes/android/exception/R.java index 44d6471..ade2304 100644 --- a/ExceptionReportViewer/gen/com/TwentyCodes/android/exception/R.java +++ b/ExceptionReportViewer/gen/com/TwentyCodes/android/exception/R.java @@ -191,6 +191,7 @@ or to a theme attribute in the form "?[package:][type:]na public static final int send=0x7f040003; public static final int sending=0x7f040002; public static final int sorry=0x7f040001; + public static final int there_was_an_error=0x7f040008; public static final int version=0x7f040005; } public static final class style { diff --git a/ExceptionReportViewer/res/values/strings.xml b/ExceptionReportViewer/res/values/strings.xml index c688b6e..ccb9433 100644 --- a/ExceptionReportViewer/res/values/strings.xml +++ b/ExceptionReportViewer/res/values/strings.xml @@ -3,5 +3,6 @@ Hello World, Main! ExceptionReportViewer + There was an error... \ No newline at end of file diff --git a/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/JSONLoader.java b/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/JSONLoader.java new file mode 100644 index 0000000..a49f534 --- /dev/null +++ b/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/JSONLoader.java @@ -0,0 +1,153 @@ +/** + * JSONLoader.java + * @date Dec 28, 2011 + * @author ricky barrette + * @author Twenty Codes, LLC + */ +package com.TwentyCodes.android.ExceptionReportViewer; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.Context; +import android.support.v4.app.LoaderManager; +import android.support.v4.app.LoaderManager.LoaderCallbacks; +import android.support.v4.content.Loader; + +/** + * This Loader will be used to download and parse a JSON object from the internet + * @author ricky barrette + */ +public class JSONLoader extends Loader { + + private LoaderCallbacks mListener; + private String mUrl; + private InputStream mContent; + + /** + * Creates an new JSONLoader + * @param url + * @param listener + * @param context + * @author ricky barrette + */ + public JSONLoader(String url, LoaderManager.LoaderCallbacks listener, Context context) { + super(context); + mListener = listener; + mUrl = url; + } + + /** + * (non-Javadoc) + * @see android.support.v4.content.Loader#onAbandon() + */ + @Override + protected void onAbandon() { + super.onAbandon(); + if(mContent != null) + try { + mContent.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * (non-Javadoc) + * @see android.support.v4.content.Loader#onForceLoad() + */ + @Override + protected void onForceLoad() { + // TODO Auto-generated method stub + super.onForceLoad(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.content.Loader#onReset() + */ + @Override + protected void onReset() { + super.onReset(); + if(mContent != null) + try { + mContent.close(); + } catch (IOException e) { + e.printStackTrace(); + } + if(mListener != null) + mListener.onLoaderReset(this); + } + + /** + * (non-Javadoc) + * @see android.support.v4.content.Loader#onStartLoading() + */ + @Override + protected void onStartLoading() { + super.onStartLoading(); + if(mListener != null) + try { + mListener.onLoadFinished(this, new JSONObject(downloadJSON(mUrl))); + } catch (IllegalStateException e) { + mListener.onLoadFinished(this, null); + } catch (ClientProtocolException e) { + mListener.onLoadFinished(this, null); + } catch (JSONException e) { + mListener.onLoadFinished(this, null); + } catch (IOException e) { + mListener.onLoadFinished(this, null); + } catch (NullPointerException e) { + mListener.onLoadFinished(this, null); + } + + } + + /** + * (non-Javadoc) + * @see android.support.v4.content.Loader#onStopLoading() + */ + @Override + protected void onStopLoading() { + super.onStopLoading(); + if(mContent != null) + try { + mContent.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Downloads exception report JSON from the Internet + * @param url + * @return + * @throws IllegalStateException + * @throws ClientProtocolException + * @throws IOException + * @author ricky barrette + */ + private String downloadJSON(String url) throws IllegalStateException, ClientProtocolException, IOException { + if(url == null) + throw new NullPointerException(); + + StringBuffer response = new StringBuffer(); + mContent = new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent(); + BufferedReader br = new BufferedReader(new InputStreamReader(mContent)); + String buff = null; + while ((buff = br.readLine()) != null){ + System.out.print(buff); + response.append(buff); + } + return response.toString(); + } + +} diff --git a/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/ReportListFragment.java b/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/ReportListFragment.java index eb525a3..f8c637a 100644 --- a/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/ReportListFragment.java +++ b/ExceptionReportViewer/src/com/TwentyCodes/android/ExceptionReportViewer/ReportListFragment.java @@ -6,37 +6,30 @@ */ package com.TwentyCodes.android.ExceptionReportViewer; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; - -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import com.TwentyCodes.android.exception.ExceptionReportActivity; -import com.TwentyCodes.android.exception.Report; - import android.content.Intent; -import android.os.Handler; -import android.os.Message; +import android.os.Bundle; import android.support.v4.app.ListFragment; +import android.support.v4.app.LoaderManager; +import android.support.v4.content.Loader; import android.view.View; import android.widget.ListView; import android.widget.Toast; +import com.TwentyCodes.android.exception.ExceptionReportActivity; +import com.TwentyCodes.android.exception.Report; + /** * This fragment will be used to display a list of exception reports to the user * @author ricky barrette */ -public class ReportListFragment extends ListFragment { +public class ReportListFragment extends ListFragment implements LoaderManager.LoaderCallbacks { - protected static final int DOWNLOADED_REPORTS = 0; - protected static final int ERROR = 1; private JSONArray mReports; + private String mUrl; /** * Creates a new ReportListFragment @@ -44,91 +37,19 @@ public class ReportListFragment extends ListFragment { * @author ricky barrette */ public ReportListFragment(final String url) { - - final Handler handler = new Handler(){ - - /** - * (non-Javadoc) - * @see android.os.Handler#handleMessage(android.os.Message) - */ - @Override - public void handleMessage(Message msg) { - switch(msg.what){ - case DOWNLOADED_REPORTS: - try { - parseJSON((String) msg.obj); - } catch (JSONException e) { - ReportListFragment.this.setEmptyText(e.getMessage()); - e.printStackTrace(); - } - break; - case ERROR: - ReportListFragment.this.setEmptyText((String) msg.obj); - break; - - } - super.handleMessage(msg); - } - - }; - - new Thread(new Runnable(){ - @Override - public void run(){ - /* - * Here we will try to download and parse the reports from the server - * if there is any errors, the user is notified via the list's empty text view - */ - try { - handler.sendMessage(handler.obtainMessage(DOWNLOADED_REPORTS, downloadJSON(url))); - } catch (IllegalStateException e) { - handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage())); - e.printStackTrace(); - } catch (ClientProtocolException e) { - handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage())); - e.printStackTrace(); - } catch (IOException e) { - handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage())); - e.printStackTrace(); - } - - } - }).start(); + mUrl = url; } /** - * parses the JSON reports and displays them in a list - * @param json - * @throws JSONException - * @author ricky barrette + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle) */ - private void parseJSON(String json) throws JSONException { - mReports = new JSONObject(json).getJSONArray("reports"); - this.setListAdapter(new ReportAdapter(this.getActivity(), mReports)); + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + getLoaderManager().initLoader(0, null, this); } - /** - * Downloads exception report JSON from the Internet - * @param url - * @return - * @throws IllegalStateException - * @throws ClientProtocolException - * @throws IOException - * @author ricky barrette - */ - private String downloadJSON(String url) throws IllegalStateException, ClientProtocolException, IOException { - if(url == null) - throw new NullPointerException(); - StringBuffer response = new StringBuffer(); - BufferedReader br = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent())); - String buff = null; - while ((buff = br.readLine()) != null){ - System.out.print(buff); - response.append(buff); - } - return response.toString(); - } - /** * Called when the user selects a report to display * (non-Javadoc) @@ -144,4 +65,38 @@ public class ReportListFragment extends ListFragment { Toast.makeText(this.getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } } + + /** + * Called when the loader is created + * (non-Javadoc) + * @see android.support.v4.app.LoaderManager.LoaderCallbacks#onCreateLoader(int, android.os.Bundle) + */ + @Override + public Loader onCreateLoader(int id, Bundle args) { + return new JSONLoader(mUrl, this, this.getActivity()); + } + + /** + * Called when the loader has finished + * (non-Javadoc) + * @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.support.v4.content.Loader, java.lang.Object) + */ + @Override + public void onLoadFinished(Loader loader, JSONObject jsonObject) { + if(jsonObject != null){ + try { + mReports = jsonObject.getJSONArray("reports"); + this.setListAdapter(new ReportAdapter(this.getActivity(), mReports)); + } catch (JSONException e) { + this.setEmptyText(getText(R.string.there_was_an_error)); + } + this.setEmptyText(getText(R.string.there_was_an_error)); + } + + } + + @Override + public void onLoaderReset(Loader loader) { + //not needed + } } \ No newline at end of file