@CalledByNative private void requestRender() { ContentViewCore contentViewCore = mCurrentContentView != null ? mCurrentContentView.getContentViewCore() : null; boolean rendererHasFrame = contentViewCore != null && contentViewCore.consumePendingRendererFrame(); if (rendererHasFrame && mPendingSwapBuffers + mPendingRenders < MAX_SWAP_BUFFER_COUNT) { TraceEvent.instant("requestRender:now"); mNeedToRender = false; mPendingRenders++; // The handler can be null if we are detached from the window. Calling // {@link View#post(Runnable)} properly handles this case, but we lose the front of // queue behavior. That is okay for this edge case. Handler handler = getHandler(); if (handler != null) { handler.postAtFrontOfQueue(mRenderRunnable); } else { post(mRenderRunnable); } mVSyncAdapter.requestUpdate(); } else if (mPendingRenders <= 0) { assert mPendingRenders == 0; TraceEvent.instant("requestRender:later"); mNeedToRender = true; mVSyncAdapter.requestUpdate(); } }
/** * Runs runnable on handler and waits for it to finish. If the current thread is the handler * thread, just runs it. */ public static void runOnHandlerSynchronously(Handler h, final Runnable runnable) { final boolean done[] = new boolean[1]; if (h.getLooper().getThread() == Thread.currentThread()) { runnable.run(); } else { h.postAtFrontOfQueue( new Runnable() { @Override public void run() { runnable.run(); synchronized (done) { done[0] = true; done.notify(); } } }); synchronized (done) { while (!done[0]) { try { done.wait(); } catch (InterruptedException e) { throw new IllegalStateException(e); } } } } }
@Override public boolean isCanceled() { // This is a little bit of a hack, but hey, why not. mCache.clear(); if (mCallback != null) { Handler handler = new Handler(Looper.getMainLooper()); handler.postAtFrontOfQueue(mCallback); } return true; }
/** Stops the communication thread. */ @Override public void close() throws IOException { if (communicationThreadHandler != null) { communicationThreadHandler.postAtFrontOfQueue( new Runnable() { @Override public void run() { Looper.myLooper().quit(); } }); communicationThreadHandler = null; } // TODO: Close Streams? }
public void closeResources() { mResolver.unregisterContentObserver(mTrackSegmentsObserver); mTrackSegmentsObserver = null; mHandler.removeCallbacks(mMediaCalculator); mHandler.removeCallbacks(mTrackCalculator); mHandler.postAtFrontOfQueue( new Runnable() { @Override public void run() { if (mWaypointsCursor != null) { mWaypointsCursor.close(); } if (mMediaCursor != null) { mMediaCursor.close(); } } }); }
/** * Method queues the image at path to load Note that the actual loading takes place in the UI * thread the ImageView and ViewSwitcher are just references for the UI thread. * * @param aPath - Path where the bitmap is located to load * @param aImageView - The ImageView the UI thread will load * @param aViewSwitcher - The ViewSwitcher that needs to display the imageview */ public synchronized void queueImageLoad(final ViewHolder holder, final FileElem elem) { // Wrap DownloadTask into another Runnable to track the statistics handler.postAtFrontOfQueue( new Runnable() { @Override public void run() { try { synchronized (holder) { // make sure this thread is the only one performing activities on this imageview BitmapFactory.Options lOptions = new BitmapFactory.Options(); lOptions.inSampleSize = 4; Bitmap lBitmap = BitmapFactory.decodeFile(elem.imagefilename, lOptions); // Load the image here signalUI(holder, elem, lBitmap); } } catch (Exception e) { e.printStackTrace(); } } }); }
public static void postRunnableAtFrontOfQueue(Runnable runnable) { mHandler.postAtFrontOfQueue(runnable); }