update tiles if outdated (use lastModified tag and only update on wifi)

This commit is contained in:
Philipp Schaefer 2013-12-21 18:39:50 +01:00
parent 8f5072dda5
commit c624a789d5
3 changed files with 8 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class IITC_TileManager {
@ -24,7 +25,6 @@ public class IITC_TileManager {
}
public WebResourceResponse getTile(String url) throws Exception {
Log.d("iitcm", "checking for tile: " + url);
String path = mIitc.getApplication().getFilesDir().toString() + "/" + url;
path = path.replace("http://", "");
path = path.replace("https://", "");
@ -33,6 +33,9 @@ public class IITC_TileManager {
path = path.replace(fileName, "");
File file = new File(path, fileName);
if (file.exists()) {
// asynchronously download tile if outdated, ignore date if not connected to wifi
if (mIitc.getWebView().isConnectedToWifi()) new DownloadTile(path, fileName).execute(url);
// return tile from storage
InputStream in = new BufferedInputStream(new FileInputStream(file));
return new WebResourceResponse(TYPE, ENCODING, in);
} else {

View File

@ -237,7 +237,7 @@ public class IITC_WebView extends WebView {
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private boolean isConnectedToWifi() {
public boolean isConnectedToWifi() {
ConnectivityManager conMan = (ConnectivityManager) getContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

View File

@ -37,9 +37,11 @@ public class DownloadTile extends AsyncTask<String, Void, Boolean> {
try {
tileUrl = new URL(urls[0]);
conn = tileUrl.openConnection();
File file = new File(mFilePath, mFileName);
// update tile if needed, else return
if (conn.getLastModified() < file.lastModified()) return true;
InputStream is = null;
is = conn.getInputStream();
File file = new File(mFilePath, mFileName);
Log.d("iitcm", "writing to file: " + file.toString());
File output = writeTileToFile(is, file, mFilePath);
} catch (IOException e) {