Fixed caching issues.

master
Tomasz Półgrabia 2016-09-18 23:03:53 +02:00
parent 225d4c0808
commit bb4540a801
6 changed files with 254 additions and 2 deletions

View File

@ -75,4 +75,5 @@ dependencies {
// workaround issue #73
exclude group: 'com.google.android', module: 'android'
}
compile 'com.google.code.gson:gson:2.7'
}

View File

@ -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";
}

View File

@ -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<PanoramioImageInfo> 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<PanoramioImageInfo> getPanoramioImages() {
return panoramioImages;
}
public void setPanoramioImages(List<PanoramioImageInfo> 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 +
'}';
}
}

View File

@ -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<WikiAppObject> appObject;
private Double longitude;
private Double latitude;
private Double altitude;
private Date fetchedAt;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public List<WikiAppObject> getAppObject() {
return appObject;
}
public void setAppObject(List<WikiAppObject> 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 +
'}';
}
}

View File

@ -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

View File

@ -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<WikiAppObject>()
: (ArrayList<WikiAppObject>)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);;
: (ArrayList<WikiAppObject>)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);
}
}
}