Search in sources :

Example 61 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObservers method register.

/**
     * Creates a wrapper that contains the caller context and a normalized request.
     * The request should be returned to the caller app, and the wrapper should be sent to this
     * object through #addObserver by the service handler.
     *
     * <p>It will register the observer asynchronously, so it is safe to call from any thread.
     *
     * @return the normalized request wrapped within {@link RequestInfo}.
     */
public DataUsageRequest register(DataUsageRequest inputRequest, Messenger messenger, IBinder binder, int callingUid, @NetworkStatsAccess.Level int accessLevel) {
    DataUsageRequest request = buildRequest(inputRequest);
    RequestInfo requestInfo = buildRequestInfo(request, messenger, binder, callingUid, accessLevel);
    if (LOGV)
        Slog.v(TAG, "Registering observer for " + request);
    getHandler().sendMessage(mHandler.obtainMessage(MSG_REGISTER, requestInfo));
    return request;
}
Also used : DataUsageRequest(android.net.DataUsageRequest)

Example 62 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsManager method registerUsageCallback.

/**
     * Registers to receive notifications about data usage on specified networks.
     *
     * <p>The callbacks will continue to be called as long as the process is live or
     * {@link #unregisterUsageCallback} is called.
     *
     * @param networkType Type of network to monitor. Either
                  {@link ConnectivityManager#TYPE_MOBILE} or {@link ConnectivityManager#TYPE_WIFI}.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param thresholdBytes Threshold in bytes to be notified on.
     * @param callback The {@link UsageCallback} that the system will call when data usage
     *            has exceeded the specified threshold.
     * @param handler to dispatch callback events through, otherwise if {@code null} it uses
     *            the calling thread.
     */
public void registerUsageCallback(int networkType, String subscriberId, long thresholdBytes, UsageCallback callback, @Nullable Handler handler) {
    checkNotNull(callback, "UsageCallback cannot be null");
    final Looper looper;
    if (handler == null) {
        looper = Looper.myLooper();
    } else {
        looper = handler.getLooper();
    }
    if (DBG) {
        Log.d(TAG, "registerUsageCallback called with: {" + " networkType=" + networkType + " subscriberId=" + subscriberId + " thresholdBytes=" + thresholdBytes + " }");
    }
    NetworkTemplate template = createTemplate(networkType, subscriberId);
    DataUsageRequest request = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, template, thresholdBytes);
    try {
        CallbackHandler callbackHandler = new CallbackHandler(looper, networkType, subscriberId, callback);
        callback.request = mService.registerUsageCallback(mContext.getOpPackageName(), request, new Messenger(callbackHandler), new Binder());
        if (DBG)
            Log.d(TAG, "registerUsageCallback returned " + callback.request);
        if (callback.request == null) {
            Log.e(TAG, "Request from callback is null; should not happen");
        }
    } catch (RemoteException e) {
        if (DBG)
            Log.d(TAG, "Remote exception when registering callback");
        throw e.rethrowFromSystemServer();
    }
}
Also used : Looper(android.os.Looper) DataUsageRequest(android.net.DataUsageRequest) Binder(android.os.Binder) NetworkTemplate(android.net.NetworkTemplate) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 63 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testRegister_thresholdTooLow_setsDefaultThreshold.

public void testRegister_thresholdTooLow_setsDefaultThreshold() throws Exception {
    long thresholdTooLowBytes = 1L;
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdTooLowBytes);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
}
Also used : DataUsageRequest(android.net.DataUsageRequest)

Example 64 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_initialSample_doesNotNotify.

public void testUpdateStats_initialSample_doesNotNotify() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = new NetworkStats(TEST_START, 1).addIfaceValues(TEST_IFACE, BASE_BYTES, 8L, BASE_BYTES, 16L);
    NetworkStats uidSnapshot = null;
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(INVALID_TYPE, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 65 with DataUsageRequest

use of android.net.DataUsageRequest in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_belowThreshold_doesNotNotify.

public void testUpdateStats_belowThreshold_doesNotNotify() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = new NetworkStats(TEST_START, 1).addIfaceValues(TEST_IFACE, BASE_BYTES, 8L, BASE_BYTES, 16L);
    NetworkStats uidSnapshot = null;
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    xtSnapshot = new NetworkStats(TEST_START, 1).addIfaceValues(TEST_IFACE, BASE_BYTES + 1024L, 10L, BASE_BYTES + 2048L, 20L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    mCv.block(WAIT_TIMEOUT);
    assertEquals(INVALID_TYPE, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Aggregations

DataUsageRequest (android.net.DataUsageRequest)75 NetworkStats (android.net.NetworkStats)32 NetworkIdentity (android.net.NetworkIdentity)28 Messenger (android.os.Messenger)9 NetworkTemplate (android.net.NetworkTemplate)5 Binder (android.os.Binder)5 Looper (android.os.Looper)5 RemoteException (android.os.RemoteException)5 ConditionVariable (android.os.ConditionVariable)4 IBinder (android.os.IBinder)3 Test (org.junit.Test)2