@Override
 public void bookmarkNodeMoved(
     BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, int newIndex) {
   BookmarkId movedBookmark =
       mEnhancedBookmarksModel.getChildAt(newParent.getId(), newIndex);
   if (movedBookmark.equals(mBookmarkId)) {
     mFolderTextView.setText(newParent.getTitle());
   }
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EnhancedBookmarkUtils.setTaskDescriptionInDocumentMode(this, getString(R.string.edit_bookmark));
    mEnhancedBookmarksModel = new EnhancedBookmarksModel();
    mBookmarkId =
        BookmarkId.getBookmarkIdFromString(getIntent().getStringExtra(INTENT_BOOKMARK_ID));
    mEnhancedBookmarksModel.addObserver(mBookmarkModelObserver);

    setContentView(R.layout.eb_edit);
    mTitleEditText = (EmptyAlertEditText) findViewById(R.id.title_text);
    mUrlEditText = (EmptyAlertEditText) findViewById(R.id.url_text);
    mFolderTextView = (TextView) findViewById(R.id.folder_text);
    mFolderTextView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            EnhancedBookmarkFolderSelectActivity.startFolderSelectActivity(
                EnhancedBookmarkEditActivity.this, mBookmarkId);
          }
        });
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    updateViewContent();
  }
    /** @return A state corresponding to the url. If the url is not valid, return all_bookmarks. */
    private static UIState createStateFromUrl(String url, EnhancedBookmarksModel bookmarkModel) {
      UIState state = new UIState();
      state.mState = STATE_INVALID;
      state.mUrl = url;

      if (url.equals(UrlConstants.BOOKMARKS_URL)) {
        state.mState = STATE_ALL_BOOKMARKS;
      } else if (url.startsWith(UrlConstants.BOOKMARKS_FOLDER_URL)) {
        String suffix = decodeSuffix(url, UrlConstants.BOOKMARKS_FOLDER_URL);
        if (!suffix.isEmpty()) {
          state.mFolder = BookmarkId.getBookmarkIdFromString(suffix);
          state.mState = STATE_FOLDER;
        }
      } else if (url.startsWith(UrlConstants.BOOKMARKS_FILTER_URL)) {
        String suffix = decodeSuffix(url, UrlConstants.BOOKMARKS_FILTER_URL);
        if (!suffix.isEmpty()) {
          state.mState = STATE_FILTER;
          state.mFilter = EnhancedBookmarkFilter.valueOf(suffix);
        }
      }

      if (!state.isValid(bookmarkModel)) {
        state.mState = STATE_ALL_BOOKMARKS;
        state.mUrl = UrlConstants.BOOKMARKS_URL;
      }

      return state;
    }
 @Override
 public void bookmarkNodeRemoved(
     BookmarkItem parent, int oldIndex, BookmarkItem node, boolean isDoingExtensiveChanges) {
   if (mBookmarkId.equals(node.getId())) {
     finish();
   }
 }
 @Override
 public void bookmarkNodeChanged(BookmarkItem node) {
   if (mBookmarkId.equals(node.getId())
       || node.getId()
           .equals(mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).getParentId())) {
     updateViewContent();
   }
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int title =
        OfflinePageBridge.isEnabled() ? R.string.offline_pages_edit_item : R.string.edit_bookmark;
    setTitle(title);
    EnhancedBookmarkUtils.setTaskDescriptionInDocumentMode(this, getString(title));
    mEnhancedBookmarksModel = new EnhancedBookmarksModel();
    mBookmarkId =
        BookmarkId.getBookmarkIdFromString(getIntent().getStringExtra(INTENT_BOOKMARK_ID));
    mEnhancedBookmarksModel.addObserver(mBookmarkModelObserver);
    assert mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).isEditable();

    setContentView(R.layout.eb_edit);
    mTitleEditText = (EmptyAlertEditText) findViewById(R.id.title_text);
    mFolderTextView = (TextView) findViewById(R.id.folder_text);
    mUrlEditText = (EmptyAlertEditText) findViewById(R.id.url_text);

    mFolderTextView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            EnhancedBookmarkFolderSelectActivity.startFolderSelectActivity(
                EnhancedBookmarkEditActivity.this, mBookmarkId);
          }
        });

    if (OfflinePageBridge.isEnabled()
        && OfflinePageBridge.canSavePage(
            mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).getUrl())) {
      mOfflinePageModelObserver =
          new OfflinePageModelObserver() {
            @Override
            public void offlinePageDeleted(BookmarkId bookmarkId) {
              if (mBookmarkId.equals(bookmarkId)) {
                updateOfflineSection();
              }
            }
          };

      mEnhancedBookmarksModel.getOfflinePageBridge().addObserver(mOfflinePageModelObserver);
      // Make offline page section visible and find controls.
      findViewById(R.id.offline_page_group).setVisibility(View.VISIBLE);
      getIntent().setExtrasClassLoader(WebContents.class.getClassLoader());
      mWebContents = getIntent().getParcelableExtra(INTENT_WEB_CONTENTS);
      mActivityWindowAndroid = new ActivityWindowAndroid(this, false);
      updateOfflineSection();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    updateViewContent();
  }
    /** @return Whether this state is valid. */
    private boolean isValid(EnhancedBookmarksModel bookmarkModel) {
      if (mUrl == null || mState == STATE_INVALID) return false;

      if (mState == STATE_FOLDER) {
        if (mFolder == null) return false;

        return bookmarkModel.doesBookmarkExist(mFolder)
            && !mFolder.equals(bookmarkModel.getRootFolderId());
      }

      return true;
    }
 private static UIState createFolderState(
     BookmarkId folder, EnhancedBookmarksModel bookmarkModel) {
   return createStateFromUrl(
       UrlConstants.BOOKMARKS_FOLDER_URL + folder.toString(), bookmarkModel);
 }
 private void openBookmark() {
   Intent intent = new Intent();
   intent.putExtra(EnhancedBookmarkActivity.INTENT_VISIT_BOOKMARK_ID, mBookmarkId.toString());
   setResult(RESULT_OK, intent);
   finish();
 }
 /**
  * Retrieve the bookmark previously saved in the arguments bundle.
  *
  * @param bundle Argument holder or savedInstanceState of the fragment/activity.
  * @return The ID of the bookmark to retrieve.
  */
 public static BookmarkId getBookmarkIdFromBundle(Bundle bundle) {
   return BookmarkId.getBookmarkIdFromString(bundle.getString(BOOKMARK_SAVE_NAME));
 }
 /**
  * Save the bookmark in bundle to save state of a fragment/activity.
  *
  * @param bundle Argument holder or savedInstanceState of the fragment/activity.
  * @param bookmark The bookmark to save.
  */
 public static void saveBookmarkIdToBundle(Bundle bundle, BookmarkId bookmark) {
   bundle.putString(BOOKMARK_SAVE_NAME, bookmark.toString());
 }
 public static void startEditActivity(Context context, BookmarkId bookmarkId) {
   Intent intent = new Intent(context, EnhancedBookmarkEditActivity.class);
   intent.putExtra(EnhancedBookmarkEditActivity.INTENT_BOOKMARK_ID, bookmarkId.toString());
   context.startActivity(intent);
 }