write a report when any export completes, and add author name to settings dialog

This commit is contained in:
Vivian Lim 2018-06-12 21:13:36 -07:00
parent 09bfe01db2
commit 5ceb0db4b6
6 changed files with 99 additions and 28 deletions

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mjlim.hovernote"
android:installLocation="auto"
android:versionCode="27"
android:versionCode="30"
android:versionName="3.1" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

View File

@ -612,7 +612,13 @@ public class HoverNoteService extends Service {
noteIconsLock.unlock();
}
public void exportNotes(RecentsDatabase.ExportStatusUpdater updater) throws Exception {
recentsDb.exportToStorage(updater);
public String exportNotes(RecentsDatabase.ExportStatusUpdater updater) throws Exception {
String exportReportPath = recentsDb.exportToStorage(updater);
if (exportReportPath != null){
this.newNoteFromFile(exportReportPath, -1);
}
return exportReportPath;
}
}

View File

@ -18,11 +18,11 @@ import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
public class RecentsDatabase {
@ -261,14 +261,14 @@ public class RecentsDatabase {
}
public void exportToStorage(ExportStatusUpdater updater) throws Exception {
public String exportToStorage(ExportStatusUpdater updater) throws Exception {
File sdcard = Environment.getExternalStorageDirectory();
File hovernoteDir = new File(sdcard + "/hovernote-exported");
hovernoteDir.mkdir();
if (!hovernoteDir.isDirectory()) {
updater.UpdateStatus("Can't export notes; please remove the file named 'hovernote-exported' from " + sdcard);
return;
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
@ -276,7 +276,7 @@ public class RecentsDatabase {
File exportDir = stubbornlyCreateSubdirectory(hovernoteDir, exportDirName);
exportToDirectory(exportDir, updater);
return exportToDirectory(exportDir, updater);
}
private File stubbornlyCreateSubdirectory(File parent, String name) throws Exception {
@ -295,12 +295,12 @@ public class RecentsDatabase {
throw new Exception("Couldn't create a directory named " + name);
}
private void exportToDirectory(File dest, ExportStatusUpdater updater) {
private String exportToDirectory(File dest, ExportStatusUpdater updater) {
final List<FilePickerOption> recents = this.getRecentFileOptions();
int numExported = 0;
List<String> errors = new ArrayList<String>();
List<Pair<String, FilePickerOption>> errors = new ArrayList<>();
for (FilePickerOption recent : recents) {
try { // Attempt to export all files, even if there are exceptions... then throw at the end.
switch (recent.getType()){
@ -320,18 +320,63 @@ public class RecentsDatabase {
}
updater.UpdateStatus("Working... " + numExported + " notes exported so far.");
} catch (Exception e){
errors.add(e.getMessage());
errors.add(Pair.create(e.getMessage(), recent));
}
}
if (!errors.isEmpty()){
Log.e("com.mjlim.hovernote.RecentsDatabase","Exceptions thrown during export: " + TextUtils.join("\n", errors));
updater.UpdateStatus("Exported " + String.valueOf(numExported) + " notes to "
+ dest.getAbsolutePath() + ", encountered problems while trying to export "
+ String.valueOf(errors.size()) + " others." );
}
else {
File exportReportFile = new File(dest + "_report.txt");
try (PrintWriter exportReport = new PrintWriter(exportReportFile)){
String statusMessage = "Exported " + String.valueOf(numExported) + " notes to "
+ dest.getAbsolutePath() + " successfully.";
exportReport.write(statusMessage + "\n");
if (!errors.isEmpty()){
Log.e("com.mjlim.hovernote.RecentsDatabase","Exceptions thrown during export: " + TextUtils.join("\n", errors));
exportReport.write("Encountered problems while trying to export " + String.valueOf(errors.size()) + " others:\n");
updater.UpdateStatus("Encountered errors, see report file for details: " + exportReportFile.getAbsolutePath());
exportReport.write("-----------------------\n");
for (Pair<String, FilePickerOption> error : errors){
String errorMessage = error.first;
FilePickerOption errorRecent = error.second;
try {
switch (errorRecent.getType()){
case FILE:
exportReport.write(errorRecent.getPath() + "\n");
break;
case RECENT:
exportReport.write(errorRecent.getData() + "\n");
exportReport.write(errorRecent.getName() + "\n");
break;
case DBXFILE:
exportReport.write(errorRecent.getPath());
exportReport.write("Unfortunately, Dropbox files can't be exported this way, but they should still be in your Dropbox.\n");
case FOLDER:
exportReport.write(errorRecent.getPath());
exportReport.write("This is a folder, and not actually a note. Somehow, hovernote has been tricked into opening a folder.\n");
default:
exportReport.write("I can't tell what type of recent note this is.\n");
exportReport.write("name: " + errorRecent.getName() + "\n");
exportReport.write("data: " + errorRecent.getData() + "\n");
exportReport.write("path: " + errorRecent.getPath() + "\n");
break;
}
exportReport.write("\"" + errorMessage + "\"\n");
}
catch (Exception e){
exportReport.write("Encountered an error while trying to write to the error log: " + e.getMessage());
}
exportReport.write("-----------------------\n");
}
}
updater.UpdateStatus("Exported " + numExported + " notes to " + dest.getAbsolutePath());
return exportReportFile.getAbsolutePath();
}
catch (FileNotFoundException fnfe) {
updater.UpdateStatus("Had trouble writing an export log file: " + fnfe.getMessage()
+ ". Some of your files may have been exported to " + dest.getAbsolutePath()
+ ", but you might want to try again");
return null;
}
}
@ -342,7 +387,7 @@ public class RecentsDatabase {
File inFile = new File(recent.getPath());
if (!inFile.exists()) {
throw new FileNotFoundException(recent.getPath());
throw new FileNotFoundException("File doesn't exist: " + recent.getPath());
}
Log.i("com.mjlim.hovernote.RecentsDatabase", "Copying a recent file to: " + inFile.getName());
@ -407,5 +452,5 @@ public class RecentsDatabase {
public interface ExportStatusUpdater {
void UpdateStatus(String newStatus);
}
}
}

View File

@ -273,7 +273,11 @@ public class SettingsDialog extends LinearLayout implements OnSeekBarChangeListe
RecentsDatabase.ExportStatusUpdater statusUpdater = (String newStatus) -> settingsExportNotesStatus.setText(newStatus);
Runnable r = () -> {
try {
note.getService().exportNotes(statusUpdater);
String reportPath = note.getService().exportNotes(statusUpdater);
if (reportPath != null){
// There was a report, and we want to look at that instead of keeping the settings dialog up.
mywindow.dismiss();
}
}
catch (Exception e) {
statusUpdater.UpdateStatus("Error while exporting: " + e.getMessage());

View File

@ -21,15 +21,30 @@
android:paddingLeft="10dp" >
<TextView
android:id="@+id/textView2"
style="@style/dialogHeaderText"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
style="@style/dialogHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/textViewAuthor"
style="@style/dialogHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:paddingLeft="5dp"
android:textSize="14dp"
android:text="@string/app_author"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<TextView

View File

@ -4,6 +4,7 @@
<string name="app_name">hovernote</string>
<string name="app_name_lowercase">hovernote</string>
<string name="app_version">v3.1</string>
<string name="app_author"> by Vivian Lim</string>
<!-- DO NOT replace dashes with entity; it's okay -->
<string name="changelog_url">http://mjlim.net/wordpress/2015/01/hovernote-v3-0/print</string>