Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2839)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/webapps/FullScreenActivityTab.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.webapps;
6
7 import android.content.Context;
8 import android.content.Intent;
9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.text.TextUtils;
12 import android.util.Log;
13 import android.view.ContextMenu;
14 import android.view.Menu;
15
16 import com.google.android.apps.chrome.R;
17
18 import org.chromium.base.annotations.SuppressFBWarnings;
19 import org.chromium.chrome.browser.ChromeActivity;
20 import org.chromium.chrome.browser.IntentHandler;
21 import org.chromium.chrome.browser.ShortcutHelper;
22 import org.chromium.chrome.browser.Tab;
23 import org.chromium.chrome.browser.TabState;
24 import org.chromium.chrome.browser.TabUma.TabCreationState;
25 import org.chromium.chrome.browser.UrlUtilities;
26 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager;
27 import org.chromium.chrome.browser.contextmenu.ContextMenuHelper;
28 import org.chromium.chrome.browser.contextmenu.ContextMenuParams;
29 import org.chromium.chrome.browser.contextmenu.ContextMenuPopulator;
30 import org.chromium.chrome.browser.document.ChromeLauncherActivity;
31 import org.chromium.chrome.browser.tab.ChromeTab;
32 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
33 import org.chromium.chrome.browser.util.StreamUtil;
34 import org.chromium.content_public.browser.LoadUrlParams;
35 import org.chromium.content_public.browser.WebContents;
36 import org.chromium.content_public.browser.WebContentsObserver;
37 import org.chromium.ui.base.Clipboard;
38 import org.chromium.ui.base.PageTransition;
39 import org.chromium.ui.base.WindowAndroid;
40
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.FileNotFoundException;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46 import java.net.URI;
47
48 /**
49 * A tab that will be used for FullScreenActivity. See {@link FullScreenActivity } for more.
50 */
51 @SuppressFBWarnings("URF_UNREAD_FIELD")
52 public class FullScreenActivityTab extends ChromeTab {
53 private static final String TAG = "FullScreenActivityTab";
54
55 /**
56 * A delegate to determine top controls visibility.
57 */
58 public interface TopControlsVisibilityDelegate {
59 /**
60 * Determines whether top controls should be shown.
61 *
62 * @param uri The URI to display.
63 * @param securityLevel Security level of the Tab.
64 * @return Whether the URL bar should be visible or not.
65 */
66 boolean shouldShowTopControls(String uri, int securityLevel);
67 }
68
69 static final String BUNDLE_TAB_ID = "tabId";
70 static final String BUNDLE_TAB_URL = "tabUrl";
71
72 private WebContentsObserver mObserver;
73 private TopControlsVisibilityDelegate mTopControlsVisibilityDelegate;
74
75 private FullScreenActivityTab(ChromeActivity activity, WindowAndroid window,
76 TopControlsVisibilityDelegate topControlsVisibilityDelegate) {
77 super(INVALID_TAB_ID, activity, false, window, TabLaunchType.FROM_MENU_O R_OVERVIEW,
78 INVALID_TAB_ID, null, null);
79 initializeFullScreenActivityTab(
80 activity.getTabContentManager(), false, topControlsVisibilityDel egate);
81 }
82
83 private FullScreenActivityTab(int id, ChromeActivity activity, WindowAndroid window,
84 TabState state, TopControlsVisibilityDelegate topControlsVisibilityD elegate) {
85 super(id, activity, false, window, TabLaunchType.FROM_RESTORE, Tab.INVAL ID_TAB_ID,
86 TabCreationState.FROZEN_ON_RESTORE, state);
87 initializeFullScreenActivityTab(
88 activity.getTabContentManager(), true, topControlsVisibilityDele gate);
89 }
90
91 private void initializeFullScreenActivityTab(TabContentManager tabContentMan ager,
92 boolean unfreeze, TopControlsVisibilityDelegate topControlsVisibilit yDelegate) {
93 initialize(null, tabContentManager, false);
94 if (unfreeze) unfreezeContents();
95 mObserver = createWebContentsObserver();
96 mTopControlsVisibilityDelegate = topControlsVisibilityDelegate;
97 }
98
99 /**
100 * Saves the state of the tab out to the {@link Bundle}.
101 */
102 void saveInstanceState(Bundle outState) {
103 outState.putInt(BUNDLE_TAB_ID, getId());
104 outState.putString(BUNDLE_TAB_URL, getUrl());
105 }
106
107 /**
108 * @return WebContentsObserver that watches for changes.
109 */
110 private WebContentsObserver createWebContentsObserver() {
111 return new WebContentsObserver(getWebContents()) {
112 @Override
113 public void didCommitProvisionalLoadForFrame(
114 long frameId, boolean isMainFrame, String url, int transitio nType) {
115 if (isMainFrame) {
116 // Notify the renderer to permanently hide the top controls since they do
117 // not apply to fullscreen content views.
118 updateTopControlsState(getTopControlsStateConstraints(),
119 getTopControlsStateConstraints(), true);
120 }
121 }
122 };
123 }
124
125 @Override
126 protected void initContentViewCore(WebContents webContents) {
127 super.initContentViewCore(webContents);
128 getContentViewCore().setFullscreenRequiredForOrientationLock(false);
129 }
130
131 /**
132 * Loads the given {@code url}.
133 * @param url URL to load.
134 */
135 public void loadUrl(String url) {
136 loadUrl(new LoadUrlParams(url, PageTransition.AUTO_TOPLEVEL));
137 }
138
139 /**
140 * Saves the tab data out to a file.
141 */
142 void saveState(File activityDirectory) {
143 File tabFile = getTabFile(activityDirectory, getId());
144
145 FileOutputStream foutput = null;
146 try {
147 foutput = new FileOutputStream(tabFile);
148 TabState.saveState(foutput, getState(), false);
149 } catch (FileNotFoundException exception) {
150 Log.e(TAG, "Failed to save out tab state.", exception);
151 } catch (IOException exception) {
152 Log.e(TAG, "Failed to save out tab state.", exception);
153 } finally {
154 StreamUtil.closeQuietly(foutput);
155 }
156 }
157
158 /**
159 * @return {@link File} pointing at the tab state for this Activity.
160 */
161 private static File getTabFile(File activityDirectory, int tabId) {
162 return new File(activityDirectory, TabState.getTabStateFilename(tabId, f alse));
163 }
164
165 /**
166 * Creates the {@link FullScreenActivityTab} used by the FullScreenActivity.
167 * If the {@code savedInstanceState} exists, then the user did not intention ally close the app
168 * by swiping it away in the recent tasks list. In that case, we try to res tore the tab from
169 * disk.
170 * @param activity Activity that will own the Tab.
171 * @param directory Directory associated with the Activity. Null implies ta b state isn't saved.
172 * @param savedInstanceState Bundle saved out when the app was killed by And roid. May be null.
173 * @param topControlsVisibilityDelegate Delegate to determine top controls v isibility.
174 * @return {@link FullScreenActivityTab} for the Activity.
175 */
176 public static FullScreenActivityTab create(ChromeActivity activity, WindowAn droid window,
177 File directory, Bundle savedInstanceState,
178 TopControlsVisibilityDelegate topControlsVisibilityDelegate) {
179 FullScreenActivityTab tab = null;
180
181 int tabId = Tab.INVALID_TAB_ID;
182 String tabUrl = null;
183 if (savedInstanceState != null) {
184 tabId = savedInstanceState.getInt(BUNDLE_TAB_ID, INVALID_TAB_ID);
185 tabUrl = savedInstanceState.getString(BUNDLE_TAB_URL);
186 }
187
188 if (tabId != Tab.INVALID_TAB_ID && tabUrl != null && directory != null) {
189 FileInputStream stream = null;
190 try {
191 // Restore the tab.
192 stream = new FileInputStream(getTabFile(directory, tabId));
193 TabState tabState = TabState.readState(stream, false);
194 tab = new FullScreenActivityTab(
195 tabId, activity, window, tabState, topControlsVisibility Delegate);
196 } catch (FileNotFoundException exception) {
197 Log.e(TAG, "Failed to restore tab state.", exception);
198 } catch (IOException exception) {
199 Log.e(TAG, "Failed to restore tab state.", exception);
200 } finally {
201 StreamUtil.closeQuietly(stream);
202 }
203 }
204
205 if (tab == null) {
206 // Create a new tab.
207 tab = new FullScreenActivityTab(activity, window, topControlsVisibil ityDelegate);
208 }
209
210 return tab;
211 }
212
213 @Override
214 protected ContextMenuPopulator createContextMenuPopulator() {
215 return new ContextMenuPopulator() {
216 private final Clipboard mClipboard;
217
218 // public ContextMenuPopulator()
219 {
220 mClipboard = new Clipboard(getApplicationContext());
221 }
222
223 @Override
224 public boolean shouldShowContextMenu(ContextMenuParams params) {
225 return params != null && params.isAnchor();
226 }
227
228 @Override
229 public boolean onItemSelected(ContextMenuHelper helper, ContextMenuP arams params,
230 int itemId) {
231 if (itemId == org.chromium.chrome.R.id.contextmenu_copy_link_add ress_text) {
232 String url = params.getUnfilteredLinkUrl();
233 mClipboard.setText(url, url);
234 return true;
235 } else if (itemId == org.chromium.chrome.R.id.contextmenu_copy_l ink_text) {
236 String text = params.getLinkText();
237 mClipboard.setText(text, text);
238 return true;
239 } else if (itemId == R.id.menu_id_open_in_chrome) {
240 Intent chromeIntent =
241 new Intent(Intent.ACTION_VIEW, Uri.parse(params.getL inkUrl()));
242 chromeIntent.setPackage(getApplicationContext().getPackageNa me());
243 chromeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
244
245 boolean activityStarted = false;
246 if (params.getPageUrl() != null) {
247 try {
248 URI pageUri = URI.create(params.getPageUrl());
249 if (UrlUtilities.isInternalScheme(pageUri)) {
250 IntentHandler.startChromeLauncherActivityForTrus tedIntent(
251 chromeIntent, getApplicationContext());
252 activityStarted = true;
253 }
254 } catch (IllegalArgumentException ex) {
255 // Ignore the exception for creating the URI and lau nch the intent
256 // without the trusted intent extras.
257 }
258 }
259
260 if (!activityStarted) {
261 getApplicationContext().startActivity(chromeIntent);
262 activityStarted = true;
263 }
264 return true;
265 }
266
267 return false;
268 }
269
270 @Override
271 public void buildContextMenu(ContextMenu menu, Context context,
272 ContextMenuParams params) {
273 menu.add(Menu.NONE, org.chromium.chrome.R.id.contextmenu_copy_li nk_address_text,
274 Menu.NONE, org.chromium.chrome.R.string.contextmenu_copy _link_address);
275
276 String linkText = params.getLinkText();
277 if (linkText != null) linkText = linkText.trim();
278
279 if (!TextUtils.isEmpty(linkText)) {
280 menu.add(Menu.NONE, org.chromium.chrome.R.id.contextmenu_cop y_link_text,
281 Menu.NONE, org.chromium.chrome.R.string.contextmenu_ copy_link_text);
282 }
283
284 menu.add(Menu.NONE, R.id.menu_id_open_in_chrome, Menu.NONE,
285 R.string.menu_open_in_chrome);
286 }
287 };
288 }
289
290 @Override
291 protected boolean isHidingTopControlsEnabled() {
292 if (getFullscreenManager() == null) return true;
293 if (getFullscreenManager().getPersistentFullscreenMode()) return true;
294 if (mTopControlsVisibilityDelegate == null) return false;
295 return !mTopControlsVisibilityDelegate.shouldShowTopControls(getUrl(), g etSecurityLevel());
296 }
297
298 @Override
299 public boolean isShowingTopControlsEnabled() {
300 // On webapp activity and embedd content view activity, it's either hidi ng or showing.
301 // Users cannot change the visibility state by sliding it in or out.
302 return !isHidingTopControlsEnabled();
303 }
304
305 @Override
306 protected FullScreenTabWebContentsDelegateAndroid createWebContentsDelegate( ) {
307 return new FullScreenTabWebContentsDelegateAndroid();
308 }
309
310 /**
311 * A FullScreenActivityTab is meant to be used for WebappActivities which
312 * behave slightly differently from tabs. The main difference being that
313 * it doesn't have a notion of active tab. Thus, some WebContentsDelegate
314 * method have to be redefined.
315 */
316 public class FullScreenTabWebContentsDelegateAndroid
317 extends TabChromeWebContentsDelegateAndroidImpl {
318 @Override
319 public void activateContents() {
320 if (!(mActivity instanceof WebappActivity)) return;
321
322 WebappInfo webAppInfo = ((WebappActivity) mActivity).getWebappInfo() ;
323 String url = webAppInfo.uri().toString();
324
325 Intent intent = new Intent();
326 intent.setAction(Intent.ACTION_MAIN);
327 intent.setPackage(mActivity.getPackageName());
328 intent.putExtra(ChromeLauncherActivity.EXTRA_BRING_WEBAPP_TO_FRONT, true);
329
330 intent.putExtra(ShortcutHelper.EXTRA_ICON, webAppInfo.getEncodedIcon ());
331 intent.putExtra(ShortcutHelper.EXTRA_ID, webAppInfo.id());
332 intent.putExtra(ShortcutHelper.EXTRA_URL, url);
333 intent.putExtra(ShortcutHelper.EXTRA_TITLE, webAppInfo.title());
334 intent.putExtra(ShortcutHelper.EXTRA_ORIENTATION, webAppInfo.orienta tion());
335 intent.putExtra(ShortcutHelper.EXTRA_MAC, ShortcutHelper.getEncodedM ac(mActivity, url));
336 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
337
338 getApplicationContext().startActivity(intent);
339 }
340 }
341 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698