Skip to content

Commit

Permalink
Make sdcard storage the default. Fix sdcard mobileorg.org file sync
Browse files Browse the repository at this point in the history
  • Loading branch information
matburt committed Sep 15, 2010
1 parent a349475 commit 8c31158
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
2 changes: 1 addition & 1 deletion res/xml/preferences.xml
Expand Up @@ -20,7 +20,7 @@
android:key="storageMode"
android:entries="@array/storageModes"
android:entryValues="@array/storageModesVals"
android:defaultValue="internal" />
android:defaultValue="sdcard" />
</PreferenceCategory>
<PreferenceCategory android:title="Other">
<PreferenceScreen
Expand Down
9 changes: 8 additions & 1 deletion src/com/matburt/mobileorg/OrgFileParser.java
Expand Up @@ -234,7 +234,14 @@ public BufferedReader getHandle(String filename) {
this.fstream = new FileInputStream("/data/data/com.matburt.mobileorg/files/" + normalized);
}
else if (this.storageMode.equals("sdcard")) {
this.fstream = new FileInputStream(this.orgDir + filename);
String dirActual = "";
if (filename.equals("mobileorg.org")) {
dirActual = "/sdcard/mobileorg/";
}
else {
dirActual = this.orgDir;
}
this.fstream = new FileInputStream(dirActual + filename);
}
else {
Log.e(LT, "[Parse] Unknown storage mechanism: " + this.storageMode);
Expand Down
119 changes: 114 additions & 5 deletions src/com/matburt/mobileorg/SDCardSynchronizer.java
Expand Up @@ -8,6 +8,7 @@
import android.preference.PreferenceManager;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
Expand All @@ -17,6 +18,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class SDCardSynchronizer implements Synchronizer
{
Expand All @@ -39,23 +42,129 @@ public void close() {
}

public void push() throws NotFoundException, ReportableError {
String storageMode = this.appSettings.getString("storageMode", "");
BufferedReader reader = null;
String fileContents = "";

if (storageMode.equals("internal") || storageMode == null) {
FileInputStream fs;
try {
fs = rootActivity.openFileInput("mobileorg.org");
reader = new BufferedReader(new InputStreamReader(fs));
}
catch (java.io.FileNotFoundException e) {
Log.i(LT, "Did not find mobileorg.org file, not pushing.");
return;
}
}
else if (storageMode.equals("sdcard")) {
try {
File root = Environment.getExternalStorageDirectory();
File morgDir = new File(root, "mobileorg");
File morgFile = new File(morgDir, "mobileorg.org");
if (!morgFile.exists()) {
Log.i(LT, "Did not find mobileorg.org file, not pushing.");
return;
}
FileReader orgFReader = new FileReader(morgFile);
reader = new BufferedReader(orgFReader);
}
catch (java.io.IOException e) {
throw new ReportableError(
r.getString(R.string.error_file_read, "mobileorg.org"),
e);
}
}
else {
throw new ReportableError(
r.getString(R.string.error_local_storage_method_unknown, storageMode),
null);
}

String thisLine = "";
try {
while ((thisLine = reader.readLine()) != null) {
fileContents += thisLine + "\n";
}
}
catch (java.io.IOException e) {
throw new ReportableError(
r.getString(R.string.error_file_read, "mobileorg.org"),
e);
}

String indexFile = this.appSettings.getString("indexFilePath", "");
File fIndexFile = new File(indexFile);
String basePath = fIndexFile.getParent();

this.appendSDCardFile(basePath + "/mobileorg.org", fileContents);
this.appdb.removeFile("mobileorg.org");

if (storageMode.equals("internal") || storageMode == null) {
this.rootActivity.deleteFile("mobileorg.org");
}
else if (storageMode.equals("sdcard")) {
File root = Environment.getExternalStorageDirectory();
File morgDir = new File(root, "mobileorg");
File morgFile = new File(morgDir, "mobileorg.org");
morgFile.delete();
}
}

private void appendSDCardFile(String path,
String content) throws NotFoundException, ReportableError {
String originalContent = "";
try {
originalContent = this.readFile(path) + "\n";
}
catch (java.io.FileNotFoundException e) {}
String newContent = originalContent + content;
this.putFile(path, newContent);
}

private void putFile(String path,
String content) throws NotFoundException, ReportableError {
Log.d(LT, "Writing to mobileorg.org file at: " + path);
BufferedWriter fWriter;
try {
File fMobileOrgFile = new File(path);
FileWriter orgFWriter = new FileWriter(fMobileOrgFile, true);
fWriter = new BufferedWriter(orgFWriter);
fWriter.write(content);
fWriter.flush();
fWriter.close();
}
catch (java.io.IOException e) {
throw new ReportableError(
r.getString(R.string.error_file_write, path),
e);

}
}

public void pull() throws NotFoundException, ReportableError {
String indexFile = this.appSettings.getString("indexFilePath","");
Log.d(LT, "Index file at: " + indexFile);
File fIndexFile = new File(indexFile);
String basePath = fIndexFile.getParent();
String filebuffer = this.readFile(indexFile);
String filebuffer = "";
try {
filebuffer = this.readFile(indexFile);
}
catch (java.io.FileNotFoundException e) {
throw new ReportableError(
r.getString(R.string.error_file_not_found, indexFile),
e);
}
HashMap<String, String> masterList = this.getOrgFilesFromMaster(filebuffer);

for (String key : masterList.keySet()) {
Log.d(LT, "Fetching: " + key + ": " + basePath + masterList.get(key));
Log.d(LT, "Fetching: " + key + ": " + basePath + "/" + masterList.get(key));
this.appdb.addOrUpdateFile(masterList.get(key), key);
}
}

private String readFile(String filePath) throws ReportableError {
private String readFile(String filePath) throws ReportableError, java.io.FileNotFoundException {
FileInputStream readerIS;
BufferedReader fReader;
File inpfile = new File(filePath);
Expand All @@ -64,8 +173,8 @@ private String readFile(String filePath) throws ReportableError {
fReader = new BufferedReader(new InputStreamReader(readerIS));
}
catch (java.io.FileNotFoundException e) {
throw new ReportableError(r.getString(R.string.error_file_not_found, filePath),
e);
Log.d(LT, "Could not locate file " + filePath);
throw e;
}
String fileBuffer = "";
String fileLine = "";
Expand Down

0 comments on commit 8c31158

Please sign in to comment.