Skip to content

Commit

Permalink
Merge pull request #514 from afischerdev/app-update
Browse files Browse the repository at this point in the history
App update: control update for serverconfig
  • Loading branch information
afischerdev committed Mar 23, 2023
2 parents a75ee2b + 4c2bf8f commit 86ae6d2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
Expand Up @@ -287,10 +287,17 @@ private void downloadProfiles() throws IOException, InterruptedException {
String profileLocation = mServerConfig.getProfilesUrl() + fileName;
URL profileUrl = new URL(profileLocation);
int size = (int) (profileFile.exists() ? profileFile.length() : 0);
downloadProgressListener.onDownloadStart(fileName, DownloadType.PROFILE);
downloadFile(profileUrl, profileFile, size, false, DownloadType.PROFILE);
downloadProgressListener.onDownloadFinished();
done.add(profileUrl);

try {
downloadProgressListener.onDownloadStart(fileName, DownloadType.PROFILE);
downloadFile(profileUrl, profileFile, size, false, DownloadType.PROFILE);
downloadProgressListener.onDownloadFinished();
done.add(profileUrl);
} catch (IOException e) {
// no need to block other updates
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage());
}
}
}
}
Expand Down
@@ -1,13 +1,20 @@
package btools.routingapp;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ServerConfig {
private static String mServerConfigName = "serverconfig.txt";

private String mSegmentUrl = "https://brouter.de/brouter/segments4/";
private String mLookupsUrl = "https://brouter.de/brouter/profiles2/";
private String mProfilesUrl = "https://brouter.de/brouter/profiles2/";
Expand All @@ -16,10 +23,19 @@ public class ServerConfig {
private String[] mProfiles = new String[0];

public ServerConfig(Context ctx) {
File configFile = new File(ConfigHelper.getBaseDir(ctx), "/brouter/segments4/serverconfig.txt");
if (configFile.exists()) {
File configFile = new File(ConfigHelper.getBaseDir(ctx), "/brouter/segments4/" + mServerConfigName);
readConfigFile(configFile);
}

public ServerConfig(Context context, File file) {
readConfigFile(file);
}

private void readConfigFile(File file) {
if (file.exists()) {
BufferedReader br = null;
try {
BufferedReader br = new BufferedReader(new FileReader(configFile));
br = new BufferedReader(new FileReader(file));
for (; ; ) {
String line = br.readLine();
if (line == null) break;
Expand All @@ -35,11 +51,84 @@ public ServerConfig(Context ctx) {
mProfiles = line.substring(15).split(",");
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

}

public static void checkForUpdate(Context context, File path, String assetZip) {
if (assetZip != null) {
writeTmpFromAsset(context, path, assetZip);

File configFileOld = new File(ConfigHelper.getBaseDir(context), "/brouter/segments4/" + mServerConfigName);
File configFileNew = new File(ConfigHelper.getBaseDir(context), "/brouter/segments4/" + mServerConfigName + ".tmp");
if (configFileOld.length() != configFileNew.length()) {
ServerConfig serverConfigOld = new ServerConfig(context, configFileOld);
ServerConfig serverConfigNew = new ServerConfig(context, configFileNew);
if (serverConfigOld.getSegmentUrl().equals(serverConfigNew.getSegmentUrl()) &&
serverConfigOld.getProfilesUrl().equals(serverConfigNew.getProfilesUrl()) &&
serverConfigOld.getLookupUrl().equals(serverConfigNew.getLookupUrl())
) {
// replace when servers wasn't changed
configFileOld.delete();
configFileNew.renameTo(configFileOld);
}
} else {
configFileNew.delete();
}
}
}

private static void writeTmpFromAsset(Context context, File path, String assetZip) {
InputStream is = null;
try {
AssetManager assetManager = context.getAssets();
is = assetManager.open(assetZip);
ZipInputStream zis = new ZipInputStream(is);
byte[] data = new byte[1024];
for (; ; ) {
ZipEntry ze = zis.getNextEntry();
if (ze == null)
break;
if (ze.isDirectory()) {
continue;
}
String name = ze.getName();
if (name.equals(mServerConfigName)) {
File outfile = new File(path, name + ".tmp");
if (!outfile.exists() && outfile.getParentFile() != null) {
outfile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(outfile);

for (; ; ) {
int len = zis.read(data, 0, 1024);
if (len < 0)
break;
fos.write(data, 0, len);
}
fos.close();
}
}
zis.closeEntry();
}
zis.close();
} catch (IOException io) {
throw new RuntimeException("error expanding " + assetZip + ": " + io);
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Expand Down

0 comments on commit 86ae6d2

Please sign in to comment.