Browse Source

CurrentTab reference removed from BrowserActivity

master
Stefano Pacifici 9 years ago
parent
commit
f0c3b743d4
  1. 255
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 16
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java

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

@ -137,7 +137,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private RelativeLayout mSearchBar; private RelativeLayout mSearchBar;
// List // List
private LightningView mCurrentView; // private LightningView mCurrentView;
private WebView mWebView; private WebView mWebView;
// Views // Views
@ -387,8 +387,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSearch.getWindowToken(), 0); imm.hideSoftInputFromWindow(mSearch.getWindowToken(), 0);
searchTheWeb(mSearch.getText().toString()); searchTheWeb(mSearch.getText().toString());
if (mCurrentView != null) { final LightningView currentView = tabsManager.getCurrentTab();
mCurrentView.requestFocus(); if (currentView != null) {
currentView.requestFocus();
} }
return true; return true;
default: default:
@ -409,8 +410,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSearch.getWindowToken(), 0); imm.hideSoftInputFromWindow(mSearch.getWindowToken(), 0);
searchTheWeb(mSearch.getText().toString()); searchTheWeb(mSearch.getText().toString());
if (mCurrentView != null) { final LightningView currentView = tabsManager.getCurrentTab();
mCurrentView.requestFocus(); if (currentView != null) {
currentView.requestFocus();
} }
return true; return true;
} }
@ -419,15 +421,16 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onFocusChange(View v, final boolean hasFocus) { public void onFocusChange(View v, final boolean hasFocus) {
if (!hasFocus && mCurrentView != null) { final LightningView currentView = tabsManager.getCurrentTab();
if (mCurrentView.getProgress() < 100) { if (!hasFocus && currentView != null) {
if (currentView.getProgress() < 100) {
setIsLoading(); setIsLoading();
} else { } else {
setIsFinishedLoading(); setIsFinishedLoading();
} }
updateUrl(mCurrentView.getUrl(), true); updateUrl(currentView.getUrl(), true);
} else if (hasFocus) { } else if (hasFocus) {
String url = mCurrentView.getUrl(); String url = currentView.getUrl();
if (url.startsWith(Constants.FILE)) { if (url.startsWith(Constants.FILE)) {
mSearch.setText(""); mSearch.setText("");
} else { } else {
@ -616,6 +619,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
private void initializePreferences() { private void initializePreferences() {
final LightningView currentView = tabsManager.getCurrentTab();
if (mPreferences == null) { if (mPreferences == null) {
mPreferences = PreferenceManager.getInstance(); mPreferences = PreferenceManager.getInstance();
} }
@ -624,9 +628,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mColorMode &= !mDarkTheme; mColorMode &= !mDarkTheme;
if (!isIncognito() && !mColorMode && !mDarkTheme && mWebpageBitmap != null) { if (!isIncognito() && !mColorMode && !mDarkTheme && mWebpageBitmap != null) {
changeToolbarBackground(mWebpageBitmap, null); changeToolbarBackground(mWebpageBitmap, null);
} else if (!isIncognito() && mCurrentView != null && !mDarkTheme } else if (!isIncognito() && currentView != null && !mDarkTheme
&& mCurrentView.getFavicon() != null) { && currentView.getFavicon() != null) {
changeToolbarBackground(mCurrentView.getFavicon(), null); changeToolbarBackground(currentView.getFavicon(), null);
} }
if (mFullScreen) { if (mFullScreen) {
@ -729,6 +733,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
final LightningView currentView = tabsManager.getCurrentTab();
// Handle action buttons // Handle action buttons
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
@ -737,13 +742,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
return true; return true;
case R.id.action_back: case R.id.action_back:
if (mCurrentView != null && mCurrentView.canGoBack()) { if (currentView != null && currentView.canGoBack()) {
mCurrentView.goBack(); currentView.goBack();
} }
return true; return true;
case R.id.action_forward: case R.id.action_forward:
if (mCurrentView != null && mCurrentView.canGoForward()) { if (currentView != null && currentView.canGoForward()) {
mCurrentView.goForward(); currentView.goForward();
} }
return true; return true;
case R.id.action_new_tab: case R.id.action_new_tab:
@ -754,11 +759,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale); overridePendingTransition(R.anim.slide_up_in, R.anim.fade_out_scale);
return true; return true;
case R.id.action_share: case R.id.action_share:
if (mCurrentView != null && !mCurrentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) {
Intent shareIntent = new Intent(Intent.ACTION_SEND); Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, mCurrentView.getTitle()); shareIntent.putExtra(Intent.EXTRA_SUBJECT, currentView.getTitle());
shareIntent.putExtra(Intent.EXTRA_TEXT, mCurrentView.getUrl()); shareIntent.putExtra(Intent.EXTRA_TEXT, currentView.getUrl());
startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.dialog_title_share))); startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.dialog_title_share)));
} }
return true; return true;
@ -766,9 +771,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
openBookmarks(); openBookmarks();
return true; return true;
case R.id.action_copy: case R.id.action_copy:
if (mCurrentView != null && !mCurrentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", mCurrentView.getUrl()); ClipData clip = ClipData.newPlainText("label", currentView.getUrl());
clipboard.setPrimaryClip(clip); clipboard.setPrimaryClip(clip);
Utils.showSnackbar(mActivity, R.string.message_link_copied); Utils.showSnackbar(mActivity, R.string.message_link_copied);
} }
@ -780,9 +785,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
openHistory(); openHistory();
return true; return true;
case R.id.action_add_bookmark: case R.id.action_add_bookmark:
if (mCurrentView != null && !mCurrentView.getUrl().startsWith(Constants.FILE)) { if (currentView != null && !currentView.getUrl().startsWith(Constants.FILE)) {
eventBus.post(new BrowserEvents.AddBookmark(mCurrentView.getTitle(), eventBus.post(new BrowserEvents.AddBookmark(currentView.getTitle(),
mCurrentView.getUrl())); currentView.getUrl()));
} }
return true; return true;
case R.id.action_find: case R.id.action_find:
@ -790,7 +795,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return true; return true;
case R.id.action_reading_mode: case R.id.action_reading_mode:
Intent read = new Intent(this, ReadingActivity.class); Intent read = new Intent(this, ReadingActivity.class);
read.putExtra(Constants.LOAD_READING_URL, mCurrentView.getUrl()); read.putExtra(Constants.LOAD_READING_URL, currentView.getUrl());
startActivity(read); startActivity(read);
return true; return true;
default: default:
@ -822,8 +827,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
private void showSearchInterfaceBar(String text) { private void showSearchInterfaceBar(String text) {
if (mCurrentView != null) { final LightningView currentView = tabsManager.getCurrentTab();
mCurrentView.find(text); if (currentView != null) {
currentView.find(text);
} }
mSearchBar = (RelativeLayout) findViewById(R.id.search_bar); mSearchBar = (RelativeLayout) findViewById(R.id.search_bar);
mSearchBar.setVisibility(View.VISIBLE); mSearchBar.setVisibility(View.VISIBLE);
@ -876,9 +882,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final LightningView currentTab = tabsManager.getCurrentTab();
final int position = mDrawerListLeft.getChildAdapterPosition(v); final int position = mDrawerListLeft.getChildAdapterPosition(v);
final LightningView tab = tabsManager.getTabAtPosition(position); final LightningView tab = tabsManager.getTabAtPosition(position);
if (tab != null && mCurrentView != tab) { if (tab != null && currentTab != tab) {
mIsNewIntent = false; mIsNewIntent = false;
showTab(tab); showTab(tab);
} }
@ -902,34 +909,35 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* displays the WebView contained in the LightningView Also handles the * displays the WebView contained in the LightningView Also handles the
* removal of previous views * removal of previous views
* *
* @param view the LightningView to show * @param newView the LightningView to show
*/ */
private synchronized void showTab(LightningView view) { private synchronized void showTab(LightningView newView) {
final LightningView currentView = tabsManager.getCurrentTab();
// Set the background color so the color mode color doesn't show through // Set the background color so the color mode color doesn't show through
mBrowserFrame.setBackgroundColor(mBackgroundColor); mBrowserFrame.setBackgroundColor(mBackgroundColor);
if (view == null) { if (newView == null) {
return; return;
} }
final float translation = mToolbarLayout.getTranslationY(); final float translation = mToolbarLayout.getTranslationY();
mBrowserFrame.removeAllViews(); mBrowserFrame.removeAllViews();
if (mCurrentView != null) { if (currentView != null) {
mCurrentView.setForegroundTab(false); currentView.setForegroundTab(false);
mCurrentView.onPause(); currentView.onPause();
} }
mCurrentView = view; tabsManager.setCurrentTab(newView);
mWebView = view.getWebView(); mWebView = newView.getWebView();
mCurrentView.setForegroundTab(true); newView.setForegroundTab(true);
if (mWebView != null) { if (mWebView != null) {
updateUrl(mCurrentView.getUrl(), true); updateUrl(newView.getUrl(), true);
updateProgress(mCurrentView.getProgress()); updateProgress(newView.getProgress());
} else { } else {
updateUrl("", true); updateUrl("", true);
updateProgress(0); updateProgress(0);
} }
mBrowserFrame.addView(mWebView, MATCH_PARENT); mBrowserFrame.addView(mWebView, MATCH_PARENT);
mCurrentView.requestFocus(); newView.requestFocus();
mCurrentView.onResume(); newView.onResume();
if (mFullScreen) { if (mFullScreen) {
// mToolbarLayout has already been removed // mToolbarLayout has already been removed
@ -960,7 +968,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}, 200); }, 200);
// Should update the bookmark status in BookmarksFragment // Should update the bookmark status in BookmarksFragment
eventBus.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl())); eventBus.post(new BrowserEvents.CurrentPageUrl(newView.getUrl()));
// new Handler().postDelayed(new Runnable() { // new Handler().postDelayed(new Runnable() {
// @Override // @Override
@ -998,12 +1006,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
private void loadUrlInCurrentView(final String url) { private void loadUrlInCurrentView(final String url) {
if (mCurrentView == null) { final LightningView currentTab = tabsManager.getCurrentTab();
if (currentTab == null) {
// This is a problem, probably an assert will be better than a return // This is a problem, probably an assert will be better than a return
return; return;
} }
mCurrentView.loadUrl(url); currentTab.loadUrl(url);
eventBus.post(new BrowserEvents.CurrentPageUrl(url)); eventBus.post(new BrowserEvents.CurrentPageUrl(url));
} }
@ -1054,20 +1063,22 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
private synchronized void deleteTab(int position) { private synchronized void deleteTab(int position) {
final LightningView reference = tabsManager.getTabAtPosition(position); final LightningView tabToDelete = tabsManager.getTabAtPosition(position);
if (reference == null) { final LightningView currentTab = tabsManager.getCurrentTab();
if (tabToDelete == null) {
return; return;
} }
// What? // What?
int current = tabsManager.getPositionForTab(mCurrentView); int current = tabsManager.getPositionForTab(currentTab);
if (current < 0) { if (current < 0) {
return; return;
} }
if (!reference.getUrl().startsWith(Constants.FILE) && !isIncognito()) { if (!tabToDelete.getUrl().startsWith(Constants.FILE) && !isIncognito()) {
mPreferences.setSavedUrl(reference.getUrl()); mPreferences.setSavedUrl(tabToDelete.getUrl());
} }
final boolean isShown = reference.isShown(); final boolean isShown = tabToDelete.isShown();
if (isShown) { if (isShown) {
mBrowserFrame.setBackgroundColor(mBackgroundColor); mBrowserFrame.setBackgroundColor(mBackgroundColor);
} }
@ -1075,7 +1086,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
tabsManager.deleteTab(position); tabsManager.deleteTab(position);
showTab(tabsManager.getTabAtPosition(current - 1)); showTab(tabsManager.getTabAtPosition(current - 1));
updateTabs(); updateTabs();
reference.onDestroy(); tabToDelete.onDestroy();
} else if (tabsManager.size() > position + 1) { } else if (tabsManager.size() > position + 1) {
if (current == position) { if (current == position) {
showTab(tabsManager.getTabAtPosition(position + 1)); showTab(tabsManager.getTabAtPosition(position + 1));
@ -1086,7 +1097,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
tabsManager.deleteTab(position); tabsManager.deleteTab(position);
} }
reference.onDestroy(); tabToDelete.onDestroy();
} else if (tabsManager.size() > 1) { } else if (tabsManager.size() > 1) {
if (current == position) { if (current == position) {
showTab(tabsManager.getTabAtPosition(position - 1)); showTab(tabsManager.getTabAtPosition(position - 1));
@ -1097,16 +1108,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
tabsManager.deleteTab(position); tabsManager.deleteTab(position);
} }
reference.onDestroy(); tabToDelete.onDestroy();
} else { } else {
if (mCurrentView.getUrl().startsWith(Constants.FILE) || mCurrentView.getUrl().equals(mHomepage)) { if (currentTab.getUrl().startsWith(Constants.FILE) || currentTab.getUrl().equals(mHomepage)) {
closeActivity(); closeActivity();
} else { } else {
tabsManager.deleteTab(position); tabsManager.deleteTab(position);
performExitCleanUp(); performExitCleanUp();
reference.pauseTimers(); tabToDelete.pauseTimers();
reference.onDestroy(); tabToDelete.onDestroy();
mCurrentView = null;
mWebView = null; mWebView = null;
mTabAdapter.notifyDataSetChanged(); mTabAdapter.notifyDataSetChanged();
finish(); finish();
@ -1123,8 +1133,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
private void performExitCleanUp() { private void performExitCleanUp() {
if (mPreferences.getClearCacheExit() && mCurrentView != null && !isIncognito()) { final LightningView currentTab = tabsManager.getCurrentTab();
WebUtils.clearCache(mCurrentView.getWebView()); if (mPreferences.getClearCacheExit() && currentTab != null && !isIncognito()) {
WebUtils.clearCache(currentTab.getWebView());
Log.d(Constants.TAG, "Cache Cleared"); Log.d(Constants.TAG, "Cache Cleared");
} }
@ -1148,8 +1159,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) { public boolean onKeyLongPress(int keyCode, KeyEvent event) {
final LightningView currentTab = tabsManager.getCurrentTab();
if (keyCode == KeyEvent.KEYCODE_BACK) { if (keyCode == KeyEvent.KEYCODE_BACK) {
showCloseDialog(tabsManager.positionOf(mCurrentView)); showCloseDialog(tabsManager.positionOf(currentTab));
} }
return true; return true;
} }
@ -1157,7 +1169,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private void closeBrowser() { private void closeBrowser() {
mBrowserFrame.setBackgroundColor(mBackgroundColor); mBrowserFrame.setBackgroundColor(mBackgroundColor);
performExitCleanUp(); performExitCleanUp();
mCurrentView = null;
mWebView = null; mWebView = null;
tabsManager.shutdown(); tabsManager.shutdown();
mTabAdapter.notifyDataSetChanged(); mTabAdapter.notifyDataSetChanged();
@ -1166,24 +1177,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onBackPressed() { public void onBackPressed() {
final LightningView currentTab = tabsManager.getCurrentTab();
if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) { if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
mDrawerLayout.closeDrawer(mDrawerLeft); mDrawerLayout.closeDrawer(mDrawerLeft);
} else if (mDrawerLayout.isDrawerOpen(mDrawerRight)) { } else if (mDrawerLayout.isDrawerOpen(mDrawerRight)) {
eventBus eventBus
.post(new BrowserEvents.UserPressedBack()); .post(new BrowserEvents.UserPressedBack());
} else { } else {
if (mCurrentView != null) { if (currentTab != null) {
Log.d(Constants.TAG, "onBackPressed"); Log.d(Constants.TAG, "onBackPressed");
if (mSearch.hasFocus()) { if (mSearch.hasFocus()) {
mCurrentView.requestFocus(); currentTab.requestFocus();
} else if (mCurrentView.canGoBack()) { } else if (currentTab.canGoBack()) {
if (!mCurrentView.isShown()) { if (!currentTab.isShown()) {
onHideCustomView(); onHideCustomView();
} else { } else {
mCurrentView.goBack(); currentTab.goBack();
} }
} else { } else {
deleteTab(tabsManager.positionOf(mCurrentView)); deleteTab(tabsManager.positionOf(currentTab));
} }
} else { } else {
Log.e(Constants.TAG, "This shouldn't happen ever"); Log.e(Constants.TAG, "This shouldn't happen ever");
@ -1195,10 +1207,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
final LightningView currentTab = tabsManager.getCurrentTab();
Log.d(Constants.TAG, "onPause"); Log.d(Constants.TAG, "onPause");
if (mCurrentView != null) { if (currentTab != null) {
mCurrentView.pauseTimers(); currentTab.pauseTimers();
mCurrentView.onPause(); currentTab.onPause();
} }
try { try {
unregisterReceiver(mNetworkReceiver); unregisterReceiver(mNetworkReceiver);
@ -1244,14 +1257,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
final LightningView currentTab = tabsManager.getCurrentTab();
Log.d(Constants.TAG, "onResume"); Log.d(Constants.TAG, "onResume");
if (mSearchAdapter != null) { if (mSearchAdapter != null) {
mSearchAdapter.refreshPreferences(); mSearchAdapter.refreshPreferences();
mSearchAdapter.refreshBookmarks(); mSearchAdapter.refreshBookmarks();
} }
if (mCurrentView != null) { if (currentTab != null) {
mCurrentView.resumeTimers(); currentTab.resumeTimers();
mCurrentView.onResume(); currentTab.onResume();
} }
mHistoryDatabase = HistoryDatabase.getInstance(); mHistoryDatabase = HistoryDatabase.getInstance();
initializePreferences(); initializePreferences();
@ -1271,13 +1285,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* checks if it is a search, url, etc. * checks if it is a search, url, etc.
*/ */
private void searchTheWeb(@NonNull String query) { private void searchTheWeb(@NonNull String query) {
final LightningView currentTab = tabsManager.getCurrentTab();
if (query.isEmpty()) { if (query.isEmpty()) {
return; return;
} }
String searchUrl = mSearchText + UrlUtils.QUERY_PLACE_HOLDER; String searchUrl = mSearchText + UrlUtils.QUERY_PLACE_HOLDER;
query = query.trim(); query = query.trim();
mCurrentView.stopLoading(); currentTab.stopLoading();
if (mCurrentView != null) { if (currentTab != null) {
loadUrlInCurrentView(UrlUtils.smartUrlFilter(query, true, searchUrl)); loadUrlInCurrentView(UrlUtils.smartUrlFilter(query, true, searchUrl));
} }
} }
@ -1496,6 +1511,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void updateUrl(String url, boolean shortUrl) { public void updateUrl(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;
} }
@ -1511,8 +1527,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearch.setText(url); mSearch.setText(url);
break; break;
case 2: // Title, show the page's title case 2: // Title, show the page's title
if (mCurrentView != null && !mCurrentView.getTitle().isEmpty()) { if (currentTab != null && !currentTab.getTitle().isEmpty()) {
mSearch.setText(mCurrentView.getTitle()); mSearch.setText(currentTab.getTitle());
} else { } else {
mSearch.setText(mUntitledTitle); mSearch.setText(mUntitledTitle);
} }
@ -1564,7 +1580,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* previously searched URLs * previously searched URLs
*/ */
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) { private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {
final LightningView currentTab = tabsManager.getCurrentTab();
getUrl.setThreshold(1); getUrl.setThreshold(1);
getUrl.setDropDownWidth(-1); getUrl.setDropDownWidth(-1);
getUrl.setDropDownAnchor(R.id.toolbar_layout); getUrl.setDropDownAnchor(R.id.toolbar_layout);
@ -1583,8 +1599,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
searchTheWeb(url); searchTheWeb(url);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0); imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0);
if (mCurrentView != null) { if (currentTab != null) {
mCurrentView.requestFocus(); currentTab.requestFocus();
} }
} catch (NullPointerException e) { } catch (NullPointerException e) {
Log.e("Browser Error: ", "NullPointerException on item click"); Log.e("Browser Error: ", "NullPointerException on item click");
@ -1798,6 +1814,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onShowCustomView(View view, CustomViewCallback callback) { public void onShowCustomView(View view, CustomViewCallback callback) {
final LightningView currentTab = tabsManager.getCurrentTab();
if (view == null) { if (view == null) {
return; return;
} }
@ -1817,7 +1834,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mFullscreenContainer.addView(mCustomView, COVER_SCREEN_PARAMS); mFullscreenContainer.addView(mCustomView, COVER_SCREEN_PARAMS);
decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS); decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
setFullscreen(true, true); setFullscreen(true, true);
mCurrentView.setVisibility(View.GONE); currentTab.setVisibility(View.GONE);
if (view instanceof FrameLayout) { if (view instanceof FrameLayout) {
if (((FrameLayout) view).getFocusedChild() instanceof VideoView) { if (((FrameLayout) view).getFocusedChild() instanceof VideoView) {
mVideoView = (VideoView) ((FrameLayout) view).getFocusedChild(); mVideoView = (VideoView) ((FrameLayout) view).getFocusedChild();
@ -1830,11 +1847,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onHideCustomView() { public void onHideCustomView() {
if (mCustomView == null || mCustomViewCallback == null || mCurrentView == null) { final LightningView currentTab = tabsManager.getCurrentTab();
if (mCustomView == null || mCustomViewCallback == null || currentTab == null) {
return; return;
} }
Log.d(Constants.TAG, "onHideCustomView"); Log.d(Constants.TAG, "onHideCustomView");
mCurrentView.setVisibility(View.VISIBLE); currentTab.setVisibility(View.VISIBLE);
try { try {
mCustomView.setKeepScreenOn(false); mCustomView.setKeepScreenOn(false);
} catch (SecurityException e) { } catch (SecurityException e) {
@ -1984,6 +2002,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
@Override @Override
public void hideActionBar() { public void hideActionBar() {
final LightningView currentTab = tabsManager.getCurrentTab();
if (mFullScreen) { if (mFullScreen) {
if (mBrowserFrame.findViewById(R.id.toolbar_layout) == null) { if (mBrowserFrame.findViewById(R.id.toolbar_layout) == null) {
mUiLayout.removeView(mToolbarLayout); mUiLayout.removeView(mToolbarLayout);
@ -1993,7 +2012,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mToolbarLayout.setTranslationY(0); mToolbarLayout.setTranslationY(0);
mWebView.setTranslationY(mToolbarLayout.getHeight()); mWebView.setTranslationY(mToolbarLayout.getHeight());
} }
if (mToolbarLayout == null || mCurrentView == null) if (mToolbarLayout == null || currentTab == null)
return; return;
final int height = mToolbarLayout.getHeight(); final int height = mToolbarLayout.getHeight();
@ -2040,7 +2059,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mToolbarLayout.setTranslationY(0); mToolbarLayout.setTranslationY(0);
mWebView.setTranslationY(height); mWebView.setTranslationY(height);
} }
if (mCurrentView == null) final LightningView currentTab = tabsManager.getCurrentTab();
if (currentTab == null)
return; return;
final WebView view = mWebView; final WebView view = mWebView;
@ -2119,6 +2139,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
final LightningView currentTab = tabsManager.getCurrentTab();
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
newTab(url, false); newTab(url, false);
@ -2131,7 +2152,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
break; break;
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:
if (mCurrentView != null) { if (currentTab != null) {
loadUrlInCurrentView(url); loadUrlInCurrentView(url);
} }
break; break;
@ -2153,6 +2174,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
final LightningView currentTab = tabsManager.getCurrentTab();
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
newTab(url, false); newTab(url, false);
@ -2165,7 +2187,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:
if (API > 8) { if (API > 8) {
Utils.downloadFile(mActivity, url, Utils.downloadFile(mActivity, url,
mCurrentView.getUserAgent(), "attachment"); currentTab.getUserAgent(), "attachment");
} }
break; break;
} }
@ -2244,41 +2266,43 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* See setIsFinishedLoading and setIsLoading for displaying the correct icon * See setIsFinishedLoading and setIsLoading for displaying the correct icon
*/ */
private void refreshOrStop() { private void refreshOrStop() {
if (mCurrentView != null) { final LightningView currentTab = tabsManager.getCurrentTab();
if (mCurrentView.getProgress() < 100) { if (currentTab != null) {
mCurrentView.stopLoading(); if (currentTab.getProgress() < 100) {
currentTab.stopLoading();
} else { } else {
mCurrentView.reload(); currentTab.reload();
} }
} }
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final LightningView currentTab = tabsManager.getCurrentTab();
switch (v.getId()) { switch (v.getId()) {
case R.id.action_back: case R.id.action_back:
if (mCurrentView != null) { if (currentTab != null) {
if (mCurrentView.canGoBack()) { if (currentTab.canGoBack()) {
mCurrentView.goBack(); currentTab.goBack();
} else { } else {
deleteTab(tabsManager.positionOf(mCurrentView)); deleteTab(tabsManager.positionOf(currentTab));
} }
} }
break; break;
case R.id.action_forward: case R.id.action_forward:
if (mCurrentView != null) { if (currentTab != null) {
if (mCurrentView.canGoForward()) { if (currentTab.canGoForward()) {
mCurrentView.goForward(); currentTab.goForward();
} }
} }
break; break;
case R.id.arrow_button: case R.id.arrow_button:
if (mSearch != null && mSearch.hasFocus()) { if (mSearch != null && mSearch.hasFocus()) {
mCurrentView.requestFocus(); currentTab.requestFocus();
} else if (mShowTabsInDrawer) { } else if (mShowTabsInDrawer) {
mDrawerLayout.openDrawer(mDrawerLeft); mDrawerLayout.openDrawer(mDrawerLeft);
} else if (mCurrentView != null) { } else if (currentTab != null) {
mCurrentView.loadHomepage(); currentTab.loadHomepage();
} }
break; break;
case R.id.new_tab_button: case R.id.new_tab_button:
@ -2296,12 +2320,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
break; break;
case R.id.action_reading: case R.id.action_reading:
Intent read = new Intent(this, ReadingActivity.class); Intent read = new Intent(this, ReadingActivity.class);
read.putExtra(Constants.LOAD_READING_URL, mCurrentView.getUrl()); read.putExtra(Constants.LOAD_READING_URL, currentTab.getUrl());
startActivity(read); startActivity(read);
break; break;
case R.id.action_toggle_desktop: case R.id.action_toggle_desktop:
mCurrentView.toggleDesktopUA(this); currentTab.toggleDesktopUA(this);
mCurrentView.reload(); currentTab.reload();
closeDrawers(); closeDrawers();
break; break;
} }
@ -2386,8 +2410,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
@Subscribe @Subscribe
public void bookmarkCurrentPage(final BookmarkEvents.WantToBookmarkCurrentPage event) { public void bookmarkCurrentPage(final BookmarkEvents.WantToBookmarkCurrentPage event) {
if (mCurrentView != null) { final LightningView currentTab = tabsManager.getCurrentTab();
eventBus.post(new BrowserEvents.AddBookmark(mCurrentView.getTitle(), mCurrentView.getUrl())); if (currentTab != null) {
eventBus.post(new BrowserEvents.AddBookmark(currentTab.getTitle(), currentTab.getUrl()));
} }
} }
@ -2409,12 +2434,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
@Subscribe @Subscribe
public void bookmarkChanged(final BookmarkEvents.BookmarkChanged event) { public void bookmarkChanged(final BookmarkEvents.BookmarkChanged event) {
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE) final LightningView currentTab = tabsManager.getCurrentTab();
&& mCurrentView.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) { if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE)
&& currentTab.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(mWebView); openBookmarkPage(mWebView);
} }
if (mCurrentView != null) { if (currentTab != null) {
eventBus.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl())); eventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));
} }
} }
@ -2425,12 +2451,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
@Subscribe @Subscribe
public void bookmarkDeleted(final BookmarkEvents.Deleted event) { public void bookmarkDeleted(final BookmarkEvents.Deleted event) {
if (mCurrentView != null && mCurrentView.getUrl().startsWith(Constants.FILE) final LightningView currentTab = tabsManager.getCurrentTab();
&& mCurrentView.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) { if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE)
&& currentTab.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(mWebView); openBookmarkPage(mWebView);
} }
if (mCurrentView != null) { if (currentTab != null) {
eventBus.post(new BrowserEvents.CurrentPageUrl(mCurrentView.getUrl())); eventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));
} }
} }

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

@ -20,6 +20,7 @@ import acr.browser.lightning.view.LightningView;
public class TabsManager { public class TabsManager {
private final List<LightningView> mWebViewList = new ArrayList<>(); private final List<LightningView> mWebViewList = new ArrayList<>();
private LightningView mCurrentTab;
@Inject @Inject
public TabsManager() { public TabsManager() {
@ -169,4 +170,19 @@ public class TabsManager {
public int getPositionForTab(final LightningView tab) { public int getPositionForTab(final LightningView tab) {
return mWebViewList.indexOf(tab); return mWebViewList.indexOf(tab);
} }
/**
* TODO We should remove also this
* @return
*/
public LightningView getCurrentTab() {
return mCurrentTab;
}
/**
* TODO We should remove also this
*/
public void setCurrentTab(final LightningView tab) {
mCurrentTab = tab;
}
} }

Loading…
Cancel
Save