-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save a copy of the downloaded data as cached data
- Loading branch information
Showing
3 changed files
with
178 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,95 @@ | ||
package net.trajano.gasprices; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Scanner; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
import android.util.Log; | ||
|
||
public final class GetDataUtil { | ||
/** | ||
* This will connect to the Internet to get the gas price data and return | ||
* the parsed {@link JSONObject}. This will throw an {@link IOException} if | ||
* there is an error parsing the data because there isn't anything that can | ||
* be done if there is a parse error, but the {@link IOException} is still | ||
* thrown for any communication errors. | ||
* | ||
* @return a parsed JSONObject. | ||
* @throws IOException | ||
* I/O error. | ||
*/ | ||
public static JSONObject getGasPricesDataFromInternet() throws IOException { | ||
try { | ||
// Read the JSON data, skip the first character since it | ||
// breaks the parsing. | ||
final String jsonData = getRawGasPricesDataFromInternet(); | ||
final Object value = new JSONTokener(jsonData).nextValue(); | ||
if (value instanceof JSONObject) { | ||
return (JSONObject) value; | ||
} else { | ||
throw new IOException("Did not get a proper JSON object"); | ||
} | ||
} catch (final JSONException e) { | ||
Log.e("GasPrices", e.getMessage()); | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
/** | ||
* This connects to the site to get the data as is. Used for the situation | ||
* where an error had occured. | ||
* | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String getRawGasPricesDataFromInternet() throws IOException { | ||
final HttpURLConnection urlConnection = (HttpURLConnection) new URL( | ||
"http://www.tomorrowsgaspricetoday.com/mobile/json_mobile_data.php") | ||
.openConnection(); | ||
try { | ||
return new Scanner(urlConnection.getInputStream()) | ||
.useDelimiter("\\A").next().substring(1); | ||
} finally { | ||
urlConnection.disconnect(); | ||
} | ||
} | ||
|
||
private GetDataUtil() { | ||
|
||
} | ||
|
||
} | ||
package net.trajano.gasprices; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.Scanner; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
import android.util.Log; | ||
|
||
public final class GetDataUtil { | ||
/** | ||
* This will connect to the Internet to get the gas price data and return | ||
* the parsed {@link JSONObject}. This will throw an {@link IOException} if | ||
* there is an error parsing the data because there isn't anything that can | ||
* be done if there is a parse error, but the {@link IOException} is still | ||
* thrown for any communication errors. | ||
* | ||
* @param cacheDir | ||
* cache directory | ||
* | ||
* @return a parsed JSONObject. | ||
* @throws IOException | ||
* I/O error. | ||
*/ | ||
public static JSONObject getGasPricesDataFromInternet(final File cacheDir) | ||
throws IOException { | ||
try { | ||
// Read the JSON data, skip the first character since it | ||
// breaks the parsing. | ||
final String jsonData = getRawGasPricesDataFromInternet(cacheDir); | ||
final Object value = new JSONTokener(jsonData).nextValue(); | ||
if (value instanceof JSONObject) { | ||
return (JSONObject) value; | ||
} else { | ||
throw new IOException("Did not get a proper JSON object"); | ||
} | ||
} catch (final JSONException e) { | ||
Log.e("GasPrices", e.getMessage()); | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
/** | ||
* This connects to the site to get the data as is. Used for the situation | ||
* where an error had occured. | ||
* | ||
* @param cacheDir | ||
* cache directory | ||
* | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static String getRawGasPricesDataFromInternet(final File cacheDir) | ||
throws IOException { | ||
final File cacheFile = new File(cacheDir, "cached"); | ||
final HttpURLConnection urlConnection = (HttpURLConnection) new URL( | ||
"http://www.tomorrowsgaspricetoday.com/mobile/json_mobile_data.php") | ||
.openConnection(); | ||
FileOutputStream cacheFileStream = null; | ||
try { | ||
final InputStream networkStream = urlConnection.getInputStream(); | ||
// Skip the first character. | ||
networkStream.read(); | ||
cacheFileStream = new FileOutputStream(cacheFile); | ||
final int c = networkStream.read(); | ||
while (c != -1) { | ||
cacheFileStream.write(c); | ||
} | ||
} finally { | ||
if (cacheFileStream != null) { | ||
cacheFileStream.close(); | ||
} | ||
urlConnection.disconnect(); | ||
} | ||
final FileInputStream cacheInputStream = new FileInputStream(cacheFile); | ||
try { | ||
return new Scanner(cacheInputStream).useDelimiter("\\A").next(); | ||
} finally { | ||
cacheInputStream.close(); | ||
} | ||
} | ||
|
||
private GetDataUtil() { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,81 @@ | ||
package net.trajano.gasprices; | ||
|
||
import java.io.IOException; | ||
|
||
import org.json.JSONObject; | ||
|
||
import android.app.AlertDialog; | ||
import android.appwidget.AppWidgetManager; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.AsyncTask; | ||
import android.util.Log; | ||
|
||
public class UpdateTask extends AsyncTask<Void, Void, Exception> { | ||
private final Context context; | ||
|
||
public UpdateTask(final Context context) { | ||
super(); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
protected Exception doInBackground(final Void... params) { | ||
final PreferenceAdaptor preferences = new PreferenceAdaptor(context); | ||
final PreferenceAdaptorEditor editor = preferences.edit(); | ||
|
||
try { | ||
final JSONObject data = GetDataUtil.getGasPricesDataFromInternet(); | ||
editor.setJsonData(data); | ||
editor.setLastUpdatedToNow(); | ||
return null; | ||
} catch (final IOException e) { | ||
// TODO fix this is yucky | ||
Log.e("GasPrices", e.getMessage() + " and cry"); | ||
try { | ||
editor.setLastError(e.getMessage(), | ||
GetDataUtil.getRawGasPricesDataFromInternet()); | ||
} catch (final IOException e2) { | ||
editor.setLastError(e.getMessage(), ""); | ||
return e2; | ||
} | ||
return e; | ||
} finally { | ||
editor.apply(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(final Exception result) { | ||
if (result == null) { | ||
final AppWidgetManager widgetManager = AppWidgetManager | ||
.getInstance(context); | ||
final ComponentName widgetComponent = new ComponentName(context, | ||
GasPricesWidgetProvider.class); | ||
final int[] widgetIds = widgetManager | ||
.getAppWidgetIds(widgetComponent); | ||
if (widgetIds.length > 0) { | ||
final Intent update = new Intent(); | ||
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds); | ||
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); | ||
context.sendBroadcast(update); | ||
} | ||
} else { | ||
new AlertDialog.Builder(context) | ||
.setTitle(R.string.error) | ||
.setMessage( | ||
context.getString(R.string.problem_loading_format, | ||
result.getLocalizedMessage())).show(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
final PreferenceAdaptor preferences = new PreferenceAdaptor(context); | ||
final PreferenceAdaptorEditor editor = preferences.edit(); | ||
editor.removeLastError(); | ||
editor.apply(); | ||
} | ||
} | ||
package net.trajano.gasprices; | ||
|
||
import java.io.IOException; | ||
|
||
import org.json.JSONObject; | ||
|
||
import android.app.AlertDialog; | ||
import android.appwidget.AppWidgetManager; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.AsyncTask; | ||
import android.util.Log; | ||
|
||
public class UpdateTask extends AsyncTask<Void, Void, Exception> { | ||
private final Context context; | ||
|
||
public UpdateTask(final Context context) { | ||
super(); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
protected Exception doInBackground(final Void... params) { | ||
final PreferenceAdaptor preferences = new PreferenceAdaptor(context); | ||
final PreferenceAdaptorEditor editor = preferences.edit(); | ||
|
||
try { | ||
final JSONObject data = GetDataUtil | ||
.getGasPricesDataFromInternet(context.getCacheDir()); | ||
editor.setJsonData(data); | ||
editor.setLastUpdatedToNow(); | ||
return null; | ||
} catch (final IOException e) { | ||
// TODO fix this is yucky | ||
Log.e("GasPrices", e.getMessage() + " and cry"); | ||
try { | ||
editor.setLastError(e.getMessage(), GetDataUtil | ||
.getRawGasPricesDataFromInternet(context.getCacheDir())); | ||
} catch (final IOException e2) { | ||
editor.setLastError(e.getMessage(), ""); | ||
return e2; | ||
} | ||
return e; | ||
} finally { | ||
editor.apply(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(final Exception result) { | ||
if (result == null) { | ||
final AppWidgetManager widgetManager = AppWidgetManager | ||
.getInstance(context); | ||
final ComponentName widgetComponent = new ComponentName(context, | ||
GasPricesWidgetProvider.class); | ||
final int[] widgetIds = widgetManager | ||
.getAppWidgetIds(widgetComponent); | ||
if (widgetIds.length > 0) { | ||
final Intent update = new Intent(); | ||
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds); | ||
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); | ||
context.sendBroadcast(update); | ||
} | ||
} else { | ||
new AlertDialog.Builder(context) | ||
.setTitle(R.string.error) | ||
.setMessage( | ||
context.getString(R.string.problem_loading_format, | ||
result.getLocalizedMessage())).show(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
final PreferenceAdaptor preferences = new PreferenceAdaptor(context); | ||
final PreferenceAdaptorEditor editor = preferences.edit(); | ||
editor.removeLastError(); | ||
editor.apply(); | ||
} | ||
} |