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

Commit 2d4630c8 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Hide NetworkTemplate details, buffered stats I/O."

parents 58c5dc1b 4e814c34
Loading
Loading
Loading
Loading
+53 −8
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.net;

import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.TYPE_WIMAX;
import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.isNetworkTypeMobile;
import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
@@ -38,41 +39,69 @@ import com.android.internal.util.Objects;
 */
public class NetworkTemplate implements Parcelable {

    /** {@hide} */
    public static final int MATCH_MOBILE_ALL = 1;
    /** {@hide} */
    public static final int MATCH_MOBILE_3G_LOWER = 2;
    /** {@hide} */
    public static final int MATCH_MOBILE_4G = 3;
    /** {@hide} */
    public static final int MATCH_WIFI = 4;
    /** {@hide} */
    public static final int MATCH_ETHERNET = 5;

    /**
     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
     * networks together. Only uses statistics for requested IMSI.
     */
    public static final int MATCH_MOBILE_ALL = 1;
    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
    }

    /**
     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
     * networks together that roughly meet a "3G" definition, or lower. Only
     * uses statistics for requested IMSI.
     */
    public static final int MATCH_MOBILE_3G_LOWER = 2;
    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId);
    }

    /**
     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
     * networks together that meet a "4G" definition. Only uses statistics for
     * requested IMSI.
     */
    public static final int MATCH_MOBILE_4G = 3;
    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId);
    }

    /**
     * Template to combine all {@link ConnectivityManager#TYPE_WIFI} style
     * networks together.
     */
    public static final int MATCH_WIFI = 4;
    public static NetworkTemplate buildTemplateWifi() {
        return new NetworkTemplate(MATCH_WIFI, null);
    }

    final int mMatchRule;
    final String mSubscriberId;
    /**
     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
     * networks together.
     */
    public static NetworkTemplate buildTemplateEthernet() {
        return new NetworkTemplate(MATCH_ETHERNET, null);
    }

    private final int mMatchRule;
    private final String mSubscriberId;

    /** {@hide} */
    public NetworkTemplate(int matchRule, String subscriberId) {
        this.mMatchRule = matchRule;
        this.mSubscriberId = subscriberId;
    }

    public NetworkTemplate(Parcel in) {
    private NetworkTemplate(Parcel in) {
        mMatchRule = in.readInt();
        mSubscriberId = in.readString();
    }
@@ -110,10 +139,12 @@ public class NetworkTemplate implements Parcelable {
        return false;
    }

    /** {@hide} */
    public int getMatchRule() {
        return mMatchRule;
    }

    /** {@hide} */
    public String getSubscriberId() {
        return mSubscriberId;
    }
@@ -131,6 +162,8 @@ public class NetworkTemplate implements Parcelable {
                return matchesMobile4g(ident);
            case MATCH_WIFI:
                return matchesWifi(ident);
            case MATCH_ETHERNET:
                return matchesEthernet(ident);
            default:
                throw new IllegalArgumentException("unknown network template");
        }
@@ -190,7 +223,17 @@ public class NetworkTemplate implements Parcelable {
        return false;
    }

    public static String getMatchRuleName(int matchRule) {
    /**
     * Check if matches Ethernet network template.
     */
    private boolean matchesEthernet(NetworkIdentity ident) {
        if (ident.mType == TYPE_ETHERNET) {
            return true;
        }
        return false;
    }

    private static String getMatchRuleName(int matchRule) {
        switch (matchRule) {
            case MATCH_MOBILE_3G_LOWER:
                return "MOBILE_3G_LOWER";
@@ -200,6 +243,8 @@ public class NetworkTemplate implements Parcelable {
                return "MOBILE_ALL";
            case MATCH_WIFI:
                return "WIFI";
            case MATCH_ETHERNET:
                return "ETHERNET";
            default:
                return "UNKNOWN";
        }
+3 −3
Original line number Diff line number Diff line
@@ -24,8 +24,8 @@ import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.EXTRA_UID;
import static android.net.ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.*;
import static android.net.ConnectivityManager.TYPE_MOBILE;
import static android.net.NetworkPolicy.LIMIT_DISABLED;
import static android.net.NetworkPolicy.WARNING_DISABLED;
@@ -42,7 +42,7 @@ import static android.net.NetworkPolicyManager.dumpRules;
import static android.net.NetworkPolicyManager.isUidValidForPolicy;
import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED;
@@ -678,7 +678,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            time.setToNow();
            final int cycleDay = time.monthDay;

            final NetworkTemplate template = new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
            final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
            mNetworkPolicy.add(
                    new NetworkPolicy(template, cycleDay, 4 * GB_IN_BYTES, LIMIT_DISABLED));
            writePolicyLocked();
+12 −11
Original line number Diff line number Diff line
@@ -73,11 +73,12 @@ import com.android.internal.os.AtomicFile;
import com.google.android.collect.Maps;
import com.google.android.collect.Sets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -719,10 +720,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        // clear any existing stats and read from disk
        mNetworkStats.clear();

        FileInputStream fis = null;
        DataInputStream in = null;
        try {
            fis = mNetworkFile.openRead();
            final DataInputStream in = new DataInputStream(fis);
            in = new DataInputStream(new BufferedInputStream(mNetworkFile.openRead()));

            // verify file magic header intact
            final int magic = in.readInt();
@@ -751,7 +751,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        } catch (IOException e) {
            Slog.e(TAG, "problem reading network stats", e);
        } finally {
            IoUtils.closeQuietly(fis);
            IoUtils.closeQuietly(in);
        }
    }

@@ -768,10 +768,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        // clear any existing stats and read from disk
        mUidStats.clear();

        FileInputStream fis = null;
        DataInputStream in = null;
        try {
            fis = mUidFile.openRead();
            final DataInputStream in = new DataInputStream(fis);
            in = new DataInputStream(new BufferedInputStream(mUidFile.openRead()));

            // verify file magic header intact
            final int magic = in.readInt();
@@ -826,7 +825,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        } catch (IOException e) {
            Slog.e(TAG, "problem reading uid stats", e);
        } finally {
            IoUtils.closeQuietly(fis);
            IoUtils.closeQuietly(in);
        }
    }

@@ -838,7 +837,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        FileOutputStream fos = null;
        try {
            fos = mNetworkFile.startWrite();
            final DataOutputStream out = new DataOutputStream(fos);
            final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos));

            out.writeInt(FILE_MAGIC);
            out.writeInt(VERSION_NETWORK_INIT);
@@ -850,6 +849,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
                history.writeToStream(out);
            }

            out.flush();
            mNetworkFile.finishWrite(fos);
        } catch (IOException e) {
            if (fos != null) {
@@ -871,7 +871,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        FileOutputStream fos = null;
        try {
            fos = mUidFile.startWrite();
            final DataOutputStream out = new DataOutputStream(fos);
            final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos));

            out.writeInt(FILE_MAGIC);
            out.writeInt(VERSION_UID_WITH_TAG);
@@ -895,6 +895,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
                }
            }

            out.flush();
            mUidFile.finishWrite(fos);
        } catch (IOException e) {
            if (fos != null) {
+1 −2
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.capture;
@@ -88,7 +87,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
    private static final long TEST_START = 1194220800000L;
    private static final String TEST_IFACE = "test0";

    private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
    private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi();

    private BroadcastInterceptingContext mServiceContext;
    private File mPolicyDir;
+7 −7
Original line number Diff line number Diff line
@@ -25,8 +25,8 @@ import static android.net.ConnectivityManager.TYPE_WIMAX;
import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifi;
import static android.net.TrafficStats.UID_REMOVED;
import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
@@ -81,9 +81,9 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
    private static final String IMSI_1 = "310004";
    private static final String IMSI_2 = "310260";

    private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
    private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1);
    private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2);
    private static NetworkTemplate sTemplateWifi = buildTemplateWifi();
    private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
    private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);

    private static final int UID_RED = 1001;
    private static final int UID_BLUE = 1002;
@@ -290,7 +290,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));

        // verify service recorded history
        history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
        history = mService.getHistoryForNetwork(sTemplateWifi);
        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L);
        assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
        assertEquals(2, history.size());
@@ -307,7 +307,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase {
        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));

        // verify identical stats, but spread across 4 buckets now
        history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
        history = mService.getHistoryForNetwork(sTemplateWifi);
        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L);
        assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
        assertEquals(4, history.size());