Move serverconfig.txt handling to own class

This commit is contained in:
Manuel Fuhr 2021-11-17 14:19:15 +01:00
parent 1e594574b5
commit 67bbc3d2ac
2 changed files with 73 additions and 45 deletions

View file

@ -11,14 +11,11 @@ import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.StatFs;
import android.util.Log;
import android.widget.Toast;
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.io.OutputStream;
@ -35,11 +32,7 @@ public class DownloadService extends Service implements ProgressListener {
private static final boolean DEBUG = false;
String segmenturl = "https://brouter.de/brouter/segments4/";
String lookupurl = "https://brouter.de/brouter/profile2/";
String profilesurl = "https://brouter.de/brouter/profile2/";
String checkLookup = "lookups.dat";
String checkProfiles = "";
private ServerConfig mServerConfig;
private NotificationHelper mNotificationHelper;
private List<String> mUrlList;
@ -78,6 +71,7 @@ public class DownloadService extends Service implements ProgressListener {
public void onCreate() {
if (DEBUG) Log.d("SERVICE", "onCreate");
serviceState = true;
mServerConfig = new ServerConfig(getApplicationContext());
HandlerThread thread = new HandlerThread("ServiceStartArguments", 1);
thread.start();
@ -109,38 +103,6 @@ public class DownloadService extends Service implements ProgressListener {
List<String> urlparts = extra.getStringArrayList("urlparts");
mUrlList = urlparts;
baseDir = dir;
File configFile = new File (dir, "segments4/serverconfig.txt");
if ( configFile.exists() ) {
try {
BufferedReader br = new BufferedReader( new FileReader( configFile ) );
for ( ;; )
{
String line = br.readLine();
if ( line == null ) break;
if ( line.trim().startsWith( "segment_url=" ) ) {
segmenturl = line.substring(12);
}
else if ( line.trim().startsWith( "lookup_url=" ) ) {
lookupurl = line.substring(11);
}
else if ( line.trim().startsWith( "profiles_url=" ) ) {
profilesurl = line.substring(13);
}
else if ( line.trim().startsWith( "check_lookup=" ) ) {
checkLookup = line.substring(13);
}
else if ( line.trim().startsWith( "check_profiles=" ) ) {
checkProfiles = line.substring(15);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
mNotificationHelper.startNotification(this);
@ -185,7 +147,7 @@ public class DownloadService extends Service implements ProgressListener {
int count = 1;
int size = mUrlList.size();
for (String part: mUrlList) {
String url = segmenturl + part + ".rd5";
String url = mServerConfig.getSegmentUrl() + part + ".rd5";
if (DEBUG) Log.d("BR", "downlaod " + url);
result = download(count, size, url);
@ -386,7 +348,7 @@ public class DownloadService extends Service implements ProgressListener {
private String checkScripts() {
String[] sa = checkLookup.split(",");
String[] sa = mServerConfig.getLookups();
for (String f: sa) {
if (f.length()>0) {
File file = new File(baseDir + "profiles2", f);
@ -394,7 +356,7 @@ public class DownloadService extends Service implements ProgressListener {
}
}
sa = checkProfiles.split(",");
sa = mServerConfig.getProfiles();
for (String f : sa) {
if (f.length()>0) {
File file = new File(baseDir + "profiles2", f);
@ -410,12 +372,12 @@ public class DownloadService extends Service implements ProgressListener {
}
private String checkOrDownloadLookup(String fileName, File f) {
String url = lookupurl + fileName;
String url = mServerConfig.getLookupUrl() + fileName;
return downloadScript(url, f);
}
private String checkOrDownloadScript(String fileName, File f) {
String url = profilesurl + fileName;
String url = mServerConfig.getProfilesUrl() + fileName;
return downloadScript(url, f);
}

View file

@ -0,0 +1,66 @@
package btools.routingapp;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ServerConfig {
private String mSegmentUrl = "https://brouter.de/brouter/segments4/";
private String mLookupsUrl = "https://brouter.de/brouter/profiles2/";
private String mProfilesUrl = "https://brouter.de/brouter/profiles2/";
private String[] mLookups = new String[]{"lookups.dat"};
private String[] mProfiles = new String[0];
public ServerConfig(Context ctx) {
File configFile = new File(ConfigHelper.getBaseDir(ctx), "/brouter/segments4/serverconfig.txt");
if (configFile.exists()) {
try {
BufferedReader br = new BufferedReader(new FileReader(configFile));
for (; ; ) {
String line = br.readLine();
if (line == null) break;
if (line.trim().startsWith("segment_url=")) {
mSegmentUrl = line.substring(12);
} else if (line.trim().startsWith("lookup_url=")) {
mLookupsUrl = line.substring(11);
} else if (line.trim().startsWith("profiles_url=")) {
mProfilesUrl = line.substring(13);
} else if (line.trim().startsWith("check_lookup=")) {
mLookups = line.substring(13).split(",");
} else if (line.trim().startsWith("check_profiles=")) {
mProfiles = line.substring(15).split(",");
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getSegmentUrl() {
return mSegmentUrl;
}
public String getLookupUrl() {
return mLookupsUrl;
}
public String getProfilesUrl() {
return mProfilesUrl;
}
public String[] getLookups() {
return mLookups;
}
public String[] getProfiles() {
return mProfiles;
}
}