Refactor and write tests for IO tasks

This commit is contained in:
2016-03-25 06:15:53 -04:00
parent 3656c51e95
commit 9e410f8395
11 changed files with 350 additions and 65 deletions

View File

@@ -48,7 +48,7 @@ public class FilePickerDialog implements AdapterView.OnItemClickListener
void onFileSelected(File file);
}
private OnFileSelectedListener fileListener;
private OnFileSelectedListener listener;
public FilePickerDialog(Activity activity, File initialDirectory)
{
@@ -81,7 +81,7 @@ public class FilePickerDialog implements AdapterView.OnItemClickListener
}
else
{
if (fileListener != null) fileListener.onFileSelected(file);
if (listener != null) listener.onFileSelected(file);
dialog.dismiss();
}
}
@@ -91,9 +91,9 @@ public class FilePickerDialog implements AdapterView.OnItemClickListener
dialog.show();
}
public void setFileListener(OnFileSelectedListener fileListener)
public void setListener(OnFileSelectedListener listener)
{
this.fileListener = fileListener;
this.listener = listener;
}
private void navigateTo(File path)

View File

@@ -26,6 +26,7 @@ import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@@ -73,7 +74,8 @@ import java.util.List;
public class ListHabitsFragment extends Fragment
implements OnSavedListener, OnItemClickListener, OnLongClickListener, DropListener,
OnClickListener, HabitListLoader.Listener, AdapterView.OnItemLongClickListener,
HabitSelectionCallback.Listener, ImportDataTask.Listener
HabitSelectionCallback.Listener, ImportDataTask.Listener, ExportCSVTask.Listener,
ExportDBTask.Listener
{
long lastLongClick = 0;
private boolean isShortToggleEnabled;
@@ -437,7 +439,7 @@ public class ListHabitsFragment extends Fragment
if(dir == null) return;
FilePickerDialog picker = new FilePickerDialog(activity, dir);
picker.setFileListener(new FilePickerDialog.OnFileSelectedListener()
picker.setListener(new FilePickerDialog.OnFileSelectedListener()
{
@Override
public void onFileSelected(File file)
@@ -447,6 +449,7 @@ public class ListHabitsFragment extends Fragment
task.execute();
}
});
picker.show();
}
@@ -472,11 +475,49 @@ public class ListHabitsFragment extends Fragment
public void exportAllHabits()
{
new ExportCSVTask(activity, Habit.getAll(true), progressBar).execute();
ExportCSVTask task = new ExportCSVTask(Habit.getAll(true), progressBar);
task.setListener(this);
task.execute();
}
@Override
public void onExportCSVFinished(@Nullable String archiveFilename)
{
if(archiveFilename != null)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/zip");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(archiveFilename)));
activity.startActivity(intent);
}
else
{
activity.showToast(R.string.could_not_export);
}
}
public void exportDB()
{
new ExportDBTask(activity, progressBar).execute();
ExportDBTask task = new ExportDBTask(progressBar);
task.setListener(this);
task.execute();
}
@Override
public void onExportDBFinished(@Nullable String filename)
{
if(filename != null)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filename)));
activity.startActivity(intent);
}
else
{
activity.showToast(R.string.could_not_export);
}
}
}

View File

