summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/ToolbarLayout.java
blob: ce074f2ee34b8c5568cce4ba4993313e961e25b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* 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.geckoview_example;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.core.content.ContextCompat;

public class ToolbarLayout extends LinearLayout {
  public interface TabListener {
    void switchToTab(int tabId);

    void onBrowserActionClick();
  }

  private LocationView mLocationView;
  private Button mTabsCountButton;
  private View mBrowserAction;
  private TabListener mTabListener;
  private TabSessionManager mSessionManager;

  public ToolbarLayout(Context context, TabSessionManager sessionManager) {
    super(context);
    mSessionManager = sessionManager;
    initView();
  }

  private void initView() {
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
    setOrientation(LinearLayout.HORIZONTAL);
    mLocationView = new LocationView(getContext());
    mLocationView.setLayoutParams(
        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f));
    mLocationView.setId(R.id.url_bar);
    addView(mLocationView);

    mTabsCountButton = getTabsCountButton();
    addView(mTabsCountButton);

    mBrowserAction = getBrowserAction();
    addView(mBrowserAction);
  }

  private Button getTabsCountButton() {
    Button button = new Button(getContext());
    button.setLayoutParams(new LayoutParams(150, LayoutParams.MATCH_PARENT));
    button.setId(R.id.tabs_button);
    button.setOnClickListener(this::onTabButtonClicked);
    button.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.tab_number_background));
    button.setTypeface(button.getTypeface(), Typeface.BOLD);
    return button;
  }

  private View getBrowserAction() {
    View browserAction =
        ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.browser_action, this, false);
    browserAction.setVisibility(GONE);
    return browserAction;
  }

  public void setBrowserActionButton(ActionButton button) {
    if (button == null) {
      mBrowserAction.setVisibility(GONE);
      return;
    }

    BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), button.icon);
    ImageView view = mBrowserAction.findViewById(R.id.browser_action_icon);
    view.setOnClickListener(this::onBrowserActionButtonClicked);
    view.setBackground(drawable);

    TextView badge = mBrowserAction.findViewById(R.id.browser_action_badge);
    if (button.text != null && !button.text.equals("")) {
      if (button.backgroundColor != null) {
        GradientDrawable backgroundDrawable = ((GradientDrawable) badge.getBackground().mutate());
        backgroundDrawable.setColor(button.backgroundColor);
        backgroundDrawable.invalidateSelf();
      }
      if (button.textColor != null) {
        badge.setTextColor(button.textColor);
      }
      badge.setText(button.text);
      badge.setVisibility(VISIBLE);
    } else {
      badge.setVisibility(GONE);
    }

    mBrowserAction.setVisibility(VISIBLE);
  }

  public void onBrowserActionButtonClicked(View view) {
    mTabListener.onBrowserActionClick();
  }

  public LocationView getLocationView() {
    return mLocationView;
  }

  public void setTabListener(TabListener listener) {
    this.mTabListener = listener;
  }

  public void updateTabCount() {
    mTabsCountButton.setText(String.valueOf(mSessionManager.sessionCount()));
  }

  public void onTabButtonClicked(View view) {
    PopupMenu tabButtonMenu = new PopupMenu(getContext(), mTabsCountButton);
    for (int idx = 0; idx < mSessionManager.sessionCount(); ++idx) {
      tabButtonMenu.getMenu().add(0, idx, idx, mSessionManager.getSession(idx).getTitle());
    }
    tabButtonMenu.setOnMenuItemClickListener(
        item -> {
          mTabListener.switchToTab(item.getItemId());
          return true;
        });
    tabButtonMenu.show();
  }
}