Browse Source

Utilize new BookmarkManager class for bookmarks

master v3.2a-beta
Anthony Restaino 10 years ago
parent
commit
df51c67909
  1. 31
      src/acr/browser/lightning/AdvancedSettingsActivity.java
  2. 31
      src/acr/browser/lightning/BookmarkManager.java
  3. 157
      src/acr/browser/lightning/BrowserActivity.java
  4. 2
      src/acr/browser/lightning/PreferenceConstants.java
  5. 10
      src/acr/browser/lightning/SearchAdapter.java
  6. 38
      src/acr/browser/lightning/Utils.java

31
src/acr/browser/lightning/AdvancedSettingsActivity.java

@ -9,7 +9,6 @@ import android.app.AlertDialog; @@ -9,7 +9,6 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@ -778,33 +777,7 @@ public class AdvancedSettingsActivity extends Activity { @@ -778,33 +777,7 @@ public class AdvancedSettingsActivity extends Activity {
}
public void importFromStockBrowser() {
if (mSystemBrowser) {
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
// use 0 for history, 1 for bookmarks
String sel = Browser.BookmarkColumns.BOOKMARK + " = 1";
Cursor mCur;
mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
String title, url;
int number = 0;
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (!mCur.isAfterLast()) {
number++;
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
if (title.length() < 1) {
title = Utils.getDomainName(url);
}
Utils.addBookmark(mContext, title, url);
mCur.moveToNext();
}
}
Utils.showToast(mContext,
number + " " + getResources().getString(R.string.message_import));
} else {
Utils.createInformativeDialog(mContext, getResources().getString(R.string.title_error),
getResources().getString(R.string.dialog_import_error));
}
BookmarkManager manager = new BookmarkManager(this);
manager.importBookmarksFromBrowser();
}
}

31
src/acr/browser/lightning/BookmarkManager.java

