@Override public void getBitmap( final String path, final int width, final int height, final PhotoCallback callback, int position) { if (position == m_DecodeBitmapThread.m_Current) { Log.d(TAG, "getBitmap: now"); m_DecodeBitmapHandler.sendMessageAtFrontOfQueue( Message.obtain( m_FileHandler, MESSAGE_GET_BITMAP, width, height, new BitmapArgs(position, path, callback))); } else { Log.d(TAG, "getBitmap: later"); m_DecodeBitmapHandler.sendMessage( Message.obtain( m_FileHandler, MESSAGE_GET_BITMAP, width, height, new BitmapArgs(position, path, callback))); } }
@Override public void run() { this.setName("V8Engine"); System.loadLibrary("bgjs"); if (BuildConfig.DEBUG) { try { sleep(30); if (DEBUG) { Log.d(TAG, "Debugger can connect now"); } } catch (InterruptedException e) { Log.e(TAG, "Cannot sleep while waiting for debugger to settle", e); } } Looper.prepare(); mHandler = new Handler(this); initializeV8(assetManager); assetManager = null; ClientAndroid.load(mNativePtr, scriptPath); ClientAndroid.run(mNativePtr); mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_READY)); mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CLEANUP), DELAY_CLEANUP); Looper.loop(); }
@Override public Handle saveMedia(final MediaSaveTask task, final int flags) { verifyAccess(); if (task != null && isRunningOrInitializing()) { m_FileHandler.sendMessageAtFrontOfQueue( Message.obtain(m_FileHandler, MESSAGE_SAVE_MEDIA, task)); } return null; }
@Test public void shouldRemoveMessageFromQueueBeforeDispatching() throws Exception { Handler h = new Handler(Looper.myLooper()) { @Override public void handleMessage(Message msg) { assertFalse(hasMessages(0)); } }; h.sendEmptyMessage(0); h.sendMessageAtFrontOfQueue(h.obtainMessage()); }
@Override public boolean queueIdle() { // Log.i(logTag, "in idle handler"); if (nativeRender()) { // Log.i(logTag, "will re-run"); Message msg = Message.obtain(); handler.sendMessageAtFrontOfQueue( msg); // force idle handler to re-run in case of no pending msgs return true; } else { // Log.i(logTag, "won't re-run"); idleHandlerActive = false; return false; } }
@Test public void testSendMessageAtFrontOfQueueThenRunMainLooperOneMsgAtATime_shouldRunFrontOfQueueMsgFirst() throws Exception { Handler handler = new Handler(); ShadowLooper.pauseMainLooper(); // Post two messages to handler. Handle first message and confirm that msg posted // to front is removed. handler.obtainMessage(123).sendToTarget(); Message frontMsg = handler.obtainMessage(345); boolean result = handler.sendMessageAtFrontOfQueue(frontMsg); assertTrue(result); assertTrue(handler.hasMessages(123)); assertTrue(handler.hasMessages(345)); ShadowLooper.runMainLooperOneTask(); assertTrue(handler.hasMessages(123)); assertFalse(handler.hasMessages(345)); ShadowLooper.runMainLooperOneTask(); assertFalse(handler.hasMessages(123)); assertFalse(handler.hasMessages(345)); }