下面列出了android.support.annotation.BinderThread#org.chromium.components.bookmarks.BookmarkId 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateFolderList() {
List<BookmarkId> folderList = new ArrayList<>();
List<Integer> depthList = new ArrayList<>();
mModel.getMoveDestinations(folderList, depthList, mBookmarksToMove);
List<FolderListEntry> entryList = new ArrayList<>(folderList.size() + 3);
if (!mIsCreatingFolder) {
entryList.add(new FolderListEntry(null, 0,
getString(R.string.bookmark_add_folder), false,
FolderListEntry.TYPE_NEW_FOLDER));
}
for (int i = 0; i < folderList.size(); i++) {
BookmarkId folder = folderList.get(i);
if (!mModel.isFolderVisible(folder)) continue;
String title = mModel.getBookmarkById(folder).getTitle();
entryList.add(new FolderListEntry(folder, depthList.get(i), title,
folder.equals(mParentId), FolderListEntry.TYPE_NORMAL));
}
mBookmarkIdsAdapter.setEntryList(entryList);
}
@Override
public void onFolderStateSet(BookmarkId folder) {
mCurrentFolder = mDelegate.getModel().getBookmarkById(folder);
getMenu().findItem(R.id.search_menu_id).setVisible(true);
getMenu().findItem(R.id.edit_menu_id).setVisible(mCurrentFolder.isEditable());
// If the parent folder is a top level node, we don't go up anymore.
if (mDelegate.getModel().getTopLevelFolderParentIDs().contains(
mCurrentFolder.getParentId())) {
if (TextUtils.isEmpty(mCurrentFolder.getTitle())) {
setTitle(R.string.bookmarks);
} else {
setTitle(mCurrentFolder.getTitle());
}
setNavigationButton(NAVIGATION_BUTTON_MENU);
} else {
setTitle(mCurrentFolder.getTitle());
setNavigationButton(NAVIGATION_BUTTON_BACK);
}
}
/**
* Set folders and bookmarks to show.
* @param folders This can be null if there is no folders to show.
*/
private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmarks) {
if (folders == null) folders = new ArrayList<BookmarkId>();
mFolderSection.clear();
mFolderSection.addAll(folders);
mBookmarkSection.clear();
mBookmarkSection.addAll(bookmarks);
updateHeader();
updateDividerSections();
// TODO(kkimlabs): Animation is disabled due to a performance issue on bookmark undo.
// http://crbug.com/484174
notifyDataSetChanged();
}
public static void saveBookmarkOffline(BookmarkId bookmarkId, Tab tab) {
// If bookmark ID is missing there is nothing to save here.
if (bookmarkId == null) return;
// Making sure the feature is enabled.
if (!OfflinePageBridge.isOfflineBookmarksEnabled()) return;
// Making sure tab is worth keeping.
if (shouldSkipSavingTabOffline(tab)) return;
OfflinePageBridge offlinePageBridge = getInstance().getOfflinePageBridge(tab.getProfile());
if (offlinePageBridge == null) return;
WebContents webContents = tab.getWebContents();
ClientId clientId = ClientId.createClientIdForBookmarkId(bookmarkId);
offlinePageBridge.savePage(webContents, clientId, new OfflinePageBridge.SavePageCallback() {
@Override
public void onSavePageDone(int savePageResult, String url, long offlineId) {
// Result of the call is ignored.
}
});
}
/**
* Delete one or multiple bookmarks from model. If more than one bookmarks are passed here, this
* method will group these delete operations into one undo bundle so that later if the user
* clicks undo, all bookmarks deleted here will be restored.
* @param bookmarks Bookmarks to delete. Note this array should not contain a folder and its
* children, because deleting folder will also remove all its children, and
* deleting children once more will cause errors.
*/
public void deleteBookmarks(BookmarkId... bookmarks) {
assert bookmarks != null && bookmarks.length > 0;
// Store all titles of bookmarks.
String[] titles = new String[bookmarks.length];
boolean isUndoable = true;
for (int i = 0; i < bookmarks.length; i++) {
titles[i] = getBookmarkTitle(bookmarks[i]);
isUndoable &= (bookmarks[i].getType() == BookmarkType.NORMAL);
}
if (bookmarks.length == 1) {
deleteBookmark(bookmarks[0]);
} else {
startGroupingUndos();
for (BookmarkId bookmark : bookmarks) {
deleteBookmark(bookmark);
}
endGroupingUndos();
}
for (BookmarkDeleteObserver observer : mDeleteObservers) {
observer.onDeleteBookmarks(titles, isUndoable);
}
}
/**
* Opens a bookmark and reports UMA.
* @param model Bookmarks model to manage the bookmark.
* @param activity Activity requesting to open the bookmark.
* @param bookmarkId ID of the bookmark to be opened.
* @param launchLocation Location from which the bookmark is being opened.
* @return Whether the bookmark was successfully opened.
*/
public static boolean openBookmark(BookmarkModel model, Activity activity,
BookmarkId bookmarkId, int launchLocation) {
if (model.getBookmarkById(bookmarkId) == null) return false;
String url = model.getBookmarkById(bookmarkId).getUrl();
RecordUserAction.record("MobileBookmarkManagerEntryOpened");
RecordHistogram.recordEnumeratedHistogram(
"Stars.LaunchLocation", launchLocation, BookmarkLaunchLocation.COUNT);
if (DeviceFormFactor.isTablet()) {
// For tablets, the bookmark manager is open in a tab in the ChromeActivity. Use
// the ComponentName of the ChromeActivity passed into this method.
openUrl(activity, url, activity.getComponentName());
} else {
// For phones, the bookmark manager is a separate activity. When the activity is
// launched, an intent extra is set specifying the parent component.
ComponentName parentComponent = IntentUtils.safeGetParcelableExtra(
activity.getIntent(), IntentHandler.EXTRA_PARENT_COMPONENT);
openUrl(activity, url, parentComponent);
}
return true;
}
@BinderThread
@Override
public long getItemId(int position) {
Bookmark bookmark = getBookmarkForPosition(position);
if (bookmark == null) return BookmarkId.INVALID_FOLDER_ID;
return bookmark.id.getId();
}
private DelayedBookmarkCallback(BookmarkId folderId, BookmarksCallback callback,
int method, BookmarkBridge handler) {
mFolderId = folderId;
mCallback = callback;
mCallbackMethod = method;
mHandler = handler;
}
/**
* Sets folders to show.
*/
void setTopFolders(List<BookmarkId> folders) {
mMiddleSection.clear();
if (folders.size() > 0) {
// Add a divider and title to the top of the section.
mMiddleSection.add(new Item(TYPE_DIVIDER));
mMiddleSection.add(new Item(TYPE_FOLDERS_TITLE));
}
// Add the rest of the items.
for (BookmarkId id : folders) {
mMiddleSection.add(new Item(id));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mModel = new BookmarkModel();
mBookmarkId = BookmarkId.getBookmarkIdFromString(
getIntent().getStringExtra(INTENT_BOOKMARK_ID));
mModel.addObserver(mBookmarkModelObserver);
BookmarkItem item = mModel.getBookmarkById(mBookmarkId);
if (!mModel.doesBookmarkExist(mBookmarkId) || item == null) {
finish();
return;
}
setContentView(R.layout.bookmark_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) {
BookmarkFolderSelectActivity.startFolderSelectActivity(
BookmarkEditActivity.this, mBookmarkId);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
updateViewContent(false);
}
static Uri createFolderUrl(BookmarkId folderId) {
Uri.Builder builder = Uri.parse(UrlConstants.BOOKMARKS_FOLDER_URL).buildUpon();
// Encodes the path and appends it to the base url. A simple appending
// does not work because there might be spaces in suffix.
builder.appendPath(folderId.toString());
return builder.build();
}
public FolderListEntry(BookmarkId bookmarkId, int depth, String title, boolean isSelected,
int type) {
assert type == TYPE_NEW_FOLDER || type == TYPE_NORMAL;
mDepth = depth;
mId = bookmarkId;
mTitle = title;
mIsSelected = isSelected;
mType = type;
}
private DelayedBookmarkCallback(BookmarkId folderId, BookmarksCallback callback,
int method, BookmarkBridge handler) {
mFolderId = folderId;
mCallback = callback;
mCallbackMethod = method;
mHandler = handler;
}
/**
* @return The top level folder's parents.
*/
public List<BookmarkId> getTopLevelFolderParentIDs() {
assert mIsNativeBookmarkModelLoaded;
List<BookmarkId> result = new ArrayList<BookmarkId>();
nativeGetTopLevelFolderParentIDs(mNativeBookmarkBridge, result);
return result;
}
/**
* Starts an add folder activity. This method should only be called by
* {@link BookmarkFolderSelectActivity}.
*/
public static void startAddFolderActivity(BookmarkFolderSelectActivity activity,
List<BookmarkId> bookmarksToMove) {
assert bookmarksToMove.size() > 0;
Intent intent = new Intent(activity, BookmarkAddEditFolderActivity.class);
intent.putExtra(INTENT_IS_ADD_MODE, true);
ArrayList<String> bookmarkStrings = new ArrayList<>(bookmarksToMove.size());
for (BookmarkId id : bookmarksToMove) {
bookmarkStrings.add(id.toString());
}
intent.putStringArrayListExtra(
BookmarkFolderSelectActivity.INTENT_BOOKMARKS_TO_MOVE, bookmarkStrings);
activity.startActivityForResult(intent,
BookmarkFolderSelectActivity.CREATE_FOLDER_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
assert mIsAddMode;
if (requestCode == PARENT_FOLDER_REQUEST_CODE && resultCode == RESULT_OK) {
BookmarkId selectedBookmark = BookmarkId.getBookmarkIdFromString(data.getStringExtra(
BookmarkFolderSelectActivity.INTENT_SELECTED_FOLDER));
updateParent(selectedBookmark);
}
}
private BookmarkItem(BookmarkId id, String title, String url, boolean isFolder,
BookmarkId parentId, boolean isEditable, boolean isManaged) {
mId = id;
mTitle = title;
mUrl = url;
mIsFolder = isFolder;
mParentId = parentId;
mIsEditable = isEditable;
mIsManaged = isManaged;
}
public FolderListEntry(BookmarkId bookmarkId, int depth, String title, boolean isSelected,
int type) {
assert type == TYPE_NEW_FOLDER || type == TYPE_NORMAL;
mDepth = depth;
mId = bookmarkId;
mTitle = title;
mIsSelected = isSelected;
mType = type;
}
/**
* @return The parent {@link BookmarkId} that the user used the last time or null if the user
* has never selected a parent folder to use.
*/
static BookmarkId getLastUsedParent(Context context) {
SharedPreferences preferences = ContextUtils.getAppSharedPreferences();
if (!preferences.contains(PREF_LAST_USED_PARENT)) return null;
return BookmarkId.getBookmarkIdFromString(
preferences.getString(PREF_LAST_USED_PARENT, null));
}
/**
* Starts a select folder activity for the new folder that is about to be created. This method
* is only supposed to be called by {@link BookmarkAddEditFolderActivity}
*/
public static void startNewFolderSelectActivity(
BookmarkAddEditFolderActivity activity, List<BookmarkId> bookmarks) {
assert bookmarks.size() > 0;
Intent intent = new Intent(activity, BookmarkFolderSelectActivity.class);
intent.putExtra(INTENT_IS_CREATING_FOLDER, true);
ArrayList<String> bookmarkStrings = new ArrayList<>(bookmarks.size());
for (BookmarkId id : bookmarks) {
bookmarkStrings.add(id.toString());
}
intent.putStringArrayListExtra(INTENT_BOOKMARKS_TO_MOVE, bookmarkStrings);
activity.startActivityForResult(intent,
BookmarkAddEditFolderActivity.PARENT_FOLDER_REQUEST_CODE);
}
/**
* Fetches the bookmarks of the current folder. Callback will be
* synchronous if the bookmark model is already loaded and async if it is loaded in the
* background.
* @param folderId The current folder id.
* @param callback Instance of a callback object.
*/
public void getBookmarksForFolder(BookmarkId folderId, BookmarksCallback callback) {
if (mIsNativeBookmarkModelLoaded) {
nativeGetBookmarksForFolder(mNativeBookmarkBridge, folderId, callback,
new ArrayList<BookmarkItem>());
} else {
mDelayedBookmarkCallbacks.add(new DelayedBookmarkCallback(folderId, callback,
DelayedBookmarkCallback.GET_BOOKMARKS_FOR_FOLDER, this));
}
}
private static void openBookmarksInNewTabs(
List<BookmarkId> bookmarks, TabDelegate tabDelegate, BookmarkModel model) {
for (BookmarkId id : bookmarks) {
tabDelegate.createNewTab(new LoadUrlParams(model.getBookmarkById(id).getUrl()),
TabLaunchType.FROM_LONGPRESS_BACKGROUND, null);
}
}
/**
* Set folders and bookmarks to show.
* @param folders This can be null if there is no folders to show.
*/
private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmarks) {
if (folders == null) folders = new ArrayList<BookmarkId>();
mFolderSection.clear();
mFolderSection.addAll(folders);
mBookmarkSection.clear();
mBookmarkSection.addAll(bookmarks);
updateHeaderAndNotify();
}
@Override
public void clearSelection() {
mSelectedBookmarks.clear();
for (BookmarkUIObserver observer : mUIObservers) {
observer.onSelectionStateChange(new ArrayList<BookmarkId>(mSelectedBookmarks));
}
}
@CalledByNative
private static void addToBookmarkMatchList(List<BookmarkMatch> bookmarkMatchList,
long id, int type, int[] titleMatchStartPositions,
int[] titleMatchEndPositions, int[] urlMatchStartPositions,
int[] urlMatchEndPositions) {
bookmarkMatchList.add(new BookmarkMatch(new BookmarkId(id, type),
createPairsList(titleMatchStartPositions, titleMatchEndPositions),
createPairsList(urlMatchStartPositions, urlMatchEndPositions)));
}
/**
* Starts a select folder activity.
*/
public static void startFolderSelectActivity(Context context, BookmarkId... bookmarks) {
assert bookmarks.length > 0;
Intent intent = new Intent(context, BookmarkFolderSelectActivity.class);
intent.putExtra(INTENT_IS_CREATING_FOLDER, false);
ArrayList<String> bookmarkStrings = new ArrayList<>(bookmarks.length);
for (BookmarkId id : bookmarks) {
bookmarkStrings.add(id.toString());
}
intent.putStringArrayListExtra(INTENT_BOOKMARKS_TO_MOVE, bookmarkStrings);
context.startActivity(intent);
}
/**
* Sets folders to show.
*/
void setTopFolders(List<BookmarkId> folders) {
mBottomSection.clear();
if (folders.size() > 0) {
// Add a divider and title to the top of the section.
mBottomSection.add(new Item(TYPE_DIVIDER));
mBottomSection.add(new Item(TYPE_FOLDERS_TITLE));
}
// Add the rest of the items.
for (BookmarkId id : folders) {
mBottomSection.add(new Item(id));
}
}
/** Starts an {@link BookmarkEditActivity} for the given {@link BookmarkId}. */
public static void startEditActivity(Context context, BookmarkId bookmarkId) {
Intent intent = new Intent(context, BookmarkEditActivity.class);
intent.putExtra(BookmarkEditActivity.INTENT_BOOKMARK_ID, bookmarkId.toString());
if (context instanceof BookmarkActivity) {
((BookmarkActivity) context).startActivityForResult(
intent, BookmarkActivity.EDIT_BOOKMARK_REQUEST_CODE);
} else {
context.startActivity(intent);
}
}
/**
* Fetches the folder hierarchy of the given folder. Callback will be
* synchronous if the bookmark model is already loaded and async if it is loaded in the
* background.
* @param folderId The current folder id.
* @param callback Instance of a callback object.
*/
public void getCurrentFolderHierarchy(BookmarkId folderId, BookmarksCallback callback) {
if (mIsNativeBookmarkModelLoaded) {
nativeGetCurrentFolderHierarchy(mNativeBookmarkBridge, folderId, callback,
new ArrayList<BookmarkItem>());
} else {
mDelayedBookmarkCallbacks.add(new DelayedBookmarkCallback(folderId, callback,
DelayedBookmarkCallback.GET_CURRENT_FOLDER_HIERARCHY, this));
}
}
/**
* Starts a select folder activity for the new folder that is about to be created. This method
* is only supposed to be called by {@link BookmarkAddEditFolderActivity}
*/
public static void startNewFolderSelectActivity(
BookmarkAddEditFolderActivity activity, List<BookmarkId> bookmarks) {
assert bookmarks.size() > 0;
Intent intent = new Intent(activity, BookmarkFolderSelectActivity.class);
intent.putExtra(INTENT_IS_CREATING_FOLDER, true);
ArrayList<String> bookmarkStrings = new ArrayList<>(bookmarks.size());
for (BookmarkId id : bookmarks) {
bookmarkStrings.add(id.toString());
}
intent.putStringArrayListExtra(INTENT_BOOKMARKS_TO_MOVE, bookmarkStrings);
activity.startActivityForResult(intent,
BookmarkAddEditFolderActivity.PARENT_FOLDER_REQUEST_CODE);
}