@ -41,11 +41,11 @@ public class BookmarkManager { @@ -41,11 +41,11 @@ public class BookmarkManager {
*
* @param item
*/
public void addBookmark(HistoryItem item) {
public synchronized boolean addBookmark(HistoryItem item) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
if (mBookmarkMap.containsKey(item.getUrl())) {
return;
return false;
}
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
@ -63,6 +63,7 @@ public class BookmarkManager { @@ -63,6 +63,7 @@ public class BookmarkManager {
} catch (JSONException e) {
e.printStackTrace();
}
return true;
}
/**
@ -70,7 +71,7 @@ public class BookmarkManager { @@ -70,7 +71,7 @@ public class BookmarkManager {
*
* @param list
*/
public void addBookmarkList(List<HistoryItem> list) {
public synchronized void addBookmarkList(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
@ -99,11 +100,12 @@ public class BookmarkManager { @@ -99,11 +100,12 @@ public class BookmarkManager {
*
* @param url
*/
public void deleteBookmark(String url) {
public synchronized boolean deleteBookmark(String url) {
List<HistoryItem> list = new ArrayList<HistoryItem>();
mBookmarkMap.remove(url);
list = getBookmarks();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
boolean bookmarkDeleted = false;
try {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
for (HistoryItem item : list) {
@ -115,6 +117,8 @@ public class BookmarkManager { @@ -115,6 +117,8 @@ public class BookmarkManager {
object.put(ORDER, item.getOrder());
fileWriter.write(object.toString());
fileWriter.newLine();
} else {
bookmarkDeleted = true;
}
}
fileWriter.close();
@ -123,13 +127,14 @@ public class BookmarkManager { @@ -123,13 +127,14 @@ public class BookmarkManager {
} catch (JSONException e) {
e.printStackTrace();
}
return bookmarkDeleted;
}
/**
* This method exports the stored bookmarks to a text file in the device's
* external download directory
*/
public void exportBookmarks() {
public synchronized void exportBookmarks() {
List<HistoryItem> bookmarkList = getBookmarks();
File bookmarksExport = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
@ -159,7 +164,7 @@ public class BookmarkManager { @@ -159,7 +164,7 @@ public class BookmarkManager {
*
* @return
*/
public List<HistoryItem> getBookmarks() {
public synchronized List<HistoryItem> getBookmarks() {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
@ -172,6 +177,7 @@ public class BookmarkManager { @@ -172,6 +177,7 @@ public class BookmarkManager {
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.add(item);
}
bookmarksReader.close();
@ -191,7 +197,7 @@ public class BookmarkManager { @@ -191,7 +197,7 @@ public class BookmarkManager {
* @param folder
* @return
*/
public List<HistoryItem> getBookmarksFromFolder(String folder) {
public synchronized List<HistoryItem> getBookmarksFromFolder(String folder) {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
@ -205,6 +211,7 @@ public class BookmarkManager { @@ -205,6 +211,7 @@ public class BookmarkManager {
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
item.setImageId(R.drawable.ic_bookmark);
bookmarks.add(item);
}
}
@ -224,7 +231,7 @@ public class BookmarkManager { @@ -224,7 +231,7 @@ public class BookmarkManager {
*
* @return
*/
private SortedMap<String, Integer> getBookmarkUrls() {
private synchronized SortedMap<String, Integer> getBookmarkUrls() {
SortedMap<String, Integer> map = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
@ -250,7 +257,7 @@ public class BookmarkManager { @@ -250,7 +257,7 @@ public class BookmarkManager {
*
* @return
*/
public List<HistoryItem> getFolders() {
public synchronized List<HistoryItem> getFolders() {
List<HistoryItem> folders = new ArrayList<HistoryItem>();
SortedMap<String, Integer> folderMap = new TreeMap<String, Integer>(
String.CASE_INSENSITIVE_ORDER);
@ -284,7 +291,7 @@ public class BookmarkManager { @@ -284,7 +291,7 @@ public class BookmarkManager {
* This method imports all bookmarks that are included in the device's
* permanent bookmark storage
*/
public void importBookmarksFromBrowser() {
public synchronized void importBookmarksFromBrowser() {
if (mContext.getSharedPreferences(PreferenceConstants.PREFERENCES, 0).getBoolean(
PreferenceConstants.SYSTEM_BROWSER_PRESENT, false)) {
@ -328,7 +335,7 @@ public class BookmarkManager { @@ -328,7 +335,7 @@ public class BookmarkManager {
* @param dir
* @param file
*/
public void importBookmarksFromFile(File dir, String file) {
public synchronized void importBookmarksFromFile(File dir, String file) {
File bookmarksImport = new File(dir, file);
List<HistoryItem> list = new ArrayList<HistoryItem>();
try {
@ -361,7 +368,7 @@ public class BookmarkManager { @@ -361,7 +368,7 @@ public class BookmarkManager {
*
* @param list
*/
public void overwriteBookmarks(List<HistoryItem> list) {
public synchronized void overwriteBookmarks(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));

157
src/acr/browser/lightning/BrowserActivity.java

@ -117,6 +117,7 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -117,6 +117,7 @@ public class BrowserActivity extends Activity implements BrowserController {
private static SearchAdapter mSearchAdapter;
private static LayoutParams mMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
private BookmarkManager mBookmarkManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -144,7 +145,12 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -144,7 +145,12 @@ public class BrowserActivity extends Activity implements BrowserController {
} else {
mWebViews = new ArrayList<LightningView>();
}
mBookmarkManager = new BookmarkManager(this);
if (!mPreferences.getBoolean(PreferenceConstants.OLD_BOOKMARKS_IMPORTED, false)) {
List<HistoryItem> old = Utils.getOldBookmarks(this);
mBookmarkManager.addBookmarkList(old);
mEditPrefs.putBoolean(PreferenceConstants.OLD_BOOKMARKS_IMPORTED, true).apply();
}
mActivity = this;
mClickHandler = new ClickHandler(this);
mBrowserFrame = (FrameLayout) findViewById(R.id.content_frame);
@ -179,7 +185,7 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -179,7 +185,7 @@ public class BrowserActivity extends Activity implements BrowserController {
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerList.setOnItemLongClickListener(new DrawerItemLongClickListener());
mBookmarkList = getBookmarks();
mBookmarkList = mBookmarkManager.getBookmarks();
mBookmarkAdapter = new BookmarkViewAdapter(this, R.layout.bookmark_list_item, mBookmarkList);
mDrawerListRight.setAdapter(mBookmarkAdapter);
mDrawerListRight.setOnItemClickListener(new BookmarkItemClickListener());
@ -715,7 +721,14 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -715,7 +721,14 @@ public class BrowserActivity extends Activity implements BrowserController {
return true;
case R.id.action_add_bookmark:
if (!mCurrentView.getUrl().startsWith(Constants.FILE)) {
addBookmark(this, mCurrentView.getTitle(), mCurrentView.getUrl());
HistoryItem bookmark = new HistoryItem(mCurrentView.getUrl(),
mCurrentView.getTitle());
if (mBookmarkManager.addBookmark(bookmark)) {
mBookmarkList.add(bookmark);
Collections.sort(mBookmarkList, new SortIgnoreCase());
notifyBookmarkDataSetChanged();
mSearchAdapter.refreshBookmarks();
}
}
return true;
case R.id.action_find:
@ -825,7 +838,13 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -825,7 +838,13 @@ public class BrowserActivity extends Activity implements BrowserController {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteBookmark(mBookmarkList.get(position).getUrl());
if (mBookmarkManager.deleteBookmark(mBookmarkList.get(position)
.getUrl())) {
mBookmarkList.remove(position);
notifyBookmarkDataSetChanged();
mSearchAdapter.refreshBookmarks();
openBookmarks();
}
}
})
.setNeutralButton(getResources().getString(R.string.action_edit),
@ -872,29 +891,7 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -872,29 +891,7 @@ public class BrowserActivity extends Activity implements BrowserController {
public void onClick(DialogInterface dialog, int which) {
mBookmarkList.get(id).setTitle(getTitle.getText().toString());
mBookmarkList.get(id).setUrl(getUrl.getText().toString());
notifyBookmarkDataSetChanged();
File book = new File(getFilesDir(), "bookmarks");
File bookUrl = new File(getFilesDir(), "bookurl");
try {
BufferedWriter bookWriter = new BufferedWriter(new FileWriter(book));
BufferedWriter urlWriter = new BufferedWriter(new FileWriter(bookUrl));
Iterator<HistoryItem> iter = mBookmarkList.iterator();
HistoryItem item;
while (iter.hasNext()) {
item = iter.next();
bookWriter.write(item.getTitle());
urlWriter.write(item.getUrl());
bookWriter.newLine();
urlWriter.newLine();
}
bookWriter.close();
urlWriter.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
mBookmarkManager.overwriteBookmarks(mBookmarkList);
Collections.sort(mBookmarkList, new SortIgnoreCase());
notifyBookmarkDataSetChanged();
if (mCurrentView != null) {
@ -1278,7 +1275,7 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -1278,7 +1275,7 @@ public class BrowserActivity extends Activity implements BrowserController {
} else if (!mHistoryHandler.isOpen()) {
mHistoryHandler = new HistoryDatabaseHandler(this);
}
mBookmarkList = getBookmarks();
mBookmarkList = mBookmarkManager.getBookmarks();
notifyBookmarkDataSetChanged();
} else {
initialize();
@ -1343,41 +1340,6 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -1343,41 +1340,6 @@ public class BrowserActivity extends Activity implements BrowserController {
}
}
public void deleteBookmark(String url) {
File book = new File(getFilesDir(), "bookmarks");
File bookUrl = new File(getFilesDir(), "bookurl");
try {
BufferedWriter bookWriter = new BufferedWriter(new FileWriter(book));
BufferedWriter urlWriter = new BufferedWriter(new FileWriter(bookUrl));
Iterator<HistoryItem> iter = mBookmarkList.iterator();
HistoryItem item;
int num = 0;
int deleteIndex = -1;
while (iter.hasNext()) {
item = iter.next();
if (!item.getUrl().equalsIgnoreCase(url)) {
bookWriter.write(item.getTitle());
urlWriter.write(item.getUrl());
bookWriter.newLine();
urlWriter.newLine();
} else {
deleteIndex = num;
}
num++;
}
if (deleteIndex != -1) {
mBookmarkList.remove(deleteIndex);
}
bookWriter.close();
urlWriter.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
notifyBookmarkDataSetChanged();
mSearchAdapter.refreshBookmarks();
openBookmarks();
}
private int pixelsToDp(int num) {
float scale = getResources().getDisplayMetrics().density;
return (int) ((num - 0.5f) / scale);
@ -1812,28 +1774,6 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -1812,28 +1774,6 @@ public class BrowserActivity extends Activity implements BrowserController {
return false;
}
// Damn it, I regret not using SQLite in the first place for this
private List<HistoryItem> getBookmarks() {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookUrl = new File(getApplicationContext().getFilesDir(), "bookurl");
File book = new File(getApplicationContext().getFilesDir(), "bookmarks");
try {
BufferedReader readUrl = new BufferedReader(new FileReader(bookUrl));
BufferedReader readBook = new BufferedReader(new FileReader(book));
String u, t;
while ((u = readUrl.readLine()) != null && (t = readBook.readLine()) != null) {
HistoryItem map = new HistoryItem(u, t);
bookmarks.add(map);
}
readBook.close();
readUrl.close();
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) {
}
Collections.sort(bookmarks, new SortIgnoreCase());
return bookmarks;
}
/**
* function that opens the HTML history page in the browser
*/
@ -1876,9 +1816,8 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -1876,9 +1816,8 @@ public class BrowserActivity extends Activity implements BrowserController {
HistoryItem helper;
while (iter.hasNext()) {
helper = iter.next();
bookmarkHtml += (BookmarkPage.PART1 + helper.getUrl()
+ BookmarkPage.PART2 + helper.getUrl() + BookmarkPage.PART3
+ helper.getTitle() + BookmarkPage.PART4);
bookmarkHtml += (BookmarkPage.PART1 + helper.getUrl() + BookmarkPage.PART2
+ helper.getUrl() + BookmarkPage.PART3 + helper.getTitle() + BookmarkPage.PART4);
}
bookmarkHtml += BookmarkPage.END;
File bookmarkWebPage = new File(mContext.getFilesDir(), BookmarkPage.FILENAME);
@ -1893,48 +1832,6 @@ public class BrowserActivity extends Activity implements BrowserController { @@ -1893,48 +1832,6 @@ public class BrowserActivity extends Activity implements BrowserController {
view.loadUrl(Constants.FILE + bookmarkWebPage);
}
/**
* adds a bookmark with a title and url. Simple.
*/
public void addBookmark(Context context, String title, String url) {
File book = new File(context.getFilesDir(), "bookmarks");
File bookUrl = new File(context.getFilesDir(), "bookurl");
HistoryItem bookmark = new HistoryItem(url, title);
try {
BufferedReader readUrlRead = new BufferedReader(new FileReader(bookUrl));
String u;
while ((u = readUrlRead.readLine()) != null) {
if (u.contentEquals(url)) {
readUrlRead.close();
return;
}
}
readUrlRead.close();
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) {
} catch (NullPointerException ignored) {
}
try {
BufferedWriter bookWriter = new BufferedWriter(new FileWriter(book, true));
BufferedWriter urlWriter = new BufferedWriter(new FileWriter(bookUrl, true));
bookWriter.write(title);
urlWriter.write(url);
bookWriter.newLine();
urlWriter.newLine();
bookWriter.close();
urlWriter.close();
mBookmarkList.add(bookmark);
Collections.sort(mBookmarkList, new SortIgnoreCase());
notifyBookmarkDataSetChanged();
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) {
} catch (NullPointerException ignored) {
}
mSearchAdapter.refreshBookmarks();
}
@Override
public void update() {
mTitleAdapter.notifyDataSetChanged();

2
src/acr/browser/lightning/PreferenceConstants.java

@ -45,4 +45,6 @@ public final class PreferenceConstants { @@ -45,4 +45,6 @@ public final class PreferenceConstants {
public static final String USE_PROXY_HOST = "useProxyHost";
public static final String USE_PROXY_PORT = "useProxyPort";
public static final String INITIAL_CHECK_FOR_TOR = "checkForTor";
public static final String OLD_BOOKMARKS_IMPORTED = "oldBookmarksImported";
}

10
src/acr/browser/lightning/SearchAdapter.java

@ -7,6 +7,7 @@ import android.graphics.Color; @@ -7,6 +7,7 @@ import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -37,6 +38,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -37,6 +38,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private boolean mUseGoogle = true;
private Context mContext;
private boolean mIncognito;
private BookmarkManager mBookmarkManager;
public SearchAdapter(Context context, boolean incognito) {
mDatabaseHandler = new HistoryDatabaseHandler(context);
@ -44,7 +46,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -44,7 +46,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
mHistory = new ArrayList<HistoryItem>();
mBookmarks = new ArrayList<HistoryItem>();
mSuggestions = new ArrayList<HistoryItem>();
mAllBookmarks = Utils.getBookmarks(context);
mBookmarkManager = new BookmarkManager(context);
mAllBookmarks = mBookmarkManager.getBookmarks();
mPreferences = context.getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
mUseGoogle = mPreferences.getBoolean(PreferenceConstants.GOOGLE_SEARCH_SUGGESTIONS, true);
mContext = context;
@ -59,7 +62,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -59,7 +62,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
}
public void refreshBookmarks() {
mAllBookmarks = Utils.getBookmarks(mContext);
mAllBookmarks = mBookmarkManager.getBookmarks();
}
@Override
@ -374,7 +377,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @@ -374,7 +377,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
}
filteredList.add(mSuggestions.get(n));
}
Log.i("MAX", "Max: "+maxSuggestions+" "+maxBookmarks+" "+maxHistory);
Log.i("SIZE", "size: "+suggestionsSize+" "+bookmarkSize+" "+historySize);
return filteredList;
}
}

38
src/acr/browser/lightning/Utils.java

@ -31,42 +31,6 @@ public final class Utils { @@ -31,42 +31,6 @@ public final class Utils {
Log.i(Constants.TAG, "Downloading" + fileName);
}
public static synchronized void addBookmark(Context context, String title, String url) {
File book = new File(context.getFilesDir(), "bookmarks");
File bookUrl = new File(context.getFilesDir(), "bookurl");
if (("Bookmarks".equals(title) || "History".equals(title)) && url.startsWith("file://")) {
return;
}
try {
BufferedReader readUrlRead = new BufferedReader(new FileReader(bookUrl));
String u;
while ((u = readUrlRead.readLine()) != null) {
if (u.contentEquals(url)) {
readUrlRead.close();
return;
}
}
readUrlRead.close();
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) {
} catch (NullPointerException ignored) {
}
try {
BufferedWriter bookWriter = new BufferedWriter(new FileWriter(book, true));
BufferedWriter urlWriter = new BufferedWriter(new FileWriter(bookUrl, true));
bookWriter.write(title);
urlWriter.write(url);
bookWriter.newLine();
urlWriter.newLine();
bookWriter.close();
urlWriter.close();
} catch (FileNotFoundException ignored) {
} catch (IOException ignored) {
} catch (NullPointerException ignored) {
}
}
public static Intent newEmailIntent(Context context, String address, String subject,
String body, String cc) {
Intent intent = new Intent(Intent.ACTION_SEND);
@ -119,7 +83,7 @@ public final class Utils { @@ -119,7 +83,7 @@ public final class Utils {
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
public static List<HistoryItem> getBookmarks(Context context) {
public static List<HistoryItem> getOldBookmarks(Context context) {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookUrl = new File(context.getFilesDir(), "bookurl");
File book = new File(context.getFilesDir(), "bookmarks");

Loading…
Cancel
Save