Browse Source

In the middle of events rewiring (back/forward)

master
Stefano Pacifici 9 years ago
parent
commit
7661ea35ee
  1. 76
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 9
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  3. 25
      app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java
  4. 7
      app/src/main/java/acr/browser/lightning/bus/TabEvents.java
  5. 49
      app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java
  6. 4
      app/src/main/res/layout/activity_main.xml
  7. 4
      app/src/main/res/layout/tab_drawer.xml

76
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()); mShowTabsInDrawer = mPreferences.getShowTabsInDrawer(!isTablet());
mActivity = this; mActivity = this;
// TODO Stefano, check this
// mWebViewList.clear();
mClickHandler = new ClickHandler(this); mClickHandler = new ClickHandler(this);
mBrowserFrame = (FrameLayout) findViewById(R.id.content_frame); 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); mUiLayout = (LinearLayout) findViewById(R.id.ui_layout);
mProgressBar = (AnimatedProgressBar) findViewById(R.id.progress_view); mProgressBar = (AnimatedProgressBar) findViewById(R.id.progress_view);
setupFrameLayoutButton(R.id.new_tab_button, R.id.icon_plus);
mDrawerLeft = (FrameLayout) findViewById(R.id.left_drawer); mDrawerLeft = (FrameLayout) findViewById(R.id.left_drawer);
// Drawer stutters otherwise // Drawer stutters otherwise
mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null); mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerRight = (ViewGroup) findViewById(R.id.right_drawer); mDrawerRight = (ViewGroup) findViewById(R.id.right_drawer);
mDrawerRight.setLayerType(View.LAYER_TYPE_HARDWARE, null); mDrawerRight.setLayerType(View.LAYER_TYPE_HARDWARE, null);
ImageView tabTitleImage = (ImageView) findViewById(R.id.plusIcon); // TODO Probably should be moved to the TabsFragement
tabTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); // ImageView tabTitleImage = (ImageView) findViewById(R.id.plusIcon);
// tabTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !mShowTabsInDrawer) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !mShowTabsInDrawer) {
getWindow().setStatusBarColor(Color.BLACK); getWindow().setStatusBarColor(Color.BLACK);
@ -313,10 +311,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mProxyUtils = ProxyUtils.getInstance(); 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_reading, R.id.icon_reading);
setupFrameLayoutButton(R.id.action_toggle_desktop, R.id.icon_desktop);
// create the search EditText in the ToolBar // create the search EditText in the ToolBar
mSearch = (AutoCompleteTextView) actionBar.getCustomView().findViewById(R.id.search); 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) { private synchronized void showTab(final int position) {
final LightningView currentView = tabsManager.getCurrentTab(); final LightningView currentView = tabsManager.getCurrentTab();
final WebView currentWebView = currentView != null ? currentView.getWebView() : null;
final LightningView newView = tabsManager.switchToTab(position); 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 // Set the background color so the color mode color doesn't show through
mBrowserFrame.setBackgroundColor(mBackgroundColor); mBrowserFrame.setBackgroundColor(mBackgroundColor);
@ -885,7 +883,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
currentView.setForegroundTab(false); currentView.setForegroundTab(false);
currentView.onPause(); currentView.onPause();
} }
final WebView currentWebView = currentView.getWebView();
newView.setForegroundTab(true); newView.setForegroundTab(true);
if (currentWebView != null) { if (currentWebView != null) {
updateUrl(newView.getUrl(), true); updateUrl(newView.getUrl(), true);
@ -895,7 +892,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
updateProgress(0); updateProgress(0);
} }
mBrowserFrame.addView(currentWebView, MATCH_PARENT); mBrowserFrame.addView(newWebView, MATCH_PARENT);
newView.requestFocus(); newView.requestFocus();
newView.onResume(); newView.onResume();
@ -909,10 +906,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mToolbarLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); mToolbarLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
height = mToolbarLayout.getMeasuredHeight(); height = mToolbarLayout.getMeasuredHeight();
} }
currentWebView.setTranslationY(translation + height); newWebView.setTranslationY(translation + height);
mToolbarLayout.setTranslationY(translation); mToolbarLayout.setTranslationY(translation);
} else { } else {
currentWebView.setTranslationY(0); newWebView.setTranslationY(0);
} }
showActionBar(); showActionBar();
@ -2102,22 +2099,23 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
final LightningView currentTab = tabsManager.getCurrentTab(); final LightningView currentTab = tabsManager.getCurrentTab();
final WebView currentWebView = currentTab.getWebView(); final WebView currentWebView = currentTab.getWebView();
switch (v.getId()) { switch (v.getId()) {
case R.id.action_back: // TODO Remove this
if (currentTab != null) { // case R.id.action_back:
if (currentTab.canGoBack()) { // if (currentTab != null) {
currentTab.goBack(); // if (currentTab.canGoBack()) {
} else { // currentTab.goBack();
deleteTab(tabsManager.positionOf(currentTab)); // } else {
} // deleteTab(tabsManager.positionOf(currentTab));
} // }
break; // }
case R.id.action_forward: // break;
if (currentTab != null) { // case R.id.action_forward:
if (currentTab.canGoForward()) { // if (currentTab != null) {
currentTab.goForward(); // if (currentTab.canGoForward()) {
} // currentTab.goForward();
} // }
break; // }
// break;
case R.id.arrow_button: case R.id.arrow_button:
if (mSearch != null && mSearch.hasFocus()) { if (mSearch != null && mSearch.hasFocus()) {
currentTab.requestFocus(); currentTab.requestFocus();
@ -2127,9 +2125,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
currentTab.loadHomepage(); currentTab.loadHomepage();
} }
break; break;
case R.id.new_tab_button:
newTab(null, true);
break;
case R.id.button_next: case R.id.button_next:
currentWebView.findNext(false); currentWebView.findNext(false);
break; break;
@ -2168,11 +2163,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return true; return true;
} }
// TODO Check if all the calls are relative to TabsFragement
private void setupFrameLayoutButton(@IdRes int buttonId, @IdRes int imageId) { 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.setOnClickListener(this);
frameButton.setOnLongClickListener(this); frameButton.setOnLongClickListener(this);
ImageView buttonImage = (ImageView) findViewById(imageId);
buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
} }
@ -2219,7 +2215,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
@Subscribe @Subscribe
public void loadBookmarkInNewTab(final BookmarkEvents.AsNewTab event) { public void loadBookmarkInNewTab(final BookmarkEvents.AsNewTab event) {
newTab(event.bookmark.getUrl(), true); BrowserActivity.this.newTab(event.bookmark.getUrl(), true);
mDrawerLayout.closeDrawers(); mDrawerLayout.closeDrawers();
} }
@ -2312,6 +2308,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* *
* @param event contains the tab position in the tabs adapter * @param event contains the tab position in the tabs adapter
*/ */
@Subscribe
public void showTab(final TabEvents.ShowTab event) { public void showTab(final TabEvents.ShowTab event) {
BrowserActivity.this.showTab(event.position); 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 * @param event contains the tab position in the tabs adapter
*/ */
@Subscribe
public void showCloseDialog(final TabEvents.ShowCloseDialog event) { public void showCloseDialog(final TabEvents.ShowCloseDialog event) {
BrowserActivity.this.showCloseDialog(event.position); 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);
}
}; };
} }

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

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.controller.BrowserController; import acr.browser.lightning.controller.BrowserController;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
@ -17,6 +18,7 @@ import acr.browser.lightning.view.LightningView;
* @author Stefano Pacifici * @author Stefano Pacifici
* @date 2015/09/14 * @date 2015/09/14
*/ */
@Singleton
public class TabsManager { public class TabsManager {
private final List<LightningView> mWebViewList = new ArrayList<>(); private final List<LightningView> mWebViewList = new ArrayList<>();
@ -205,11 +207,4 @@ public class TabsManager {
return tab; return tab;
} }
} }
// /**
// * TODO We should remove also this
// */
// public void setCurrentTab(final LightningView tab) {
// mCurrentTab = tab;
// }
} }

