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.content.Context; |
| 8 import android.graphics.Bitmap; |
| 9 |
| 10 import org.chromium.base.ObserverList; |
| 11 import org.chromium.chrome.browser.ForeignSessionHelper; |
| 12 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSession; |
| 13 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSessionCallback; |
| 14 import org.chromium.chrome.browser.ForeignSessionHelper.ForeignSessionTab; |
| 15 import org.chromium.chrome.browser.NewTabPagePrefs; |
| 16 import org.chromium.chrome.browser.RecentlyClosedBridge; |
| 17 import org.chromium.chrome.browser.RecentlyClosedBridge.RecentlyClosedCallback; |
| 18 import org.chromium.chrome.browser.RecentlyClosedBridge.RecentlyClosedTab; |
| 19 import org.chromium.chrome.browser.Tab; |
| 20 import org.chromium.chrome.browser.UrlConstants; |
| 21 import org.chromium.chrome.browser.favicon.FaviconHelper; |
| 22 import org.chromium.chrome.browser.favicon.FaviconHelper.FaviconImageCallback; |
| 23 import org.chromium.chrome.browser.ntp.RecentTabsPromoView.SyncPromoModel; |
| 24 import org.chromium.chrome.browser.profiles.Profile; |
| 25 import org.chromium.chrome.browser.signin.SigninManager; |
| 26 import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; |
| 27 import org.chromium.chrome.browser.sync.SyncController; |
| 28 import org.chromium.content_public.browser.LoadUrlParams; |
| 29 import org.chromium.sync.AndroidSyncSettings; |
| 30 import org.chromium.sync.AndroidSyncSettings.AndroidSyncSettingsObserver; |
| 31 import org.chromium.sync.signin.ChromeSigninController; |
| 32 |
| 33 import java.util.Collections; |
| 34 import java.util.List; |
| 35 |
| 36 /** |
| 37 * Provides the domain logic and data for RecentTabsPage and RecentTabsRowAdapte
r. |
| 38 */ |
| 39 public class RecentTabsManager implements AndroidSyncSettingsObserver, SignInSta
teObserver, |
| 40 SyncPromoModel { |
| 41 |
| 42 /** |
| 43 * Implement this to receive updates when the page contents change. |
| 44 */ |
| 45 interface UpdatedCallback { |
| 46 /** |
| 47 * Called when the list of recently closed tabs or foreign sessions chan
ges. |
| 48 */ |
| 49 void onUpdated(); |
| 50 } |
| 51 |
| 52 private static final int RECENTLY_CLOSED_MAX_TAB_COUNT = 5; |
| 53 |
| 54 private final Profile mProfile; |
| 55 private final Tab mTab; |
| 56 private final Context mContext; |
| 57 private final ObserverList<AndroidSyncSettingsObserver> mObservers = |
| 58 new ObserverList<AndroidSyncSettingsObserver>(); |
| 59 |
| 60 private FaviconHelper mFaviconHelper; |
| 61 private ForeignSessionHelper mForeignSessionHelper; |
| 62 private List<ForeignSession> mForeignSessions; |
| 63 private List<RecentlyClosedTab> mRecentlyClosedTabs; |
| 64 private NewTabPagePrefs mNewTabPagePrefs; |
| 65 private RecentlyClosedBridge mRecentlyClosedBridge; |
| 66 private SigninManager mSignInManager; |
| 67 private UpdatedCallback mUpdatedCallback; |
| 68 |
| 69 /** |
| 70 * Create an RecentTabsManager to be used with RecentTabsPage and RecentTabs
RowAdapter. |
| 71 * |
| 72 * @param tab The Tab that is showing this recent tabs page. |
| 73 * @param profile Profile that is associated with the current session. |
| 74 * @param context the Android context this manager will work in. |
| 75 */ |
| 76 public RecentTabsManager(Tab tab, Profile profile, Context context) { |
| 77 mProfile = profile; |
| 78 mTab = tab; |
| 79 mForeignSessionHelper = buildForeignSessionHelper(mProfile); |
| 80 mNewTabPagePrefs = buildNewTabPagePrefs(mProfile); |
| 81 mFaviconHelper = buildFaviconHelper(); |
| 82 mRecentlyClosedBridge = buildRecentlyClosedBridge(mProfile); |
| 83 mSignInManager = SigninManager.get(context); |
| 84 mContext = context; |
| 85 |
| 86 updateRecentlyClosedTabs(); |
| 87 registerForForeignSessionUpdates(); |
| 88 updateForeignSessions(); |
| 89 mForeignSessionHelper.triggerSessionSync(); |
| 90 registerForSignInAndSyncNotifications(); |
| 91 } |
| 92 |
| 93 /** |
| 94 * Should be called when this object is no longer needed. Performs necessary
listener tear down. |
| 95 */ |
| 96 public void destroy() { |
| 97 AndroidSyncSettings.unregisterObserver(mContext, this); |
| 98 |
| 99 mSignInManager.removeSignInStateObserver(this); |
| 100 mSignInManager = null; |
| 101 |
| 102 mFaviconHelper.destroy(); |
| 103 mFaviconHelper = null; |
| 104 |
| 105 mRecentlyClosedBridge.destroy(); |
| 106 mRecentlyClosedBridge = null; |
| 107 |
| 108 mForeignSessionHelper.destroy(); |
| 109 mForeignSessionHelper = null; |
| 110 |
| 111 mUpdatedCallback = null; |
| 112 |
| 113 mNewTabPagePrefs.destroy(); |
| 114 mNewTabPagePrefs = null; |
| 115 } |
| 116 |
| 117 private static ForeignSessionHelper buildForeignSessionHelper(Profile profil
e) { |
| 118 return new ForeignSessionHelper(profile); |
| 119 } |
| 120 |
| 121 private static NewTabPagePrefs buildNewTabPagePrefs(Profile profile) { |
| 122 return new NewTabPagePrefs(profile); |
| 123 } |
| 124 |
| 125 private static FaviconHelper buildFaviconHelper() { |
| 126 return new FaviconHelper(); |
| 127 } |
| 128 |
| 129 private RecentlyClosedBridge buildRecentlyClosedBridge(Profile profile) { |
| 130 RecentlyClosedBridge bridge = new RecentlyClosedBridge(profile); |
| 131 bridge.setRecentlyClosedCallback(new RecentlyClosedCallback() { |
| 132 @Override |
| 133 public void onUpdated() { |
| 134 updateRecentlyClosedTabs(); |
| 135 postUpdate(); |
| 136 } |
| 137 }); |
| 138 return bridge; |
| 139 } |
| 140 |
| 141 private void registerForForeignSessionUpdates() { |
| 142 mForeignSessionHelper.setOnForeignSessionCallback(new ForeignSessionCall
back() { |
| 143 @Override |
| 144 public void onUpdated() { |
| 145 updateForeignSessions(); |
| 146 postUpdate(); |
| 147 } |
| 148 }); |
| 149 } |
| 150 |
| 151 private void registerForSignInAndSyncNotifications() { |
| 152 AndroidSyncSettings.registerObserver(mContext, this); |
| 153 mSignInManager.addSignInStateObserver(this); |
| 154 } |
| 155 |
| 156 protected void updateCurrentlyOpenTabs() { |
| 157 } |
| 158 |
| 159 private void updateRecentlyClosedTabs() { |
| 160 mRecentlyClosedTabs = mRecentlyClosedBridge.getRecentlyClosedTabs( |
| 161 RECENTLY_CLOSED_MAX_TAB_COUNT); |
| 162 } |
| 163 |
| 164 private void updateForeignSessions() { |
| 165 mForeignSessions = mForeignSessionHelper.getForeignSessions(); |
| 166 if (mForeignSessions == null) { |
| 167 mForeignSessions = Collections.emptyList(); |
| 168 } |
| 169 } |
| 170 |
| 171 /** |
| 172 * @return Most up-to-date list of currently open tabs. |
| 173 */ |
| 174 public List<CurrentlyOpenTab> getCurrentlyOpenTabs() { |
| 175 return null; |
| 176 } |
| 177 |
| 178 /** |
| 179 * @return Most up-to-date list of foreign sessions. |
| 180 */ |
| 181 public List<ForeignSession> getForeignSessions() { |
| 182 return mForeignSessions; |
| 183 } |
| 184 |
| 185 /** |
| 186 * @return Most up-to-date list of recently closed tabs. |
| 187 */ |
| 188 public List<RecentlyClosedTab> getRecentlyClosedTabs() { |
| 189 return mRecentlyClosedTabs; |
| 190 } |
| 191 |
| 192 /** |
| 193 * Opens a new tab navigating to ForeignSessionTab. |
| 194 * |
| 195 * @param session The foreign session that the tab belongs to. |
| 196 * @param tab The tab to open. |
| 197 * @param windowDisposition The WindowOpenDisposition flag. |
| 198 */ |
| 199 public void openForeignSessionTab(ForeignSession session, ForeignSessionTab
tab, |
| 200 int windowDisposition) { |
| 201 NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_FOREIGN_SESSION); |
| 202 mForeignSessionHelper.openForeignSessionTab(mTab, session, tab, windowDi
sposition); |
| 203 } |
| 204 |
| 205 /** |
| 206 * Restores a recently closed tab. |
| 207 * |
| 208 * @param tab The tab to open. |
| 209 * @param windowDisposition The WindowOpenDisposition value specifying wheth
er the tab should |
| 210 * be restored into the current tab or a new tab. |
| 211 */ |
| 212 public void openRecentlyClosedTab(RecentlyClosedTab tab, int windowDispositi
on) { |
| 213 NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_RECENTLY_CLOSED_E
NTRY); |
| 214 mRecentlyClosedBridge.openRecentlyClosedTab(mTab, tab, windowDisposition
); |
| 215 } |
| 216 |
| 217 /** |
| 218 * Opens the history page. |
| 219 */ |
| 220 public void openHistoryPage() { |
| 221 mTab.loadUrl(new LoadUrlParams(UrlConstants.HISTORY_URL)); |
| 222 } |
| 223 |
| 224 /** |
| 225 * Returns a 16x16 favicon for a given synced url. |
| 226 * |
| 227 * @param url The url to fetch the favicon for. |
| 228 * @return 16x16 favicon or null if favicon unavailable. |
| 229 */ |
| 230 public Bitmap getSyncedFaviconImageForURL(String url) { |
| 231 return mFaviconHelper.getSyncedFaviconImageForURL(mProfile, url); |
| 232 } |
| 233 |
| 234 /** |
| 235 * Fetches a favicon for snapshot document url which is returned via callbac
k. |
| 236 * |
| 237 * @param url The url to fetch a favicon for. |
| 238 * @param size the desired favicon size. |
| 239 * @param faviconCallback the callback to be invoked when the favicon is ava
ilable. |
| 240 * |
| 241 * @return may return false if we could not fetch the favicon. |
| 242 */ |
| 243 public boolean getLocalFaviconForUrl(String url, int size, |
| 244 FaviconImageCallback faviconCallback) { |
| 245 return mFaviconHelper.getLocalFaviconImageForURL(mProfile, url, |
| 246 FaviconHelper.FAVICON | FaviconHelper.TOUCH_ICON |
| 247 | FaviconHelper.TOUCH_PRECOMPOSED_ICON, |
| 248 size, faviconCallback); |
| 249 } |
| 250 |
| 251 /** |
| 252 * Sets a callback to be invoked when recently closed tabs or foreign sessio
ns documents have |
| 253 * been updated. |
| 254 * |
| 255 * @param updatedCallback the listener to be invoked. |
| 256 */ |
| 257 public void setUpdatedCallback(UpdatedCallback updatedCallback) { |
| 258 mUpdatedCallback = updatedCallback; |
| 259 } |
| 260 |
| 261 /** |
| 262 * Sets the persistent expanded/collapsed state of the currently open tabs l
ist. |
| 263 * |
| 264 * @param isCollapsed Whether the currently open tabs list is collapsed. |
| 265 */ |
| 266 public void setCurrentlyOpenTabsCollapsed(boolean isCollapsed) { |
| 267 mNewTabPagePrefs.setCurrentlyOpenTabsCollapsed(isCollapsed); |
| 268 } |
| 269 |
| 270 /** |
| 271 * Determine the expanded/collapsed state of the currently open tabs list. |
| 272 * |
| 273 * @return Whether the currently open tabs list is collapsed. |
| 274 */ |
| 275 public boolean isCurrentlyOpenTabsCollapsed() { |
| 276 return mNewTabPagePrefs.getCurrentlyOpenTabsCollapsed(); |
| 277 } |
| 278 |
| 279 /** |
| 280 * Sets the state for showing all tabs in the currently open tabs list. This
is intended to |
| 281 * be overridden in extending classes and set to true when the user clicks t
he "More" button |
| 282 * at the end of the list. |
| 283 * @param showingAll Whether the currently open tabs list should start to sh
ow all. |
| 284 */ |
| 285 public void setCurrentlyOpenTabsShowAll(boolean showingAll) { |
| 286 } |
| 287 |
| 288 /** |
| 289 * @return Whether the currently open tabs group shows all tabs. If it is no
t, only a limited |
| 290 * number of tabs is shown with a "More" button at the end of the list to sh
ow all. |
| 291 */ |
| 292 public boolean isCurrentlyOpenTabsShowingAll() { |
| 293 return false; |
| 294 } |
| 295 |
| 296 /** |
| 297 * Sets the persistent expanded/collapsed state of a foreign session list. |
| 298 * |
| 299 * @param session foreign session to collapsed. |
| 300 * @param isCollapsed Whether the session is collapsed or expanded. |
| 301 */ |
| 302 public void setForeignSessionCollapsed(ForeignSession session, boolean isCol
lapsed) { |
| 303 mNewTabPagePrefs.setForeignSessionCollapsed(session, isCollapsed); |
| 304 } |
| 305 |
| 306 /** |
| 307 * Determine the expanded/collapsed state of a foreign session list. |
| 308 * |
| 309 * @param session foreign session whose state to obtain. |
| 310 * |
| 311 * @return Whether the session is collapsed. |
| 312 */ |
| 313 public boolean getForeignSessionCollapsed(ForeignSession session) { |
| 314 return mNewTabPagePrefs.getForeignSessionCollapsed(session); |
| 315 } |
| 316 |
| 317 /** |
| 318 * Sets the persistent expanded/collapsed state of the recently closed tabs
list. |
| 319 * |
| 320 * @param isCollapsed Whether the recently closed tabs list is collapsed. |
| 321 */ |
| 322 public void setRecentlyClosedTabsCollapsed(boolean isCollapsed) { |
| 323 mNewTabPagePrefs.setRecentlyClosedTabsCollapsed(isCollapsed); |
| 324 } |
| 325 |
| 326 /** |
| 327 * Determine the expanded/collapsed state of the recently closed tabs list. |
| 328 * |
| 329 * @return Whether the recently closed tabs list is collapsed. |
| 330 */ |
| 331 public boolean isRecentlyClosedTabsCollapsed() { |
| 332 return mNewTabPagePrefs.getRecentlyClosedTabsCollapsed(); |
| 333 } |
| 334 |
| 335 /** |
| 336 * Remove Foreign session to display. Note that it might reappear during the
next sync if the |
| 337 * session is not orphaned. |
| 338 * |
| 339 * This is mainly for when user wants to delete an orphaned session. |
| 340 * @param session Session to be deleted. |
| 341 */ |
| 342 public void deleteForeignSession(ForeignSession session) { |
| 343 mForeignSessionHelper.deleteForeignSession(session); |
| 344 } |
| 345 |
| 346 /** |
| 347 * Clears the list of recently closed tabs. |
| 348 */ |
| 349 public void clearRecentlyClosedTabs() { |
| 350 mRecentlyClosedBridge.clearRecentlyClosedTabs(); |
| 351 } |
| 352 |
| 353 /** |
| 354 * Determine whether the sync promo needs to be displayed. |
| 355 * |
| 356 * @return Whether sync promo should be displayed. |
| 357 */ |
| 358 public boolean shouldDisplaySyncPromo() { |
| 359 if (SigninManager.get(mContext).isSigninDisabledByPolicy()) { |
| 360 return false; |
| 361 } |
| 362 |
| 363 return !AndroidSyncSettings.isSyncEnabled(mContext) || mForeignSessions.
isEmpty(); |
| 364 } |
| 365 |
| 366 /** |
| 367 * Collapse the sync promo. |
| 368 * |
| 369 * @param isCollapsed Whether the sync promo is collapsed. |
| 370 */ |
| 371 public void setSyncPromoCollapsed(boolean isCollapsed) { |
| 372 mNewTabPagePrefs.setSyncPromoCollapsed(isCollapsed); |
| 373 } |
| 374 |
| 375 /** |
| 376 * Determine whether the sync promo is collapsed. |
| 377 * |
| 378 * @return Whether the sync promo is collapsed. |
| 379 */ |
| 380 public boolean isSyncPromoCollapsed() { |
| 381 return mNewTabPagePrefs.getSyncPromoCollapsed(); |
| 382 } |
| 383 |
| 384 protected void postUpdate() { |
| 385 if (mUpdatedCallback != null) { |
| 386 mUpdatedCallback.onUpdated(); |
| 387 } |
| 388 } |
| 389 |
| 390 // SignInStateObserver |
| 391 @Override |
| 392 public void onSignedIn() { |
| 393 androidSyncSettingsChanged(); |
| 394 } |
| 395 |
| 396 @Override |
| 397 public void onSignedOut() { |
| 398 androidSyncSettingsChanged(); |
| 399 } |
| 400 |
| 401 // AndroidSyncSettingsObserver |
| 402 @Override |
| 403 public void androidSyncSettingsChanged() { |
| 404 updateForeignSessions(); |
| 405 postUpdate(); |
| 406 for (AndroidSyncSettingsObserver observer : mObservers) { |
| 407 observer.androidSyncSettingsChanged(); |
| 408 } |
| 409 } |
| 410 |
| 411 // SyncPromoModel |
| 412 @Override |
| 413 public boolean isSyncEnabled() { |
| 414 return AndroidSyncSettings.isSyncEnabled(mContext); |
| 415 } |
| 416 |
| 417 @Override |
| 418 public boolean isSignedIn() { |
| 419 return ChromeSigninController.get(mContext).isSignedIn(); |
| 420 } |
| 421 |
| 422 @Override |
| 423 public void enableSync() { |
| 424 SyncController.get(mContext).start(); |
| 425 } |
| 426 |
| 427 @Override |
| 428 public void registerForSyncUpdates(AndroidSyncSettingsObserver changeListene
r) { |
| 429 mObservers.addObserver(changeListener); |
| 430 } |
| 431 |
| 432 @Override |
| 433 public void unregisterForSyncUpdates(AndroidSyncSettingsObserver changeListe
ner) { |
| 434 mObservers.removeObserver(changeListener); |
| 435 } |
| 436 } |
OLD | NEW |