Skip to content

Commit 1dc993b

Browse files
committed
Use stored metadata when offline
1 parent d86e90a commit 1dc993b

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

core/src/main/java/io/snabble/sdk/MetadataDownloader.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.snabble.sdk;
22

3-
import android.os.Bundle;
4-
53
import com.google.gson.Gson;
64
import com.google.gson.JsonObject;
75
import com.google.gson.JsonSyntaxException;
@@ -23,15 +21,19 @@ class MetadataDownloader extends StringDownloader {
2321
private JsonObject metadata;
2422
private JsonObject project;
2523

24+
private boolean hasData = false;
25+
2626
public MetadataDownloader(SnabbleSdk sdk,
2727
String bundledFileAssetPath) {
2828
super(sdk.getOkHttpClient());
2929

3030
this.sdk = sdk;
31+
File storageFile = new File(sdk.getInternalStorageDirectory(), "metadata.json");
3132

3233
if (bundledFileAssetPath != null) {
33-
setBundledData(sdk.getApplication(), bundledFileAssetPath,
34-
new File(sdk.getInternalStorageDirectory(), "metadata.json"));
34+
setBundledData(sdk.getApplication(), bundledFileAssetPath, storageFile);
35+
} else {
36+
setStorageFile(storageFile);
3537
}
3638

3739
setUrl(sdk.getMetadataUrl());
@@ -42,8 +44,6 @@ protected void onDownloadFinished(String content) {
4244
try {
4345
Map<String, String> urls = new HashMap<>();
4446
Map<String, String> extras = new HashMap<>();
45-
Bundle flags = new Bundle();
46-
Bundle projectSettings = new Bundle();
4747

4848
Gson gson = new Gson();
4949
JsonObject jsonObject = gson.fromJson(content, JsonObject.class);
@@ -73,11 +73,16 @@ protected void onDownloadFinished(String content) {
7373
this.extras = Collections.unmodifiableMap(extras);
7474

7575
updateStorage(content);
76+
hasData = true;
7677
} catch (JsonSyntaxException e){
7778
Logger.e(e.getMessage());
7879
}
7980
}
8081

82+
public boolean hasData() {
83+
return hasData;
84+
}
85+
8186
public Map<String, String> getUrls() {
8287
return urls;
8388
}

core/src/main/java/io/snabble/sdk/SnabbleSdk.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ protected void onDataLoaded(boolean wasStillValid) {
298298

299299
@Override
300300
protected void onError() {
301-
setupError(setupCompletionListener, Error.CONNECTION_TIMEOUT);
301+
if(metadataDownloader.hasData()){
302+
setupSdk(config, setupCompletionListener);
303+
} else {
304+
setupError(setupCompletionListener, Error.CONNECTION_TIMEOUT);
305+
}
302306
}
303307
});
304308
}

utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ public void setBundledData(Context context, String assetPath, File storageFile)
4242
//so we check for the install time of the app
4343
//this implicitly means that while developing you always start with the seeded data
4444
//and that app updates should always contain updated seeds
45-
if (storageFile.exists() && storageFile.lastModified() > packageInfo.lastUpdateTime) {
46-
FileInputStream fis = new FileInputStream(this.storageFile);
47-
Logger.d("Using saved data: %s", storageFile.getAbsolutePath());
48-
onDownloadFinished(IOUtils.toString(fis, Charset.forName("UTF-8")));
49-
fis.close();
45+
if(storageFile.exists() && storageFile.lastModified() > packageInfo.lastUpdateTime) {
46+
loadFromSavedData();
5047
} else {
5148
Logger.d("Using initial seed: %s", storageFile.getAbsolutePath());
5249
onDownloadFinished(IOUtils.toString(bundledDataInputStream, Charset.forName("UTF-8")));
@@ -56,8 +53,23 @@ public void setBundledData(Context context, String assetPath, File storageFile)
5653
}
5754
}
5855

56+
private void loadFromSavedData() throws IOException {
57+
FileInputStream fis = new FileInputStream(storageFile);
58+
Logger.d("Using saved data: %s", storageFile.getAbsolutePath());
59+
onDownloadFinished(IOUtils.toString(fis, Charset.forName("UTF-8")));
60+
fis.close();
61+
}
62+
5963
public void setStorageFile(File storageFile) {
6064
this.storageFile = storageFile;
65+
66+
if (storageFile.exists()) {
67+
try {
68+
loadFromSavedData();
69+
} catch (IOException e) {
70+
Logger.e("Could not load saved data: %s", storageFile.getAbsolutePath());
71+
}
72+
}
6173
}
6274

6375
/**
@@ -88,8 +100,6 @@ protected void onResponse(Response response) throws IOException {
88100
if (body != null) {
89101
onDownloadFinished(body.string());
90102
Logger.d("Received data for %s", getUrl());
91-
} else {
92-
Logger.d("Error receiving data for %s", getUrl());
93103
}
94104
}
95105

0 commit comments

Comments
 (0)