Skip to content

Commit

Permalink
Bug 1332546 - Refactory ActionBar by extracting methods to another cl…
Browse files Browse the repository at this point in the history
…ass r=sebastian

ActionBar of CustomTabsActivity is getting complicated. There will be
more components in ActionBar, such as Site-identity-icon.

Move some action-bar related view code to another class.

MozReview-Commit-ID: I5sOSCQKnlv

--HG--
extra : rebase_source : 6822593af92c657c496339ba8df7769ea463c681
  • Loading branch information
walkingice committed Mar 3, 2017
1 parent 04b1a97 commit cb7ce66
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* -*- 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.gecko.customtabs;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.util.ColorUtil;

import java.lang.reflect.Field;

/**
* This class is used to maintain appearance of ActionBar of CustomTabsActivity, includes background
* color, custom-view and so on.
*/
public class ActionBarPresenter {

private static final String LOGTAG = "CustomTabsActionBar";
private final ActionBar mActionBar;
private boolean useDomainTitle = true;

ActionBarPresenter(@NonNull final ActionBar actionBar, @NonNull Toolbar toolbar) {
mActionBar = actionBar;
initActionBar(toolbar);
}

private void initActionBar(@NonNull final Toolbar toolbar) {
try {
// Since we don't create the Toolbar's TextView ourselves, this seems
// to be the only way of changing the ellipsize setting.
final Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
final TextView textView = (TextView) f.get(toolbar);
textView.setEllipsize(TextUtils.TruncateAt.START);
} catch (Exception e) {
// If we can't ellipsize at the start of the title, we shouldn't display the host
// so as to avoid displaying a misleadingly truncated host.
Log.w(LOGTAG, "Failed to get Toolbar TextView, using default title.");
useDomainTitle = false;
}
}

/**
* Update appearance of ActionBar, includes its Title.
*
* @param title A string to be used as Title in Actionbar
*/
@UiThread
public void update(@Nullable final String title) {
if (useDomainTitle || TextUtils.isEmpty(title)) {
mActionBar.setTitle(AppConstants.MOZ_APP_BASENAME);
} else {
mActionBar.setTitle(title);
}
}

/**
* Set background color to ActionBar, as well as Status bar.
*
* @param color the color to apply to ActionBar
* @param window Window instance for changing color status bar, or null if won't change it.
*/
@UiThread
public void setBackgroundColor(@ColorInt final int color,
@Nullable final Window window) {
mActionBar.setBackgroundDrawable(new ColorDrawable(color));

if (window != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ColorUtil.darken(color, 0.25));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
Expand All @@ -29,10 +28,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.GeckoApp;
Expand All @@ -44,7 +40,6 @@
import org.mozilla.gecko.util.ColorUtil;
import org.mozilla.gecko.widget.GeckoPopupMenu;

import java.lang.reflect.Field;
import java.util.List;

import static android.support.customtabs.CustomTabsIntent.EXTRA_TOOLBAR_COLOR;
Expand All @@ -55,10 +50,9 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
private static final String SAVED_TOOLBAR_TITLE = "SavedToolbarTitle";
private static final int NO_COLOR = -1;
private final SparseArrayCompat<PendingIntent> menuItemsIntent = new SparseArrayCompat<>();
private ActionBar actionBar;
private GeckoPopupMenu popupMenu;
private int tabId = -1;
private boolean useDomainTitle = true;
private ActionBarPresenter actionBarPresenter;
private int toolbarColor;
private String toolbarTitle;
// A state to indicate whether this activity is finishing with customize animation
Expand All @@ -78,34 +72,15 @@ public void onCreate(Bundle savedInstanceState) {

setThemeFromToolbarColor();

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
updateActionBarWithToolbar(toolbar);
try {
// Since we don't create the Toolbar's TextView ourselves, this seems
// to be the only way of changing the ellipsize setting.
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
TextView textView = (TextView) f.get(toolbar);
textView.setEllipsize(TextUtils.TruncateAt.START);
} catch (Exception e) {
// If we can't ellipsize at the start of the title, we shouldn't display the host
// so as to avoid displaying a misleadingly truncated host.
Log.w(LOGTAG, "Failed to get Toolbar TextView, using default title.");
useDomainTitle = false;
}
actionBar = getSupportActionBar();
actionBar.setTitle(toolbarTitle);
updateToolbarColor(toolbar);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
bindNavigationCallback(toolbar);

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Tabs tabs = Tabs.getInstance();
final Tab tab = tabs.getSelectedTab();
tabs.closeTab(tab);
finish();
}
});
actionBarPresenter = new ActionBarPresenter(actionBar, toolbar);
actionBarPresenter.setBackgroundColor(toolbarColor, getWindow());
actionBarPresenter.update(toolbarTitle);

Tabs.registerOnTabsChangedListener(this);
}
Expand Down Expand Up @@ -187,12 +162,12 @@ public void onTabChanged(Tab tab, Tabs.TabEvents msg, String data) {
if (uri != null) {
title = uri.getHost();
}
if (!useDomainTitle || title == null || title.isEmpty()) {
if (title == null || title.isEmpty()) {
toolbarTitle = AppConstants.MOZ_APP_BASENAME;
} else {
toolbarTitle = title;
}
actionBar.setTitle(toolbarTitle);
actionBarPresenter.update(toolbarTitle);
}

updateMenuItemForward();
Expand Down Expand Up @@ -306,26 +281,16 @@ MenuItem insertActionButton(Menu menu, Intent intent) {
return item;
}

private void updateActionBarWithToolbar(final Toolbar toolbar) {
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
}

private void updateToolbarColor(final Toolbar toolbar) {
if (toolbarColor == NO_COLOR) {
return;
}

toolbar.setBackgroundColor(toolbarColor);

final Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ColorUtil.darken(toolbarColor, 0.25));
}
private void bindNavigationCallback(@NonNull final Toolbar toolbar) {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Tabs tabs = Tabs.getInstance();
final Tab tab = tabs.getSelectedTab();
tabs.closeTab(tab);
finish();
}
});
}

private void performPendingIntent(@NonNull PendingIntent pendingIntent) {
Expand Down
1 change: 1 addition & 0 deletions mobile/android/base/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'cleanup/FileCleanupController.java',
'cleanup/FileCleanupService.java',
'CustomEditText.java',
'customtabs/ActionBarPresenter.java',
'customtabs/CustomTabsActivity.java',
'customtabs/GeckoCustomTabsService.java',
'customtabs/IntentUtil.java',
Expand Down

0 comments on commit cb7ce66

Please sign in to comment.