From 7661ea35eebaf9146636425122c3457c64420da3 Mon Sep 17 00:00:00 2001 From: Stefano Pacifici Date: Tue, 15 Sep 2015 14:24:31 +0200 Subject: [PATCH] In the middle of events rewiring (back/forward) --- .../lightning/activity/BrowserActivity.java | 76 ++++++++++--------- .../lightning/activity/TabsManager.java | 9 +-- .../lightning/bus/NavigationEvents.java | 25 ++++++ .../acr/browser/lightning/bus/TabEvents.java | 7 ++ .../lightning/fragment/TabsFragment.java | 49 +++++++++++- app/src/main/res/layout/activity_main.xml | 4 +- app/src/main/res/layout/tab_drawer.xml | 4 +- 7 files changed, 127 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index d235410..93a5221 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -235,8 +235,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mShowTabsInDrawer = mPreferences.getShowTabsInDrawer(!isTablet()); mActivity = this; - // TODO Stefano, check this - // mWebViewList.clear(); mClickHandler = new ClickHandler(this); mBrowserFrame = (FrameLayout) findViewById(R.id.content_frame); @@ -246,15 +244,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mUiLayout = (LinearLayout) findViewById(R.id.ui_layout); mProgressBar = (AnimatedProgressBar) findViewById(R.id.progress_view); - setupFrameLayoutButton(R.id.new_tab_button, R.id.icon_plus); mDrawerLeft = (FrameLayout) findViewById(R.id.left_drawer); // Drawer stutters otherwise mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerRight = (ViewGroup) findViewById(R.id.right_drawer); mDrawerRight.setLayerType(View.LAYER_TYPE_HARDWARE, null); - ImageView tabTitleImage = (ImageView) findViewById(R.id.plusIcon); - tabTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); +// TODO Probably should be moved to the TabsFragement +// ImageView tabTitleImage = (ImageView) findViewById(R.id.plusIcon); +// tabTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !mShowTabsInDrawer) { getWindow().setStatusBarColor(Color.BLACK); @@ -313,10 +311,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mProxyUtils = ProxyUtils.getInstance(); - setupFrameLayoutButton(R.id.action_back, R.id.icon_back); - setupFrameLayoutButton(R.id.action_forward, R.id.icon_forward); - setupFrameLayoutButton(R.id.action_toggle_desktop, R.id.icon_desktop); setupFrameLayoutButton(R.id.action_reading, R.id.icon_reading); + setupFrameLayoutButton(R.id.action_toggle_desktop, R.id.icon_desktop); // create the search EditText in the ToolBar mSearch = (AutoCompleteTextView) actionBar.getCustomView().findViewById(R.id.search); @@ -870,7 +866,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ private synchronized void showTab(final int position) { final LightningView currentView = tabsManager.getCurrentTab(); + final WebView currentWebView = currentView != null ? currentView.getWebView() : null; final LightningView newView = tabsManager.switchToTab(position); + final WebView newWebView = newView != null ? newView.getWebView() : null; // Set the background color so the color mode color doesn't show through mBrowserFrame.setBackgroundColor(mBackgroundColor); @@ -885,7 +883,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements currentView.setForegroundTab(false); currentView.onPause(); } - final WebView currentWebView = currentView.getWebView(); newView.setForegroundTab(true); if (currentWebView != null) { updateUrl(newView.getUrl(), true); @@ -895,7 +892,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements updateProgress(0); } - mBrowserFrame.addView(currentWebView, MATCH_PARENT); + mBrowserFrame.addView(newWebView, MATCH_PARENT); newView.requestFocus(); newView.onResume(); @@ -909,10 +906,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mToolbarLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); height = mToolbarLayout.getMeasuredHeight(); } - currentWebView.setTranslationY(translation + height); + newWebView.setTranslationY(translation + height); mToolbarLayout.setTranslationY(translation); } else { - currentWebView.setTranslationY(0); + newWebView.setTranslationY(0); } showActionBar(); @@ -2102,22 +2099,23 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements final LightningView currentTab = tabsManager.getCurrentTab(); final WebView currentWebView = currentTab.getWebView(); switch (v.getId()) { - case R.id.action_back: - if (currentTab != null) { - if (currentTab.canGoBack()) { - currentTab.goBack(); - } else { - deleteTab(tabsManager.positionOf(currentTab)); - } - } - break; - case R.id.action_forward: - if (currentTab != null) { - if (currentTab.canGoForward()) { - currentTab.goForward(); - } - } - break; +// TODO Remove this +// case R.id.action_back: +// if (currentTab != null) { +// if (currentTab.canGoBack()) { +// currentTab.goBack(); +// } else { +// deleteTab(tabsManager.positionOf(currentTab)); +// } +// } +// break; +// case R.id.action_forward: +// if (currentTab != null) { +// if (currentTab.canGoForward()) { +// currentTab.goForward(); +// } +// } +// break; case R.id.arrow_button: if (mSearch != null && mSearch.hasFocus()) { currentTab.requestFocus(); @@ -2127,9 +2125,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements currentTab.loadHomepage(); } break; - case R.id.new_tab_button: - newTab(null, true); - break; case R.id.button_next: currentWebView.findNext(false); break; @@ -2168,11 +2163,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements return true; } + // TODO Check if all the calls are relative to TabsFragement private void setupFrameLayoutButton(@IdRes int buttonId, @IdRes int imageId) { - View frameButton = findViewById(buttonId); + final View frameButton = findViewById(buttonId); + final ImageView buttonImage = (ImageView) findViewById(imageId); frameButton.setOnClickListener(this); frameButton.setOnLongClickListener(this); - ImageView buttonImage = (ImageView) findViewById(imageId); buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); } @@ -2219,7 +2215,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ @Subscribe public void loadBookmarkInNewTab(final BookmarkEvents.AsNewTab event) { - newTab(event.bookmark.getUrl(), true); + BrowserActivity.this.newTab(event.bookmark.getUrl(), true); mDrawerLayout.closeDrawers(); } @@ -2312,6 +2308,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements * * @param event contains the tab position in the tabs adapter */ + @Subscribe public void showTab(final TabEvents.ShowTab event) { BrowserActivity.this.showTab(event.position); } @@ -2321,8 +2318,19 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements * * @param event contains the tab position in the tabs adapter */ + @Subscribe public void showCloseDialog(final TabEvents.ShowCloseDialog event) { BrowserActivity.this.showCloseDialog(event.position); } + + /** + * The user wants to create a new tab + * + * @param event a marker + */ + @Subscribe + public void newTab(final TabEvents.NewTab event) { + BrowserActivity.this.newTab(null, true); + } }; } diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index 3f3a994..d4cf26b 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import javax.inject.Inject; +import javax.inject.Singleton; import acr.browser.lightning.controller.BrowserController; import acr.browser.lightning.view.LightningView; @@ -17,6 +18,7 @@ import acr.browser.lightning.view.LightningView; * @author Stefano Pacifici * @date 2015/09/14 */ +@Singleton public class TabsManager { private final List mWebViewList = new ArrayList<>(); @@ -205,11 +207,4 @@ public class TabsManager { return tab; } } - -// /** -// * TODO We should remove also this -// */ -// public void setCurrentTab(final LightningView tab) { -// mCurrentTab = tab; -// } } diff --git a/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java b/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java new file mode 100644 index 0000000..2bdaded --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java @@ -0,0 +1,25 @@ +package acr.browser.lightning.bus; + +/** + * @author Stefano Pacifici + * @date 2015/09/15 + */ +public class NavigationEvents { + private NavigationEvents() { + // No instances please + } + + /** + * Fired by {@link acr.browser.lightning.fragment.TabsFragment} when the user presses back + * button. + */ + public static class GoBack { + } + + /** + * Fired by {@link acr.browser.lightning.fragment.TabsFragment} when teh user presses forward + * button. + */ + public static class GoForward { + } +} diff --git a/app/src/main/java/acr/browser/lightning/bus/TabEvents.java b/app/src/main/java/acr/browser/lightning/bus/TabEvents.java index 5fd4118..f219da6 100644 --- a/app/src/main/java/acr/browser/lightning/bus/TabEvents.java +++ b/app/src/main/java/acr/browser/lightning/bus/TabEvents.java @@ -46,4 +46,11 @@ public final class TabEvents { this.position = position; } } + + /** + * Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user want to create a + * new tab. + */ + public static class NewTab { + } } diff --git a/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java index d42aeb3..7a2a30c 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java @@ -13,6 +13,8 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; +import android.support.annotation.IdRes; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewCompat; @@ -38,6 +40,7 @@ import acr.browser.lightning.R; import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.bus.BrowserEvents; +import acr.browser.lightning.bus.NavigationEvents; import acr.browser.lightning.bus.TabEvents; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.Utils; @@ -47,7 +50,7 @@ import acr.browser.lightning.view.LightningView; * @author Stefano Pacifici based on Anthony C. Restaino's code * @date 2015/09/14 */ -public class TabsFragment extends Fragment { +public class TabsFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener { private static final String TAG = TabsFragment.class.getSimpleName(); @@ -86,6 +89,11 @@ public class TabsFragment extends Fragment { if (vertical) { view = inflater.inflate(R.layout.tab_drawer, container, false); layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false); + // TODO Handle also long press + setupFrameLayoutButton(view, R.id.new_tab_button, R.id.icon_plus); + setupFrameLayoutButton(view, R.id.action_back, R.id.icon_back); + setupFrameLayoutButton(view, R.id.action_forward, R.id.icon_forward); + } else { view = inflater.inflate(R.layout.tab_strip, container, false); layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); @@ -98,6 +106,15 @@ public class TabsFragment extends Fragment { return view; } + private void setupFrameLayoutButton(@NonNull final View root, @IdRes final int buttonId, + @IdRes final int imageId) { + final View frameButton = root.findViewById(buttonId); + final ImageView buttonImage = (ImageView) root.findViewById(imageId); + frameButton.setOnClickListener(this); + frameButton.setOnLongClickListener(this); + buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); + } + @Override public void onDestroyView() { mRecyclerView = null; @@ -110,6 +127,13 @@ public class TabsFragment extends Fragment { bus.register(this); } + @Override + public void onResume() { + super.onResume(); + // Force adapter refresh + mTabsAdapter.notifyDataSetChanged(); + } + @Override public void onStop() { super.onStop(); @@ -123,6 +147,28 @@ public class TabsFragment extends Fragment { } } + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.new_tab_button: + bus.post(new TabEvents.NewTab()); + break; + case R.id.action_back: + bus.post(new NavigationEvents.GoBack()); + break; + case R.id.action_forward: + bus.post(new NavigationEvents.GoForward()); + break; + default: + break; + } + } + + @Override + public boolean onLongClick(View v) { + return false; + } + public class LightningViewAdapter extends RecyclerView.Adapter { private final int layoutResourceId; @@ -277,5 +323,4 @@ public class TabsFragment extends Fragment { } } } - } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 655df01..5f87668 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,7 +30,9 @@ android:layout_height="match_parent" android:background="?attr/drawerBackground" android:id="@+id/left_drawer" - android:fitsSystemWindows="true" /> + android:fitsSystemWindows="true" + android:layout_gravity="start" + android:weightSum="1" />