Search in sources :

Example 1 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by ParanoidAndroid.

the class CameraTest method initializeMessageLooper.

/*
     * Initializes the message looper so that the Camera object can 
     * receive the callback messages.
     */
private void initializeMessageLooper() {
    final ConditionVariable startDone = new ConditionVariable();
    Log.v(TAG, "start looper");
    new Thread() {

        @Override
        public void run() {
            // Set up a looper to be used by camera.
            Looper.prepare();
            Log.v(TAG, "start loopRun");
            // Save the looper so that we can terminate this thread 
            // after we are done with it.
            mLooper = Looper.myLooper();
            mCamera = Camera.open(CAMERA_ID);
            startDone.open();
            // Blocks forever until Looper.quit() is called.
            Looper.loop();
            Log.v(TAG, "initializeMessageLooper: quit.");
        }
    }.start();
    if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
        fail("initializeMessageLooper: start timeout");
    }
}
Also used : ConditionVariable(android.os.ConditionVariable)

Example 2 with ConditionVariable

use of android.os.ConditionVariable in project Meltdown by phubbard.

the class RestClient method verifyLogin.

// Verify that our credentials are correct by opening the API URL. If they are, 
// we'll get 'auth:1' in the result. We also check for the min API version (3).
public Boolean verifyLogin() {
    login_ok = false;
    condv = new ConditionVariable();
    class ltask extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... params) {
            setLoginResult(checkAuth());
            return null;
        }
    }
    new ltask().execute();
    if (!condv.block(10000L))
        Log.w(TAG, "Timed out on login check!");
    return login_ok;
}
Also used : ConditionVariable(android.os.ConditionVariable) AsyncTask(android.os.AsyncTask)

Example 3 with ConditionVariable

use of android.os.ConditionVariable in project platform_frameworks_base by android.

the class CameraTest method initializeMessageLooper.

/*
     * Initializes the message looper so that the Camera object can 
     * receive the callback messages.
     */
private void initializeMessageLooper() {
    final ConditionVariable startDone = new ConditionVariable();
    Log.v(TAG, "start looper");
    new Thread() {

        @Override
        public void run() {
            // Set up a looper to be used by camera.
            Looper.prepare();
            Log.v(TAG, "start loopRun");
            // Save the looper so that we can terminate this thread 
            // after we are done with it.
            mLooper = Looper.myLooper();
            mCamera = Camera.open(CAMERA_ID);
            startDone.open();
            // Blocks forever until Looper.quit() is called.
            Looper.loop();
            Log.v(TAG, "initializeMessageLooper: quit.");
        }
    }.start();
    if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
        fail("initializeMessageLooper: start timeout");
    }
}
Also used : ConditionVariable(android.os.ConditionVariable)

Example 4 with ConditionVariable

use of android.os.ConditionVariable in project platform_frameworks_base by android.

the class SyncRunner method scheduleFilterWake.

protected void scheduleFilterWake(Filter filter, int delay) {
    // Close the wake condition
    mWakeCondition.close();
    // Schedule the wake-up
    final Filter filterToSchedule = filter;
    final ConditionVariable conditionToWake = mWakeCondition;
    mWakeExecutor.schedule(new Runnable() {

        @Override
        public void run() {
            filterToSchedule.unsetStatus(Filter.STATUS_SLEEPING);
            conditionToWake.open();
        }
    }, delay, TimeUnit.MILLISECONDS);
}
Also used : ConditionVariable(android.os.ConditionVariable)

Example 5 with ConditionVariable

use of android.os.ConditionVariable in project platform_frameworks_base by android.

the class NetworkStatsServiceTest method testRegisterUsageCallback.

@Test
public void testRegisterUsageCallback() throws Exception {
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectBandwidthControlCheck();
    mService.forceUpdateIfaces();
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    String callingPackage = "the.calling.package";
    // very small; should be overriden by framework
    long thresholdInBytes = 1L;
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
    // Create a messenger that waits for callback activity
    ConditionVariable cv = new ConditionVariable(false);
    LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
    Messenger messenger = new Messenger(latchedHandler);
    // Force poll
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    // Register and verify request and that binder was called
    DataUsageRequest request = mService.registerUsageCallback(callingPackage, inputRequest, messenger, mBinder);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request.template));
    // 2 MB
    long minThresholdInBytes = 2 * 1024 * 1024;
    assertEquals(minThresholdInBytes, request.thresholdInBytes);
    // Send dummy message to make sure that any previous message has been handled
    mHandler.sendMessage(mHandler.obtainMessage(-1));
    mHandlerThread.waitForIdle(WAIT_TIMEOUT);
    // Make sure that the caller binder gets connected
    verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt());
    // modify some number on wifi, and trigger poll event
    // not enough traffic to call data usage callback
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
    // make sure callback has not being called
    assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
    // and bump forward again, with counters going higher. this is
    // important, since it will trigger the data usage callback
    incrementCurrentTime(DAY_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
    // Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
    cv.close();
    // Allow binder to disconnect
    when(mBinder.unlinkToDeath(any(IBinder.DeathRecipient.class), anyInt())).thenReturn(true);
    // Unregister request
    mService.unregisterUsageRequest(request);
    // Wait for the caller to ack receipt of CALLBACK_RELEASED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
    // Make sure that the caller binder gets disconnected
    verify(mBinder).unlinkToDeath(any(IBinder.DeathRecipient.class), anyInt());
}
Also used : ConditionVariable(android.os.ConditionVariable) DataUsageRequest(android.net.DataUsageRequest) NetworkStats(android.net.NetworkStats) Messenger(android.os.Messenger) Test(org.junit.Test)

Aggregations

ConditionVariable (android.os.ConditionVariable)122 LargeTest (android.test.suitebuilder.annotation.LargeTest)36 NetworkRequest (android.net.NetworkRequest)24 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 Handler (android.os.Handler)16 Messenger (android.os.Messenger)8 Test (org.junit.Test)6 Network (android.net.Network)5 Message (android.os.Message)5 PendingIntent (android.app.PendingIntent)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 Intent (android.content.Intent)4 IntentFilter (android.content.IntentFilter)4 ConnectivityManager (android.net.ConnectivityManager)4 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)4 DataUsageRequest (android.net.DataUsageRequest)4 NetworkCapabilities (android.net.NetworkCapabilities)4 NetworkStats (android.net.NetworkStats)4 HandlerThread (android.os.HandlerThread)4