Search in sources :

Example 86 with NetworkStatsHistory

use of android.net.NetworkStatsHistory in project android_frameworks_base by AOSPA.

the class NetworkStatsCollection method getHistory.

/**
     * Combine all {@link NetworkStatsHistory} in this collection which match
     * the requested parameters.
     */
public NetworkStatsHistory getHistory(NetworkTemplate template, int uid, int set, int tag, int fields, long start, long end, @NetworkStatsAccess.Level int accessLevel, int callerUid) {
    if (!NetworkStatsAccess.isAccessibleToUser(uid, callerUid, accessLevel)) {
        throw new SecurityException("Network stats history of uid " + uid + " is forbidden for caller " + callerUid);
    }
    final NetworkStatsHistory combined = new NetworkStatsHistory(mBucketDuration, start == end ? 1 : estimateBuckets(), fields);
    // shortcut when we know stats will be empty
    if (start == end)
        return combined;
    for (int i = 0; i < mStats.size(); i++) {
        final Key key = mStats.keyAt(i);
        if (key.uid == uid && NetworkStats.setMatches(set, key.set) && key.tag == tag && templateMatches(template, key.ident)) {
            final NetworkStatsHistory value = mStats.valueAt(i);
            combined.recordHistory(value, start, end);
        }
    }
    return combined;
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 87 with NetworkStatsHistory

use of android.net.NetworkStatsHistory in project android_frameworks_base by AOSPA.

the class NetworkStatsCollection method findOrCreateHistory.

private NetworkStatsHistory findOrCreateHistory(NetworkIdentitySet ident, int uid, int set, int tag) {
    final Key key = new Key(ident, uid, set, tag);
    final NetworkStatsHistory existing = mStats.get(key);
    // update when no existing, or when bucket duration changed
    NetworkStatsHistory updated = null;
    if (existing == null) {
        updated = new NetworkStatsHistory(mBucketDuration, 10);
    } else if (existing.getBucketDuration() != mBucketDuration) {
        updated = new NetworkStatsHistory(existing, mBucketDuration);
    }
    if (updated != null) {
        mStats.put(key, updated);
        return updated;
    } else {
        return existing;
    }
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 88 with NetworkStatsHistory

use of android.net.NetworkStatsHistory in project android_frameworks_base by ResurrectionRemix.

the class DataUsageController method getDataUsageInfo.

public DataUsageInfo getDataUsageInfo(NetworkTemplate template) {
    final INetworkStatsSession session = getSession();
    if (session == null) {
        return warn("no stats session");
    }
    final NetworkPolicy policy = findNetworkPolicy(template);
    try {
        final NetworkStatsHistory history = session.getHistoryForNetwork(template, FIELDS);
        final long now = System.currentTimeMillis();
        final long start, end;
        if (policy != null && policy.cycleDay > 0) {
            // period = determined from cycleDay
            if (DEBUG)
                Log.d(TAG, "Cycle day=" + policy.cycleDay + " tz=" + policy.cycleTimezone);
            final Time nowTime = new Time(policy.cycleTimezone);
            nowTime.setToNow();
            final Time policyTime = new Time(nowTime);
            policyTime.set(policy.cycleDay, policyTime.month, policyTime.year);
            policyTime.normalize(false);
            if (nowTime.after(policyTime)) {
                start = policyTime.toMillis(false);
                end = addMonth(policyTime, 1).toMillis(false);
            } else {
                start = addMonth(policyTime, -1).toMillis(false);
                end = policyTime.toMillis(false);
            }
        } else {
            // period = last 4 wks
            end = now;
            start = now - DateUtils.WEEK_IN_MILLIS * 4;
        }
        final long callStart = System.currentTimeMillis();
        final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
        final long callEnd = System.currentTimeMillis();
        if (DEBUG)
            Log.d(TAG, String.format("history call from %s to %s now=%s took %sms: %s", new Date(start), new Date(end), new Date(now), callEnd - callStart, historyEntryToString(entry)));
        if (entry == null) {
            return warn("no entry data");
        }
        final long totalBytes = entry.rxBytes + entry.txBytes;
        final DataUsageInfo usage = new DataUsageInfo();
        usage.startDate = start;
        usage.usageLevel = totalBytes;
        usage.period = formatDateRange(start, end);
        if (policy != null) {
            usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
            usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
        } else {
            usage.warningLevel = getDefaultWarningLevel();
        }
        if (usage != null && mNetworkController != null) {
            usage.carrier = mNetworkController.getMobileDataNetworkName();
        }
        return usage;
    } catch (RemoteException e) {
        return warn("remote call failed");
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) NetworkStatsHistory(android.net.NetworkStatsHistory) RemoteException(android.os.RemoteException) Date(java.util.Date)

Example 89 with NetworkStatsHistory

use of android.net.NetworkStatsHistory in project android_frameworks_base by ResurrectionRemix.

the class NetworkStatsCollection method read.

public void read(DataInputStream in) throws IOException {
    // verify file magic header intact
    final int magic = in.readInt();
    if (magic != FILE_MAGIC) {
        throw new ProtocolException("unexpected magic: " + magic);
    }
    final int version = in.readInt();
    switch(version) {
        case VERSION_UNIFIED_INIT:
            {
                // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                final int identSize = in.readInt();
                for (int i = 0; i < identSize; i++) {
                    final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                    final int size = in.readInt();
                    for (int j = 0; j < size; j++) {
                        final int uid = in.readInt();
                        final int set = in.readInt();
                        final int tag = in.readInt();
                        final Key key = new Key(ident, uid, set, tag);
                        final NetworkStatsHistory history = new NetworkStatsHistory(in);
                        recordHistory(key, history);
                    }
                }
                break;
            }
        default:
            {
                throw new ProtocolException("unexpected version: " + version);
            }
    }
}
Also used : ProtocolException(java.net.ProtocolException) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 90 with NetworkStatsHistory

use of android.net.NetworkStatsHistory in project android_frameworks_base by ResurrectionRemix.

the class NetworkStatsCollection method readLegacyUid.

@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_UID_INIT:
                {
                    // mapping into NetworkIdentitySet.
                    break;
                }
            case VERSION_UID_WITH_IDENT:
                {
                    // for a short time.
                    break;
                }
            case VERSION_UID_WITH_TAG:
            case VERSION_UID_WITH_SET:
                {
                    // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                    final int identSize = in.readInt();
                    for (int i = 0; i < identSize; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final int size = in.readInt();
                        for (int j = 0; j < size; j++) {
                            final int uid = in.readInt();
                            final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
                            final int tag = in.readInt();
                            final Key key = new Key(ident, uid, set, tag);
                            final NetworkStatsHistory history = new NetworkStatsHistory(in);
                            if ((tag == TAG_NONE) != onlyTags) {
                                recordHistory(key, history);
                            }
                        }
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Aggregations

NetworkStatsHistory (android.net.NetworkStatsHistory)109 NetworkStats (android.net.NetworkStats)21 ProtocolException (java.net.ProtocolException)18 AtomicFile (android.util.AtomicFile)12 BufferedInputStream (java.io.BufferedInputStream)12 DataInputStream (java.io.DataInputStream)12 FileNotFoundException (java.io.FileNotFoundException)12 RemoteException (android.os.RemoteException)10 ArrayList (java.util.ArrayList)6 INetworkStatsSession (android.net.INetworkStatsSession)5 NetworkPolicy (android.net.NetworkPolicy)5 Time (android.text.format.Time)5 ArrayMap (android.util.ArrayMap)5 IntArray (android.util.IntArray)5 Date (java.util.Date)5 Suppress (android.test.suitebuilder.annotation.Suppress)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1