Skip to content

Commit

Permalink
Implement overflow menu. (mozilla-mobile#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo committed Mar 2, 2017
1 parent c47391e commit fa724d9
Show file tree
Hide file tree
Showing 19 changed files with 343 additions and 61 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:customtabs:25.1.1'
compile 'com.android.support:design:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:recyclerview-v7:25.1.1'

geckoCompile(name: 'geckoview-195049fabb7a', ext: 'aar')

Expand Down
45 changes: 15 additions & 30 deletions app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.PopupMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.mozilla.focus.R;
import org.mozilla.focus.activity.SettingsActivity;
import org.mozilla.focus.menu.BrowserMenu;
import org.mozilla.focus.web.IWebView;

/**
* Fragment for displaying the browser UI.
*/
public class BrowserFragment extends Fragment implements View.OnClickListener, PopupMenu.OnMenuItemClickListener {
public class BrowserFragment extends Fragment implements View.OnClickListener {
public static final String FRAGMENT_TAG = "browser";

private static final int ANIMATION_DURATION = 300;
Expand Down Expand Up @@ -122,13 +119,8 @@ public void onProgress(int progress) {
public void onClick(View view) {
switch (view.getId()) {
case R.id.menu:
final PopupMenu popupMenu = new PopupMenu(getActivity(), menuView);
popupMenu.getMenuInflater().inflate(R.menu.menu_browser, popupMenu.getMenu());
popupMenu.getMenu().findItem(R.id.forward).setEnabled(webView.canGoForward());
popupMenu.getMenu().findItem(R.id.back).setEnabled(webView.canGoBack());
popupMenu.setOnMenuItemClickListener(this);
popupMenu.setGravity(Gravity.TOP);
popupMenu.show();
BrowserMenu menu = new BrowserMenu(getActivity(), this);
menu.show(menuView);
break;

case R.id.url:
Expand All @@ -150,42 +142,35 @@ public void onClick(View view) {
Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.feedback_erase, Snackbar.LENGTH_LONG).show();

break;
}
}

@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.forward:
webView.goForward();
return true;

case R.id.back:
webView.goBack();
return true;
break;

case R.id.refresh:
webView.reload();
return true;
break;

case R.id.share:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, webView.getUrl());
startActivity(shareIntent);
return true;

case R.id.open:
// TODO: Switch to full featured browser (Issue #26)
return true;
break;

case R.id.settings:
final Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivity(settingsIntent);
return true;
break;

case R.id.open:
// TODO: Switch to full featured browser (Issue #26)
break;
}
}

return false;
public boolean canGoForward() {
return webView.canGoForward();
}

public boolean canGoBack() {
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/org/mozilla/focus/menu/BrowserMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.menu;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import org.mozilla.focus.R;
import org.mozilla.focus.fragment.BrowserFragment;

public class BrowserMenu extends PopupWindow {
public BrowserMenu(Context context, BrowserFragment fragment) {
final View view = LayoutInflater.from(context).inflate(R.layout.menu, null);
setContentView(view);

RecyclerView menuList = (RecyclerView) view.findViewById(R.id.list);
menuList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
menuList.setAdapter(new BrowserMenuAdapter(this, fragment));

setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

setFocusable(true);

setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

setElevation(context.getResources().getDimension(R.dimen.menu_elevation));
}

public void show(View anchor) {
super.showAsDropDown(anchor, 0, -(anchor.getHeight() + anchor.getPaddingBottom()));
}
}
76 changes: 76 additions & 0 deletions app/src/main/java/org/mozilla/focus/menu/BrowserMenuAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.menu;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import org.mozilla.focus.R;
import org.mozilla.focus.fragment.BrowserFragment;

public class BrowserMenuAdapter extends RecyclerView.Adapter<BrowserMenuViewHolder> {
static class MenuItem {
public final int id;
public final int label;

public MenuItem(int id, int label) {
this.id = id;
this.label = label;
}
}

private static final MenuItem[] MENU_ITEMS = new MenuItem[] {
new MenuItem(R.id.share, R.string.menu_share),
new MenuItem(R.id.open, R.string.menu_open_with),
new MenuItem(R.id.settings, R.string.menu_settings)
};

private final BrowserMenu menu;
private final BrowserFragment fragment;

public BrowserMenuAdapter(BrowserMenu menu, BrowserFragment fragment) {
this.menu = menu;
this.fragment = fragment;
}

@Override
public BrowserMenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

if (viewType == NavigationItemViewHolder.LAYOUT_ID) {
return new NavigationItemViewHolder(inflater.inflate(R.layout.menu_navigation, parent, false), fragment);
} else if (viewType == MenuItemViewHolder.LAYOUT_ID) {
return new MenuItemViewHolder(inflater.inflate(R.layout.menu_item, parent, false));
}

throw new IllegalArgumentException("Unknown view type: " + viewType);
}

@Override
public void onBindViewHolder(BrowserMenuViewHolder holder, int position) {
holder.setMenu(menu);
holder.setOnClickListener(fragment);

if (position > 0) {
((MenuItemViewHolder) holder).bind(MENU_ITEMS[position - 1]);
}
}

@Override
public int getItemViewType(int position) {
if (position == 0) {
return NavigationItemViewHolder.LAYOUT_ID;
} else {
return MenuItemViewHolder.LAYOUT_ID;
}
}

@Override
public int getItemCount() {
return 1 + MENU_ITEMS.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.menu;

import android.support.v7.widget.RecyclerView;
import android.view.View;

public abstract class BrowserMenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private BrowserMenu menu;
private View.OnClickListener listener;

public BrowserMenuViewHolder(View itemView) {
super(itemView);
}

public void setMenu(BrowserMenu menu) {
this.menu = menu;
}

public void setOnClickListener(View.OnClickListener listener) {
this.listener = listener;
}

@Override
public void onClick(View view) {
if (menu != null) {
menu.dismiss();
}

if (listener != null) {
listener.onClick(view);
}
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/org/mozilla/focus/menu/MenuItemViewHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.menu;

import android.view.View;
import android.widget.TextView;

import org.mozilla.focus.R;
import org.mozilla.focus.fragment.BrowserFragment;

/* package-private */ class MenuItemViewHolder extends BrowserMenuViewHolder {
/* package-private */ static final int LAYOUT_ID = R.layout.menu_item;

private TextView menuItemView;

/* package-private */ MenuItemViewHolder(View itemView) {
super(itemView);

menuItemView = (TextView) itemView;
}

/* package-private */ void bind(BrowserMenuAdapter.MenuItem menuItem) {
menuItemView.setId(menuItem.id);
menuItemView.setText(menuItem.label);
menuItemView.setOnClickListener(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.menu;

import android.view.View;

import org.mozilla.focus.R;
import org.mozilla.focus.fragment.BrowserFragment;

public class NavigationItemViewHolder extends BrowserMenuViewHolder {
public static int LAYOUT_ID = R.layout.menu_navigation;

public NavigationItemViewHolder(View itemView, BrowserFragment fragment) {
super(itemView);

itemView.findViewById(R.id.refresh).setOnClickListener(this);

final View forwardView = itemView.findViewById(R.id.forward);
if (!fragment.canGoForward()) {
forwardView.setEnabled(false);
forwardView.setAlpha(0.5f);
} else {
forwardView.setOnClickListener(this);
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_forward.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z"
android:fillColor="#ffffff"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"
android:fillColor="#ffffff"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_browser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="?android:attr/selectableItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:padding="4dp"
android:contentDescription="@string/content_description_menu"
android:src="@drawable/ic_menu" />
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">

<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="250dp"
android:layout_height="wrap_content" />

</android.support.v7.widget.CardView>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/menu_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:clickable="true"
android:textSize="16sp"
android:gravity="center_vertical"
android:background="?android:attr/selectableItemBackground"
style="@android:style/TextAppearance.Material.Menu"/>
Loading

0 comments on commit fa724d9

Please sign in to comment.