Search in sources :

Example 11 with MessageQueue

use of android.os.MessageQueue in project Rocket by mozilla-tw.

the class Engine method getReferenceQueue.

private ReferenceQueue<EngineResource<?>> getReferenceQueue() {
    if (resourceReferenceQueue == null) {
        resourceReferenceQueue = new ReferenceQueue<>();
        MessageQueue queue = Looper.myQueue();
        queue.addIdleHandler(new RefQueueIdleHandler(activeResources, resourceReferenceQueue));
    }
    return resourceReferenceQueue;
}
Also used : MessageQueue(android.os.MessageQueue)

Example 12 with MessageQueue

use of android.os.MessageQueue in project 360-Engine-for-Android by 360.

the class ContactSyncEngineTest method testAllSyncs.

/**
 * Checks different sync request that come from: -first time sync -fake
 * database change event -full sync -server sync
 */
@Suppress
public // Breaks tests.
void testAllSyncs() {
    Log.i(LOG_TAG, "**** testAllSyncs() begin ****");
    final ArrayList<ProcessorLog> processorLogs = new ArrayList<ProcessorLog>();
    final UiEventCall uiEventCall = new UiEventCall();
    final IEngineEventCallback engineEventCallback = new HelperClasses.EngineCallbackBase() {

        @Override
        public void onUiEvent(ServiceUiRequest event, int request, int status, Object data) {
            Log.i(LOG_TAG, "onUiEvent: " + event + ", " + request + ", " + status + ", " + data);
            uiEventCall.event = event.ordinal();
            uiEventCall.request = request;
            uiEventCall.status = status;
            uiEventCall.data = data;
        }
    };
    final ProcessorFactory factory = new ProcessorFactory() {

        @Override
        public BaseSyncProcessor create(int type, IContactSyncCallback callback, DatabaseHelper dbHelper) {
            Log.i(LOG_TAG, "create(), type=" + type);
            ProcessorLog log = new ProcessorLog();
            log.type = type;
            log.time = System.currentTimeMillis();
            processorLogs.add(log);
            return new DummySyncProcessor(mContactSyncEngine, null);
        }
    };
    minimalEngineSetup(engineEventCallback, factory);
    // set the connection to be fine
    NetworkAgent.setAgentState(NetworkAgent.AgentState.CONNECTED);
    long nextRuntime = mContactSyncEngine.getNextRunTime();
    // should be equal to -1 because first time sync has not been yet
    // started
    assertEquals(-1, mContactSyncEngine.getNextRunTime());
    // force a first time sync
    mContactSyncEngine.addUiStartFullSync();
    nextRuntime = mContactSyncEngine.getNextRunTime();
    // next runtime should be now
    assertEquals(0, nextRuntime);
    // perform the first time sync
    mContactSyncEngine.run();
    // check that first time sync is completed
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.SUCCESS.ordinal());
    // check that a thumbnail sync is scheduled for now
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(0, nextRuntime);
    // reset the processor logs
    processorLogs.clear();
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // check processor calls
    ProcessorLog log;
    assertEquals(2, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(1);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_THUMBNAILS, log.type);
    // check that native sync is scheduled for now
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(0, nextRuntime);
    // reset the processor logs
    processorLogs.clear();
    // get the native sync to be run
    mContactSyncEngine.run();
    // check processor calls
    assertEquals(1, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.UPDATE_NATIVE_CONTACTS, log.type);
    // reset the processor logs
    processorLogs.clear();
    // request a full sync
    mContactSyncEngine.addUiStartFullSync();
    nextRuntime = mContactSyncEngine.getNextRunTime();
    // next runtime should be now
    assertEquals(0, nextRuntime);
    // perform the full sync
    mContactSyncEngine.run();
    // check that first time sync is completed
    assertEquals(ServiceUiRequest.UI_REQUEST_COMPLETE.ordinal(), uiEventCall.event);
    assertEquals(uiEventCall.status, ServiceStatus.SUCCESS.ordinal());
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // get the native sync to be run
    mContactSyncEngine.run();
    // get the thumbnail sync to be run
    mContactSyncEngine.run();
    // check nothing to be done
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertEquals(-1, nextRuntime);
    // fake a database change notification
    Handler handler = getContactSyncEngineHandler(mContactSyncEngine);
    Message msg = new Message();
    msg.what = ServiceUiRequest.DATABASE_CHANGED_EVENT.ordinal();
    msg.arg1 = DatabaseHelper.DatabaseChangeType.CONTACTS.ordinal();
    msg.arg2 = 0;
    handler.sendMessage(msg);
    final Looper looper = Looper.myLooper();
    final MessageQueue queue = Looper.myQueue();
    queue.addIdleHandler(new MessageQueue.IdleHandler() {

        @Override
        public boolean queueIdle() {
            // message has been processed and the looper is now in idle
            // state
            // quit the loop() otherwise we would not be able to carry on
            looper.quit();
            return false;
        }
    });
    // get the message processed by the thread event loop
    Looper.loop();
    // check sync is scheduled within 30 seconds
    nextRuntime = mContactSyncEngine.getNextRunTime();
    assertTrue(isValueInsideErrorMargin(30000, nextRuntime - System.currentTimeMillis(), 5));
    final long timeBeforeWait = System.currentTimeMillis();
    // reset the processor logs
    processorLogs.clear();
    // call run() and check that nothing is performed
    mContactSyncEngine.run();
    assertEquals(0, processorLogs.size());
    // wait until we get the nextRuntime to now
    boolean isNextRuntimeNow = false;
    while (!isNextRuntimeNow) {
        try {
            long timeToWait = mContactSyncEngine.getNextRunTime();
            timeToWait = (timeToWait <= 0) ? 0 : timeToWait - System.currentTimeMillis();
            if (timeToWait > 0) {
                synchronized (this) {
                    Log.i(LOG_TAG, "timeToWait=" + timeToWait);
                    wait(timeToWait);
                }
            }
            if (mContactSyncEngine.getNextRunTime() < System.currentTimeMillis()) {
                isNextRuntimeNow = true;
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "testAllSyncs(): error while waiting for the runtime now");
        }
    }
    long timeAfterWait = System.currentTimeMillis();
    // check that we have waited about 30 seconds
    assertTrue(isValueInsideErrorMargin(timeAfterWait - timeBeforeWait, 30000, 5));
    // call run() until the sync is performed
    final long startTime = System.currentTimeMillis();
    while (processorLogs.size() < 7) {
        if (System.currentTimeMillis() - startTime > TEST_TIMEOUT) {
            fail("It seems that the engine is stuck, the processor logs should contain 7 objects by now!");
        }
        mContactSyncEngine.run();
    }
    // check processor calls
    assertEquals(6, processorLogs.size());
    log = processorLogs.get(0);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_CONTACTS, log.type);
    log = processorLogs.get(1);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_CONTACTS, log.type);
    log = processorLogs.get(2);
    assertEquals(ProcessorFactory.DOWNLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(3);
    assertEquals(ProcessorFactory.UPLOAD_SERVER_THUMBNAILS, log.type);
    log = processorLogs.get(4);
    assertEquals(ProcessorFactory.SYNC_ME_PROFILE, log.type);
    log = processorLogs.get(5);
    assertEquals(ProcessorFactory.UPDATE_NATIVE_CONTACTS, log.type);
    Log.i(LOG_TAG, "**** testAllSyncs() end ****");
}
Also used : Message(android.os.Message) ArrayList(java.util.ArrayList) Handler(android.os.Handler) ServiceUiRequest(com.vodafone360.people.service.ServiceUiRequest) Looper(android.os.Looper) MessageQueue(android.os.MessageQueue) DatabaseHelper(com.vodafone360.people.database.DatabaseHelper) ProcessorFactory(com.vodafone360.people.engine.contactsync.ProcessorFactory) IContactSyncCallback(com.vodafone360.people.engine.contactsync.IContactSyncCallback) IEngineEventCallback(com.vodafone360.people.engine.IEngineEventCallback) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 13 with MessageQueue

