OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.hosted; |
| 6 |
| 7 import android.content.Intent; |
| 8 import android.net.Uri; |
| 9 import android.view.MenuItem; |
| 10 import android.view.View; |
| 11 import android.view.View.OnClickListener; |
| 12 import android.view.ViewGroup; |
| 13 |
| 14 import com.google.android.apps.chrome.R; |
| 15 |
| 16 import org.chromium.base.ApiCompatibilityUtils; |
| 17 import org.chromium.chrome.browser.CompositorChromeActivity; |
| 18 import org.chromium.chrome.browser.IntentHandler; |
| 19 import org.chromium.chrome.browser.Tab; |
| 20 import org.chromium.chrome.browser.appmenu.AppMenuHandler; |
| 21 import org.chromium.chrome.browser.appmenu.AppMenuObserver; |
| 22 import org.chromium.chrome.browser.compositor.layouts.LayoutManagerDocument; |
| 23 import org.chromium.chrome.browser.document.BrandColorUtils; |
| 24 import org.chromium.chrome.browser.tabmodel.SingleTabModelSelector; |
| 25 import org.chromium.chrome.browser.toolbar.ToolbarControlContainer; |
| 26 import org.chromium.chrome.browser.toolbar.ToolbarHelper; |
| 27 import org.chromium.chrome.browser.widget.findinpage.FindToolbarManager; |
| 28 import org.chromium.content_public.browser.LoadUrlParams; |
| 29 |
| 30 /** |
| 31 * The activity for hosted mode. It will be launched on top of a client's task. |
| 32 */ |
| 33 public class HostedActivity extends CompositorChromeActivity { |
| 34 private HostedTab mTab; |
| 35 private ToolbarHelper mToolbarHelper; |
| 36 private HostedAppMenuPropertiesDelegate mAppMenuPropertiesDelegate; |
| 37 private AppMenuHandler mAppMenuHandler; |
| 38 private FindToolbarManager mFindToolbarManager; |
| 39 private HostedIntentDataProvider mIntentDataProvider; |
| 40 |
| 41 // This is to give the right package name while using the client's resources
during an |
| 42 // overridePendingTransition call. |
| 43 // TODO(ianwen, yusufo): Figure out a solution to extract external resources
without having to |
| 44 // change the package name. |
| 45 private boolean mShouldOverridePackage; |
| 46 |
| 47 @Override |
| 48 public void preInflationStartup() { |
| 49 super.preInflationStartup(); |
| 50 setTabModelSelector(new SingleTabModelSelector(this, false, true)); |
| 51 mIntentDataProvider = new HostedIntentDataProvider(getIntent(), this); |
| 52 } |
| 53 |
| 54 @Override |
| 55 public void postInflationStartup() { |
| 56 super.postInflationStartup(); |
| 57 ToolbarControlContainer controlContainer = |
| 58 ((ToolbarControlContainer) findViewById(R.id.control_container))
; |
| 59 mAppMenuPropertiesDelegate = new HostedAppMenuPropertiesDelegate(this, |
| 60 mIntentDataProvider.getMenuTitles()); |
| 61 mAppMenuHandler = new AppMenuHandler(this, mAppMenuPropertiesDelegate, R
.menu.hosted_menu); |
| 62 mToolbarHelper = new ToolbarHelper(this, controlContainer, |
| 63 mAppMenuHandler, mAppMenuPropertiesDelegate, |
| 64 getCompositorViewHolder().getInvalidator()); |
| 65 mToolbarHelper.setThemeColor(mIntentDataProvider.getToolbarColor()); |
| 66 setStatusBarColor(mIntentDataProvider.getToolbarColor()); |
| 67 if (mIntentDataProvider.shouldShowActionButton()) { |
| 68 mToolbarHelper.addCustomActionButton(mIntentDataProvider.getActionBu
ttonIcon(), |
| 69 new OnClickListener() { |
| 70 @Override |
| 71 public void onClick(View v) { |
| 72 mIntentDataProvider.sendButtonPendingIntentWithUrl( |
| 73 getApplicationContext(), mTab.getUrl()); |
| 74 } |
| 75 }); |
| 76 } |
| 77 mAppMenuHandler.addObserver(new AppMenuObserver() { |
| 78 @Override |
| 79 public void onMenuVisibilityChanged(boolean isVisible) { |
| 80 if (!isVisible) { |
| 81 mAppMenuPropertiesDelegate.onMenuDismissed(); |
| 82 } |
| 83 } |
| 84 }); |
| 85 } |
| 86 |
| 87 @Override |
| 88 public void finishNativeInitialization() { |
| 89 String url = IntentHandler.getUrlFromIntent(getIntent()); |
| 90 long sessionId = mIntentDataProvider.getSessionId(); |
| 91 mTab = new HostedTab(this, getWindowAndroid(), sessionId, url, Tab.INVAL
ID_TAB_ID); |
| 92 getTabModelSelector().setTab(mTab); |
| 93 |
| 94 ToolbarControlContainer controlContainer = (ToolbarControlContainer) fin
dViewById( |
| 95 R.id.control_container); |
| 96 LayoutManagerDocument layoutDriver = new LayoutManagerDocument(getCompos
itorViewHolder()); |
| 97 initializeCompositorContent(layoutDriver, findViewById(R.id.url_bar), |
| 98 (ViewGroup) findViewById(android.R.id.content), controlContainer
); |
| 99 mFindToolbarManager = new FindToolbarManager(this, getTabModelSelector()
, |
| 100 mToolbarHelper.getContextualMenuBar().getCustomSelectionActionMo
deCallback()); |
| 101 controlContainer.setFindToolbarManager(mFindToolbarManager); |
| 102 mToolbarHelper.initializeControls( |
| 103 mFindToolbarManager, null, layoutDriver, null, null, null, |
| 104 new OnClickListener() { |
| 105 @Override |
| 106 public void onClick(View v) { |
| 107 HostedActivity.this.finish(); |
| 108 } |
| 109 }); |
| 110 |
| 111 mTab.setFullscreenManager(getFullscreenManager()); |
| 112 mTab.loadUrl(new LoadUrlParams(url)); |
| 113 super.finishNativeInitialization(); |
| 114 } |
| 115 |
| 116 @Override |
| 117 protected void onDeferredStartup() { |
| 118 super.onDeferredStartup(); |
| 119 mToolbarHelper.onDeferredStartup(); |
| 120 } |
| 121 |
| 122 @Override |
| 123 public boolean hasDoneFirstDraw() { |
| 124 return mToolbarHelper.hasDoneFirstDraw(); |
| 125 } |
| 126 |
| 127 /** |
| 128 * Calculate the proper color for status bar and update it. Only works on L
and future versions. |
| 129 */ |
| 130 private void setStatusBarColor(int color) { |
| 131 // If the client did not specify the toolbar color, we do not change the
status bar color. |
| 132 if (color == getResources().getColor(R.color.default_primary_color)) ret
urn; |
| 133 |
| 134 ApiCompatibilityUtils.setStatusBarColor(getWindow(), |
| 135 BrandColorUtils.computeStatusBarColor(color)); |
| 136 } |
| 137 |
| 138 @Override |
| 139 public SingleTabModelSelector getTabModelSelector() { |
| 140 return (SingleTabModelSelector) super.getTabModelSelector(); |
| 141 } |
| 142 |
| 143 @Override |
| 144 protected int getControlContainerLayoutId() { |
| 145 return R.layout.hosted_control_container; |
| 146 } |
| 147 |
| 148 @Override |
| 149 protected int getControlContainerHeightResource() { |
| 150 return R.dimen.hosted_control_container_height; |
| 151 } |
| 152 |
| 153 @Override |
| 154 public String getPackageName() { |
| 155 if (mShouldOverridePackage) return mIntentDataProvider.getClientPackageN
ame(); |
| 156 return super.getPackageName(); |
| 157 } |
| 158 |
| 159 @Override |
| 160 public void finish() { |
| 161 super.finish(); |
| 162 if (mIntentDataProvider.shouldAnimateOnFinish()) { |
| 163 mShouldOverridePackage = true; |
| 164 overridePendingTransition(mIntentDataProvider.getAnimationEnterRes()
, |
| 165 mIntentDataProvider.getAnimationExitRes()); |
| 166 mShouldOverridePackage = false; |
| 167 } |
| 168 } |
| 169 |
| 170 @Override |
| 171 protected boolean handleBackPressed() { |
| 172 if (mTab == null) return false; |
| 173 if (mTab.canGoBack()) { |
| 174 mTab.goBack(); |
| 175 } else { |
| 176 finish(); |
| 177 } |
| 178 return true; |
| 179 } |
| 180 |
| 181 @Override |
| 182 public boolean shouldShowAppMenu() { |
| 183 return mTab != null && mToolbarHelper.isInitialized(); |
| 184 } |
| 185 |
| 186 @Override |
| 187 public boolean onOptionsItemSelected(MenuItem item) { |
| 188 int menuIndex = mAppMenuPropertiesDelegate.getIndexOfMenuItem(item); |
| 189 if (menuIndex >= 0) { |
| 190 mIntentDataProvider.clickMenuItemWithUrl(getApplicationContext(), me
nuIndex, |
| 191 getTabModelSelector().getCurrentTab().getUrl()); |
| 192 return true; |
| 193 } |
| 194 return super.onOptionsItemSelected(item); |
| 195 } |
| 196 |
| 197 @Override |
| 198 public boolean onMenuOrKeyboardAction(int id, boolean fromMenu) { |
| 199 if (id == R.id.show_menu) { |
| 200 if (shouldShowAppMenu()) { |
| 201 mAppMenuHandler.showAppMenu(mToolbarHelper.getMenuAnchor(), true
, false); |
| 202 return true; |
| 203 } |
| 204 } else if (id == R.id.open_in_chrome_id) { |
| 205 String url = getTabModelSelector().getCurrentTab().getUrl(); |
| 206 Intent chromeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
; |
| 207 chromeIntent.setPackage(getApplicationContext().getPackageName()); |
| 208 chromeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 209 startActivity(chromeIntent); |
| 210 return true; |
| 211 } else if (id == R.id.find_in_page_id) { |
| 212 mFindToolbarManager.showToolbar(); |
| 213 return true; |
| 214 } |
| 215 return super.onMenuOrKeyboardAction(id, fromMenu); |
| 216 } |
| 217 } |
OLD | NEW |