mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-17 16:07:48 +00:00
add simple html importer
This commit is contained in:
@@ -103,4 +103,7 @@ dependencies {
|
|||||||
compile 'com.flurry.android:analytics:6.4.2'
|
compile 'com.flurry.android:analytics:6.4.2'
|
||||||
|
|
||||||
compile 'net.danlew:android.joda:2.9.5'
|
compile 'net.danlew:android.joda:2.9.5'
|
||||||
|
// https://mvnrepository.com/artifact/org.jsoup/jsoup
|
||||||
|
compile group: 'org.jsoup', name: 'jsoup', version: '1.10.1'
|
||||||
|
compile 'ru.bartwell:exfilepicker:1.8'
|
||||||
}
|
}
|
||||||
|
@@ -50,6 +50,11 @@
|
|||||||
android:label="NoteSyncService" />
|
android:label="NoteSyncService" />
|
||||||
|
|
||||||
<activity android:name=".ui.SearchActivity" />
|
<activity android:name=".ui.SearchActivity" />
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name="ru.bartwell.exfilepicker.ExFilePickerActivity"
|
||||||
|
android:configChanges="orientation|screenSize"
|
||||||
|
android:theme="@style/ExFilePickerThemeDark" />
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
120
app/src/main/java/org/houxg/leamonax/service/HtmlImporter.java
Normal file
120
app/src/main/java/org/houxg/leamonax/service/HtmlImporter.java
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package org.houxg.leamonax.service;
|
||||||
|
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.houxg.leamonax.Leamonax;
|
||||||
|
import org.houxg.leamonax.model.Note;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.BufferedSource;
|
||||||
|
import okio.Okio;
|
||||||
|
import okio.Source;
|
||||||
|
|
||||||
|
public class HtmlImporter {
|
||||||
|
|
||||||
|
|
||||||
|
public Note from(File file) {
|
||||||
|
String html;
|
||||||
|
try {
|
||||||
|
BufferedSource source = Okio.buffer(Okio.source(file));
|
||||||
|
html = source.readUtf8();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Log.i("will", "html=" + html);
|
||||||
|
if (TextUtils.isEmpty(html)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Note note = new Note();
|
||||||
|
String name = file.getName();
|
||||||
|
note.setTitle(name.substring(0, name.lastIndexOf(".html")));
|
||||||
|
note.setUserId(AccountService.getCurrent().getUserId());
|
||||||
|
note.insert();
|
||||||
|
|
||||||
|
Document document = Jsoup.parse(html);
|
||||||
|
Elements imgs = document.select("img");
|
||||||
|
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() && isImage(imageFile.getAbsolutePath())) {
|
||||||
|
try {
|
||||||
|
String cacheName = new ObjectId().toString() + "." + getExtension(imageFile.getName());
|
||||||
|
File targetFile = new File(Leamonax.getContext().getCacheDir(), cacheName);
|
||||||
|
Source source = Okio.source(imageFile);
|
||||||
|
BufferedSink bufferedSink = Okio.buffer(Okio.sink(targetFile));
|
||||||
|
bufferedSink.writeAll(source);
|
||||||
|
bufferedSink.flush();
|
||||||
|
source.close();
|
||||||
|
bufferedSink.close();
|
||||||
|
|
||||||
|
Uri noteFile = NoteFileService.createImageFile(note.getId(), targetFile.getAbsolutePath());
|
||||||
|
imgNode.attr("src", noteFile.toString());
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.w("will", "image file not exist");
|
||||||
|
e.printStackTrace();
|
||||||
|
continue;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Document.OutputSettings settings = new Document.OutputSettings();
|
||||||
|
settings.prettyPrint(true)
|
||||||
|
.charset("UTF-8")
|
||||||
|
.syntax(Document.OutputSettings.Syntax.html);
|
||||||
|
document.outputSettings(settings);
|
||||||
|
String output = document.body().outerHtml();
|
||||||
|
Log.i("will", "output=" + output);
|
||||||
|
Log.i("will", "output=" + document.body().children().outerHtml());
|
||||||
|
note.setContent(output);
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
note.setCreatedTimeVal(time);
|
||||||
|
note.setUpdatedTimeVal(time);
|
||||||
|
note.update();
|
||||||
|
Log.i("will", "output note=" + note.getId());
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isLocalPath(String path) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isImage(String path) {
|
||||||
|
switch (getExtension(path)) {
|
||||||
|
case "png":
|
||||||
|
case "jpg":
|
||||||
|
case "jpeg":
|
||||||
|
case "bmp":
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,6 @@
|
|||||||
package org.houxg.leamonax.ui;
|
package org.houxg.leamonax.ui;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -15,8 +16,10 @@ import org.houxg.leamonax.R;
|
|||||||
import org.houxg.leamonax.database.AppDataBase;
|
import org.houxg.leamonax.database.AppDataBase;
|
||||||
import org.houxg.leamonax.model.Note;
|
import org.houxg.leamonax.model.Note;
|
||||||
import org.houxg.leamonax.service.AccountService;
|
import org.houxg.leamonax.service.AccountService;
|
||||||
|
import org.houxg.leamonax.service.HtmlImporter;
|
||||||
import org.houxg.leamonax.utils.TestUtils;
|
import org.houxg.leamonax.utils.TestUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -24,6 +27,7 @@ import java.util.List;
|
|||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
|
import ru.bartwell.exfilepicker.ExFilePickerParcelObject;
|
||||||
import rx.Observable;
|
import rx.Observable;
|
||||||
import rx.Subscriber;
|
import rx.Subscriber;
|
||||||
import rx.schedulers.Schedulers;
|
import rx.schedulers.Schedulers;
|
||||||
@@ -77,5 +81,24 @@ public class AboutActivity extends BaseActivity {
|
|||||||
.subscribe();
|
.subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.ll_test)
|
||||||
|
void test() {
|
||||||
|
Intent intent = new Intent(getApplicationContext(), ru.bartwell.exfilepicker.ExFilePickerActivity.class);
|
||||||
|
startActivityForResult(intent, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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.from(new File(object.path + object.names.get(0)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -127,6 +127,21 @@
|
|||||||
|
|
||||||
<include layout="@layout/divider" />
|
<include layout="@layout/divider" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/ll_test"
|
||||||
|
style="@style/SettingsPanel"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
style="@style/SettingsSecondaryText"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Test" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<include layout="@layout/divider" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
Reference in New Issue
Block a user