Browse Source

Fixed bug where opening a URL in the browser wouldn't work, refactored the ui controller, fixed bad database practices.

master
Anthony Restaino 9 years ago
parent
commit
9f755aeed7
  1. 53
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 4
      app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java
  3. 4
      app/src/main/java/acr/browser/lightning/activity/MainActivity.java
  4. 44
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  5. 8
      app/src/main/java/acr/browser/lightning/controller/UIController.java
  6. 55
      app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java
  7. 6
      app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java
  8. 34
      app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java
  9. 19
      app/src/main/java/acr/browser/lightning/view/LightningView.java
  10. 15
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

53
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -97,7 +97,7 @@ import acr.browser.lightning.bus.TabEvents;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.controller.BrowserController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
@ -116,7 +116,7 @@ import acr.browser.lightning.view.LightningView;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public abstract class BrowserActivity extends ThemableBrowserActivity implements BrowserController, OnClickListener, OnLongClickListener { public abstract class BrowserActivity extends ThemableBrowserActivity implements UIController, OnClickListener, OnLongClickListener {
// Static Layout // Static Layout
@Bind(R.id.drawer_layout) @Bind(R.id.drawer_layout)
@ -213,11 +213,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
protected abstract boolean isIncognito(); protected abstract boolean isIncognito();
// abstract void initializeTabs();
abstract void closeActivity(); abstract void closeActivity();
public abstract void updateHistory(final String title, final String url); public abstract void updateHistory(@Nullable final String title, @NonNull final String url);
abstract void updateCookiePreference(); abstract void updateCookiePreference();
@ -348,7 +346,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath()); WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
} }
tabsManager.restoreTabs(this, mDarkTheme, isIncognito()); tabsManager.restoreTabsAndHandleIntent(this, getIntent(), isIncognito());
// At this point we always have at least a tab in the tab manager // At this point we always have at least a tab in the tab manager
showTab(0); showTab(0);
@ -882,14 +880,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Should update the bookmark status in BookmarksFragment // Should update the bookmark status in BookmarksFragment
mEventBus.post(new BrowserEvents.CurrentPageUrl(newView.getUrl())); mEventBus.post(new BrowserEvents.CurrentPageUrl(newView.getUrl()));
// new Handler().postDelayed(new Runnable() { // new Handler().postDelayed(new Runnable() {
// @Override // @Override
// public void run() { // public void run() {
// Remove browser frame background to reduce overdraw // Remove browser frame background to reduce overdraw
//TODO evaluate performance //TODO evaluate performance
// mBrowserFrame.setBackgroundColor(Color.TRANSPARENT); // mBrowserFrame.setBackgroundColor(Color.TRANSPARENT);
// } // }
// }, 300); // }, 300);
} }
@ -955,7 +953,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return false; return false;
} }
mIsNewIntent = false; mIsNewIntent = false;
LightningView startingTab = tabsManager.newTab(this, url, mDarkTheme, isIncognito()); LightningView startingTab = tabsManager.newTab(this, url, isIncognito());
if (mIdGenerator == 0) { if (mIdGenerator == 0) {
startingTab.resumeTimers(); startingTab.resumeTimers();
} }
@ -967,13 +965,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// TODO Check is this is callable directly from LightningView // TODO Check is this is callable directly from LightningView
mEventBus.post(new BrowserEvents.TabsChanged()); mEventBus.post(new BrowserEvents.TabsChanged());
// TODO Restore this // TODO Restore this
// new Handler().postDelayed(new Runnable() { // new Handler().postDelayed(new Runnable() {
// @Override // @Override
// public void run() { // public void run() {
// mDrawerListLeft.smoothScrollToPosition(tabsManager.size() - 1); // mDrawerListLeft.smoothScrollToPosition(tabsManager.size() - 1);
// } // }
// }, 300); // }, 300);
return true; return true;
} }
@ -1257,6 +1255,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}); });
} }
@Override
public boolean getUseDarkTheme() {
return mDarkTheme;
}
@ColorInt @ColorInt
@Override @Override
public int getUiColor() { public int getUiColor() {
@ -1264,11 +1267,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
@Override @Override
public void updateUrl(String url, boolean shortUrl) { public void updateUrl(@Nullable String url, boolean shortUrl) {
final LightningView currentTab = tabsManager.getCurrentTab();
if (url == null || mSearch == null || mSearch.hasFocus()) { if (url == null || mSearch == null || mSearch.hasFocus()) {
return; return;
} }
final LightningView currentTab = tabsManager.getCurrentTab();
mEventBus.post(new BrowserEvents.CurrentPageUrl(url)); mEventBus.post(new BrowserEvents.CurrentPageUrl(url));
if (shortUrl && !url.startsWith(Constants.FILE)) { if (shortUrl && !url.startsWith(Constants.FILE)) {
switch (mPreferences.getUrlBoxContentChoice()) { switch (mPreferences.getUrlBoxContentChoice()) {
@ -1302,7 +1305,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mProgressBar.setProgress(n); mProgressBar.setProgress(n);
} }
void addItemToHistory(final String title, final String url) { void addItemToHistory(@Nullable final String title, @NonNull final String url) {
Runnable update = new Runnable() { Runnable update = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -1317,7 +1320,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
} }
}; };
if (url != null && !url.startsWith(Constants.FILE)) { if (!url.startsWith(Constants.FILE)) {
new Thread(update).start(); new Thread(update).start();
} }
} }
@ -1893,7 +1896,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
super.onReceive(context, intent); super.onReceive(context, intent);
boolean isConnected = isConnected(context); boolean isConnected = isConnected(context);
Log.d(Constants.TAG, "Network Connected: " + String.valueOf(isConnected)); Log.d(Constants.TAG, "Network Connected: " + String.valueOf(isConnected));
tabsManager.notifyConnectioneStatus(isConnected); tabsManager.notifyConnectionStatus(isConnected);
} }
}; };

