Skip to content

Commit

Permalink
Fix app crashes when downloads fail
Browse files Browse the repository at this point in the history
- Add exception handling and Toast messages for download failures in
cases where phone has no connection to the Internet
- Reformat code
- Bump version number to 1.0.4
  • Loading branch information
pdpiech committed Oct 1, 2014
1 parent 80792f1 commit 06a5c19
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 258 deletions.
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Current features:

Upcoming features:

- Dining Hall Menus
- RPI Directory
- Dining Hall Menus
- MorningMail


This application is currently released in beta v0.8 on the Google Play Store.
This application is currently released as version 1.o.4 on the Google Play Store.

This application is licensed as stated in LICENSE.txt

Expand Down
4 changes: 2 additions & 2 deletions RPIMobile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.rpi.rpimobile"
android:versionCode="11"
android:versionName="1.0.3" >
android:versionCode="13"
android:versionName="1.0.4" >

<!-- Target Version 2.3.3 and up -->
<uses-sdk
Expand Down
54 changes: 28 additions & 26 deletions RPIMobile/src/edu/rpi/rpimobile/AthleticsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;



//Sports news feeds
public class AthleticsFragment extends SherlockFragment {

public class AthleticsFragment extends SherlockFragment
{
//All variables to be used throughout the function
private ArrayList<RSSArticle> stories;
private ArrayList<RSSArticle> tempstories;
Expand Down Expand Up @@ -59,15 +58,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
//set an adapter up for the listview to handle displaying the data
rsslist = (ListView) rootView.findViewById(R.id.rsslist);
rssadapter = new RSSListAdapter(this.getActivity(), this, stories);
rsslist.setAdapter(rssadapter);//*/
rsslist.setAdapter(rssadapter);

//Initialize the download cycle to 0
cyclenum = 0;
//start the download cycle
refreshcycle();




return rootView;
}

Expand Down Expand Up @@ -138,23 +135,22 @@ public void loadpage(int pagenum){
,"http://www.rpiathletics.com/rss.aspx?path=wsoc","http://www.rpiathletics.com/rss.aspx?path=wswim"
,"http://www.rpiathletics.com/rss.aspx?path=wten","http://www.rpiathletics.com/rss.aspx?path=wtrack"
,"http://www.rpiathletics.com/rss.aspx?path=wbball"};



//The new and improved version of the refreshcycle.
//This allows the various rss feeds to be downloaded sequentially
public void refreshcycle(){

//shared preference object. To retrieve the user preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

if(cyclenum==0){
if(cyclenum == 0){
//for the first item set the action bar to show indeterminate progress and clear the list of stories
getActivity().setProgressBarIndeterminateVisibility(Boolean.TRUE);
stories.clear();
}


if(cyclenum==tags.length){
if(cyclenum == tags.length){
//if it's the last item set the action bar back to normal and reset the cycle counter
getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
logcat( "Loading complete List size:"+stories.size());
Expand All @@ -164,7 +160,7 @@ public void refreshcycle(){
//for all other items save the cycle number
int ctemp = cyclenum;
//increment the cycle number
cyclenum++;
++cyclenum;
//the first object is defaulted to true and all others are false by defualt
//so the default bit of the shared preference call is ctemp==0,
//which is true for the 0th item and false for all other items
Expand Down Expand Up @@ -196,11 +192,11 @@ protected Boolean doInBackground(String... params) {
//clear the temporary list
tempstories.clear();
logcat( "Begin doInBackground");
RSSReader reader = new RSSReader();
try {
//try to download all of the RSS data
logcat( "Initializing variables");
//This project uses the android-rss library to handle all RSS calls: https://github.com/ahorn/android-rss
RSSReader reader = new RSSReader();
String uri = params[0];
logcat( "Downloading feed items");
RSSFeed feed = reader.load(uri);
Expand All @@ -218,16 +214,29 @@ protected Boolean doInBackground(String... params) {
tempstories.add(temp);
}
logcat( "Feed parsed");
reader.close(); // Code edited by Peter Piech on 3/14/2014: added line to fix warning of resource leak
} catch (RSSReaderException e) {

} catch (Exception e) {
e.printStackTrace();
}
return false;
}
finally
{
reader.close();
}

logcat( "Exiting AsynchTask");
return true;
}

protected void onPostExecute(Boolean results) {
protected void onPostExecute(Boolean results)
{
//Set the action bar back to normal
getSherlockActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
if (!results)
{
Toast.makeText(getSherlockActivity(), "Athletics download failed. Try again later.", Toast.LENGTH_LONG).show();
return;
}
//code to be ran in the UI thread after the background thread has completed
logcat( "Notifying list");

Expand All @@ -243,11 +252,7 @@ protected void onPostExecute(Boolean results) {
}
//continue the refresh cycle
refreshcycle();

}



}

//class to add objects to the main list
Expand Down Expand Up @@ -314,10 +319,8 @@ else if (finlist.get(i).getTitle().equals(finlist.get(j).getTitle()))
//Assign the temporary list to the "stories" list
assign(stories, finlist);
logcat( "Lists combined. Source list:"+stories.size());

}



//Class to deal with the problem of copying ArrayLists in Java
//Because ArrayLists really just store pointers to their objects a deepcopy must be made of each
//item and passed to the list individually. This is much more efficient than using the
Expand All @@ -338,5 +341,4 @@ private void logcat(String logtext){
if(PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("debugging", false))
Log.d("RPI", logtext);
}

}
43 changes: 16 additions & 27 deletions RPIMobile/src/edu/rpi/rpimobile/EventsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;


//Events Calendar fragment
public class EventsFragment extends SherlockFragment {
public class EventsFragment extends SherlockFragment
{

//All variables to be used throughout the function
private JSONObject jObj;
Expand All @@ -36,7 +36,6 @@ public class EventsFragment extends SherlockFragment {
private MenuItem refreshbutton;
private JSONCalendarTask downloadtask;


//Initial function
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand All @@ -61,8 +60,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

return rootView;
}



//Class to be run when the fragment is terminated
@Override
public void onStop(){
Expand Down Expand Up @@ -98,11 +96,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
//This passes the call back up the chain to the main class, which also handles onOptionsitemSeleced events
return super.onOptionsItemSelected(item);
}






//AsyncTask thread to download calendar data
private class JSONCalendarTask extends AsyncTask<Void, Void, Boolean> {

Expand Down Expand Up @@ -132,8 +126,8 @@ protected Boolean doInBackground(Void... params) {
catch(Exception e){
//if the download failed quit the thread and notify the user
e.printStackTrace();
Toast.makeText(getSherlockActivity(), "Calendar Download Failed", Toast.LENGTH_SHORT).show();
return true;
Toast.makeText(getSherlockActivity(), "Events download failed. Try again later.", Toast.LENGTH_SHORT).show();
return false;
}
//Try to read all of the JSON objects into their respective variables
try {
Expand Down Expand Up @@ -170,23 +164,26 @@ protected Boolean doInBackground(Void... params) {

} catch (JSONException e) {
e.printStackTrace();
return false;
}
//Quit the looper now that we're done with it
Looper.myLooper().quit();
logcat( "Finished Download");
return true;

}




@Override
protected void onPostExecute(Boolean results) {
//code to be ran in the UI thread after the background thread has completed
logcat( "Updating List");
protected void onPostExecute(Boolean results)
{
//Set the action bar back to normal
getSherlockActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
if (!results)
{
Toast.makeText(getSherlockActivity(), "Events download failed. Try again later.", Toast.LENGTH_SHORT).show();
return;
}
//code to be ran in the UI thread after the background thread has completed
logcat( "Updating List");

try{
//Notify the list of new data
Expand All @@ -195,20 +192,12 @@ protected void onPostExecute(Boolean results) {
catch(Exception e){
logcat( e.toString());
}

}


private void logcat(String logtext){
//code to write a log.d message if the user allows it in preferences
if(PreferenceManager.getDefaultSharedPreferences(getSherlockActivity()).getBoolean("debugging", false))
Log.d("RPI", logtext);
}




}


}
16 changes: 13 additions & 3 deletions RPIMobile/src/edu/rpi/rpimobile/LaundryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
Expand Down Expand Up @@ -140,8 +141,10 @@ protected Boolean doInBackground(Void... params) {
source = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}

//This code parses the webpage source and saves each laundry room's name, free washers and dryers, and used washers and dryers.
Expand Down Expand Up @@ -199,13 +202,20 @@ protected Boolean doInBackground(Void... params) {
return true;
}

protected void onPostExecute(Boolean results) {
protected void onPostExecute(Boolean results)
{
//Set the action bar back to normal
getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
if (!results)
{
Toast.makeText(getSherlockActivity(), "Laundry download failed. Try again later.", Toast.LENGTH_SHORT).show();
return;
}
//code to be ran in the UI thread after the background thread has completed
logcat( "Notifying list");
// sort the laundryrooms ArrayList so that it displays in alphabetical order
Collections.sort(laundryrooms);
//Set the action bar back to normal
getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);

try{
//if the fragment is visible update the list adapter
if(LaundryFragment.this.isVisible())
Expand Down
1 change: 0 additions & 1 deletion RPIMobile/src/edu/rpi/rpimobile/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
//MainActivity. Holds the navigation drawer and the fragment frame
public class MainActivity extends SherlockFragmentActivity
{

// Declare Variables to be used in this function
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Expand Down
7 changes: 2 additions & 5 deletions RPIMobile/src/edu/rpi/rpimobile/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import edu.rpi.rpimobile.model.MapLocation;

import android.content.SharedPreferences;
Expand Down Expand Up @@ -141,6 +136,7 @@ private void parseDatabase() // TODO: Update numeric.xml value dbVersion every t
}
catch (IOException f)
{
Toast.makeText(getSherlockActivity(), "Map failed. Please re-install app.", Toast.LENGTH_SHORT).show();
throw new Error("Error copying database");
}
}
Expand All @@ -150,6 +146,7 @@ private void parseDatabase() // TODO: Update numeric.xml value dbVersion every t
}
catch (SQLException e) // this should never be reached, or something is terribly wrong
{
Toast.makeText(getSherlockActivity(), "Map failed. Please re-install app.", Toast.LENGTH_SHORT).show();
throw new Error("Error opening external database");
}

Expand Down
Loading

0 comments on commit 06a5c19

Please sign in to comment.