Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break note creation out into it's own module instead of inside the Ca…
…pture module... this way we have a common place to modify and create new notes.
- Loading branch information
Showing
2 changed files
with
139 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.matburt.mobileorg; | ||
import android.preference.PreferenceManager; | ||
import android.content.Context; | ||
import android.app.Activity; | ||
import android.content.SharedPreferences; | ||
import java.io.OutputStreamWriter; | ||
import java.io.BufferedWriter; | ||
import java.io.StringWriter; | ||
import java.io.FileWriter; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.FileNotFoundException; | ||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import android.util.Log; | ||
import android.os.Environment; | ||
|
||
public class CreateEditNote | ||
{ | ||
private SharedPreferences appSettings; | ||
private MobileOrgDatabase appdb; | ||
private Activity appactivity; | ||
public static final String LT = "MobileOrg"; | ||
|
||
CreateEditNote(Activity parentActivity) { | ||
this.appactivity = parentActivity; | ||
this.appSettings = PreferenceManager.getDefaultSharedPreferences( | ||
parentActivity.getBaseContext()); | ||
this.appdb = new MobileOrgDatabase((Context)parentActivity); | ||
} | ||
|
||
public void close() { | ||
this.appdb.close(); | ||
} | ||
|
||
public void writeNewNote(String message) { | ||
String newNote = this.transformBuffer(message); | ||
this.writeNote(newNote); | ||
} | ||
|
||
public boolean writeNote(String message) { | ||
String storageMode = this.appSettings.getString("storageMode", ""); | ||
BufferedWriter writer = new BufferedWriter(new StringWriter()); | ||
|
||
if (storageMode.equals("internal") || storageMode == null) { | ||
FileOutputStream fs; | ||
try { | ||
fs = this.appactivity.openFileOutput("mobileorg.org", Context.MODE_APPEND); | ||
writer = new BufferedWriter(new OutputStreamWriter(fs)); | ||
} | ||
catch (java.io.FileNotFoundException e) { | ||
Log.e(LT, "Caught FNFE trying to open mobileorg.org file"); | ||
} | ||
catch (java.io.IOException e) { | ||
Log.e(LT, "IO Exception initializing writer on file mobileorg.org"); | ||
} | ||
} | ||
else if (storageMode.equals("sdcard")) { | ||
try { | ||
File root = Environment.getExternalStorageDirectory(); | ||
File morgDir = new File(root, "mobileorg"); | ||
morgDir.mkdir(); | ||
if (morgDir.canWrite()){ | ||
File orgFileCard = new File(morgDir, "mobileorg.org"); | ||
FileWriter orgFWriter = new FileWriter(orgFileCard, true); | ||
writer = new BufferedWriter(orgFWriter); | ||
} | ||
else { | ||
Log.e(LT, "Write permission denied"); | ||
return false; | ||
} | ||
} catch (java.io.IOException e) { | ||
Log.e(LT, "IO Exception initializing writer on sdcard file"); | ||
return false; | ||
} | ||
} | ||
else { | ||
Log.e(LT, "Unknown storage mechanism " + storageMode); | ||
return false; | ||
} | ||
|
||
try { | ||
writer.write(message); | ||
this.appdb.addOrUpdateFile("mobileorg.org", "MobileOrg"); | ||
writer.flush(); | ||
writer.close(); | ||
} | ||
catch (java.io.IOException e) { | ||
Log.e(LT, "IO Exception trying to write file mobileorg.org"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public String transformBuffer(String givenText) { | ||
return this.transformCreateBuffer(givenText); | ||
} | ||
|
||
public String transformEditBuffer(String givenText) { | ||
return ""; | ||
} | ||
|
||
// * first line of the note | ||
// [2009-09-09 Wed 09:25] | ||
// Rest of the note | ||
public String transformCreateBuffer(String givenText) { | ||
String xformed = ""; | ||
String[] bufferLines = givenText.split("\\n"); | ||
for (int idx = 0; idx < bufferLines.length; idx++) { | ||
if (idx == 0) { | ||
if (bufferLines[idx].length() > 0 && | ||
!bufferLines[idx].substring(0,1).equals("*")) { | ||
Date date = new Date(); | ||
String DATE_FORMAT = "[yyyy-MM-dd EEE HH:mm]"; | ||
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); | ||
xformed += "* " + bufferLines[idx] + "\n"; | ||
xformed += " " + sdf.format(date) + "\n"; | ||
} | ||
else { | ||
xformed = givenText; | ||
break; | ||
} | ||
} | ||
else { | ||
xformed += " " + bufferLines[idx] + "\n"; | ||
} | ||
} | ||
return xformed + "\n\n"; | ||
} | ||
} |