4
app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java

@ -2,6 +2,8 @@ package acr.browser.lightning.activity;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Menu; import android.view.Menu;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.CookieSyncManager; import android.webkit.CookieSyncManager;
@ -44,7 +46,7 @@ public class IncognitoActivity extends BrowserActivity {
} }
@Override @Override
public void updateHistory(String title, String url) { public void updateHistory(@Nullable String title, @NonNull String url) {
// addItemToHistory(title, url); // addItemToHistory(title, url);
} }

4
app/src/main/java/acr/browser/lightning/activity/MainActivity.java

@ -2,6 +2,8 @@ package acr.browser.lightning.activity;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Menu; import android.view.Menu;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.CookieSyncManager; import android.webkit.CookieSyncManager;
@ -45,7 +47,7 @@ public class MainActivity extends BrowserActivity {
} }
@Override @Override
public void updateHistory(String title, String url) { public void updateHistory(@Nullable String title, @NonNull String url) {
addItemToHistory(title, url); addItemToHistory(title, url);
} }

44
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -1,7 +1,10 @@
package acr.browser.lightning.activity; package acr.browser.lightning.activity;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import android.webkit.WebView; import android.webkit.WebView;
import java.util.ArrayList; import java.util.ArrayList;
@ -21,6 +24,7 @@ import acr.browser.lightning.view.LightningView;
@Singleton @Singleton
public class TabsManager { public class TabsManager {
private static final String TAG = TabsManager.class.getSimpleName();
private final List<LightningView> mWebViewList = new ArrayList<>(); private final List<LightningView> mWebViewList = new ArrayList<>();
private LightningView mCurrentTab; private LightningView mCurrentTab;
@ -28,24 +32,30 @@ public class TabsManager {
PreferenceManager mPreferenceManager; PreferenceManager mPreferenceManager;
@Inject @Inject
public TabsManager() { public TabsManager() {}
}
public void restoreTabs(BrowserActivity activity, boolean darkTheme, boolean incognito) { public void restoreTabsAndHandleIntent(Activity activity, Intent intent, boolean incognito) {
String url = null;
if (intent != null) {
url = intent.getDataString();
}
mWebViewList.clear(); mWebViewList.clear();
mCurrentTab = null; mCurrentTab = null;
if (url != null) {
newTab(activity, url, incognito);
}
if (!incognito && mPreferenceManager.getRestoreLostTabsEnabled()) { if (!incognito && mPreferenceManager.getRestoreLostTabsEnabled()) {
final String mem = mPreferenceManager.getMemoryUrl(); final String mem = mPreferenceManager.getMemoryUrl();
mPreferenceManager.setMemoryUrl(""); mPreferenceManager.setMemoryUrl("");
String[] array = Utils.getArray(mem); String[] array = Utils.getArray(mem);
for (String urlString : array) { for (String urlString : array) {
if (!urlString.isEmpty()) { if (!urlString.isEmpty()) {
newTab(activity, urlString, darkTheme, incognito); newTab(activity, urlString, incognito);
} }
} }
} }
if (mWebViewList.size() == 0) { if (mWebViewList.size() == 0) {
newTab(activity, null, darkTheme, incognito); newTab(activity, null, incognito);
} }
// mCurrentTab = mWebViewList.get(0); // mCurrentTab = mWebViewList.get(0);
} }
@ -68,7 +78,7 @@ public class TabsManager {
* @return the corespondent {@link LightningView}, or null if the index is invalid * @return the corespondent {@link LightningView}, or null if the index is invalid
*/ */
@Nullable @Nullable
public LightningView getTabAtPosition(final int position) { public synchronized LightningView getTabAtPosition(final int position) {
if (position < 0 || position >= mWebViewList.size()) { if (position < 0 || position >= mWebViewList.size()) {
return null; return null;
} }
@ -79,7 +89,7 @@ public class TabsManager {
/** /**
* Try to low memory pressure * Try to low memory pressure
*/ */
public void freeMemory() { public synchronized void freeMemory() {
for (LightningView tab : mWebViewList) { for (LightningView tab : mWebViewList) {
tab.freeMemory(); tab.freeMemory();
} }
@ -111,7 +121,7 @@ public class TabsManager {
* *
* @param isConnected * @param isConnected
*/ */
public synchronized void notifyConnectioneStatus(final boolean isConnected) { public synchronized void notifyConnectionStatus(final boolean isConnected) {
for (LightningView tab : mWebViewList) { for (LightningView tab : mWebViewList) {
final WebView webView = tab.getWebView(); final WebView webView = tab.getWebView();
if (webView != null) { if (webView != null) {
@ -123,7 +133,7 @@ public class TabsManager {
/** /**
* @return The number of currently opened tabs * @return The number of currently opened tabs
*/ */
public int size() { public synchronized int size() {
return mWebViewList.size(); return mWebViewList.size();
} }
@ -132,14 +142,13 @@ public class TabsManager {
* *
* @param activity * @param activity
* @param url * @param url
* @param darkTheme
* @param isIncognito * @param isIncognito
* @return * @return
*/ */
public synchronized LightningView newTab(final BrowserActivity activity, public synchronized LightningView newTab(final Activity activity,
final String url, final boolean darkTheme, final String url,
final boolean isIncognito) { final boolean isIncognito) {
final LightningView tab = new LightningView(activity, url, darkTheme, isIncognito); final LightningView tab = new LightningView(activity, url, isIncognito);
mWebViewList.add(tab); mWebViewList.add(tab);
return tab; return tab;
} }
@ -166,7 +175,7 @@ public class TabsManager {
* @param tab the tab to look for * @param tab the tab to look for
* @return the position of the tab or -1 if the tab is not in the list * @return the position of the tab or -1 if the tab is not in the list
*/ */
public int positionOf(final LightningView tab) { public synchronized int positionOf(final LightningView tab) {
return mWebViewList.indexOf(tab); return mWebViewList.indexOf(tab);
} }
@ -190,7 +199,7 @@ public class TabsManager {
* @return a {@link WebView} or null * @return a {@link WebView} or null
*/ */
@Nullable @Nullable
public WebView getCurrentWebView() { public synchronized WebView getCurrentWebView() {
return mCurrentTab != null ? mCurrentTab.getWebView() : null; return mCurrentTab != null ? mCurrentTab.getWebView() : null;
} }
@ -200,7 +209,7 @@ public class TabsManager {
* @return * @return
*/ */
@Nullable @Nullable
public LightningView getCurrentTab() { public synchronized LightningView getCurrentTab() {
return mCurrentTab; return mCurrentTab;
} }
@ -212,8 +221,9 @@ public class TabsManager {
* @return the selected tab or null if position is out of tabs range * @return the selected tab or null if position is out of tabs range
*/ */
@Nullable @Nullable
public LightningView switchToTab(final int position) { public synchronized LightningView switchToTab(final int position) {
if (position < 0 || position >= mWebViewList.size()) { if (position < 0 || position >= mWebViewList.size()) {
Log.e(TAG, "Returning a null LightningView requested for position: " + position);
return null; return null;
} else { } else {
final LightningView tab = mWebViewList.get(position); final LightningView tab = mWebViewList.get(position);

8
app/src/main/java/acr/browser/lightning/controller/BrowserController.java → app/src/main/java/acr/browser/lightning/controller/UIController.java

@ -16,18 +16,20 @@ import android.webkit.WebChromeClient.CustomViewCallback;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
public interface BrowserController { public interface UIController {
void changeToolbarBackground(@NonNull Bitmap favicon, @Nullable Drawable drawable); void changeToolbarBackground(@NonNull Bitmap favicon, @Nullable Drawable drawable);
@ColorInt @ColorInt
int getUiColor(); int getUiColor();
void updateUrl(String title, boolean shortUrl); boolean getUseDarkTheme();
void updateUrl(@Nullable String title, boolean shortUrl);
void updateProgress(int n); void updateProgress(int n);
void updateHistory(String title, String url); void updateHistory(@Nullable String title, @NonNull String url);
void openFileChooser(ValueCallback<Uri> uploadMsg); void openFileChooser(ValueCallback<Uri> uploadMsg);

55
app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java

@ -8,6 +8,8 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -38,10 +40,6 @@ public class HistoryDatabase extends SQLiteOpenHelper {
private SQLiteDatabase mDatabase; private SQLiteDatabase mDatabase;
private static HistoryDatabase mInstance;
private boolean mLock;
@Inject @Inject
public HistoryDatabase(Context context) { public HistoryDatabase(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION); super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
@ -66,23 +64,21 @@ public class HistoryDatabase extends SQLiteOpenHelper {
onCreate(db); onCreate(db);
} }
public void deleteHistory() { public synchronized void deleteHistory() {
mDatabase.delete(TABLE_HISTORY, null, null); mDatabase.delete(TABLE_HISTORY, null, null);
mDatabase.close(); mDatabase.close();
mDatabase = this.getWritableDatabase(); mDatabase = this.getWritableDatabase();
} }
private boolean isClosed() { private synchronized boolean isClosed() {
return mDatabase == null || !mDatabase.isOpen(); return mDatabase == null || !mDatabase.isOpen();
} }
@Override @Override
public synchronized void close() { public synchronized void close() {
if (!mLock) { if (mDatabase != null) {
if (mDatabase != null) { mDatabase.close();
mDatabase.close(); mDatabase = null;
mDatabase = null;
}
} }
super.close(); super.close();
} }
@ -94,42 +90,35 @@ public class HistoryDatabase extends SQLiteOpenHelper {
} }
public synchronized void deleteHistoryItem(String url) { public synchronized void deleteHistoryItem(String url) {
mLock = true;
openIfNecessary(); openIfNecessary();
mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url}); mDatabase.delete(TABLE_HISTORY, KEY_URL + " = ?", new String[]{url});
mLock = false;
} }
public synchronized void visitHistoryItem(String url, String title) { public synchronized void visitHistoryItem(@NonNull String url, @Nullable String title) {
mLock = true;
openIfNecessary(); openIfNecessary();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_TITLE, title); values.put(KEY_TITLE, title == null ? "" : title);
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); values.put(KEY_TIME_VISITED, System.currentTimeMillis());
Cursor q = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL}, Cursor q = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL},
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
if (q.getCount() > 0) { if (q.getCount() > 0) {
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url});
} else { } else {
addHistoryItem(new HistoryItem(url, title)); addHistoryItem(new HistoryItem(url, title == null ? "" : title));
} }
q.close(); q.close();
mLock = false;
} }
private synchronized void addHistoryItem(HistoryItem item) { private synchronized void addHistoryItem(@NonNull HistoryItem item) {
mLock = true;
openIfNecessary(); openIfNecessary();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(KEY_URL, item.getUrl()); values.put(KEY_URL, item.getUrl());
values.put(KEY_TITLE, item.getTitle()); values.put(KEY_TITLE, item.getTitle());
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); values.put(KEY_TIME_VISITED, System.currentTimeMillis());
mDatabase.insert(TABLE_HISTORY, null, values); mDatabase.insert(TABLE_HISTORY, null, values);
mLock = false;
} }
String getHistoryItem(String url) { synchronized String getHistoryItem(String url) {
mLock = true;
openIfNecessary(); openIfNecessary();
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE}, Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE},
KEY_URL + " = ?", new String[]{url}, null, null, null, null); KEY_URL + " = ?", new String[]{url}, null, null, null, null);
@ -140,14 +129,15 @@ public class HistoryDatabase extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
mLock = false;
return m; return m;
} }
public List<HistoryItem> findItemsContaining(String search) { public synchronized List<HistoryItem> findItemsContaining(@Nullable String search) {
mLock = true;
openIfNecessary(); openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>(5); List<HistoryItem> itemList = new ArrayList<>(5);
if (search == null) {
return itemList;
}
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%" String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%"
+ search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY " + search + "%' OR " + KEY_URL + " LIKE '%" + search + "%' " + "ORDER BY "
+ KEY_TIME_VISITED + " DESC LIMIT 5"; + KEY_TIME_VISITED + " DESC LIMIT 5";
@ -165,12 +155,10 @@ public class HistoryDatabase extends SQLiteOpenHelper {
} while (cursor.moveToNext() && n < 5); } while (cursor.moveToNext() && n < 5);
} }
cursor.close(); cursor.close();
mLock = false;
return itemList; return itemList;
} }
public List<HistoryItem> getLastHundredItems() { public synchronized List<HistoryItem> getLastHundredItems() {
mLock = true;
openIfNecessary(); openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>(100); List<HistoryItem> itemList = new ArrayList<>(100);
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED
@ -189,12 +177,10 @@ public class HistoryDatabase extends SQLiteOpenHelper {
} while (cursor.moveToNext() && counter < 100); } while (cursor.moveToNext() && counter < 100);
} }
cursor.close(); cursor.close();
mLock = false;
return itemList; return itemList;
} }
public List<HistoryItem> getAllHistoryItems() { public synchronized List<HistoryItem> getAllHistoryItems() {
mLock = true;
openIfNecessary(); openIfNecessary();
List<HistoryItem> itemList = new ArrayList<>(); List<HistoryItem> itemList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " ORDER BY " + KEY_TIME_VISITED
@ -212,18 +198,15 @@ public class HistoryDatabase extends SQLiteOpenHelper {
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
mLock = false;
return itemList; return itemList;
} }
public int getHistoryItemsCount() { public synchronized int getHistoryItemsCount() {
mLock = true;
openIfNecessary(); openIfNecessary();
String countQuery = "SELECT * FROM " + TABLE_HISTORY; String countQuery = "SELECT * FROM " + TABLE_HISTORY;
Cursor cursor = mDatabase.rawQuery(countQuery, null); Cursor cursor = mDatabase.rawQuery(countQuery, null);
int n = cursor.getCount(); int n = cursor.getCount();
cursor.close(); cursor.close();
mLock = false;
return n; return n;
} }
} }

6
app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

@ -40,7 +40,7 @@ import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.bus.NavigationEvents; import acr.browser.lightning.bus.NavigationEvents;
import acr.browser.lightning.bus.TabEvents; import acr.browser.lightning.bus.TabEvents;
import acr.browser.lightning.controller.BrowserController; import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@ -71,7 +71,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
private RecyclerView mRecyclerView; private RecyclerView mRecyclerView;
private LightningViewAdapter mTabsAdapter; private LightningViewAdapter mTabsAdapter;
private BrowserController mUiController; private UIController mUiController;
@Inject @Inject
TabsManager tabsManager; TabsManager tabsManager;
@ -91,7 +91,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Bundle arguments = getArguments(); final Bundle arguments = getArguments();
final Context context = getContext(); final Context context = getContext();
mUiController = (BrowserController) getActivity(); mUiController = (UIController) getActivity();
mIsIncognito = arguments.getBoolean(IS_INCOGNITO, false); mIsIncognito = arguments.getBoolean(IS_INCOGNITO, false);
mShowInNavigationDrawer = arguments.getBoolean(VERTICAL_MODE, true); mShowInNavigationDrawer = arguments.getBoolean(VERTICAL_MODE, true);
mDarkTheme = mPreferences.getUseTheme() != 0 || mIsIncognito; mDarkTheme = mPreferences.getUseTheme() != 0 || mIsIncognito;

34
app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java

@ -1,6 +1,7 @@
package acr.browser.lightning.view; package acr.browser.lightning.view;
import android.Manifest; import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -23,10 +24,10 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.utils.PermissionsManager; import acr.browser.lightning.utils.PermissionsManager;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@ -38,12 +39,14 @@ class LightningChromeClient extends WebChromeClient {
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
private final BrowserActivity mActivity; private final Activity mActivity;
private final LightningView mLightningView; private final LightningView mLightningView;
private final UIController mUIController;
private final Bus eventBus; private final Bus eventBus;
LightningChromeClient(BrowserActivity activity, LightningView lightningView) { LightningChromeClient(Activity activity, LightningView lightningView) {
mActivity = activity; mActivity = activity;
mUIController = (UIController) activity;
mLightningView = lightningView; mLightningView = lightningView;
eventBus = BrowserApp.getAppComponent().getBus(); eventBus = BrowserApp.getAppComponent().getBus();
} }
@ -51,7 +54,7 @@ class LightningChromeClient extends WebChromeClient {
@Override @Override
public void onProgressChanged(WebView view, int newProgress) { public void onProgressChanged(WebView view, int newProgress) {
if (mLightningView.isShown()) { if (mLightningView.isShown()) {
mActivity.updateProgress(newProgress); mUIController.updateProgress(newProgress);
} }
} }
@ -103,7 +106,7 @@ class LightningChromeClient extends WebChromeClient {
} }
eventBus.post(new BrowserEvents.TabsChanged()); eventBus.post(new BrowserEvents.TabsChanged());
if (view != null) { if (view != null) {
mActivity.updateHistory(title, view.getUrl()); mUIController.updateHistory(title, view.getUrl());
} }
} }
@ -152,30 +155,33 @@ class LightningChromeClient extends WebChromeClient {
@Override @Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture,
Message resultMsg) { Message resultMsg) {
mActivity.onCreateWindow(resultMsg); mUIController.onCreateWindow(resultMsg);
return true; return true;
} }
@Override @Override
public void onCloseWindow(WebView window) { public void onCloseWindow(WebView window) {
mActivity.onCloseWindow(mLightningView); mUIController.onCloseWindow(mLightningView);
} }
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) { public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mActivity.openFileChooser(uploadMsg); mUIController.openFileChooser(uploadMsg);
} }
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mActivity.openFileChooser(uploadMsg); mUIController.openFileChooser(uploadMsg);
} }
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mActivity.openFileChooser(uploadMsg); mUIController.openFileChooser(uploadMsg);
} }
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) { WebChromeClient.FileChooserParams fileChooserParams) {
mActivity.showFileChooser(filePathCallback); mUIController.showFileChooser(filePathCallback);
return true; return true;
} }
@ -206,7 +212,7 @@ class LightningChromeClient extends WebChromeClient {
@Override @Override
public void onHideCustomView() { public void onHideCustomView() {
mActivity.onHideCustomView(); mUIController.onHideCustomView();
super.onHideCustomView(); super.onHideCustomView();
} }
@ -224,7 +230,7 @@ class LightningChromeClient extends WebChromeClient {
// video.setVisibility(View.GONE); // video.setVisibility(View.GONE);
// } // }
// } else { // } else {
mActivity.onShowCustomView(view, callback); mUIController.onShowCustomView(view, callback);
// } // }
@ -247,7 +253,7 @@ class LightningChromeClient extends WebChromeClient {
// video.setVisibility(View.GONE); // video.setVisibility(View.GONE);
// } // }
// } else { // } else {
mActivity.onShowCustomView(view, callback); mUIController.onShowCustomView(view, callback);
// } // }

19
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -5,6 +5,7 @@
package acr.browser.lightning.view; package acr.browser.lightning.view;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
@ -40,12 +41,12 @@ import java.lang.ref.WeakReference;
import javax.inject.Inject; import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
@ -58,8 +59,9 @@ public class LightningView {
final LightningViewTitle mTitle; final LightningViewTitle mTitle;
private WebView mWebView; private WebView mWebView;
final boolean mIsIncognitoTab; final boolean mIsIncognitoTab;
private final UIController mUIController;
private final GestureDetector mGestureDetector; private final GestureDetector mGestureDetector;
private final BrowserActivity mActivity; private final Activity mActivity;
private static String mHomepage; private static String mHomepage;
private static String mDefaultUserAgent; private static String mDefaultUserAgent;
private final Paint mPaint = new Paint(); private final Paint mPaint = new Paint();
@ -87,12 +89,13 @@ public class LightningView {
LightningDialogBuilder mBookmarksDialogBuilder; LightningDialogBuilder mBookmarksDialogBuilder;
@SuppressLint("NewApi") @SuppressLint("NewApi")
public LightningView(BrowserActivity activity, String url, boolean darkTheme, boolean isIncognito) { public LightningView(Activity activity, String url, boolean isIncognito) {
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);
mActivity = activity; mActivity = activity;
mUIController = (UIController) activity;
mWebView = new WebView(activity); mWebView = new WebView(activity);
mIsIncognitoTab = isIncognito; mIsIncognitoTab = isIncognito;
mTitle = new LightningViewTitle(activity, darkTheme); mTitle = new LightningViewTitle(activity, mUIController.getUseDarkTheme());
mMaxFling = ViewConfiguration.get(activity).getScaledMaximumFlingVelocity(); mMaxFling = ViewConfiguration.get(activity).getScaledMaximumFlingVelocity();
@ -710,9 +713,9 @@ public class LightningView {
} else if (mAction == MotionEvent.ACTION_UP) { } else if (mAction == MotionEvent.ACTION_UP) {
final float distance = (mY - mLocation); final float distance = (mY - mLocation);
if (distance > SCROLL_UP_THRESHOLD && view.getScrollY() < SCROLL_UP_THRESHOLD) { if (distance > SCROLL_UP_THRESHOLD && view.getScrollY() < SCROLL_UP_THRESHOLD) {
mActivity.showActionBar(); mUIController.showActionBar();
} else if (distance < -SCROLL_UP_THRESHOLD) { } else if (distance < -SCROLL_UP_THRESHOLD) {
mActivity.hideActionBar(); mUIController.hideActionBar();
} }
mLocation = 0; mLocation = 0;
} }
@ -727,9 +730,9 @@ public class LightningView {
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int power = (int) (velocityY * 100 / mMaxFling); int power = (int) (velocityY * 100 / mMaxFling);
if (power < -10) { if (power < -10) {
mActivity.hideActionBar(); mUIController.hideActionBar();
} else if (power > 15) { } else if (power > 15) {
mActivity.showActionBar(); mUIController.showActionBar();
} }
return super.onFling(e1, e2, velocityX, velocityY); return super.onFling(e1, e2, velocityX, velocityY);
} }

15
app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

@ -1,6 +1,7 @@
package acr.browser.lightning.view; package acr.browser.lightning.view;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -31,10 +32,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.activity.BrowserActivity;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.AdBlock;
import acr.browser.lightning.utils.IntentUtils; import acr.browser.lightning.utils.IntentUtils;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
@ -46,14 +47,16 @@ import acr.browser.lightning.utils.Utils;
*/ */
class LightningWebClient extends WebViewClient { class LightningWebClient extends WebViewClient {
private final BrowserActivity mActivity; private final Activity mActivity;
private final LightningView mLightningView; private final LightningView mLightningView;
private final UIController mUIController;
private final AdBlock mAdBlock; private final AdBlock mAdBlock;
private final Bus mEventBus; private final Bus mEventBus;
private final IntentUtils mIntentUtils; private final IntentUtils mIntentUtils;
LightningWebClient(BrowserActivity activity, LightningView lightningView) { LightningWebClient(Activity activity, LightningView lightningView) {
mActivity = activity; mActivity = activity;
mUIController = (UIController) activity;
mLightningView = lightningView; mLightningView = lightningView;
mAdBlock = AdBlock.getInstance(activity); mAdBlock = AdBlock.getInstance(activity);
mAdBlock.updatePreference(); mAdBlock.updatePreference();
@ -86,7 +89,7 @@ class LightningWebClient extends WebViewClient {
@Override @Override
public void onPageFinished(WebView view, String url) { public void onPageFinished(WebView view, String url) {
if (view.isShown()) { if (view.isShown()) {
mActivity.updateUrl(url, true); mUIController.updateUrl(url, true);
view.postInvalidate(); view.postInvalidate();
} }
if (view.getTitle() == null || view.getTitle().isEmpty()) { if (view.getTitle() == null || view.getTitle().isEmpty()) {
@ -105,8 +108,8 @@ class LightningWebClient extends WebViewClient {
public void onPageStarted(WebView view, String url, Bitmap favicon) { public void onPageStarted(WebView view, String url, Bitmap favicon) {
mLightningView.mTitle.setFavicon(null); mLightningView.mTitle.setFavicon(null);
if (mLightningView.isShown()) { if (mLightningView.isShown()) {
mActivity.updateUrl(url, false); mUIController.updateUrl(url, false);
mActivity.showActionBar(); mUIController.showActionBar();
} }
mEventBus.post(new BrowserEvents.TabsChanged()); mEventBus.post(new BrowserEvents.TabsChanged());
} }

Loading…
Cancel
Save