Permalink
Browse files

Added support for logging to internal memory when external is unavail…

…able.
  • Loading branch information...
1 parent a5b55ba commit ee91ef00994ffd24c0c6c2b98a1240b3aa1f5b27 @jwf committed Apr 26, 2012
View
@@ -27,10 +27,10 @@
<string name="notification_upload_title">Humansense Uploader</string>
<!-- Utility Strings -->
- <string name="live_file_path">hsandroidapp/data/live/</string>
- <string name="recent_file_path">hsandroidapp/data/recent/</string>
- <string name="uploaded_file_path">hsandroidapp/data/uploaded/</string>
- <string name="activity_file_path">hsandroidapp/data/activity/</string>
+ <string name="live_file_path">data/live/</string>
+ <string name="recent_file_path">data/recent/</string>
+ <string name="uploaded_file_path">data/uploaded/</string>
+ <string name="activity_file_path">data/activity/</string>
<!-- Magnitude Graph Strings -->
<string name="mag_graph_title">Select a section of the graph to add a label:</string>
@@ -43,8 +43,8 @@
<string name="mag_graph_close_label">Close</string>
<!-- TDEClassifier plugin Strings -->
- <string name="model_ini_path">hsandroidapp/models/models.ini</string>
- <string name="models_path">hsandroidapp/models/</string>
+ <string name="model_ini_path">models/models.ini</string>
+ <string name="models_path">models/</string>
<string name="manage_model_files_pref_title">Manage Model Files</string>
<string name="manage_model_files_pref_summary">Manage Model Files and choose which models to use for classification.</string>
<string name="start_building_button_text">Build</string>
@@ -5,12 +5,15 @@
*/
package ca.mcgill.hs;
+import java.io.File;
+
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Debug;
+import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@@ -79,6 +82,25 @@ public static TableLayout getFreeSpace() {
return freeSpace;
}
+ public static File getStorageDirectory() {
+ final String state = Environment.getExternalStorageState();
+ boolean externalStorageAvailable = false;
+ boolean externalStorageWriteable = false;
+ if (Environment.MEDIA_MOUNTED.equals(state)) {
+ externalStorageAvailable = externalStorageWriteable = true;
+ } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
+ externalStorageAvailable = true;
+ externalStorageWriteable = false;
+ } else {
+ externalStorageAvailable = externalStorageWriteable = false;
+ }
+ if (externalStorageAvailable && externalStorageWriteable) {
+ return context.getExternalFilesDir(null);
+ } else {
+ return context.getFilesDir();
+ }
+ }
+
/**
* Updates the main starting button according to whether the service is
* running or not. Should be called whenever the state of the service is
@@ -16,7 +16,6 @@
import java.util.Timer;
import java.util.TimerTask;
-import android.os.Environment;
import ca.mcgill.hs.HSAndroid;
import ca.mcgill.hs.R;
import ca.mcgill.hs.util.Log;
@@ -129,9 +128,8 @@ public MotionStateClusterer(final LocationSet locations) {
final Date d = new Date(System.currentTimeMillis());
final SimpleDateFormat dfm = new SimpleDateFormat("yy-MM-dd-HHmmss");
- final File recent_dir = new File(Environment
- .getExternalStorageDirectory(), HSAndroid
- .getAppString(R.string.recent_file_path));
+ final File recent_dir = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.recent_file_path));
final File f = new File(recent_dir, dfm.format(d) + "-clusters.log");
try {
outputLog = new BufferedWriter(new FileWriter(f));
@@ -184,7 +182,8 @@ public void close() {
try {
if (outputLog != null) {
Log.d(TAG, "Computing Statistics");
- final File f = new File("/sdcard/hsandroidapp/clusters.dat");
+ final File f = new File(HSAndroid.getStorageDirectory(),
+ "clusters.dat");
BufferedWriter statsDmp = null;
try {
statsDmp = new BufferedWriter(new FileWriter(f, false));
@@ -313,9 +312,9 @@ public void run() {
}
try {
if (outputLog != null) {
- outputLog.write(dfm
- .format(new Date(System.currentTimeMillis()))
- + "," + currentCluster + "\n");
+ outputLog
+ .write(dfm.format(new Date(System.currentTimeMillis()))
+ + "," + currentCluster + "\n");
}
} catch (final IOException e) {
Log.e(TAG, e);
@@ -28,13 +28,13 @@
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
-import android.os.Environment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
+import ca.mcgill.hs.HSAndroid;
import ca.mcgill.hs.R;
import ca.mcgill.hs.util.ActivityIndex;
import ca.mcgill.hs.util.Log;
@@ -235,10 +235,8 @@ public MagnitudeGraphView(final Context context, final String title,
this.indexOfActivities = new ActivityIndex(acts, codes);
try {
- final File j = new File(
- Environment.getExternalStorageDirectory(),
- (String) context.getResources().getText(
- R.string.activity_file_path));
+ final File j = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.activity_file_path));
if (!j.isDirectory()) {
if (!j.mkdirs()) {
Log.e("Output Dir",
@@ -264,10 +262,8 @@ public MagnitudeGraphView(final Context context, final String title,
// Code to read the activity index file, this will stay here.
try {
- final File j = new File(
- Environment.getExternalStorageDirectory(),
- (String) context.getResources().getText(
- R.string.activity_file_path));
+ final File j = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.activity_file_path));
final File file = new File(j, "ActivityIndex.aif");
final FileInputStream fis = new FileInputStream(file);
final ObjectInputStream ois = new ObjectInputStream(fis);
@@ -19,9 +19,9 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
-import android.os.Environment;
import android.preference.Preference;
import android.preference.PreferenceActivity;
+import ca.mcgill.hs.HSAndroid;
import ca.mcgill.hs.R;
import ca.mcgill.hs.plugin.BluetoothLogger.BluetoothPacket;
import ca.mcgill.hs.plugin.GPSLogger.GPSPacket;
@@ -338,9 +338,8 @@ private synchronized void closeAll() {
// files) into the recent directory.
try {
// Current live directory
- final File directory = new File(
- Environment.getExternalStorageDirectory(), (String) context
- .getResources().getText(R.string.live_file_path));
+ final File directory = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.live_file_path));
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
throw new IOException("ERROR: Unable to create directory "
@@ -355,10 +354,8 @@ private synchronized void closeAll() {
if (filesInDirectory != null) {
// Destination directory
- final File dest = new File(
- Environment.getExternalStorageDirectory(),
- (String) context.getResources().getText(
- R.string.recent_file_path));
+ final File dest = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.recent_file_path));
if (!dest.isDirectory()) {
if (!dest.mkdirs()) {
throw new IOException(
@@ -411,10 +408,8 @@ private synchronized DataOutputStream getFileForPacketType(final int id,
final String extension) {
if (!fileHandles.containsKey(id)) {
try {
- final File j = new File(
- Environment.getExternalStorageDirectory(),
- (String) context.getResources().getText(
- R.string.live_file_path));
+ final File j = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.live_file_path));
if (!j.isDirectory()) {
if (!j.mkdirs()) {
Log.e("Output Dir",
@@ -9,7 +9,6 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
-import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@@ -28,12 +27,12 @@
public class DeleteUnUploadedFileManager extends FileManager {
/** Directory where unuploaded files live. */
public static final File RECENT_FILES_DIRECTORY = new File(
- Environment.getExternalStorageDirectory(),
+ HSAndroid.getStorageDirectory(),
HSAndroid.getAppString(R.string.recent_file_path));
/** Directory where uploaded files should go. */
public static final File UPLOADED_FILES_DIRECTORY = new File(
- Environment.getExternalStorageDirectory(),
+ HSAndroid.getStorageDirectory(),
HSAndroid.getAppString(R.string.uploaded_file_path));
@Override
@@ -14,7 +14,6 @@
import java.io.IOException;
import java.util.HashSet;
-import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@@ -32,14 +31,14 @@
public class ManageModelsFileManager extends FileManager {
/** Directory where the models live. */
- public static final File MODELS_DIR = new File(Environment
- .getExternalStorageDirectory(), HSAndroid
- .getAppString(R.string.models_path));
+ public static final File MODELS_DIR = new File(
+ HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.models_path));
/** File containing the active models, one per line. */
- public static final File MODELS_INI_FILE = new File(Environment
- .getExternalStorageDirectory(), HSAndroid
- .getAppString(R.string.model_ini_path));
+ public static final File MODELS_INI_FILE = new File(
+ HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.model_ini_path));
private static final String TAG = "ManageModelsFileManager";
@@ -83,8 +82,8 @@ public boolean accept(final File dir, final String filename) {
}
// Create the entries.
for (int i = 0; i < files.length; i++) {
- entries[i] = new CheckListEntry(files[i], checkedEntries
- .contains(files[i]));
+ entries[i] = new CheckListEntry(files[i],
+ checkedEntries.contains(files[i]));
}
return entries;
}
@@ -39,9 +39,9 @@
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
-import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;
+import ca.mcgill.hs.HSAndroid;
import ca.mcgill.hs.R;
import ca.mcgill.hs.prefs.HSAndroidPreferences;
import ca.mcgill.hs.prefs.PreferenceFactory;
@@ -438,7 +438,7 @@ private void unregisterConnectReceiver() {
*/
private synchronized void updateFileList() {
- final File path = new File(Environment.getExternalStorageDirectory(),
+ final File path = new File(HSAndroid.getStorageDirectory(),
UNUPLOADED_PATH);
if (!path.isDirectory()) {
@@ -480,8 +480,7 @@ private int uploadFile(final String fileName) {
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
httppost = new HttpPost(UPLOAD_URL);
- final File file = new File(Environment.getExternalStorageDirectory(),
- fileName);
+ final File file = new File(HSAndroid.getStorageDirectory(), fileName);
if (!file.exists()) {
// File may be deleted while in the queue for uploading
Log.d(TAG, "Unable to upload " + fileName
@@ -513,10 +512,8 @@ private int uploadFile(final String fileName) {
// Move files to uploaded folder if successful
else {
Log.i(TAG, "Moving file to uploaded directory.");
- final File dest = new File(
- Environment.getExternalStorageDirectory(),
- (String) getResources().getText(
- R.string.uploaded_file_path));
+ final File dest = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.uploaded_file_path));
if (!dest.isDirectory()) {
if (!dest.mkdirs()) {
throw new IOException(
@@ -17,14 +17,14 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
-import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.Process;
-import ca.mcgill.hs.util.Log;
+import ca.mcgill.hs.HSAndroid;
import ca.mcgill.hs.R;
import ca.mcgill.hs.prefs.HSAndroidPreferences;
import ca.mcgill.hs.prefs.PreferenceFactory;
+import ca.mcgill.hs.util.Log;
/**
* Work in progress. A new, more modular file uploader. Still does not work.
@@ -92,11 +92,11 @@ public void run() {
Constants.ACTION_RETRY);
intent.setClassName("ca.mcgill.hs.uploader",
UploadReceiver.class.getName());
- alarms.set(AlarmManager.RTC_WAKEUP, System
- .currentTimeMillis()
- + wakeUp, PendingIntent.getBroadcast(
- context, 0, intent,
- PendingIntent.FLAG_ONE_SHOT));
+ alarms.set(AlarmManager.RTC_WAKEUP,
+ System.currentTimeMillis() + wakeUp,
+ PendingIntent.getBroadcast(context, 0,
+ intent,
+ PendingIntent.FLAG_ONE_SHOT));
}
}
}
@@ -235,8 +235,8 @@ public IBinder onBind(final Intent i) {
@Override
public void onCreate() {
- recentDir = new File(Environment.getExternalStorageDirectory(),
- (String) getResources().getText(R.string.recent_file_path));
+ recentDir = new File(HSAndroid.getStorageDirectory(),
+ HSAndroid.getAppString(R.string.recent_file_path));
dirObserver = new DirectoryObserver(recentDir.getAbsolutePath(),
FileObserver.MOVED_TO);
prefs = PreferenceFactory.getSharedPreferences(this);
@@ -7,7 +7,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
-import android.os.Environment;
+import ca.mcgill.hs.HSAndroid;
/**
* Wrap the android logging methods to allow for optional logging to a file, and
@@ -88,9 +88,8 @@ public static void setLogToFile(final boolean logToFile) {
logWriter = null;
} else if (!Log.logToFile && logToFile) {
// Start Logging
- final File logDir = new File(
- Environment.getExternalStorageDirectory(),
- "hsandroidapp/log/");
+ final File logDir = new File(HSAndroid.getStorageDirectory(),
+ "log/");
if (!logDir.isDirectory()) {
logDir.mkdirs();
}

0 comments on commit ee91ef0

Please sign in to comment.