Skip to content

Commit

Permalink
Support Mozilla HTML bookmarks and release 1.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mthli committed Jun 9, 2015
1 parent 3f602a0 commit 783c66c
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 232 deletions.
320 changes: 118 additions & 202 deletions Ninja/.idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Ninja/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.mthli.Ninja"
android:versionCode="17"
android:versionCode="18"
android:versionName="@string/app_version">

<!-- SDK -->
Expand Down
9 changes: 9 additions & 0 deletions Ninja/Ninja.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/libs/jsoup-1.8.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added Ninja/libs/jsoup-1.8.2.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion Ninja/res/values/strings_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Ninja
</string>
<string name="app_version">
1.2.5
1.2.6
</string>

<!-- AndroidSlidingUpPanel -->
Expand Down
2 changes: 1 addition & 1 deletion Ninja/res/values/strings_key.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<!-- TODO: change when new version code -->
<string name="sp_first">
SP_FIRST_17
SP_FIRST_18
</string>

<string name="sp_ad_block">
Expand Down
4 changes: 2 additions & 2 deletions Ninja/res/xml/preference_clear.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<CheckBoxPreference android:key="@string/sp_clear_bookmarks"
android:defaultValue="false"
android:title="@string/clear_title_passwords">
android:title="@string/clear_title_bookmarks">
</CheckBoxPreference>

<CheckBoxPreference android:key="@string/sp_clear_cache"
Expand All @@ -24,7 +24,7 @@


<CheckBoxPreference android:key="@string/sp_clear_form_data"
android:defaultValue="true"
android:defaultValue="false"
android:title="@string/clear_title_form_data">
</CheckBoxPreference>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void clear() {
boolean clearBookmarks = sp.getBoolean(getString(R.string.sp_clear_bookmarks), false);
boolean clearCache = sp.getBoolean(getString(R.string.sp_clear_cache), true);
boolean clearCookie = sp.getBoolean(getString(R.string.sp_clear_cookie), false);
boolean clearFormData = sp.getBoolean(getString(R.string.sp_clear_form_data), true);
boolean clearFormData = sp.getBoolean(getString(R.string.sp_clear_form_data), false);
boolean clearHistory = sp.getBoolean(getString(R.string.sp_clear_history), true);
boolean clearPasswords = sp.getBoolean(getString(R.string.sp_clear_passwords), false);

Expand Down
2 changes: 1 addition & 1 deletion Ninja/src/io/github/mthli/Ninja/Service/ClearService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void clear() {
boolean clearBookmarks = sp.getBoolean(getString(R.string.sp_clear_bookmarks), false);
boolean clearCache = sp.getBoolean(getString(R.string.sp_clear_cache), true);
boolean clearCookie = sp.getBoolean(getString(R.string.sp_clear_cookie), false);
boolean clearFormData = sp.getBoolean(getString(R.string.sp_clear_form_data), true);
boolean clearFormData = sp.getBoolean(getString(R.string.sp_clear_form_data), false);
boolean clearHistory = sp.getBoolean(getString(R.string.sp_clear_history), true);
boolean clearPasswords = sp.getBoolean(getString(R.string.sp_clear_passwords), false);

Expand Down
60 changes: 38 additions & 22 deletions Ninja/src/io/github/mthli/Ninja/Unit/BrowserUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
import io.github.mthli.Ninja.Database.RecordAction;
import io.github.mthli.Ninja.R;
import io.github.mthli.Ninja.View.NinjaToast;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Locale;
import java.util.*;
import java.util.regex.Pattern;

public class BrowserUnit {
public static final int PROGRESS_MAX = 100;
public static final int PROGRESS_MIN = 0;
public static final String SUFFIX_HTML = ".html";
public static final String SUFFIX_PNG = ".png";
public static final String SUFFIX_TXT = ".txt";

Expand All @@ -40,6 +43,10 @@ public class BrowserUnit {
public static final String MIME_TYPE_IMAGE = "image/*";

public static final String BASE_URL = "file:///android_asset/";
public static final String NINJA_BOOKMARK_TYPE = "<DT><A HREF=\"{url}\" ADD_DATE=\"{time}\">{title}</A>";
public static final String NINJA_BOOKMARK_TITLE = "{title}";
public static final String NINJA_BOOKMARK_URL = "{url}";
public static final String NINJA_BOOKMARK_TIME = "{time}";
public static final String NINJA_INTRODUCTION_EN = "ninja_introduction_en.html";
public static final String NINJA_INTRODUCTION_ZH = "ninja_introduction_zh.html";

Expand Down Expand Up @@ -242,21 +249,21 @@ public static String exportBookmarks(Context context) {
action.close();

String filename = context.getString(R.string.bookmarks_filename);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename + SUFFIX_TXT);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename + SUFFIX_HTML);
int count = 0;
while (file.exists()) {
count++;
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename + "." + count + SUFFIX_TXT);
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename + "." + count + SUFFIX_HTML);
}

try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file, false));
for (Record record : list) {
JSONObject object = new JSONObject();
object.put(RecordUnit.COLUMN_TITLE, record.getTitle());
object.put(RecordUnit.COLUMN_URL, record.getURL());
object.put(RecordUnit.COLUMN_TIME, record.getTime());
writer.write(object.toString());
String type = NINJA_BOOKMARK_TYPE;
type = type.replace(NINJA_BOOKMARK_TITLE, record.getTitle());
type = type.replace(NINJA_BOOKMARK_URL, record.getURL());
type = type.replace(NINJA_BOOKMARK_TIME, String.valueOf(record.getTime()));
writer.write(type);
writer.newLine();
}
writer.close();
Expand Down Expand Up @@ -297,29 +304,38 @@ public static int importBookmarks(Context context, File file) {
if (file == null) {
return -1;
}
List<Record> list = new ArrayList<>();

int count = 0;
try {
RecordAction action = new RecordAction(context);
action.open(true);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
JSONObject object = new JSONObject(line);

Document document = Jsoup.parse(file, URL_ENCODING);
Elements elements = document.select("a");
for (Element element : elements) {
Record record = new Record();
record.setTitle(object.getString(RecordUnit.COLUMN_TITLE).trim());
record.setURL(object.getString(RecordUnit.COLUMN_URL).trim());
record.setTime(object.getLong(RecordUnit.COLUMN_TIME));
record.setTitle(element.text());
record.setURL(element.attr("href").trim());
record.setTime(System.currentTimeMillis());
if (!action.checkBookmark(record)) {
action.addBookmark(record);
count++;
list.add(record);
}
}
reader.close();

Collections.sort(list, new Comparator<Record>() {
@Override
public int compare(Record first, Record second) {
return first.getTitle().compareTo(second.getTitle());
}
});

for (Record record : list) {
action.addBookmark(record);
}
action.close();
} catch (Exception e) {}

return count;
return list.size();
}

public static int importWhitelist(Context context, File file) {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A web browser that open links in background without ever leaving your favorite a

[Ninja in Coolapk](http://coolapk.com/apk/io.github.mthli.Ninja "Ninja in Cookapk")

[Download latest Ninja.apk](https://github.com/mthli/Ninja/releases/download/v1.2.5/Ninja.1.2.5.apk "Ninja.1.2.5.apk")
[Download latest Ninja.apk](https://github.com/mthli/Ninja/releases/download/v1.2.6/Ninja.1.2.6.apk "Ninja.1.2.6.apk")

__SUPPORT: Android 4.1+__

Expand Down

0 comments on commit 783c66c

Please sign in to comment.