From 7cec3bd6e4dbdcbf1791e15a213debd8a9afc307 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 27 Sep 2015 11:58:37 -0400 Subject: [PATCH] Add back importing from stock browser and an attempt to add import from chrome import from the default built in browser, stock browser ususally, but chrome on marshmallow and above. --- app/src/main/AndroidManifest.xml | 19 +-- .../lightning/database/BookmarkLocalSync.java | 141 ++++++++++++++++++ .../lightning/database/BookmarkManager.java | 2 +- .../lightning/download/DownloadHandler.java | 2 + .../fragment/BookmarkSettingsFragment.java | 55 ++++++- app/src/main/res/xml/preference_bookmarks.xml | 3 + 6 files changed, 211 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cbb5f66..c3d47ec 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,13 +1,14 @@ - - + + package="acr.browser.lightning"> + + + android:label="@string/app_name"> + android:theme="@style/Theme.LightTheme"> @@ -96,7 +97,7 @@ android:name=".activity.SettingsActivity" android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/settings" - android:theme="@style/Theme.SettingsTheme" > + android:theme="@style/Theme.SettingsTheme"> @@ -109,9 +110,9 @@ android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/app_name" android:launchMode="singleTask" + android:process=":incognito" android:theme="@style/Theme.DarkTheme" - android:windowSoftInputMode="stateHidden" - android:process=":incognito"> + android:windowSoftInputMode="stateHidden"> @@ -122,7 +123,7 @@ android:name=".activity.ReadingActivity" android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/reading_mode" - android:theme="@style/Theme.SettingsTheme" > + android:theme="@style/Theme.SettingsTheme"> diff --git a/app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java b/app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java new file mode 100644 index 0000000..7d12988 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/database/BookmarkLocalSync.java @@ -0,0 +1,141 @@ +package acr.browser.lightning.database; + +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; +import android.util.Log; + +import java.util.ArrayList; +import java.util.List; + +import acr.browser.lightning.utils.Utils; + +public class BookmarkLocalSync { + + private static final String TAG = BookmarkLocalSync.class.getSimpleName(); + + private static final String STOCK_BOOKMARKS_CONTENT = "content://browser/bookmarks"; + private static final String CHROME_BOOKMARKS_CONTENT = "content://com.android.chrome.browser/bookmarks"; + + private static final String COLUMN_TITLE = "title"; + private static final String COLUMN_URL = "url"; + private static final String COLUMN_BOOKMARK = "bookmark"; + + private Context mContext; + + public BookmarkLocalSync(Context context) { + mContext = context; + } + + @NonNull + @WorkerThread + public List getBookmarksFromStockBrowser() { + List list = new ArrayList<>(); + if (!isStockSupported()) { + return list; + } + Cursor cursor = getStockCursor(); + try { + if (cursor != null) { + for (int n = 0; n < cursor.getColumnCount(); n++) { + Log.d(TAG, cursor.getColumnName(n)); + } + + while (cursor.moveToNext()) { + if (cursor.getInt(2) == 1) { + String url = cursor.getString(0); + String title = cursor.getString(1); + if (url.isEmpty()) { + continue; + } + if (title == null || title.isEmpty()) { + title = Utils.getDomainName(url); + } + list.add(new HistoryItem(url, title)); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + Utils.close(cursor); + return list; + } + + @NonNull + @WorkerThread + public List getBookmarksFromChrome() { + List list = new ArrayList<>(); + if (!isChromeSupported()) { + return list; + } + Cursor cursor = getStockCursor(); + try { + if (cursor != null) { + for (int n = 0; n < cursor.getColumnCount(); n++) { + Log.d(TAG, cursor.getColumnName(n)); + } + + while (cursor.moveToNext()) { + if (cursor.getInt(2) == 1) { + String url = cursor.getString(0); + String title = cursor.getString(1); + if (url.isEmpty()) { + continue; + } + if (title == null || title.isEmpty()) { + title = Utils.getDomainName(url); + } + list.add(new HistoryItem(url, title)); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + Utils.close(cursor); + return list; + } + + public boolean isStockSupported() { + Cursor cursor = getStockCursor(); + Utils.close(cursor); + return cursor != null; + } + + public boolean isChromeSupported() { + Cursor cursor = getChromeCursor(); + Utils.close(cursor); + return cursor != null; + } + + @Nullable + private Cursor getChromeCursor() { + Cursor cursor; + Uri uri = Uri.parse(CHROME_BOOKMARKS_CONTENT); + try { + cursor = mContext.getContentResolver().query(uri, + new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null); + } catch (IllegalArgumentException e) { + return null; + } + return cursor; + } + + @Nullable + private Cursor getStockCursor() { + Cursor cursor; + Uri uri = Uri.parse(STOCK_BOOKMARKS_CONTENT); + try { + cursor = mContext.getContentResolver().query(uri, + new String[]{COLUMN_URL, COLUMN_TITLE, COLUMN_BOOKMARK}, null, null, null); + } catch (IllegalArgumentException e) { + return null; + } + return cursor; + } + +} diff --git a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java index ad309e5..4e348ca 100644 --- a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java +++ b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java @@ -213,7 +213,7 @@ public class BookmarkManager { * * @param list the list of HistoryItems to add to bookmarks */ - private synchronized void addBookmarkList(List list) { + public synchronized void addBookmarkList(List list) { if (list == null || list.isEmpty()) { return; } diff --git a/app/src/main/java/acr/browser/lightning/download/DownloadHandler.java b/app/src/main/java/acr/browser/lightning/download/DownloadHandler.java index 2904c7f..19338f2 100644 --- a/app/src/main/java/acr/browser/lightning/download/DownloadHandler.java +++ b/app/src/main/java/acr/browser/lightning/download/DownloadHandler.java @@ -235,6 +235,8 @@ public class DownloadHandler { e.printStackTrace(); Utils.showSnackbar(activity, R.string.cannot_download); } catch (SecurityException e) { + // TODO write a download utility that downloads files rather than rely on the system + // because the system can only handle Environment.getExternal... as a path Utils.showSnackbar(activity, R.string.problem_location_download); } } diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java index a2926a2..2678d4d 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java @@ -6,6 +6,7 @@ package acr.browser.lightning.fragment; import android.Manifest; import android.app.Activity; import android.content.DialogInterface; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -16,29 +17,66 @@ import android.support.v7.app.AlertDialog; import java.io.File; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import javax.inject.Inject; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.database.BookmarkLocalSync; import acr.browser.lightning.database.BookmarkManager; +import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.utils.PermissionsManager; +import acr.browser.lightning.utils.Utils; public class BookmarkSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener { private static final String SETTINGS_EXPORT = "export_bookmark"; private static final String SETTINGS_IMPORT = "import_bookmark"; + private static final String SETTINGS_IMPORT_BROWSER = "import_browser"; private Activity mActivity; - @Inject BookmarkManager mBookmarkManager; + @Inject + BookmarkManager mBookmarkManager; private File[] mFileList; private String[] mFileNameList; + private BookmarkLocalSync mSync; private static final String[] REQUIRED_PERMISSIONS = new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; private static final File mPath = new File(Environment.getExternalStorageDirectory().toString()); + private class ImportBookmarksTask extends AsyncTask { + @Override + protected Integer doInBackground(Void... params) { + List list = null; + if (mSync.isStockSupported()) { + list = mSync.getBookmarksFromStockBrowser(); + } else if (mSync.isChromeSupported()) { + list = mSync.getBookmarksFromChrome(); + } + int count = 0; + if (list != null && !list.isEmpty()) { + mBookmarkManager.addBookmarkList(list); + count = list.size(); + } + return count; + } + + @Override + protected void onPostExecute(Integer num) { + super.onPostExecute(num); + if (mActivity != null) { + int number = num; + final String message = mActivity.getResources().getString(R.string.message_import); + Utils.showSnackbar(mActivity, number + " " + message); + } + } + } + + ; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -56,13 +94,25 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref } } + @Override + public void onDestroy() { + super.onDestroy(); + mActivity = null; + } + private void initPrefs() { Preference exportpref = findPreference(SETTINGS_EXPORT); Preference importpref = findPreference(SETTINGS_IMPORT); + Preference importStock = findPreference(SETTINGS_IMPORT_BROWSER); + + mSync = new BookmarkLocalSync(mActivity); + + importStock.setEnabled(mSync.isStockSupported() || mSync.isChromeSupported()); exportpref.setOnPreferenceClickListener(this); importpref.setOnPreferenceClickListener(this); + importStock.setOnPreferenceClickListener(this); } @Override @@ -79,6 +129,9 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref createDialog(); } return true; + case SETTINGS_IMPORT_BROWSER: + new ImportBookmarksTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + return true; default: return false; } diff --git a/app/src/main/res/xml/preference_bookmarks.xml b/app/src/main/res/xml/preference_bookmarks.xml index 92c3116..afb06c8 100644 --- a/app/src/main/res/xml/preference_bookmarks.xml +++ b/app/src/main/res/xml/preference_bookmarks.xml @@ -8,5 +8,8 @@ + \ No newline at end of file