下面列出了android.view.ViewTreeObserver.OnGlobalFocusChangeListener#org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void search() {
RecordUserAction.record("MobileActionMode.WebSearch");
if (mTab.getTabModelSelector() == null) return;
String query = ActionModeCallbackHelper.sanitizeQuery(mHelper.getSelectedText(),
ActionModeCallbackHelper.MAX_SEARCH_QUERY_LENGTH);
if (TextUtils.isEmpty(query)) return;
String url = TemplateUrlService.getInstance().getUrlForSearchQuery(query);
String headers = GeolocationHeader.getGeoHeader(url, mTab);
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setVerbatimHeaders(headers);
loadUrlParams.setTransitionType(PageTransition.GENERATED);
mTab.getTabModelSelector().openNewTab(loadUrlParams,
TabLaunchType.FROM_LONGPRESS_FOREGROUND, mTab, mTab.isIncognito());
}
/**
* @param type Type of the tab launch.
* @param intent The intent causing the tab launch.
* @return The page transition type constant.
*/
private int getTransitionType(TabLaunchType type, Intent intent) {
int transition = PageTransition.LINK;
switch (type) {
case FROM_RESTORE:
case FROM_LINK:
case FROM_EXTERNAL_APP:
transition = PageTransition.LINK | PageTransition.FROM_API;
break;
case FROM_CHROME_UI:
case FROM_LONGPRESS_FOREGROUND:
case FROM_LONGPRESS_BACKGROUND:
transition = PageTransition.AUTO_TOPLEVEL;
break;
default:
assert false;
break;
}
return IntentHandler.getTransitionTypeFromIntent(mActivity.getApplicationContext(),
intent, transition);
}
@Override
public boolean createTabWithWebContents(Tab parent, WebContents webContents, int parentId,
TabLaunchType type, String url) {
// The parent tab was already closed. Do not open child tabs.
if (mTabModel.isClosurePending(parentId)) return false;
// If parent is in the same tab model, place the new tab next to it.
int position = TabModel.INVALID_TAB_INDEX;
int index = TabModelUtils.getTabIndexById(mTabModel, parentId);
if (index != TabModel.INVALID_TAB_INDEX) position = index + 1;
boolean openInForeground = mOrderController.willOpenInForeground(type, mIncognito);
TabDelegateFactory delegateFactory = parent == null ? new TabDelegateFactory()
: parent.getDelegateFactory();
Tab tab = Tab.createLiveTab(Tab.INVALID_TAB_ID, mActivity, mIncognito,
mNativeWindow, type, parentId, !openInForeground);
tab.initialize(webContents, mTabContentManager, delegateFactory, !openInForeground, false);
mTabModel.addTab(tab, position, type);
return true;
}
private void search() {
RecordUserAction.record("MobileActionMode.WebSearch");
if (mTab.getTabModelSelector() == null) return;
String query = mHelper.sanitizeQuery(mHelper.getSelectedText(),
ActionModeCallbackHelper.MAX_SEARCH_QUERY_LENGTH);
if (TextUtils.isEmpty(query)) return;
String url = TemplateUrlService.getInstance().getUrlForSearchQuery(query);
String headers = GeolocationHeader.getGeoHeader(mContext.getApplicationContext(),
url, mTab.isIncognito());
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setVerbatimHeaders(headers);
loadUrlParams.setTransitionType(PageTransition.GENERATED);
mTab.getTabModelSelector().openNewTab(loadUrlParams,
TabLaunchType.FROM_LONGPRESS_FOREGROUND, mTab, mTab.isIncognito());
}
@Override
public void openNewTab(String url, String extraHeaders, ResourceRequestBody postData,
int disposition, boolean isRendererInitiated) {
// If attempting to open an incognito tab, always send the user to tabbed mode.
if (disposition == WindowOpenDisposition.OFF_THE_RECORD) {
if (isRendererInitiated) {
throw new IllegalStateException(
"Invalid attempt to open an incognito tab from the renderer");
}
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setVerbatimHeaders(extraHeaders);
loadUrlParams.setPostData(postData);
loadUrlParams.setIsRendererInitiated(isRendererInitiated);
Class<? extends ChromeTabbedActivity> tabbedClass =
MultiWindowUtils.getInstance().getTabbedActivityForIntent(
null, ContextUtils.getApplicationContext());
AsyncTabCreationParams tabParams = new AsyncTabCreationParams(loadUrlParams,
new ComponentName(ContextUtils.getApplicationContext(), tabbedClass));
new TabDelegate(true).createNewTab(tabParams,
TabLaunchType.FROM_LONGPRESS_FOREGROUND, TabModel.INVALID_TAB_INDEX);
return;
}
super.openNewTab(url, extraHeaders, postData, disposition, isRendererInitiated);
}
/**
* Determine the insertion index of the next tab.
*
* @param type The launch type of the new tab.
* @return Where to insert the tab.
*/
public int determineInsertionIndex(TabLaunchType type, Tab newTab) {
TabModel currentModel = mTabModelSelector.getCurrentModel();
Tab currentTab = TabModelUtils.getCurrentTab(currentModel);
if (currentTab == null) {
assert (currentModel.getCount() == 0);
return 0;
}
int currentId = currentTab.getId();
int currentIndex = TabModelUtils.getTabIndexById(currentModel, currentId);
if (sameModelType(currentModel, newTab)) {
if (willOpenInForeground(type, newTab.isIncognito())) {
// If the tab was opened in the foreground, insert it adjacent to
// the tab that opened that link.
return currentIndex + 1;
} else {
// If the tab was opened in the background, position at the end of
// it's 'group'.
int index = getIndexOfLastTabOpenedBy(currentId, currentIndex);
if (index != NO_TAB) {
return index + 1;
} else {
return currentIndex + 1;
}
}
} else {
// If the tab is opening in the other model type, just put it at the end.
return mTabModelSelector.getModel(newTab.isIncognito()).getCount();
}
}
/**
* Creates a fresh tab. initialize() needs to be called afterwards to complete the second level
* initialization.
* @param initiallyHidden true iff the tab being created is initially in background
*/
public static Tab createLiveTab(int id, ChromeActivity activity, boolean incognito,
WindowAndroid nativeWindow, TabLaunchType type, int parentId, boolean initiallyHidden) {
return new Tab(id, parentId, incognito, activity, nativeWindow, type, initiallyHidden
? TabCreationState.LIVE_IN_BACKGROUND
: TabCreationState.LIVE_IN_FOREGROUND, null);
}
@Override
public void openResolvedSearchUrlInNewTab() {
if (mSearchRequest != null && mSearchRequest.getSearchUrlForPromotion() != null) {
TabModelSelector tabModelSelector = mActivity.getTabModelSelector();
tabModelSelector.openNewTab(
new LoadUrlParams(mSearchRequest.getSearchUrlForPromotion()),
TabLaunchType.FROM_LINK,
tabModelSelector.getCurrentTab(),
tabModelSelector.isIncognitoSelected());
}
}
@Override
public void finishNativeInitialization() {
super.finishNativeInitialization();
mTab = new Tab(TabIdManager.getInstance().generateValidId(Tab.INVALID_TAB_ID),
Tab.INVALID_TAB_ID, false, this, getWindowAndroid(),
TabLaunchType.FROM_EXTERNAL_APP, null, null);
mTab.initialize(WebContentsFactory.createWebContents(false, false), null,
new TabDelegateFactory(), false, false);
mTab.loadUrl(new LoadUrlParams("about:blank"));
mSearchBoxDataProvider.onNativeLibraryReady(mTab);
mSearchBox.onNativeLibraryReady();
// Force the user to choose a search engine if they have to.
final Callback<Boolean> deferredCallback = new Callback<Boolean>() {
@Override
public void onResult(Boolean result) {
if (result == null || !result.booleanValue()) {
Log.e(TAG, "User failed to select a default search engine.");
finish();
return;
}
finishDeferredInitialization();
}
};
if (!getActivityDelegate().showSearchEngineDialogIfNeeded(this, deferredCallback)) {
mHandler.post(new Runnable() {
@Override
public void run() {
finishDeferredInitialization();
}
});
}
}
@Override
protected void tabCreated(int id, int sourceId, TabLaunchType launchType, boolean isIncognito,
boolean willBeSelected, float originX, float originY) {
super.tabCreated(id, sourceId, launchType, isIncognito, willBeSelected, originX, originY);
if (willBeSelected) {
Tab newTab = TabModelUtils.getTabById(getTabModelSelector().getModel(isIncognito), id);
if (newTab != null) newTab.requestFocus();
}
}
/**
* Creates a new tab and loads the NTP.
*/
public final void launchNTP() {
try {
TraceEvent.begin("TabCreator.launchNTP");
launchUrl(UrlConstants.NTP_URL, TabModel.TabLaunchType.FROM_CHROME_UI);
} finally {
TraceEvent.end("TabCreator.launchNTP");
}
}
/**
* Set the texts that notify the user about data in their google account and that deleting
* cookies doesn't sign you out of chrome.
*/
protected void initFootnote() {
// The general information footnote informs users about data that will not be deleted.
// If the user is signed in, it also informs users about the behavior of synced deletions.
// and we show an additional Google-specific footnote. This footnote informs users that they
// will not be signed out of their Google account, and if the web history service indicates
// that they have other forms of browsing history, then also about that.
TextMessageWithLinkAndIconPreference google_summary =
(TextMessageWithLinkAndIconPreference) findPreference(PREF_GOOGLE_SUMMARY);
TextMessageWithLinkAndIconPreference general_summary =
(TextMessageWithLinkAndIconPreference) findPreference(PREF_GENERAL_SUMMARY);
google_summary.setLinkClickDelegate(new Runnable() {
@Override
public void run() {
new TabDelegate(false /* incognito */).launchUrl(
WEB_HISTORY_URL, TabLaunchType.FROM_CHROME_UI);
}
});
general_summary.setLinkClickDelegate(new Runnable() {
@Override
public void run() {
HelpAndFeedback.getInstance(getActivity()).show(
getActivity(),
getResources().getString(R.string.help_context_clear_browsing_data),
Profile.getLastUsedProfile(),
null);
}
});
if (ChromeSigninController.get().isSignedIn()) {
general_summary.setSummary(
R.string.clear_browsing_data_footnote_sync_and_site_settings);
} else {
getPreferenceScreen().removePreference(google_summary);
general_summary.setSummary(R.string.clear_browsing_data_footnote_site_settings);
}
}
/**
* Creates a new tab and posts to UI.
* @param loadUrlParams parameters of the url load.
* @param type Information about how the tab was launched.
* @param parent the parent tab, if present.
* @param intent the source of the url if it isn't null.
* @return The new tab.
*/
private Tab createNewTab(LoadUrlParams loadUrlParams, TabModel.TabLaunchType type,
Tab parent, Intent intent) {
// If parent is in the same tab model, place the new tab next to it.
int position = TabModel.INVALID_TAB_INDEX;
int index = mTabModel.indexOf(parent);
if (index != TabModel.INVALID_TAB_INDEX) position = index + 1;
return createNewTab(loadUrlParams, type, parent, position, intent);
}
/**
* Creates a Tab to host the given WebContents asynchronously.
* @param asyncParams Parameters to create the Tab with, including the URL.
* @param type Information about how the tab was launched.
* @param parentId ID of the parent tab, if it exists.
*/
public void createNewTab(
AsyncTabCreationParams asyncParams, TabLaunchType type, int parentId) {
assert asyncParams != null;
// Tabs should't be launched in affiliated mode when a webcontents exists.
assert !(type == TabLaunchType.FROM_LONGPRESS_BACKGROUND
&& asyncParams.getWebContents() != null);
Intent intent = createNewTabIntent(
asyncParams, parentId, type == TabLaunchType.FROM_CHROME_UI);
IntentHandler.startActivityForTrustedIntent(intent);
}
/**
* Creates a new tab and posts to UI.
* @param loadUrlParams parameters of the url load.
* @param type Information about how the tab was launched.
* @param parent the parent tab, if present.
* @param intent the source of the url if it isn't null.
* @return The new tab.
*/
public Tab createNewTab(LoadUrlParams loadUrlParams, TabModel.TabLaunchType type,
Tab parent, Intent intent) {
// If parent is in the same tab model, place the new tab next to it.
int position = TabModel.INVALID_TAB_INDEX;
int index = mTabModel.indexOf(parent);
if (index != TabModel.INVALID_TAB_INDEX) position = index + 1;
return createNewTab(loadUrlParams, type, parent, position, intent);
}
@Override
public Tab openNewTab(LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent,
boolean incognito) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(loadUrlParams.getUrl()));
intent.setPackage(mApplicationContext.getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mApplicationContext.startActivity(intent);
return null;
}
/**
* Constructs an observer that should be notified of tabs changes for all tabs owned
* by a specified {@link TabModelSelector}. Any Tabs created after this call will be
* observed as well, and Tabs removed will no longer have their information broadcast.
*
* <p>
* {@link #destroy()} must be called to unregister this observer.
*
* @param selector The selector that owns the Tabs that should notify this observer.
*/
public TabModelSelectorTabObserver(TabModelSelector selector) {
mTabModelSelector = selector;
mTabModelObserver = new TabModelSelectorTabModelObserver(selector) {
@Override
public void didAddTab(Tab tab, TabLaunchType type) {
// This observer is automatically removed by tab when it is destroyed.
tab.addObserver(TabModelSelectorTabObserver.this);
}
@Override
public void tabRemoved(Tab tab) {
tab.removeObserver(TabModelSelectorTabObserver.this);
}
@Override
protected void onRegistrationComplete() {
List<TabModel> tabModels = mTabModelSelector.getModels();
for (int i = 0; i < tabModels.size(); i++) {
TabModel tabModel = tabModels.get(i);
TabList comprehensiveTabList = tabModel.getComprehensiveModel();
for (int j = 0; j < comprehensiveTabList.getCount(); j++) {
comprehensiveTabList.getTabAt(j).addObserver(
TabModelSelectorTabObserver.this);
}
}
}
};
}
/**
* Creates a new tab and loads the NTP.
*/
public final void launchNTP() {
try {
TraceEvent.begin("TabCreator.launchNTP");
launchUrl(UrlConstants.NTP_URL, TabModel.TabLaunchType.FROM_CHROME_UI);
} finally {
TraceEvent.end("TabCreator.launchNTP");
}
}
/**
* Determine the insertion index of the next tab. If it's not the result of
* a link being pressed, the provided index will be returned.
*
* @param type The launch type of the new tab.
* @param position The provided position.
* @return Where to insert the tab.
*/
public int determineInsertionIndex(TabLaunchType type, int position, Tab newTab) {
if (linkClicked(type)) {
position = determineInsertionIndex(type, newTab);
}
if (willOpenInForeground(type, newTab.isIncognito())) {
// Forget any existing relationships, we don't want to make things
// too confusing by having multiple groups active at the same time.
forgetAllOpeners();
}
return position;
}
@Override
public void didAddTab(Tab tab, TabLaunchType type) {
if (type == TabLaunchType.FROM_RESTORE) {
getActiveLayout().onTabRestored(time(), tab.getId());
} else {
int newIndex = TabModelUtils.getTabIndexById(getTabModelSelector().getModel(false),
tab.getId());
getActiveLayout().onTabCreated(time(), tab.getId(), newIndex,
getTabModelSelector().getCurrentTabId(), false, false, 0f, 0f);
}
}
/**
* Determine the insertion index of the next tab.
*
* @param type The launch type of the new tab.
* @return Where to insert the tab.
*/
public int determineInsertionIndex(TabLaunchType type, Tab newTab) {
TabModel currentModel = mTabModelSelector.getCurrentModel();
Tab currentTab = TabModelUtils.getCurrentTab(currentModel);
if (currentTab == null) {
assert (currentModel.getCount() == 0);
return 0;
}
int currentId = currentTab.getId();
int currentIndex = TabModelUtils.getTabIndexById(currentModel, currentId);
if (sameModelType(currentModel, newTab)) {
if (willOpenInForeground(type, newTab.isIncognito())) {
// If the tab was opened in the foreground, insert it adjacent to
// the tab that opened that link.
return currentIndex + 1;
} else {
// If the tab was opened in the background, position at the end of
// it's 'group'.
int index = getIndexOfLastTabOpenedBy(currentId, currentIndex);
if (index != NO_TAB) {
return index + 1;
} else {
return currentIndex + 1;
}
}
} else {
// If the tab is opening in the other model type, just put it at the end.
return mTabModelSelector.getModel(newTab.isIncognito()).getCount();
}
}
@Override
public boolean createTabWithWebContents(Tab parent, WebContents webContents, int parentId,
TabLaunchType type, String url) {
if (url == null) url = "";
AsyncTabCreationParams asyncParams =
new AsyncTabCreationParams(
new LoadUrlParams(url, PageTransition.AUTO_TOPLEVEL), webContents);
createNewTab(asyncParams, type, parentId);
return true;
}
/**
* Called when Chrome decides to override URL loading and show an intent picker.
*/
private void onOverrideUrlLoadingAndLaunchIntent() {
if (mTab.getWebContents() == null) return;
// Before leaving Chrome, close the empty child tab.
// If a new tab is created through JavaScript open to load this
// url, we would like to close it as we will load this url in a
// different Activity.
if (shouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent()) {
if (mTab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP) {
// Moving task back before closing the tab allows back button to function better
// when Chrome was an intermediate link redirector between two apps.
// crbug.com/487938.
mTab.getActivity().moveTaskToBack(false);
}
mTab.getTabModelSelector().closeTab(mTab);
} else if (mTab.getTabRedirectHandler().isOnNavigation()) {
int lastCommittedEntryIndexBeforeNavigation = mTab.getTabRedirectHandler()
.getLastCommittedEntryIndexBeforeStartingNavigation();
if (getLastCommittedEntryIndex() > lastCommittedEntryIndexBeforeNavigation) {
// http://crbug/426679 : we want to go back to the last committed entry index which
// was saved before this navigation, and remove the empty entries from the
// navigation history.
mClearAllForwardHistoryRequired = true;
mTab.getWebContents().getNavigationController().goToNavigationIndex(
lastCommittedEntryIndexBeforeNavigation);
}
}
}
@Override
public Tab openNewTab(LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent,
boolean incognito) {
TabDelegate delegate = getTabCreator(incognito);
delegate.createNewTab(loadUrlParams, type, parent);
return null;
}
@Override
public Tab openNewTab(LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent,
boolean incognito) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(loadUrlParams.getUrl()));
intent.setPackage(mApplicationContext.getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mApplicationContext.startActivity(intent);
return null;
}
private Tab createMainTab() {
CustomTabsConnection customTabsConnection =
CustomTabsConnection.getInstance(getApplication());
String url = getUrlToLoad();
// Get any referrer that has been explicitly set by the app.
String referrerUrl = IntentHandler.getReferrerUrlIncludingExtraHeaders(getIntent(), this);
if (referrerUrl == null) {
Referrer referrer = customTabsConnection.getReferrerForSession(mSession);
if (referrer != null) referrerUrl = referrer.getUrl();
}
Tab tab = new Tab(TabIdManager.getInstance().generateValidId(Tab.INVALID_TAB_ID),
Tab.INVALID_TAB_ID, false, this, getWindowAndroid(),
TabLaunchType.FROM_EXTERNAL_APP, null, null);
tab.setAppAssociatedWith(customTabsConnection.getClientPackageNameForSession(mSession));
mPrerenderedUrl = customTabsConnection.getPrerenderedUrl(mSession);
WebContents webContents =
customTabsConnection.takePrerenderedUrl(mSession, url, referrerUrl);
mHasPrerendered = webContents != null;
if (webContents == null) webContents = customTabsConnection.takeSpareWebContents();
if (webContents == null) webContents = WebContentsFactory.createWebContents(false, false);
tab.initialize(webContents, getTabContentManager(),
new CustomTabDelegateFactory(mIntentDataProvider.shouldEnableUrlBarHiding()), false,
false);
tab.getTabRedirectHandler().updateIntent(getIntent());
tab.getView().requestFocus();
mTabObserver = new CustomTabObserver(getApplication(), mSession);
tab.addObserver(mTabObserver);
return tab;
}
@Override
public void onOpenInNewTab(String url, Referrer referrer) {
RecordUserAction.record("MobileNewTabOpened");
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setReferrer(referrer);
Tab newTab = mTab.getTabModelSelector().openNewTab(
loadUrlParams, TabLaunchType.FROM_LONGPRESS_BACKGROUND, mTab, isIncognito());
// {@code newTab} is null in document mode. Do not record metrics for document mode.
if (mTab.getTabUma() != null && newTab != null) {
mTab.getTabUma().onBackgroundTabOpenedFromContextMenu(newTab);
}
}
/**
* Determine the insertion index of the next tab. If it's not the result of
* a link being pressed, the provided index will be returned.
*
* @param type The launch type of the new tab.
* @param position The provided position.
* @return Where to insert the tab.
*/
public int determineInsertionIndex(TabLaunchType type, int position, Tab newTab) {
if (linkClicked(type)) {
position = determineInsertionIndex(type, newTab);
}
if (willOpenInForeground(type, newTab.isIncognito())) {
// Forget any existing relationships, we don't want to make things
// too confusing by having multiple groups active at the same time.
forgetAllOpeners();
}
return position;
}
@Override
public void didAddTab(Tab tab, TabLaunchType type) {
if (type == TabLaunchType.FROM_RESTORE) {
getActiveLayout().onTabRestored(time(), tab.getId());
} else {
int newIndex = TabModelUtils.getTabIndexById(getTabModelSelector().getModel(false),
tab.getId());
getActiveLayout().onTabCreated(time(), tab.getId(), newIndex,
getTabModelSelector().getCurrentTabId(), false, false, 0f, 0f);
}
}
/**
* Creates a fresh tab. initialize() needs to be called afterwards to complete the second level
* initialization.
* @param initiallyHidden true iff the tab being created is initially in background
*/
public static Tab createLiveTab(int id, ChromeActivity activity, boolean incognito,
WindowAndroid nativeWindow, TabLaunchType type, int parentId, boolean initiallyHidden) {
return new Tab(id, parentId, incognito, activity, nativeWindow, type, initiallyHidden
? TabCreationState.LIVE_IN_BACKGROUND
: TabCreationState.LIVE_IN_FOREGROUND, null);
}