1 package org.mozilla.geckoview_example;
2 
3 import android.content.Context;
4 import android.graphics.Bitmap;
5 import android.graphics.PorterDuff;
6 import android.graphics.Typeface;
7 import android.graphics.drawable.BitmapDrawable;
8 import android.graphics.drawable.GradientDrawable;
9 import android.graphics.drawable.ShapeDrawable;
10 import android.support.v4.content.ContextCompat;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 import android.widget.LinearLayout;
16 import android.widget.PopupMenu;
17 import android.widget.TextView;
18 
19 public class ToolbarLayout extends LinearLayout {
20     public interface TabListener {
switchToTab(int tabId)21         void switchToTab(int tabId);
onBrowserActionClick()22         void onBrowserActionClick();
23     }
24 
25     private LocationView mLocationView;
26     private Button mTabsCountButton;
27     private View mBrowserAction;
28     private TabListener mTabListener;
29     private TabSessionManager mSessionManager;
30 
ToolbarLayout(Context context, TabSessionManager sessionManager)31     public ToolbarLayout(Context context, TabSessionManager sessionManager) {
32         super(context);
33         mSessionManager = sessionManager;
34         initView();
35     }
36 
initView()37     private void initView() {
38         setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
39         setOrientation(LinearLayout.HORIZONTAL);
40         mLocationView = new LocationView(getContext());
41         mLocationView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f));
42         mLocationView.setId(R.id.url_bar);
43         addView(mLocationView);
44 
45         mTabsCountButton = getTabsCountButton();
46         addView(mTabsCountButton);
47 
48         mBrowserAction = getBrowserAction();
49         addView(mBrowserAction);
50     }
51 
getTabsCountButton()52     private Button getTabsCountButton() {
53         Button button = new Button(getContext());
54         button.setLayoutParams(new LayoutParams(150, LayoutParams.MATCH_PARENT));
55         button.setId(R.id.tabs_button);
56         button.setOnClickListener(this::onTabButtonClicked);
57         button.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.tab_number_background));
58         button.setTypeface(button.getTypeface(), Typeface.BOLD);
59         return button;
60     }
61 
getBrowserAction()62     private View getBrowserAction() {
63         View browserAction = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
64             .inflate(R.layout.browser_action, this, false);
65         browserAction.setVisibility(GONE);
66         return browserAction;
67     }
68 
setBrowserActionButton(ActionButton button)69     public void setBrowserActionButton(ActionButton button) {
70         if (button == null) {
71             mBrowserAction.setVisibility(GONE);
72             return;
73         }
74 
75         BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), button.icon);
76         ImageView view = mBrowserAction.findViewById(R.id.browser_action_icon);
77         view.setOnClickListener(this::onBrowserActionButtonClicked);
78         view.setBackground(drawable);
79 
80         TextView badge = mBrowserAction.findViewById(R.id.browser_action_badge);
81         if (button.text != null && !button.text.equals("")) {
82             if (button.backgroundColor != null) {
83                 GradientDrawable backgroundDrawable = ((GradientDrawable) badge.getBackground().mutate());
84                 backgroundDrawable.setColor(button.backgroundColor);
85                 backgroundDrawable.invalidateSelf();
86             }
87             if (button.textColor != null) {
88                 badge.setTextColor(button.textColor);
89             }
90             badge.setText(button.text);
91             badge.setVisibility(VISIBLE);
92         } else {
93             badge.setVisibility(GONE);
94         }
95 
96         mBrowserAction.setVisibility(VISIBLE);
97     }
98 
onBrowserActionButtonClicked(View view)99     public void onBrowserActionButtonClicked(View view) {
100         mTabListener.onBrowserActionClick();
101     }
102 
getLocationView()103     public LocationView getLocationView() {
104         return mLocationView;
105     }
106 
setTabListener(TabListener listener)107     public void setTabListener(TabListener listener) {
108         this.mTabListener = listener;
109     }
110 
updateTabCount()111     public void updateTabCount() {
112         mTabsCountButton.setText(String.valueOf(mSessionManager.sessionCount()));
113     }
114 
onTabButtonClicked(View view)115     public void onTabButtonClicked(View view) {
116         PopupMenu tabButtonMenu = new PopupMenu(getContext(), mTabsCountButton);
117         for(int idx = 0; idx < mSessionManager.sessionCount(); ++idx) {
118             tabButtonMenu.getMenu().add(0, idx, idx,
119                     mSessionManager.getSession(idx).getTitle());
120         }
121         tabButtonMenu.setOnMenuItemClickListener(item -> {
122             mTabListener.switchToTab(item.getItemId());
123             return true;
124         });
125         tabButtonMenu.show();
126     }
127 
128 }
129