Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5acf3257 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a method to get the total network usage."

parents f3f30ac1 0f1dc900
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import android.util.Log;
import android.util.Pair;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;

import java.time.ZonedDateTime;
import java.util.Date;
@@ -86,7 +87,8 @@ public class DataUsageController {
                * mContext.getResources().getInteger(R.integer.default_data_warning_level_mb);
    }

    private INetworkStatsSession getSession() {
    @VisibleForTesting
    INetworkStatsSession getSession() {
        if (mSession == null) {
            try {
                mSession = mStatsService.openSession();
@@ -176,6 +178,30 @@ public class DataUsageController {
        }
    }

    /**
     * Get the total usage level recorded in the network history
     * @param template the network template to retrieve the network history
     * @return the total usage level recorded in the network history
     */
    public long getHistoriclUsageLevel(NetworkTemplate template) {
        final INetworkStatsSession session = getSession();
        if (session != null) {
            try {
                final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
                final long now = System.currentTimeMillis();
                final NetworkStatsHistory.Entry entry =
                        history.getValues(0L /* start */, now /* end */, now, null /* recycle */);
                if (entry != null) {
                    return entry.rxBytes + entry.txBytes;
                }
                Log.w(TAG, "Failed to get data usage, no entry data");
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to get data usage, remote call failed");
            }
        }
        return 0L;
    }

    private NetworkPolicy findNetworkPolicy(NetworkTemplate template) {
        if (mPolicyManager == null || template == null) return null;
        final NetworkPolicy[] policies = mPolicyManager.getNetworkPolicies();
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.net;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.INetworkStatsSession;
import android.net.NetworkStatsHistory;
import android.net.NetworkStatsHistory.Entry;
import android.net.NetworkTemplate;
import android.os.RemoteException;
import android.text.format.DateUtils;

import com.android.settingslib.SettingsLibRobolectricTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;

@RunWith(SettingsLibRobolectricTestRunner.class)
public class DataUsageControllerTest {

    @Mock
    private INetworkStatsSession mSession;

    private Context mContext;
    private DataUsageController mController;
    private NetworkStatsHistory mNetworkStatsHistory;

    @Before
    public void setUp() throws RemoteException {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mController = spy(new DataUsageController(mContext));
        mNetworkStatsHistory = spy(
                new NetworkStatsHistory(DateUtils.DAY_IN_MILLIS /* bucketDuration */));
        doReturn(mNetworkStatsHistory)
                .when(mSession).getHistoryForNetwork(any(NetworkTemplate.class), anyInt());
    }

    @Test
    public void getHistoriclUsageLevel_noNetworkSession_shouldReturn0() {
        doReturn(null).when(mController).getSession();

        assertThat(mController.getHistoriclUsageLevel(null /* template */)).isEqualTo(0L);

    }

    @Test
    public void getHistoriclUsageLevel_noUsageData_shouldReturn0() {
        doReturn(mSession).when(mController).getSession();

        assertThat(mController.getHistoriclUsageLevel(NetworkTemplate.buildTemplateWifiWildcard()))
                .isEqualTo(0L);

    }

    @Test
    public void getHistoriclUsageLevel_hasUsageData_shouldReturnTotalUsage() {
        doReturn(mSession).when(mController).getSession();
        final long receivedBytes = 743823454L;
        final long transmittedBytes = 16574289L;
        final Entry entry = new Entry();
        entry.bucketStart = 1521583200000L;
        entry.rxBytes = receivedBytes;
        entry.txBytes = transmittedBytes;
        when(mNetworkStatsHistory.getValues(eq(0L), anyLong(), anyLong(), nullable(Entry.class)))
                .thenReturn(entry);

        assertThat(mController.getHistoriclUsageLevel(NetworkTemplate.buildTemplateWifiWildcard()))
                .isEqualTo(receivedBytes + transmittedBytes);

    }
}