From df51c67909c3b6979caccf3a93d3bde6928f7ebc Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Fri, 22 Aug 2014 13:32:22 -0400 Subject: [PATCH] Utilize new BookmarkManager class for bookmarks --- .../lightning/AdvancedSettingsActivity.java | 31 +--- .../browser/lightning/BookmarkManager.java | 31 ++-- .../browser/lightning/BrowserActivity.java | 157 +++--------------- .../lightning/PreferenceConstants.java | 2 + src/acr/browser/lightning/SearchAdapter.java | 10 +- src/acr/browser/lightning/Utils.java | 38 +---- 6 files changed, 58 insertions(+), 211 deletions(-) diff --git a/src/acr/browser/lightning/AdvancedSettingsActivity.java b/src/acr/browser/lightning/AdvancedSettingsActivity.java index ab4c76dfa..ff673c5d8 100644 --- a/src/acr/browser/lightning/AdvancedSettingsActivity.java +++ b/src/acr/browser/lightning/AdvancedSettingsActivity.java @@ -9,7 +9,6 @@ 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 void onClick(DialogInterface dialog, int which) { } 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(); } } diff --git a/src/acr/browser/lightning/BookmarkManager.java b/src/acr/browser/lightning/BookmarkManager.java index 9bf7ae64a..b4f77d32a 100644 --- a/src/acr/browser/lightning/BookmarkManager.java +++ b/src/acr/browser/lightning/BookmarkManager.java @@ -41,11 +41,11 @@ public BookmarkManager(Context context) { * * @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 void addBookmark(HistoryItem item) { } catch (JSONException e) { e.printStackTrace(); } + return true; } /** @@ -70,7 +71,7 @@ public void addBookmark(HistoryItem item) { * * @param list */ - public void addBookmarkList(List list) { + public synchronized void addBookmarkList(List list) { File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); try { BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true)); @@ -99,11 +100,12 @@ public void addBookmarkList(List list) { * * @param url */ - public void deleteBookmark(String url) { + public synchronized boolean deleteBookmark(String url) { List list = new ArrayList(); 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 void deleteBookmark(String url) { object.put(ORDER, item.getOrder()); fileWriter.write(object.toString()); fileWriter.newLine(); + } else { + bookmarkDeleted = true; } } fileWriter.close(); @@ -123,13 +127,14 @@ public void deleteBookmark(String url) { } 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 bookmarkList = getBookmarks(); File bookmarksExport = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), @@ -159,7 +164,7 @@ public void exportBookmarks() { * * @return */ - public List getBookmarks() { + public synchronized List getBookmarks() { List bookmarks = new ArrayList(); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); try { @@ -172,6 +177,7 @@ public List getBookmarks() { 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 List getBookmarks() { * @param folder * @return */ - public List getBookmarksFromFolder(String folder) { + public synchronized List getBookmarksFromFolder(String folder) { List bookmarks = new ArrayList(); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); try { @@ -205,6 +211,7 @@ public List getBookmarksFromFolder(String folder) { 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 List getBookmarksFromFolder(String folder) { * * @return */ - private SortedMap getBookmarkUrls() { + private synchronized SortedMap getBookmarkUrls() { SortedMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); try { @@ -250,7 +257,7 @@ private SortedMap getBookmarkUrls() { * * @return */ - public List getFolders() { + public synchronized List getFolders() { List folders = new ArrayList(); SortedMap folderMap = new TreeMap( String.CASE_INSENSITIVE_ORDER); @@ -284,7 +291,7 @@ public List getFolders() { * 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 void importBookmarksFromBrowser() { * @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 list = new ArrayList(); try { @@ -361,7 +368,7 @@ public void importBookmarksFromFile(File dir, String file) { * * @param list */ - public void overwriteBookmarks(List list) { + public synchronized void overwriteBookmarks(List list) { File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); try { BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, false)); diff --git a/src/acr/browser/lightning/BrowserActivity.java b/src/acr/browser/lightning/BrowserActivity.java index 1867fb181..ada422163 100644 --- a/src/acr/browser/lightning/BrowserActivity.java +++ b/src/acr/browser/lightning/BrowserActivity.java @@ -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 @@ private synchronized void initialize() { } else { mWebViews = new ArrayList(); } - + mBookmarkManager = new BookmarkManager(this); + if (!mPreferences.getBoolean(PreferenceConstants.OLD_BOOKMARKS_IMPORTED, false)) { + List 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 @@ private synchronized void initialize() { 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 boolean onOptionsItemSelected(MenuItem item) { 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 void onClick(DialogInterface dialog, int id) { @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 synchronized void editBookmark(final int id) { 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 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 @@ protected void onResume() { } else if (!mHistoryHandler.isOpen()) { mHistoryHandler = new HistoryDatabaseHandler(this); } - mBookmarkList = getBookmarks(); + mBookmarkList = mBookmarkManager.getBookmarks(); notifyBookmarkDataSetChanged(); } else { initialize(); @@ -1343,41 +1340,6 @@ void searchTheWeb(String query) { } } - 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 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 boolean isIncognito() { return false; } - // Damn it, I regret not using SQLite in the first place for this - private List getBookmarks() { - List bookmarks = new ArrayList(); - 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 void openBookmarkPage(WebView view) { 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 void openBookmarkPage(WebView view) { 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(); diff --git a/src/acr/browser/lightning/PreferenceConstants.java b/src/acr/browser/lightning/PreferenceConstants.java index 439f1f7a8..3cfcb0b3a 100644 --- a/src/acr/browser/lightning/PreferenceConstants.java +++ b/src/acr/browser/lightning/PreferenceConstants.java @@ -45,4 +45,6 @@ private 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"; } diff --git a/src/acr/browser/lightning/SearchAdapter.java b/src/acr/browser/lightning/SearchAdapter.java index b85d0c439..a5b5c0492 100644 --- a/src/acr/browser/lightning/SearchAdapter.java +++ b/src/acr/browser/lightning/SearchAdapter.java @@ -7,6 +7,7 @@ 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 { 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 SearchAdapter(Context context, boolean incognito) { mHistory = new ArrayList(); mBookmarks = new ArrayList(); mSuggestions = new ArrayList(); - 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 void refreshPreferences() { } public void refreshBookmarks() { - mAllBookmarks = Utils.getBookmarks(mContext); + mAllBookmarks = mBookmarkManager.getBookmarks(); } @Override @@ -374,7 +377,8 @@ public List getSuggestions() { } filteredList.add(mSuggestions.get(n)); } - + Log.i("MAX", "Max: "+maxSuggestions+" "+maxBookmarks+" "+maxHistory); + Log.i("SIZE", "size: "+suggestionsSize+" "+bookmarkSize+" "+historySize); return filteredList; } } diff --git a/src/acr/browser/lightning/Utils.java b/src/acr/browser/lightning/Utils.java index 828b0eb91..ce1eae07e 100644 --- a/src/acr/browser/lightning/Utils.java +++ b/src/acr/browser/lightning/Utils.java @@ -31,42 +31,6 @@ public static void downloadFile(final Activity activity, final String url, 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 static String getDomainName(String url) { return domain.startsWith("www.") ? domain.substring(4) : domain; } - public static List getBookmarks(Context context) { + public static List getOldBookmarks(Context context) { List bookmarks = new ArrayList(); File bookUrl = new File(context.getFilesDir(), "bookurl"); File book = new File(context.getFilesDir(), "bookmarks");