paralle import

This commit is contained in:
houxg
2016-12-20 17:37:40 +08:00
parent 12c327da62
commit 25004b4f39
6 changed files with 208 additions and 38 deletions

View File

@@ -2,11 +2,12 @@ package org.houxg.leamonax.service;
import android.net.Uri;
import android.util.Log;
import android.text.TextUtils;
import org.bson.types.ObjectId;
import org.houxg.leamonax.Leamonax;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.utils.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
@@ -32,16 +33,25 @@ public class HtmlImporter {
.charset("UTF-8")
.syntax(Document.OutputSettings.Syntax.html);
private String mDefaultNotebookId;
public void setPureContent(boolean isPureContent) {
mShouldRemoveAttributes = isPureContent;
}
public void setNotebookId(String notebookId) {
mDefaultNotebookId = notebookId;
}
public Note from(File file) {
Note note = new Note();
String name = file.getName();
note.setTitle(name.substring(0, name.lastIndexOf(".html")));
note.setUserId(AccountService.getCurrent().getUserId());
if (!TextUtils.isEmpty(mDefaultNotebookId)) {
note.setNoteBookId(mDefaultNotebookId);
}
note.insert();
Document document;
@@ -55,11 +65,10 @@ public class HtmlImporter {
File parentFile = file.getParentFile();
for (Element imgNode : imgs) {
String imgPath = imgNode.attr("src");
Log.i("will", "img src=" + imgPath);
File imageFile = new File(parentFile, imgPath);
if (imageFile.isFile()) {
try {
String cacheName = new ObjectId().toString() + "." + getExtension(imageFile.getName());
String cacheName = new ObjectId().toString() + "." + FileUtils.getExtension(imageFile.getName());
File targetFile = new File(Leamonax.getContext().getCacheDir(), cacheName);
Source source = Okio.source(imageFile);
BufferedSink bufferedSink = Okio.buffer(Okio.sink(targetFile));
@@ -102,7 +111,19 @@ public class HtmlImporter {
}
document.outputSettings(mOutPutSettings);
String output = document.body().children().outerHtml();
String output;
String html = document.body().html().trim();
int firstTokenizerIndex = html.indexOf("<");
if (firstTokenizerIndex > 0) {
String firstContent = "<p>" + html.substring(0, firstTokenizerIndex) + "</p>";
String formattedContent = html.substring(firstTokenizerIndex, html.length());
html = firstContent + formattedContent;
document.body().html(html);
} else if (firstTokenizerIndex < 0){
document.body().html("<p>" + document.body().text() + "</p>");
}
output = document.body().children().outerHtml();
note.setContent(output);
long time = System.currentTimeMillis();
note.setCreatedTimeVal(time);
@@ -148,14 +169,4 @@ public class HtmlImporter {
return true;
}
}
private String getExtension(String fileName) {
String ext = "";
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
ext = fileName.substring(i + 1).toLowerCase();
}
return ext;
}
}

View File

@@ -1,6 +1,5 @@
package org.houxg.leamonax.ui;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
@@ -16,11 +15,9 @@ import org.houxg.leamonax.R;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.HtmlImporter;
import org.houxg.leamonax.utils.OpenUtils;
import org.houxg.leamonax.utils.TestUtils;
import java.io.File;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
@@ -28,7 +25,6 @@ import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ru.bartwell.exfilepicker.ExFilePickerParcelObject;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;
@@ -40,7 +36,6 @@ public class AboutActivity extends BaseActivity {
@BindView(R.id.ll_debug)
View mDebugPanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -84,28 +79,11 @@ public class AboutActivity extends BaseActivity {
@OnClick(R.id.ll_test)
void test() {
Intent intent = new Intent(getApplicationContext(), ru.bartwell.exfilepicker.ExFilePickerActivity.class);
startActivityForResult(intent, 1);
}
@OnClick(R.id.ll_github)
void clickedGithub() {
OpenUtils.openUrl(this, "https://github.com/houxg/Leamonax");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (data != null) {
ExFilePickerParcelObject object = data.getParcelableExtra(ExFilePickerParcelObject.class.getCanonicalName());
if (object.count > 0) {
// Here is object contains selected files names and path
HtmlImporter importer = new HtmlImporter();
importer.setPureContent(true);
importer.from(new File(object.path + object.names.get(0)));
}
}
}
}
}

View File

@@ -15,36 +15,51 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.tencent.bugly.crashreport.CrashReport;
import org.houxg.leamonax.BuildConfig;
import org.houxg.leamonax.R;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.BaseResponse;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.RelationshipOfNoteTag;
import org.houxg.leamonax.model.Note_Table;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.Notebook_Table;
import org.houxg.leamonax.model.RelationshipOfNoteTag;
import org.houxg.leamonax.model.RelationshipOfNoteTag_Table;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.model.Tag_Table;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.HtmlImporter;
import org.houxg.leamonax.utils.DialogDisplayer;
import org.houxg.leamonax.utils.FileUtils;
import org.houxg.leamonax.utils.ToastUtils;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ru.bartwell.exfilepicker.ExFilePicker;
import ru.bartwell.exfilepicker.ExFilePickerParcelObject;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
public class SettingsActivity extends BaseActivity {
private final String[] mEditors = new String[]{"RichText", "Markdown"};
private static final int REQ_CHOOSE_HTML = 15;
@BindView(R.id.tv_editor)
TextView mEditorTv;
@BindView(R.id.tv_image_size)
@@ -60,6 +75,8 @@ public class SettingsActivity extends BaseActivity {
@BindView(R.id.ll_clear)
View mClearDataView;
private HtmlImporter mHtmlImporter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -68,6 +85,8 @@ public class SettingsActivity extends BaseActivity {
ButterKnife.bind(this);
refresh();
mClearDataView.setVisibility(BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
mHtmlImporter = new HtmlImporter();
mHtmlImporter.setPureContent(true);
}
@OnClick(R.id.ll_editor)
@@ -180,6 +199,130 @@ public class SettingsActivity extends BaseActivity {
.show();
}
@OnClick(R.id.ll_import_html)
void clickedImportHtml() {
Intent intent = new Intent(getApplicationContext(), ru.bartwell.exfilepicker.ExFilePickerActivity.class);
intent.putExtra(ExFilePicker.SET_CHOICE_TYPE, ExFilePicker.CHOICE_TYPE_ALL);
startActivityForResult(intent, REQ_CHOOSE_HTML);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CHOOSE_HTML) {
if (data != null) {
ExFilePickerParcelObject object = data.getParcelableExtra(ExFilePickerParcelObject.class.getCanonicalName());
if (object.count > 0) {
scanHtml(object.path, object.names)
.flatMapIterable(new Func1<List<String>, Iterable<String>>() {
@Override
public Iterable<String> call(List<String> strings) {
return strings;
}
})
.flatMap(new Func1<String, Observable<Note>>() {
@Override
public Observable<Note> call(final String filePath) {
return Observable.create(new Observable.OnSubscribe<Note>() {
@Override
public void call(Subscriber<? super Note> subscriber) {
if (!subscriber.isUnsubscribed()) {
File file = new File(filePath);
Note note = mHtmlImporter.from(file);
subscriber.onNext(note);
subscriber.onCompleted();
}
}
});
}
})
.doOnSubscribe(new Action0() {
@Override
public void call() {
runOnUiThread(new Runnable() {
@Override
public void run() {
DialogDisplayer.showProgress(SettingsActivity.this, getString(R.string.parsing_html));
}
});
}
})
.doOnCompleted(new Action0() {
@Override
public void call() {
runOnUiThread(new Runnable() {
@Override
public void run() {
DialogDisplayer.dismissProgress();
}
});
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Note>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
CrashReport.postCatchedException(e);
ToastUtils.show(SettingsActivity.this, R.string.parse_error);
}
@Override
public void onNext(Note note) {
}
});
}
}
}
}
private Observable<List<String>> scanHtml(final String path, final List<String> names) {
return Observable.create(
new Observable.OnSubscribe<List<String>>() {
@Override
public void call(Subscriber<? super List<String>> subscriber) {
if (!subscriber.isUnsubscribed()) {
List<String> absPaths = new ArrayList<>();
for (String name : names) {
File file = new File(path + name);
scanHtmlFile(absPaths, file);
}
subscriber.onNext(absPaths);
subscriber.onCompleted();
}
}
});
}
private void scanHtmlFile(List<String> absPaths, File file) {
if (file.isDirectory()) {
File[] children = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String extension = FileUtils.getExtension(pathname.getName()).toLowerCase();
return pathname.isDirectory() || (pathname.isFile() && "html".equals(extension));
}
});
if (children != null && children.length > 0) {
for (File child : children) {
scanHtmlFile(absPaths, child);
}
}
} else if (file.isFile() && "html".equals(FileUtils.getExtension(file.getName()).toLowerCase())) {
absPaths.add(file.getAbsolutePath());
}
}
private void clearData() {
Observable.create(
new Observable.OnSubscribe<Void>() {

View File

@@ -0,0 +1,15 @@
package org.houxg.leamonax.utils;
public class FileUtils {
public static String getExtension(String fileName) {
String ext = "";
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
ext = fileName.substring(i + 1).toLowerCase();
}
return ext;
}
}

View File

@@ -180,6 +180,26 @@
<include layout="@layout/divider" />
</LinearLayout>
<include layout="@layout/divider"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/ll_import_html"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
style="@style/SettingsSecondaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/import_from_html" />
<include layout="@layout/divider" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_log_out"
style="@style/SettingsPanel"

View File

@@ -68,4 +68,7 @@
<string name="clear">Clear</string>
<string name="multiple_links">Multiple links</string>
<string name="apply">Apply</string>
<string name="import_from_html">Import from html</string>
<string name="parsing_html">Parsing html</string>
<string name="parse_error">Parse error</string>
</resources>