下面列出了怎么用android.support.annotation.BinderThread的API类实例代码及写法,或者点击链接到github查看源代码。
@BinderThread
private BookmarkFolder loadBookmarks(final BookmarkId folderId) {
final LinkedBlockingQueue<BookmarkFolder> resultQueue = new LinkedBlockingQueue<>(1);
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
new BookmarkLoader(mContext, folderId, new BookmarkLoaderCallback() {
@Override
public void onBookmarksLoaded(BookmarkFolder folder) {
resultQueue.add(folder);
}
});
}
});
try {
return resultQueue.take();
} catch (InterruptedException e) {
return null;
}
}
@BinderThread
private BookmarkFolder loadBookmarks(final BookmarkId folderId) {
final LinkedBlockingQueue<BookmarkFolder> resultQueue = new LinkedBlockingQueue<>(1);
//A reference of BookmarkLoader is needed in binder thread to
//prevent it from being garbage collected.
final BookmarkLoader bookmarkLoader = new BookmarkLoader();
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
bookmarkLoader.initialize(mContext, folderId, new BookmarkLoaderCallback() {
@Override
public void onBookmarksLoaded(BookmarkFolder folder) {
resultQueue.add(folder);
}
});
}
});
try {
return resultQueue.take();
} catch (InterruptedException e) {
return null;
}
}
@BinderThread
@Override
public int getCount() {
//On some Sony devices, getCount() could be called before onDatasetChanged()
//returns. If it happens, refresh widget until the bookmarks are all loaded.
if (mCurrentFolder == null || !mPreferences.getString(PREF_CURRENT_FOLDER, "")
.equals(mCurrentFolder.folder.id.toString())) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
refreshWidget();
}
});
}
if (mCurrentFolder == null) {
return 0;
}
return mCurrentFolder.children.size() + (mCurrentFolder.parent != null ? 1 : 0);
}
@BinderThread
private BookmarkFolder loadBookmarks(final BookmarkId folderId) {
final LinkedBlockingQueue<BookmarkFolder> resultQueue = new LinkedBlockingQueue<>(1);
//A reference of BookmarkLoader is needed in binder thread to
//prevent it from being garbage collected.
final BookmarkLoader bookmarkLoader = new BookmarkLoader();
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
bookmarkLoader.initialize(mContext, folderId, new BookmarkLoaderCallback() {
@Override
public void onBookmarksLoaded(BookmarkFolder folder) {
resultQueue.add(folder);
}
});
}
});
try {
return resultQueue.take();
} catch (InterruptedException e) {
return null;
}
}
@BinderThread
private Bookmark getBookmarkForPosition(int position) {
if (mCurrentFolder == null) return null;
// The position 0 is saved for an entry of the current folder used to go up.
// This is not the case when the current node has no parent (it's the root node).
if (mCurrentFolder.parent != null) {
if (position == 0) return mCurrentFolder.folder;
position--;
}
// This is necessary because when Chrome is cleared from Application settings, Bookmark
// widget will not be notified and it causes inconsistency between model and widget.
// Then if the widget is quickly scrolled down, this has an IndexOutOfBound error.
if (mCurrentFolder.children.size() <= position) return null;
return mCurrentFolder.children.get(position);
}
@BinderThread
@Override
public int getCount() {
//On some Sony devices, getCount() could be called before onDatasetChanged()
//returns. If it happens, refresh widget until the bookmarks are all loaded.
if (mCurrentFolder == null || !mPreferences.getString(PREF_CURRENT_FOLDER, "")
.equals(mCurrentFolder.folder.id.toString())) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
refreshWidget();
}
});
}
if (mCurrentFolder == null) {
return 0;
}
return mCurrentFolder.children.size() + (mCurrentFolder.parent != null ? 1 : 0);
}
@BinderThread
@Override
public void onDestroy() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBookmarkModel != null) mBookmarkModel.destroy();
}
});
deleteWidgetState(mContext, mWidgetId);
}
@BinderThread
private void updateBookmarkList() {
BookmarkId folderId = BookmarkId
.getBookmarkIdFromString(mPreferences.getString(PREF_CURRENT_FOLDER, null));
mCurrentFolder = loadBookmarks(folderId);
mPreferences.edit().putString(PREF_CURRENT_FOLDER, mCurrentFolder.folder.id.toString())
.apply();
}
@BinderThread
private Bookmark getBookmarkForPosition(int position) {
if (mCurrentFolder == null) return null;
// The position 0 is saved for an entry of the current folder used to go up.
// This is not the case when the current node has no parent (it's the root node).
if (mCurrentFolder.parent != null) {
if (position == 0) return mCurrentFolder.folder;
position--;
}
return mCurrentFolder.children.get(position);
}
@BinderThread
@Override
public void onDestroy() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBookmarkModel != null) mBookmarkModel.destroy();
}
});
deleteWidgetState(mContext, mWidgetId);
}
@BinderThread
private void updateBookmarkList() {
BookmarkId folderId = BookmarkId
.getBookmarkIdFromString(mPreferences.getString(PREF_CURRENT_FOLDER, null));
mCurrentFolder = loadBookmarks(folderId);
mPreferences.edit().putString(PREF_CURRENT_FOLDER, mCurrentFolder.folder.id.toString())
.apply();
}
@BinderThread
private Bookmark getBookmarkForPosition(int position) {
if (mCurrentFolder == null) return null;
// The position 0 is saved for an entry of the current folder used to go up.
// This is not the case when the current node has no parent (it's the root node).
if (mCurrentFolder.parent != null) {
if (position == 0) return mCurrentFolder.folder;
position--;
}
return mCurrentFolder.children.get(position);
}
@Override
@BinderThread
public void start(Bundle invocationData, IJobCallback callback) {
JobInvocation.Builder invocation = getJobCoder().decode(invocationData);
if (invocation == null) {
Log.wtf(TAG, "start: unknown invocation provided");
return;
}
JobService.this.handleStartJobRequest(invocation.build(), callback);
}
@Override
@BinderThread
public void stop(Bundle invocationData, boolean needToSendResult) {
JobInvocation.Builder invocation = getJobCoder().decode(invocationData);
if (invocation == null) {
Log.wtf(TAG, "stop: unknown invocation provided");
return;
}
JobService.this.handleStopJobRequest(invocation.build(), needToSendResult);
}
@BinderThread
@Override
public void onDestroy() {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mBookmarkModel != null) mBookmarkModel.destroy();
}
});
deleteWidgetState(mContext, mWidgetId);
}
@BinderThread
private void updateBookmarkList() {
BookmarkId folderId = BookmarkId
.getBookmarkIdFromString(mPreferences.getString(PREF_CURRENT_FOLDER, null));
mCurrentFolder = loadBookmarks(folderId);
mPreferences.edit().putString(PREF_CURRENT_FOLDER, mCurrentFolder.folder.id.toString())
.apply();
}
@BinderThread
@Override
public long getItemId(int position) {
Bookmark bookmark = getBookmarkForPosition(position);
if (bookmark == null) return BookmarkId.INVALID_FOLDER_ID;
return bookmark.id.getId();
}
@BinderThread
@Override
public void onDataSetChanged() {
updateBookmarkList();
}
@BinderThread
@Override
public int getViewTypeCount() {
return 2;
}
@BinderThread
@Override
public boolean hasStableIds() {
return false;
}
@BinderThread
@Override
public int getCount() {
if (mCurrentFolder == null) return 0;
return mCurrentFolder.children.size() + (mCurrentFolder.parent != null ? 1 : 0);
}
@BinderThread
@Override
public long getItemId(int position) {
return getBookmarkForPosition(position).id.getId();
}
@BinderThread
@Override
public RemoteViews getLoadingView() {
return new RemoteViews(mContext.getPackageName(), R.layout.bookmark_widget_item);
}
@BinderThread
@Override
public void onDataSetChanged() {
updateBookmarkList();
}
@BinderThread
@Override
public int getViewTypeCount() {
return 2;
}
@BinderThread
@Override
public boolean hasStableIds() {
return false;
}
@BinderThread
@Override
public long getItemId(int position) {
return getBookmarkForPosition(position).id.getId();
}
@BinderThread
@Override
public RemoteViews getLoadingView() {
return new RemoteViews(mContext.getPackageName(), R.layout.bookmark_widget_item);
}
@BinderThread
public void workOnBinderThread(){
logThreadInfo();
}
/**
* Asks the {@code job} to start running. Calls {@link #onStartJob} on the main thread. Once
* complete, the {@code callback} will be used to send the result back.
*/
@BinderThread
private void handleStartJobRequest(JobParameters job, IJobCallback callback) {
backgroundExecutor.execute(UnitOfWork.handleStartJobRequest(this, job, callback));
}