@@ -124,8 +124,11 @@ public class DatabaseHelper
}
@Nullable
public static File getFilesDir(Context context, String prefix)
public static File getFilesDir(String prefix)
{
Context context = HabitsApplication.getContext();
if(context == null) return null;
File chosenDir = null;
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
if(externalFilesDirs == null) return null;

View File

@@ -19,14 +19,11 @@
package org.isoron.uhabits.tasks;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ProgressBar;
import org.isoron.uhabits.R;
import org.isoron.uhabits.ReplayableActivity;
import org.isoron.uhabits.helpers.DatabaseHelper;
import org.isoron.uhabits.io.HabitsCSVExporter;
import org.isoron.uhabits.models.Habit;
@@ -37,17 +34,25 @@ import java.util.List;
public class ExportCSVTask extends AsyncTask<Void, Void, Void>
{
private final ReplayableActivity activity;
public interface Listener
{
void onExportCSVFinished(@Nullable String archiveFilename);
}
private ProgressBar progressBar;
private final List<Habit> selectedHabits;
String archiveFilename;
private String archiveFilename;
private ExportCSVTask.Listener listener;
public ExportCSVTask(ReplayableActivity activity, List<Habit> selectedHabits,
ProgressBar progressBar)
public ExportCSVTask(List<Habit> selectedHabits, ProgressBar progressBar)
{
this.selectedHabits = selectedHabits;
this.progressBar = progressBar;
this.activity = activity;
}
public void setListener(Listener listener)
{
this.listener = listener;
}
@Override
@@ -63,19 +68,8 @@ public class ExportCSVTask extends AsyncTask<Void, Void, Void>
@Override
protected void onPostExecute(Void aVoid)
{
if(archiveFilename != null)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/zip");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(archiveFilename)));
activity.startActivity(intent);
}
else
{
activity.showToast(R.string.could_not_export);
}
if(listener != null)
listener.onExportCSVFinished(archiveFilename);
if(progressBar != null)
progressBar.setVisibility(View.GONE);
@@ -86,7 +80,7 @@ public class ExportCSVTask extends AsyncTask<Void, Void, Void>
{
try
{
File dir = DatabaseHelper.getFilesDir(activity, "CSV");
File dir = DatabaseHelper.getFilesDir("CSV");
if(dir == null) return null;
HabitsCSVExporter exporter = new HabitsCSVExporter(selectedHabits, dir);

View File

@@ -19,14 +19,11 @@
package org.isoron.uhabits.tasks;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ProgressBar;
import org.isoron.uhabits.R;
import org.isoron.uhabits.ReplayableActivity;
import org.isoron.uhabits.helpers.DatabaseHelper;
import java.io.File;
@@ -34,14 +31,23 @@ import java.io.IOException;
public class ExportDBTask extends AsyncTask<Void, Void, Void>
{
private final ReplayableActivity activity;
public interface Listener
{
void onExportDBFinished(@Nullable String filename);
}
private ProgressBar progressBar;
private String filename;
private Listener listener;
public ExportDBTask(ReplayableActivity activity, ProgressBar progressBar)
public ExportDBTask(ProgressBar progressBar)
{
this.progressBar = progressBar;
this.activity = activity;
}
public void setListener(Listener listener)
{
this.listener = listener;
}
@Override
@@ -57,19 +63,8 @@ public class ExportDBTask extends AsyncTask<Void, Void, Void>
@Override
protected void onPostExecute(Void aVoid)
{
if(filename != null)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filename)));
activity.startActivity(intent);
}
else
{
activity.showToast(R.string.could_not_export);
}
if(listener != null)
listener.onExportDBFinished(filename);
if(progressBar != null)
progressBar.setVisibility(View.GONE);
@@ -82,7 +77,7 @@ public class ExportDBTask extends AsyncTask<Void, Void, Void>
try
{
File dir = DatabaseHelper.getFilesDir(activity, "Backups");
File dir = DatabaseHelper.getFilesDir("Backups");
if(dir == null) return null;
filename = DatabaseHelper.saveDatabaseCopy(dir);

View File

@@ -139,13 +139,13 @@
<string name="custom_frequency">Custom …</string>
<string name="help">Help &amp; FAQ</string>
<string name="could_not_export">Failed to export data.</string>
<string name="could_not_import">Failed to import habits from file.</string>
<string name="file_not_recognized">File type not recognized.</string>
<string name="could_not_import">Failed to import data.</string>
<string name="file_not_recognized">File not recognized.</string>
<string name="habits_imported">Habits imported successfully.</string>
<string name="import_data_summary">Supports full backups exported by this app, as well as files generated by Tickmate, HabitBull or Rewire. See FAQ for more information.</string>
<string name="import_data">Import data</string>
<string name="export_as_csv_summary">Generates files that can be opened by spreadsheet software such as Microsoft Excel or OpenOffice Calc, but cannot be imported back.</string>
<string name="export_full_backup">Export full backup</string>
<string name="export_full_backup_summary">Generates a file that contains all your data, and that can be imported back.</string>
<string name="full_backup_success">Full backup successfully exported.</string>
<string name="import_data">Import data</string>
<string name="export_full_backup">Export full backup</string>
<string name="import_data_summary">Supports full backups exported by this app, as well as files generated by Tickmate, HabitBull or Rewire. See FAQ for more information.</string>
<string name="export_as_csv_summary">Generates files that can be opened by spreadsheet software such as Microsoft Excel or OpenOffice Calc. This file cannot be imported back.</string>
<string name="export_full_backup_summary">Generates a file that contains all your data. This file can be imported back.</string>
</resources>