Skip to content

Commit ff2580a

Browse files
committed
Write metadata updates to a tempfile first and improve error handling
1 parent 49e74ca commit ff2580a

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public MetadataDownloader(SnabbleSdk sdk,
4545
}
4646

4747
@Override
48-
protected void onDownloadFinished(String content) {
48+
protected synchronized void onDownloadFinished(String content) {
4949
try {
5050
Map<String, String> urls = new HashMap<>();
5151
Map<String, String> extras = new HashMap<>();
@@ -68,7 +68,6 @@ protected void onDownloadFinished(String content) {
6868

6969
if (jsonObject.has("metadata")) {
7070
this.metadata = jsonObject.get("metadata").getAsJsonObject();
71-
7271
}
7372

7473
if (jsonObject.has("project")) {
@@ -82,7 +81,7 @@ protected void onDownloadFinished(String content) {
8281

8382
updateStorage(content);
8483
hasData = true;
85-
} catch (JsonSyntaxException e){
84+
} catch (Exception e) {
8685
Logger.e(e.getMessage());
8786
}
8887
}

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.pm.PackageManager;
66

77
import org.apache.commons.io.FileUtils;
8+
import org.apache.commons.io.FilenameUtils;
89
import org.apache.commons.io.IOUtils;
910

1011
import java.io.File;
@@ -20,6 +21,8 @@
2021

2122
public abstract class StringDownloader extends Downloader {
2223
private File storageFile = null;
24+
private String assetPath;
25+
private Context context;
2326

2427
public StringDownloader(OkHttpClient okHttpClient) {
2528
super(okHttpClient);
@@ -32,10 +35,11 @@ public StringDownloader(OkHttpClient okHttpClient) {
3235
* uses the app files data unless the app gets updated.
3336
*/
3437
public void setBundledData(Context context, String assetPath, File storageFile) {
38+
this.context = context;
3539
this.storageFile = storageFile;
40+
this.assetPath = assetPath;
3641

3742
try {
38-
InputStream bundledDataInputStream = context.getAssets().open(assetPath);
3943
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
4044

4145
//you can not check for file modified time in the android assets folder,
@@ -46,28 +50,45 @@ public void setBundledData(Context context, String assetPath, File storageFile)
4650
loadFromSavedData();
4751
} else {
4852
Logger.d("Using initial seed: %s", storageFile.getAbsolutePath());
49-
onDownloadFinished(IOUtils.toString(bundledDataInputStream, Charset.forName("UTF-8")));
53+
loadFromBundledData();
5054
}
5155
} catch (IOException | PackageManager.NameNotFoundException e) {
5256
Logger.e("Could not load saved data: %s", storageFile.getAbsolutePath());
57+
loadFromBundledData();
5358
}
5459
}
5560

5661
private void loadFromSavedData() throws IOException {
5762
FileInputStream fis = new FileInputStream(storageFile);
5863
Logger.d("Using saved data: %s", storageFile.getAbsolutePath());
59-
onDownloadFinished(IOUtils.toString(fis, Charset.forName("UTF-8")));
64+
String savedData = IOUtils.toString(fis, Charset.forName("UTF-8"));
65+
if(savedData != null && savedData.length() > 0){
66+
onDownloadFinished(savedData);
67+
} else {
68+
Logger.d("Data corrupted, using initial seed: %s", storageFile.getAbsolutePath());
69+
loadFromBundledData();
70+
}
6071
fis.close();
6172
}
6273

74+
private void loadFromBundledData() {
75+
try {
76+
InputStream bundledDataInputStream = context.getAssets().open(assetPath);
77+
onDownloadFinished(IOUtils.toString(bundledDataInputStream, Charset.forName("UTF-8")));
78+
bundledDataInputStream.close();
79+
} catch (IOException e) {
80+
Logger.e("Could not bundled data: %s", e.toString());
81+
}
82+
}
83+
6384
public void setStorageFile(File storageFile) {
6485
this.storageFile = storageFile;
6586

6687
if (storageFile.exists()) {
6788
try {
6889
loadFromSavedData();
6990
} catch (IOException e) {
70-
Logger.e("Could not load saved data: %s", storageFile.getAbsolutePath());
91+
Logger.e("Could not load saved data: %s", e.toString());
7192
}
7293
}
7394
}
@@ -78,17 +99,22 @@ public void setStorageFile(File storageFile) {
7899
* Note that app updates are causing the updated data written here to be overwritten again,
79100
* as it is assumed that new builds always have newer data.
80101
*/
81-
public void updateStorage(String content) {
102+
public synchronized void updateStorage(String content) {
82103
if (storageFile != null) {
83-
FileUtils.deleteQuietly(storageFile);
84-
85104
try {
86-
FileOutputStream fos = new FileOutputStream(storageFile);
105+
File tempFile = new File(FilenameUtils.getFullPath(storageFile.getPath())
106+
+ FilenameUtils.getBaseName(storageFile.getName())
107+
+ "_temp." + FilenameUtils.getExtension(storageFile.getName()));
108+
109+
FileOutputStream fos = new FileOutputStream(tempFile);
87110
IOUtils.write(content, fos, Charset.forName("UTF-8"));
88111
fos.close();
112+
113+
FileUtils.deleteQuietly(storageFile);
114+
FileUtils.moveFile(tempFile, storageFile);
89115
Logger.d("Updated saved data:%s", storageFile.getAbsolutePath());
90116
} catch (IOException e) {
91-
Logger.e("Could not update saved data %s", storageFile.getAbsolutePath());
117+
Logger.e("Could not update saved data %s", e.toString());
92118
}
93119
}
94120
}

0 commit comments

Comments
 (0)