From bb4540a801b24a97c417e57d1bb9aa960abb3b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sun, 18 Sep 2016 23:03:53 +0200 Subject: [PATCH] Fixed caching issues. --- app/build.gradle | 1 + .../urbanexplorer/AppConstants.java | 2 + .../dto/panoramio/PanoramioCacheDto.java | 70 +++++++++++++++++ .../urbanexplorer/dto/wiki/WikiCacheDto.java | 75 +++++++++++++++++++ .../urbanexplorer/fragments/HomeFragment.java | 58 +++++++++++++- .../fragments/WikiLocationsFragment.java | 50 ++++++++++++- 6 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioCacheDto.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/WikiCacheDto.java diff --git a/app/build.gradle b/app/build.gradle index 1ba45e6..434911a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -75,4 +75,5 @@ dependencies { // workaround issue #73 exclude group: 'com.google.android', module: 'android' } + compile 'com.google.code.gson:gson:2.7' } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index 546e1b2..1e89385 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -25,4 +25,6 @@ public class AppConstants { public static final String PANORAMIO_BULK_SIZE_KEY = "pref_panoramio_bulk_size"; public static final int PANORAMIO_BULK_SIZE_DEF_VALUE = 50; + public static final String PANORAMIO_CACHE_FILENAME = "panoramio-cache.dat"; + public static final String WIKI_CACHE_FILENAME = "wiki-cache.dat"; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioCacheDto.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioCacheDto.java new file mode 100644 index 0000000..be327c5 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/panoramio/PanoramioCacheDto.java @@ -0,0 +1,70 @@ +package pl.tpolgrabia.urbanexplorer.dto.panoramio; + +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * Created by tpolgrabia on 18.09.16. + */ +public class PanoramioCacheDto implements Serializable{ + private static final long serialVersionUID = -8856222832500878380L; + private List panoramioImages; + private Double latitude; + private Double longitude; + private Double altitude; + private Date fetchedAt; + + public Date getFetchedAt() { + return fetchedAt; + } + + public void setFetchedAt(Date fetchedAt) { + this.fetchedAt = fetchedAt; + } + + public List getPanoramioImages() { + return panoramioImages; + } + + public void setPanoramioImages(List panoramioImages) { + this.panoramioImages = panoramioImages; + } + + public Double getLatitude() { + return latitude; + } + + public void setLatitude(Double latitude) { + this.latitude = latitude; + } + + public Double getLongitude() { + return longitude; + } + + public void setLongitude(Double longitude) { + this.longitude = longitude; + } + + public Double getAltitude() { + return altitude; + } + + public void setAltitude(Double altitude) { + this.altitude = altitude; + } + + @Override + public String toString() { + return "PanoramioCacheDto{" + + "panoramioImages=" + panoramioImages + + ", latitude=" + latitude + + ", longitude=" + longitude + + ", altitude=" + altitude + + ", fetchedAt=" + fetchedAt + + '}'; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/WikiCacheDto.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/WikiCacheDto.java new file mode 100644 index 0000000..e4e0260 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/WikiCacheDto.java @@ -0,0 +1,75 @@ +package pl.tpolgrabia.urbanexplorer.dto.wiki; + +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * Created by tpolgrabia on 18.09.16. + */ +public class WikiCacheDto implements Serializable{ + private static final long serialVersionUID = 3957902509576625035L; + + private List appObject; + private Double longitude; + private Double latitude; + private Double altitude; + private Date fetchedAt; + + public static long getSerialVersionUID() { + return serialVersionUID; + } + + public List getAppObject() { + return appObject; + } + + public void setAppObject(List appObject) { + this.appObject = appObject; + } + + public Double getLongitude() { + return longitude; + } + + public void setLongitude(Double longitude) { + this.longitude = longitude; + } + + public Double getLatitude() { + return latitude; + } + + public void setLatitude(Double latitude) { + this.latitude = latitude; + } + + public Double getAltitude() { + return altitude; + } + + public void setAltitude(Double altitude) { + this.altitude = altitude; + } + + public Date getFetchedAt() { + return fetchedAt; + } + + public void setFetchedAt(Date fetchedAt) { + this.fetchedAt = fetchedAt; + } + + @Override + public String toString() { + return "WikiCacheDto{" + + "appObject=" + appObject + + ", longitude=" + longitude + + ", latitude=" + latitude + + ", altitude=" + altitude + + ", fetchedAt=" + fetchedAt + + '}'; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index 4d3e86f..b101bd7 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -13,6 +13,8 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.*; +import com.google.gson.Gson; +import com.google.gson.stream.JsonReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.AppConstants; @@ -21,12 +23,14 @@ import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.callbacks.PanoramioResponseCallback; import pl.tpolgrabia.urbanexplorer.callbacks.PanoramioResponseStatus; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback; +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils; -import java.io.Serializable; +import java.io.*; import java.util.ArrayList; +import java.util.GregorianCalendar; import java.util.List; import java.util.concurrent.Semaphore; @@ -142,6 +146,31 @@ public class HomeFragment extends Fragment { } } + if (photos == null || photos.isEmpty()) { + // maybe we find something in our cache file + try (Reader br = + new InputStreamReader( + new FileInputStream( + new File(getActivity().getCacheDir(), + AppConstants.PANORAMIO_CACHE_FILENAME)))) { + PanoramioCacheDto dto = new Gson().fromJson(new JsonReader(br), PanoramioCacheDto.class); + if (dto != null) { + photos = new ArrayList<>(dto.getPanoramioImages()); + lg.trace("Photos size from I/O cache is {}", photos.size()); + } else { + lg.trace("Sorry, photos I/O cache is null"); + } + + } catch (FileNotFoundException e) { + lg.error("File not found", e); + } catch (IOException e) { + lg.error("I/O error", e); + } catch (Throwable t) { + lg.error("Throwable", t); + } + lg.trace("I've read photos from I/O cache"); + } + locations.setAdapter(new PanoramioAdapter(getActivity(), R.layout.location_item, photos)); lg.trace("Photos initialized {}", photos); @@ -408,6 +437,33 @@ public class HomeFragment extends Fragment { public void onDestroy() { super.onDestroy(); lg.trace("onDestroy"); + + File cacheDir = getActivity().getCacheDir(); + try (BufferedWriter br = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream( + new File(cacheDir, AppConstants.PANORAMIO_CACHE_FILENAME))))) { + + PanoramioCacheDto dto = new PanoramioCacheDto(); + dto.setPanoramioImages(photos); + LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); + Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); + if (location != null) { + dto.setLongitude(location.getLongitude()); + dto.setLatitude(location.getLatitude()); + dto.setAltitude(location.getAltitude()); + } + + dto.setFetchedAt(new GregorianCalendar().getTime()); + // FIXME this should be a fetch time, not persist time + + new Gson().toJson(dto, br); + + } catch (FileNotFoundException e) { + lg.error("File not found", e); + } catch (IOException e) { + lg.error("I/O Exception", e); + } } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index f3a72ee..7859d30 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -14,21 +14,26 @@ import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import com.google.gson.Gson; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; import pl.tpolgrabia.urbanexplorer.callbacks.FetchWikiLocationsCallback; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback; import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; +import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; +import java.io.*; import java.util.ArrayList; +import java.util.GregorianCalendar; import java.util.List; import static android.content.Context.LOCATION_SERVICE; @@ -58,7 +63,23 @@ public class WikiLocationsFragment extends Fragment { super.onCreate(savedInstanceState); lg.trace("onCreate {}", System.identityHashCode(this)); appObjects = savedInstanceState == null ? new ArrayList() - : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);; + : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS); + + if (appObjects == null) { + try (InputStreamReader ir = new InputStreamReader( + new FileInputStream( + new File(getActivity().getCacheDir(), + AppConstants.WIKI_CACHE_FILENAME)))) { + + WikiCacheDto dto = new Gson().fromJson(ir, WikiCacheDto.class); + appObjects = new ArrayList<>(dto.getAppObject()); + + } catch (FileNotFoundException e) { + lg.error("File not found", e); + } catch (IOException e) { + lg.error("I/O error", e); + } + } } @Override @@ -228,5 +249,32 @@ public class WikiLocationsFragment extends Fragment { public void onDestroy() { super.onDestroy(); lg.trace("onDestroy {}", System.identityHashCode(this)); + + try (BufferedWriter bw = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream( + new File(getActivity().getCacheDir(), + AppConstants.WIKI_CACHE_FILENAME))))) { + + WikiCacheDto dto = new WikiCacheDto(); + dto.setAppObject(appObjects); + LocationManager locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); + Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); + if (location != null) { + dto.setLongitude(location.getLongitude()); + dto.setLatitude(location.getLatitude()); + dto.setAltitude(location.getAltitude()); + } + + dto.setFetchedAt(new GregorianCalendar().getTime()); + // FIXME should be a fetched time, not persist time + + new Gson().toJson(bw); + + } catch (FileNotFoundException e) { + lg.error("File not found", e); + } catch (IOException e) { + lg.error("I/O error", e); + } } }