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.ntp; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.app.Activity; |
| 9 import android.app.ActivityManager; |
| 10 import android.app.ActivityManager.RecentTaskInfo; |
| 11 import android.app.Dialog; |
| 12 import android.content.Intent; |
| 13 import android.os.Build; |
| 14 import android.text.TextUtils; |
| 15 |
| 16 import org.chromium.base.ThreadUtils; |
| 17 import org.chromium.chrome.browser.ChromeMobileApplication; |
| 18 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSession; |
| 19 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSessionTab; |
| 20 import org.chromium.chrome.browser.IntentHandler; |
| 21 import org.chromium.chrome.browser.RecentlyClosedBridge.RecentlyClosedTab; |
| 22 import org.chromium.chrome.browser.Tab; |
| 23 import org.chromium.chrome.browser.UrlConstants; |
| 24 import org.chromium.chrome.browser.document.DocumentUtils; |
| 25 import org.chromium.chrome.browser.tabmodel.document.ActivityDelegate; |
| 26 import org.chromium.chrome.browser.tabmodel.document.DocumentTabModel; |
| 27 import org.chromium.chrome.browser.tabmodel.document.DocumentTabModelImpl; |
| 28 import org.chromium.ui.WindowOpenDisposition; |
| 29 |
| 30 import java.util.ArrayList; |
| 31 import java.util.List; |
| 32 |
| 33 /** |
| 34 * ChromeHome specific version of RecentTabsManager that allows opening new Docu
mentActivities |
| 35 * instead of tabs. |
| 36 */ |
| 37 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 38 public class DocumentRecentTabsManager extends RecentTabsManager { |
| 39 /** |
| 40 * The number of ms to delay opening a new tab, so that we have time to hide
dialog before |
| 41 * the screenshot of the current page is taken. |
| 42 */ |
| 43 private static final int NEW_TAB_DELAY_MS = 150; |
| 44 private final Activity mActivity; |
| 45 private final boolean mFinishActivityOnOpen; |
| 46 private final List<CurrentlyOpenTab> mCurrentlyOpenTabs; |
| 47 private final DocumentTabModel mTabModel; |
| 48 private final DocumentTabModel.InitializationObserver mUpdateOpenTabsObserve
r; |
| 49 private Dialog mDialog; |
| 50 |
| 51 private boolean mShowingAllInCurrentTabs; |
| 52 |
| 53 /** |
| 54 * @param activity Activity that should be used to launch intents. |
| 55 */ |
| 56 public DocumentRecentTabsManager(Tab tab, Activity activity, boolean finishA
ctivityOnOpen) { |
| 57 super(tab, tab.getProfile().getOriginalProfile(), activity); |
| 58 mActivity = activity; |
| 59 mFinishActivityOnOpen = finishActivityOnOpen; |
| 60 mCurrentlyOpenTabs = new ArrayList<CurrentlyOpenTab>(); |
| 61 mTabModel = |
| 62 ChromeMobileApplication.getDocumentTabModelSelector().getModel(t
ab.isIncognito()); |
| 63 mUpdateOpenTabsObserver = new DocumentTabModel.InitializationObserver(mT
abModel) { |
| 64 @Override |
| 65 public boolean isSatisfied(int currentState) { |
| 66 return currentState >= DocumentTabModelImpl.STATE_FULLY_LOAD
ED; |
| 67 } |
| 68 |
| 69 @Override |
| 70 public boolean isCanceled() { |
| 71 return mActivity.isDestroyed() || mActivity.isFinishing(); |
| 72 } |
| 73 |
| 74 @Override |
| 75 protected void runImmediately() { |
| 76 updateCurrentlyOpenTabsWhenDatabaseReady(); |
| 77 } |
| 78 }; |
| 79 updateCurrentlyOpenTabs(); |
| 80 } |
| 81 |
| 82 /** |
| 83 * @param dialog Dialog that displays the RecentTabsPage and will be dismiss
ed when |
| 84 * a link is opened. |
| 85 */ |
| 86 public void setDialog(Dialog dialog) { |
| 87 mDialog = dialog; |
| 88 } |
| 89 |
| 90 @Override |
| 91 public void destroy() { |
| 92 super.destroy(); |
| 93 } |
| 94 |
| 95 @Override |
| 96 public void openForeignSessionTab(final ForeignSession session, final Foreig
nSessionTab tab, |
| 97 int windowDisposition) { |
| 98 // Hide the dialog for screenshot. We don't want to dismiss yet because
that will destroy |
| 99 // the NativePage objects we need in the delayed runnable. |
| 100 if (mDialog != null) mDialog.hide(); |
| 101 ThreadUtils.postOnUiThreadDelayed(new Runnable() { |
| 102 @Override |
| 103 public void run() { |
| 104 DocumentRecentTabsManager.super.openForeignSessionTab( |
| 105 session, tab, WindowOpenDisposition.NEW_FOREGROUND_TAB); |
| 106 if (mDialog != null) mDialog.dismiss(); |
| 107 if (mFinishActivityOnOpen) mActivity.finishAndRemoveTask(); |
| 108 } |
| 109 }, NEW_TAB_DELAY_MS); |
| 110 } |
| 111 |
| 112 @Override |
| 113 public void openRecentlyClosedTab(final RecentlyClosedTab tab, int windowDis
position) { |
| 114 // Hide the dialog for screenshot. We don't want to dismiss yet because
that will destroy |
| 115 // the NativePage objects we need in the delayed runnable. |
| 116 if (mDialog != null) mDialog.hide(); |
| 117 ThreadUtils.postOnUiThreadDelayed(new Runnable() { |
| 118 @Override |
| 119 public void run() { |
| 120 DocumentRecentTabsManager.super.openRecentlyClosedTab( |
| 121 tab, WindowOpenDisposition.NEW_FOREGROUND_TAB); |
| 122 if (mDialog != null) mDialog.dismiss(); |
| 123 if (mFinishActivityOnOpen) mActivity.finishAndRemoveTask(); |
| 124 } |
| 125 }, NEW_TAB_DELAY_MS); |
| 126 } |
| 127 |
| 128 @Override |
| 129 public void openHistoryPage() { |
| 130 if (mDialog != null) mDialog.dismiss(); |
| 131 super.openHistoryPage(); |
| 132 } |
| 133 |
| 134 @Override |
| 135 public List<CurrentlyOpenTab> getCurrentlyOpenTabs() { |
| 136 return mCurrentlyOpenTabs; |
| 137 } |
| 138 |
| 139 @Override |
| 140 public void setCurrentlyOpenTabsShowAll(boolean showingAll) { |
| 141 mShowingAllInCurrentTabs = showingAll; |
| 142 postUpdate(); |
| 143 } |
| 144 |
| 145 @Override |
| 146 public boolean isCurrentlyOpenTabsShowingAll() { |
| 147 return mShowingAllInCurrentTabs; |
| 148 } |
| 149 |
| 150 @Override |
| 151 protected void updateCurrentlyOpenTabs() { |
| 152 mUpdateOpenTabsObserver.runWhenReady(); |
| 153 } |
| 154 |
| 155 private void updateCurrentlyOpenTabsWhenDatabaseReady() { |
| 156 final int currentTabId = ActivityDelegate.getTabIdFromIntent(mActivity.g
etIntent()); |
| 157 |
| 158 ActivityManager am = (ActivityManager) mActivity.getSystemService( |
| 159 Activity.ACTIVITY_SERVICE); |
| 160 List<ActivityManager.AppTask> taskList = am.getAppTasks(); |
| 161 for (int i = 0; i < taskList.size(); i++) { |
| 162 RecentTaskInfo taskInfo = DocumentUtils.getTaskInfoFromTask(taskList
.get(i)); |
| 163 if (taskInfo == null) continue; |
| 164 |
| 165 final Intent baseIntent = taskInfo.baseIntent; |
| 166 final int tabId = ActivityDelegate.getTabIdFromIntent(baseIntent); |
| 167 String url = mTabModel.getCurrentUrlForDocument(tabId); |
| 168 if (TextUtils.isEmpty(url) || url.startsWith(UrlConstants.CHROME_NAT
IVE_SCHEME)) { |
| 169 continue; |
| 170 } |
| 171 |
| 172 CharSequence description = taskInfo.description; |
| 173 String title = description != null ? description.toString() : ""; |
| 174 |
| 175 final Runnable startNewDocument = new Runnable() { |
| 176 @Override |
| 177 public void run() { |
| 178 Intent newIntent = new Intent(); |
| 179 newIntent.setAction(Intent.ACTION_MAIN); |
| 180 newIntent.setPackage(mActivity.getPackageName()); |
| 181 newIntent.putExtra( |
| 182 IntentHandler.TabOpenType.BRING_TAB_TO_FRONT.name(),
tabId); |
| 183 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 184 mActivity.startActivity(newIntent); |
| 185 } |
| 186 }; |
| 187 |
| 188 Runnable onClickRunnable = new Runnable() { |
| 189 @Override |
| 190 public void run() { |
| 191 if (mDialog != null) mDialog.dismiss(); |
| 192 if (currentTabId != tabId) { |
| 193 ThreadUtils.postOnUiThreadDelayed(startNewDocument, NEW_
TAB_DELAY_MS); |
| 194 } |
| 195 } |
| 196 }; |
| 197 mCurrentlyOpenTabs.add(new CurrentlyOpenTab(url, title, onClickRunna
ble)); |
| 198 } |
| 199 } |
| 200 } |
OLD | NEW |