25
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 {
}
}

7
app/src/main/java/acr/browser/lightning/bus/TabEvents.java

@ -46,4 +46,11 @@ public final class TabEvents {
this.position = position; this.position = position;
} }
} }
/**
* Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user want to create a
* new tab.
*/
public static class NewTab {
}
} }

49
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.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat; 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.activity.TabsManager;
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.bus.NavigationEvents;
import acr.browser.lightning.bus.TabEvents; import acr.browser.lightning.bus.TabEvents;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils; 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 * @author Stefano Pacifici based on Anthony C. Restaino's code
* @date 2015/09/14 * @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(); private static final String TAG = TabsFragment.class.getSimpleName();
@ -86,6 +89,11 @@ public class TabsFragment extends Fragment {
if (vertical) { if (vertical) {
view = inflater.inflate(R.layout.tab_drawer, container, false); view = inflater.inflate(R.layout.tab_drawer, container, false);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, 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 { } else {
view = inflater.inflate(R.layout.tab_strip, container, false); view = inflater.inflate(R.layout.tab_strip, container, false);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
@ -98,6 +106,15 @@ public class TabsFragment extends Fragment {
return view; 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 @Override
public void onDestroyView() { public void onDestroyView() {
mRecyclerView = null; mRecyclerView = null;
@ -110,6 +127,13 @@ public class TabsFragment extends Fragment {
bus.register(this); bus.register(this);
} }
@Override
public void onResume() {
super.onResume();
// Force adapter refresh
mTabsAdapter.notifyDataSetChanged();
}
@Override @Override
public void onStop() { public void onStop() {
super.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<LightningViewAdapter.LightningViewHolder> { public class LightningViewAdapter extends RecyclerView.Adapter<LightningViewAdapter.LightningViewHolder> {
private final int layoutResourceId; private final int layoutResourceId;
@ -277,5 +323,4 @@ public class TabsFragment extends Fragment {
} }
} }
} }
} }

4
app/src/main/res/layout/activity_main.xml

@ -30,7 +30,9 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="?attr/drawerBackground" android:background="?attr/drawerBackground"
android:id="@+id/left_drawer" android:id="@+id/left_drawer"
android:fitsSystemWindows="true" /> android:fitsSystemWindows="true"
android:layout_gravity="start"
android:weightSum="1" />
<FrameLayout <FrameLayout
android:weightSum="1" android:weightSum="1"

4
app/src/main/res/layout/tab_drawer.xml

@ -1,11 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/navigation_width" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/drawerBackground" android:background="?attr/drawerBackground"
android:clickable="true" android:clickable="true"
android:fitsSystemWindows="true"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout

Loading…
Cancel
Save