/**
  * Initialization method that has 3 different behaviors based on whether bookmark model is loaded.
  * If the bookmark model is not loaded yet, it pushes a loading state to backstack which contains
  * the url from preference. If the model is loaded and the backstack is empty, it creates a state
  * by fetching the last visited bookmark url stored in preference. If the bookmark model is loaded
  * but backstack contains a pending loading state, it creates a new state by getting the url of
  * the loading state and replace the previous loading state with the new normal state.
  */
 private void initializeIfBookmarkModelLoaded() {
   if (mEnhancedBookmarksModel.isBookmarkModelLoaded()) {
     mSearchView.onEnhancedBookmarkDelegateInitialized(this);
     mDrawerListView.onEnhancedBookmarkDelegateInitialized(this);
     mContentView.onEnhancedBookmarkDelegateInitialized(this);
     if (mStateStack.isEmpty()) {
       setState(UIState.createStateFromUrl(getUrlFromPreference(), mEnhancedBookmarksModel));
     } else if (mStateStack.peek().mState == UIState.STATE_LOADING) {
       String url = mStateStack.pop().mUrl;
       setState(UIState.createStateFromUrl(url, mEnhancedBookmarksModel));
     }
   } else {
     mContentView.showLoadingUi();
     mDrawerListView.showLoadingUi();
     mContentView.showLoadingUi();
     if (mStateStack.isEmpty() || mStateStack.peek().mState != UIState.STATE_LOADING) {
       setState(UIState.createLoadingState(getUrlFromPreference()));
     } else if (!mStateStack.isEmpty()) {
       // Refresh the UI. This is needed because on tablet, updateForUrl might set up
       // loading state too early and at that time all UI components are not created yet.
       // Therefore we need to set the previous loading state once again to trigger all UI
       // updates.
       setState(mStateStack.pop());
     }
   }
 }
 /**
  * Updates UI based on the new URL on tablet. If the bookmark model is not loaded yet, creates a
  * temporary loading state carrying this url. This method is supposed to align with {@link
  * EnhancedBookmarkPage#updateForUrl(String)}
  *
  * <p>
  *
  * @param url The url to navigate to.
  */
 public void updateForUrl(String url) {
   if (mEnhancedBookmarksModel != null && mEnhancedBookmarksModel.isBookmarkModelLoaded()) {
     setState(UIState.createStateFromUrl(url, mEnhancedBookmarksModel));
   } else {
     // Note this does not guarantee to update the UI, as at this time the onCreateView()
     // might not has even been called yet.
     setState(UIState.createLoadingState(url));
   }
 }
 @Override
 public void bookmarkModelChanged() {
   // If the folder no longer exists in folder mode, we need to fall back. Relying on the
   // default behavior by setting the folder mode again.
   if (getCurrentState() == UIState.STATE_FOLDER) {
     setState(mStateStack.peek());
   }
   clearSelection();
 }
  /**
   * Called when the user presses the back key. This is only going to be called on Phone.
   *
   * @return True if manager handles this event, false if it decides to ignore.
   */
  public boolean onBackPressed() {
    if (doesDrawerExist()) {
      if (mDrawer.isDrawerVisible(Gravity.START)) {
        mDrawer.closeDrawer(Gravity.START);
        return true;
      }
    }

    if (mContentView.onBackPressed()) return true;

    if (!mStateStack.empty()) {
      mStateStack.pop();
      if (!mStateStack.empty()) {
        setState(mStateStack.pop());
        return true;
      }
    }
    return false;
  }
 @Override
 public void openFilter(EnhancedBookmarkFilter filter) {
   setState(UIState.createFilterState(filter, mEnhancedBookmarksModel));
 }
 @Override
 public void openAllBookmarks() {
   closeSearchUI();
   setState(UIState.createAllBookmarksState(mEnhancedBookmarksModel));
 }
 @Override
 public void openFolder(BookmarkId folder) {
   closeSearchUI();
   setState(UIState.createFolderState(folder, mEnhancedBookmarksModel));
 }