use of android.os.MessageQueue in project android_packages_apps_Camera by CyanogenMod.

the class PhotoModule method addIdleHandler.

private void addIdleHandler() {
    MessageQueue queue = Looper.myQueue();
    queue.addIdleHandler(new MessageQueue.IdleHandler() {

        @Override
        public boolean queueIdle() {
            Storage.getStorage().ensureOSXCompatible();
            return false;
        }
    });
}
Also used : MessageQueue(android.os.MessageQueue)

Example 14 with MessageQueue

use of android.os.MessageQueue in project DroidPlugin by DroidPluginTeam.

the class PluginHelper method fixMiUiLbeSecurity.

// 解决小米JLB22.0 4.1.1系统自带的小米安全中心(lbe.security.miui)广告拦截组件导致的插件白屏问题
private void fixMiUiLbeSecurity() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    // 卸载掉LBE安全的ApplicationLoaders.mLoaders钩子
    Class ApplicationLoaders = Class.forName("android.app.ApplicationLoaders");
    Object applicationLoaders = MethodUtils.invokeStaticMethod(ApplicationLoaders, "getDefault");
    Object mLoaders = FieldUtils.readField(applicationLoaders, "mLoaders", true);
    if (mLoaders instanceof HashMap) {
        HashMap oldValue = ((HashMap) mLoaders);
        if ("com.lbe.security.client.ClientContainer$MonitoredLoaderMap".equals(mLoaders.getClass().getName())) {
            HashMap value = new HashMap();
            value.putAll(oldValue);
            FieldUtils.writeField(applicationLoaders, "mLoaders", value, true);
        }
    }
    // 卸载掉LBE安全的ActivityThread.mPackages钩子
    Object currentActivityThread = ActivityThreadCompat.currentActivityThread();
    Object mPackages = FieldUtils.readField(currentActivityThread, "mPackages", true);
    if (mPackages instanceof HashMap) {
        HashMap oldValue = ((HashMap) mPackages);
        if ("com.lbe.security.client.ClientContainer$MonitoredPackageMap".equals(mPackages.getClass().getName())) {
            HashMap value = new HashMap();
            value.putAll(oldValue);
            FieldUtils.writeField(currentActivityThread, "mPackages", value, true);
        }
    }
    // 当前已经处在主线程消息队列中的所有消息,找出lbe消息并remove之
    if (Looper.getMainLooper() == Looper.myLooper()) {
        final MessageQueue queue = Looper.myQueue();
        try {
            Object mMessages = FieldUtils.readField(queue, "mMessages", true);
            if (mMessages instanceof Message) {
                findLbeMessageAndRemoveIt((Message) mMessages);
            }
            Log.e(TAG, "getMainLooper MessageQueue.IdleHandler:" + mMessages);
        } catch (Exception e) {
            Log.e(TAG, "fixMiUiLbeSecurity:error on remove lbe message", e);
        }
    }
}
Also used : MessageQueue(android.os.MessageQueue) Message(android.os.Message) HashMap(java.util.HashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 15 with MessageQueue

use of android.os.MessageQueue in project double-espresso by JakeWharton.

the class QueueInterrogator method initializeQueue.

private void initializeQueue() {
    if (interrogatedLooper == Looper.myLooper()) {
        interrogatedQueue = Looper.myQueue();
    } else {
        Handler oneShotHandler = new Handler(interrogatedLooper);
        FutureTask<MessageQueue> queueCapture = new FutureTask<MessageQueue>(new Callable<MessageQueue>() {

            @Override
            public MessageQueue call() {
                return Looper.myQueue();
            }
        });
        oneShotHandler.postAtFrontOfQueue(queueCapture);
        try {
            interrogatedQueue = queueCapture.get();
        } catch (ExecutionException ee) {
            throw propagate(ee.getCause());
        } catch (InterruptedException ie) {
            throw propagate(ie);
        }
    }
}
Also used : MessageQueue(android.os.MessageQueue) FutureTask(java.util.concurrent.FutureTask) Handler(android.os.Handler) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MessageQueue (android.os.MessageQueue)15 Handler (android.os.Handler)8 Message (android.os.Message)3 Looper (android.os.Looper)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 Suppress (android.test.suitebuilder.annotation.Suppress)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 DatabaseHelper (com.vodafone360.people.database.DatabaseHelper)1 IEngineEventCallback (com.vodafone360.people.engine.IEngineEventCallback)1 IContactSyncCallback (com.vodafone360.people.engine.contactsync.IContactSyncCallback)1 ProcessorFactory (com.vodafone360.people.engine.contactsync.ProcessorFactory)1 ServiceUiRequest (com.vodafone360.people.service.ServiceUiRequest)1 DownloadCallback (huyifei.mymvp.architecture.common.DownloadCallback)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1