Commit ec7832af authored by Piotr Swigon's avatar Piotr Swigon Committed by Commit Bot

[CustomTab] First step of a refactoring introducing CustomTabsUiType.

This should be a no-op functionality wise.

This patch introduces CustomTabsUiType as a better way to specify the
look&feel of the UI. In this and following patches I will aim to
minimize number of intent EXTRAs and replace multiple booleans
indicating visibility of various UI elements with a single value
specifying a UI type.

This patch takes care of only few low hanging fruits to make it easy to
review. I plan to make a few more code structure and readability
improvements on top of this later on.

This refactoring is considered a prerequisite to introducing Minimal-UI
PWA display mode, which is yet another UI type for Custom Tab.

Bug: 604390
Change-Id: Ifa75e341dd1f7ac64cfa70c4dbb507b3df9c7f6c
Reviewed-on: https://chromium-review.googlesource.com/567789
Commit-Queue: Piotr Swigon <piotrs@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486681}
parent 2f67c627
......@@ -4,6 +4,9 @@
package org.chromium.chrome.browser.customtabs;
import static org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_DEFAULT;
import static org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_INFO_PAGE;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
......@@ -803,12 +806,13 @@ public class CustomTabActivity extends ChromeActivity {
@Override
protected AppMenuPropertiesDelegate createAppMenuPropertiesDelegate() {
return new CustomTabAppMenuPropertiesDelegate(this, mIntentDataProvider.getMenuTitles(),
return new CustomTabAppMenuPropertiesDelegate(this,
mIntentDataProvider.getUiType(),
mIntentDataProvider.getMenuTitles(),
mIntentDataProvider.isOpenedByChrome(),
mIntentDataProvider.shouldShowShareMenuItem(),
mIntentDataProvider.isOpenedByChrome(), mIntentDataProvider.isMediaViewer(),
mIntentDataProvider.shouldShowStarButton(),
mIntentDataProvider.shouldShowDownloadButton(),
mIntentDataProvider.isPaymentRequestUI());
mIntentDataProvider.shouldShowDownloadButton());
}
@Override
......@@ -1185,7 +1189,7 @@ public class CustomTabActivity extends ChromeActivity {
Intent intent = ChromeLauncherActivity.createCustomTabActivityIntent(
context, customTabIntent.intent, false);
intent.setPackage(context.getPackageName());
intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_INFO_PAGE, true);
intent.putExtra(CustomTabIntentDataProvider.EXTRA_UI_TYPE, CUSTOM_TABS_UI_TYPE_INFO_PAGE);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
IntentHandler.addTrustedIntentExtras(intent);
......@@ -1197,8 +1201,8 @@ public class CustomTabActivity extends ChromeActivity {
protected boolean requiresFirstRunToBeCompleted(Intent intent) {
// Custom Tabs can be used to open Chrome help pages before the ToS has been accepted.
if (IntentHandler.isIntentChromeOrFirstParty(intent)
&& IntentUtils.safeGetBooleanExtra(
intent, CustomTabIntentDataProvider.EXTRA_IS_INFO_PAGE, false)) {
&& IntentUtils.safeGetIntExtra(intent, CustomTabIntentDataProvider.EXTRA_UI_TYPE,
CUSTOM_TABS_UI_TYPE_DEFAULT) == CUSTOM_TABS_UI_TYPE_INFO_PAGE) {
return false;
}
......
......@@ -4,6 +4,9 @@
package org.chromium.chrome.browser.customtabs;
import static org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER;
import static org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST;
import android.view.Menu;
import android.view.MenuItem;
......@@ -13,6 +16,7 @@ import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.DefaultBrowserInfo;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.appmenu.AppMenuPropertiesDelegate;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CustomTabsUiType;
import org.chromium.chrome.browser.download.DownloadUtils;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.share.ShareHelper;
......@@ -26,14 +30,11 @@ import java.util.Map;
* App menu properties delegate for {@link CustomTabActivity}.
*/
public class CustomTabAppMenuPropertiesDelegate extends AppMenuPropertiesDelegate {
private static final String SAMPLE_URL = "https://www.google.com";
private final @CustomTabsUiType int mUiType;
private final boolean mShowShare;
private final boolean mIsMediaViewer;
private final boolean mShowStar;
private final boolean mShowDownload;
private final boolean mIsOpenedByChrome;
private final boolean mIsPaymentRequestUI;
private final List<String> mMenuEntries;
private final Map<MenuItem, Integer> mItemToIndexMap = new HashMap<MenuItem, Integer>();
......@@ -44,17 +45,15 @@ public class CustomTabAppMenuPropertiesDelegate extends AppMenuPropertiesDelegat
* Creates an {@link CustomTabAppMenuPropertiesDelegate} instance.
*/
public CustomTabAppMenuPropertiesDelegate(final ChromeActivity activity,
List<String> menuEntries, boolean showShare, final boolean isOpenedByChrome,
final boolean isMediaViewer, boolean showStar, boolean showDownload,
final boolean isPaymentRequestUI) {
@CustomTabsUiType final int uiType, List<String> menuEntries, boolean isOpenedByChrome,
boolean showShare, boolean showStar, boolean showDownload) {
super(activity);
mUiType = uiType;
mMenuEntries = menuEntries;
mIsOpenedByChrome = isOpenedByChrome;
mShowShare = showShare;
mIsMediaViewer = isMediaViewer;
mShowStar = showStar;
mShowDownload = showDownload;
mIsOpenedByChrome = isOpenedByChrome;
mIsPaymentRequestUI = isPaymentRequestUI;
}
@Override
......@@ -84,25 +83,29 @@ public class CustomTabAppMenuPropertiesDelegate extends AppMenuPropertiesDelegat
boolean addToHomeScreenVisible = true;
updateRequestDesktopSiteMenuItem(menu, currentTab);
if (mIsMediaViewer) {
if (mUiType == CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER) {
// Most of the menu items don't make sense when viewing media.
iconRow.setVisible(false);
openInChromeItem.setVisible(false);
menu.findItem(R.id.find_in_page_id).setVisible(false);
menu.findItem(R.id.request_desktop_site_row_menu_id).setVisible(false);
addToHomeScreenVisible = false;
} else if (mIsPaymentRequestUI) {
} else if (mUiType == CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST) {
// Only the icon row and 'find in page' are shown for openning payment request UI
// from Chrome.
openInChromeItem.setVisible(false);
menu.findItem(R.id.request_desktop_site_id).setVisible(false);
addToHomeScreenVisible = false;
downloadItem.setVisible(false);
bookmarkItem.setVisible(false);
} else {
openInChromeItem.setTitle(
DefaultBrowserInfo.getTitleOpenInDefaultBrowser(mIsOpenedByChrome));
updateBookmarkMenuItem(bookmarkItem, currentTab);
}
bookmarkItem.setVisible(mShowStar);
if (!mShowStar) {
bookmarkItem.setVisible(false);
}
downloadItem.setVisible(mShowDownload);
if (!FirstRunStatus.getFirstRunFlowComplete()) {
openInChromeItem.setVisible(false);
......@@ -146,7 +149,7 @@ public class CustomTabAppMenuPropertiesDelegate extends AppMenuPropertiesDelegat
@Override
public int getFooterResourceId() {
return mIsMediaViewer ? 0 : R.layout.powered_by_chrome_footer;
return mUiType == CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER ? 0 : R.layout.powered_by_chrome_footer;
}
/**
......
......@@ -16,6 +16,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.customtabs.CustomTabsIntent;
import android.support.customtabs.CustomTabsSessionToken;
import android.text.TextUtils;
......@@ -33,6 +34,8 @@ import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.util.IntentUtils;
import org.chromium.chrome.browser.widget.TintedDrawable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
......@@ -43,6 +46,20 @@ import java.util.List;
public class CustomTabIntentDataProvider {
private static final String TAG = "CustomTabIntentData";
// The type of UI for Custom Tab to use.
@Retention(RetentionPolicy.SOURCE)
@IntDef({
CUSTOM_TABS_UI_TYPE_DEFAULT,
CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER,
CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST,
CUSTOM_TABS_UI_TYPE_INFO_PAGE,
})
public @interface CustomTabsUiType {}
public static final int CUSTOM_TABS_UI_TYPE_DEFAULT = 0;
public static final int CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER = 1;
public static final int CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST = 2;
public static final int CUSTOM_TABS_UI_TYPE_INFO_PAGE = 3;
/**
* Extra used to keep the caller alive. Its value is an Intent.
*/
......@@ -55,10 +72,6 @@ public class CustomTabIntentDataProvider {
public static final String EXTRA_IS_OPENED_BY_CHROME =
"org.chromium.chrome.browser.customtabs.IS_OPENED_BY_CHROME";
/** Indicates that the Custom Tab should style itself as a media viewer. */
public static final String EXTRA_IS_MEDIA_VIEWER =
"org.chromium.chrome.browser.customtabs.IS_MEDIA_VIEWER";
/** URL that should be loaded in place of the URL passed along in the data. */
public static final String EXTRA_MEDIA_VIEWER_URL =
"org.chromium.chrome.browser.customtabs.MEDIA_VIEWER_URL";
......@@ -67,13 +80,9 @@ public class CustomTabIntentDataProvider {
public static final String EXTRA_ENABLE_EMBEDDED_MEDIA_EXPERIENCE =
"org.chromium.chrome.browser.customtabs.EXTRA_ENABLE_EMBEDDED_MEDIA_EXPERIENCE";
/** Indicates that the Custom Tab should style itself as payment request UI. */
public static final String EXTRA_IS_PAYMENT_REQUEST_UI =
"org.chromium.chrome.browser.customtabs.EXTRA_IS_PAYMENT_REQUEST_UI";
/** Indicates that the Custom Tab should style itself as an info page. */
public static final String EXTRA_IS_INFO_PAGE =
"org.chromium.chrome.browser.customtabs.IS_INFO_PAGE";
/** Indicates the type of UI Custom Tab should use. */
public static final String EXTRA_UI_TYPE =
"org.chromium.chrome.browser.customtabs.EXTRA_UI_TYPE";
/** Extra that defines the initial background color (RGB color stored as an integer). */
public static final String EXTRA_INITIAL_BACKGROUND_COLOR =
......@@ -104,12 +113,10 @@ public class CustomTabIntentDataProvider {
private final CustomTabsSessionToken mSession;
private final boolean mIsTrustedIntent;
private final Intent mKeepAliveServiceIntent;
private final int mUiType;
private final int mTitleVisibilityState;
private final boolean mIsMediaViewer;
private final String mMediaViewerUrl;
private final boolean mEnableEmbeddedMediaExperience;
private final boolean mIsPaymentRequestUI;
private final boolean mIsInfoPage;
private final int mInitialBackgroundColor;
private final boolean mDisableStar;
private final boolean mDisableDownload;
......@@ -137,10 +144,8 @@ public class CustomTabIntentDataProvider {
* Add extras to customize menu items for openning payment request UI custom tab from Chrome.
*/
public static void addPaymentRequestUIExtras(Intent intent) {
intent.putExtra(EXTRA_IS_PAYMENT_REQUEST_UI, true);
intent.putExtra(EXTRA_UI_TYPE, CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST);
intent.putExtra(EXTRA_IS_OPENED_BY_CHROME, true);
intent.putExtra(EXTRA_DISABLE_STAR_BUTTON, true);
intent.putExtra(EXTRA_DISABLE_DOWNLOAD_BUTTON, true);
IntentHandler.addTrustedIntentExtras(intent);
}
......@@ -191,6 +196,19 @@ public class CustomTabIntentDataProvider {
mIsOpenedByChrome =
IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_OPENED_BY_CHROME, false);
final int requestedUiType =
IntentUtils.safeGetIntExtra(intent, EXTRA_UI_TYPE, CUSTOM_TABS_UI_TYPE_DEFAULT);
if (requestedUiType == CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER && mIsTrustedIntent) {
mUiType = CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER;
} else if (requestedUiType == CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST && mIsTrustedIntent
&& mIsOpenedByChrome) {
mUiType = CUSTOM_TABS_UI_TYPE_PAYMENT_REQUEST;
} else if (requestedUiType == CUSTOM_TABS_UI_TYPE_INFO_PAGE && mIsTrustedIntent) {
mUiType = CUSTOM_TABS_UI_TYPE_INFO_PAGE;
} else {
mUiType = CUSTOM_TABS_UI_TYPE_DEFAULT;
}
mAnimationBundle = IntentUtils.safeGetBundleExtra(
intent, CustomTabsIntent.EXTRA_EXIT_ANIMATION_BUNDLE);
mTitleVisibilityState = IntentUtils.safeGetIntExtra(intent,
......@@ -203,17 +221,12 @@ public class CustomTabIntentDataProvider {
CustomTabsIntent.EXTRA_REMOTEVIEWS_VIEW_IDS);
mRemoteViewsPendingIntent = IntentUtils.safeGetParcelableExtra(intent,
CustomTabsIntent.EXTRA_REMOTEVIEWS_PENDINGINTENT);
mIsMediaViewer = mIsTrustedIntent
&& IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_MEDIA_VIEWER, false);
mMediaViewerUrl = mIsMediaViewer
? IntentUtils.safeGetStringExtra(intent, EXTRA_MEDIA_VIEWER_URL) : null;
mMediaViewerUrl = isMediaViewer()
? IntentUtils.safeGetStringExtra(intent, EXTRA_MEDIA_VIEWER_URL)
: null;
mEnableEmbeddedMediaExperience = mIsTrustedIntent
&& IntentUtils.safeGetBooleanExtra(
intent, EXTRA_ENABLE_EMBEDDED_MEDIA_EXPERIENCE, false);
mIsPaymentRequestUI = mIsTrustedIntent && mIsOpenedByChrome
&& IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_PAYMENT_REQUEST_UI, false);
mIsInfoPage = mIsTrustedIntent
&& IntentUtils.safeGetBooleanExtra(intent, EXTRA_IS_INFO_PAGE, false);
mDisableStar = IntentUtils.safeGetBooleanExtra(intent, EXTRA_DISABLE_STAR_BUTTON, false);
mDisableDownload = IntentUtils.safeGetBooleanExtra(intent, EXTRA_DISABLE_DOWNLOAD_BUTTON,
false);
......@@ -518,10 +531,15 @@ public class CustomTabIntentDataProvider {
}
/**
* @return See {@link #EXTRA_IS_MEDIA_VIEWER}.
* @return See {@link #EXTRA_UI_TYPE}.
*/
boolean isMediaViewer() {
return mIsMediaViewer;
return mUiType == CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER;
}
@CustomTabsUiType
int getUiType() {
return mUiType;
}
/**
......@@ -538,20 +556,12 @@ public class CustomTabIntentDataProvider {
return mEnableEmbeddedMediaExperience;
}
/**
* @return true If the Custom Tab is opened as payment request UI.
* @See {@link #EXTRA_IS_PAYMENT_REQUEST_UI}.
*/
boolean isPaymentRequestUI() {
return mIsPaymentRequestUI;
}
/**
* @return If the Custom Tab is an info page.
* See {@link #EXTRA_IS_INFO_PAGE}.
* See {@link #EXTRA_UI_TYPE}.
*/
boolean isInfoPage() {
return mIsInfoPage;
return mUiType == CUSTOM_TABS_UI_TYPE_INFO_PAGE;
}
/**
......
......@@ -487,7 +487,8 @@ public class DownloadUtils {
Intent intent = builder.build().intent;
intent.setPackage(context.getPackageName());
intent.setData(contentUri);
intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_MEDIA_VIEWER, true);
intent.putExtra(CustomTabIntentDataProvider.EXTRA_UI_TYPE,
CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER);
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MEDIA_VIEWER_URL, fileUri.toString());
intent.putExtra(CustomTabIntentDataProvider.EXTRA_ENABLE_EMBEDDED_MEDIA_EXPERIENCE, true);
intent.putExtra(
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.customtabs;
import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE;
import static org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule.LONG_TIMEOUT_MS;
import static org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider.CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER;
import android.app.Activity;
import android.app.Instrumentation;
......@@ -548,7 +549,8 @@ public class CustomTabActivityTest {
@RetryOnFailure
public void testAppMenuForMediaViewer() throws InterruptedException {
Intent intent = createMinimalCustomTabIntent();
intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_MEDIA_VIEWER, true);
intent.putExtra(
CustomTabIntentDataProvider.EXTRA_UI_TYPE, CUSTOM_TABS_UI_TYPE_MEDIA_VIEWER);
IntentHandler.addTrustedIntentExtras(intent);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment