Search in sources :

Example 31 with NetworkTemplate

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

the class NetworkStatsManager method querySummaryForUser.

/**
     * Query network usage statistics summaries. Result is summarised data usage for all uids
     * belonging to calling user. Result is a single Bucket aggregated over time, state and uid.
     * This means the bucket's start and end timestamp are going to be the same as the 'startTime'
     * and 'endTime' parameters, state is going to be {@link NetworkStats.Bucket#STATE_ALL} and uid
     * {@link NetworkStats.Bucket#UID_ALL}.
     *
     * @param networkType As defined in {@link ConnectivityManager}, e.g.
     *            {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI}
     *            etc.
     * @param subscriberId If applicable, the subscriber id of the network interface.
     * @param startTime Start of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @param endTime End of period. Defined in terms of "Unix time", see
     *            {@link java.lang.System#currentTimeMillis}.
     * @return Bucket object or null if permissions are insufficient or error happened during
     *         statistics collection.
     */
public Bucket querySummaryForUser(int networkType, String subscriberId, long startTime, long endTime) throws SecurityException, RemoteException {
    NetworkTemplate template;
    try {
        template = createTemplate(networkType, subscriberId);
    } catch (IllegalArgumentException e) {
        if (DBG)
            Log.e(TAG, "Cannot create template", e);
        return null;
    }
    NetworkStats stats;
    stats = new NetworkStats(mContext, template, startTime, endTime);
    stats.startSummaryEnumeration();
    stats.close();
    return stats.getSummaryAggregate();
}
Also used : NetworkTemplate(android.net.NetworkTemplate)

Example 32 with NetworkTemplate

use of android.net.NetworkTemplate in project android_frameworks_base by DirtyUnicorns.

the class DataIdleTest method testWifiIdle.

/**
     * Test that dumps all the data usage metrics for wifi to instrumentation out.
     */
public void testWifiIdle() {
    NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
    fetchStats(template);
}
Also used : NetworkTemplate(android.net.NetworkTemplate)

Example 33 with NetworkTemplate

use of android.net.NetworkTemplate in project android_frameworks_base by DirtyUnicorns.

the class DataIdleTest method testMobile.

/**
     * Test that dumps all the data usage metrics for all mobile to instrumentation out.
     */
public void testMobile() {
    String subscriberId = mTelephonyManager.getSubscriberId();
    NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
    fetchStats(template);
}
Also used : NetworkTemplate(android.net.NetworkTemplate)

Example 34 with NetworkTemplate

use of android.net.NetworkTemplate in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsService method createSession.

private INetworkStatsSession createSession(final String callingPackage, boolean pollOnCreate) {
    assertBandwidthControlEnabled();
    if (pollOnCreate) {
        final long ident = Binder.clearCallingIdentity();
        try {
            performPoll(FLAG_PERSIST_ALL);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }
    return new INetworkStatsSession.Stub() {

        private NetworkStatsCollection mUidComplete;

        private NetworkStatsCollection mUidTagComplete;

        private String mCallingPackage = callingPackage;

        private NetworkStatsCollection getUidComplete() {
            synchronized (mStatsLock) {
                if (mUidComplete == null) {
                    mUidComplete = mUidRecorder.getOrLoadCompleteLocked();
                }
                return mUidComplete;
            }
        }

        private NetworkStatsCollection getUidTagComplete() {
            synchronized (mStatsLock) {
                if (mUidTagComplete == null) {
                    mUidTagComplete = mUidTagRecorder.getOrLoadCompleteLocked();
                }
                return mUidTagComplete;
            }
        }

        @Override
        public int[] getRelevantUids() {
            return getUidComplete().getRelevantUids(checkAccessLevel(mCallingPackage));
        }

        @Override
        public NetworkStats getDeviceSummaryForNetwork(NetworkTemplate template, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (accessLevel < NetworkStatsAccess.Level.DEVICESUMMARY) {
                throw new SecurityException("Calling package " + mCallingPackage + " cannot access device summary network stats");
            }
            NetworkStats result = new NetworkStats(end - start, 1);
            final long ident = Binder.clearCallingIdentity();
            try {
                // Using access level higher than the one we checked for above.
                // Reason is that we are combining usage data in a way that is not PII
                // anymore.
                result.combineAllValues(internalGetSummaryForNetwork(template, start, end, NetworkStatsAccess.Level.DEVICE));
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
            return result;
        }

        @Override
        public NetworkStats getSummaryForNetwork(NetworkTemplate template, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            return internalGetSummaryForNetwork(template, start, end, accessLevel);
        }

        @Override
        public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            return internalGetHistoryForNetwork(template, fields, accessLevel);
        }

        @Override
        public NetworkStats getSummaryForAllUid(NetworkTemplate template, long start, long end, boolean includeTags) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            final NetworkStats stats = getUidComplete().getSummary(template, start, end, accessLevel);
            if (includeTags) {
                final NetworkStats tagStats = getUidTagComplete().getSummary(template, start, end, accessLevel);
                stats.combineAllValues(tagStats);
            }
            return stats;
        }

        @Override
        public NetworkStatsHistory getHistoryForUid(NetworkTemplate template, int uid, int set, int tag, int fields) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (tag == TAG_NONE) {
                return getUidComplete().getHistory(template, uid, set, tag, fields, accessLevel);
            } else {
                return getUidTagComplete().getHistory(template, uid, set, tag, fields, accessLevel);
            }
        }

        @Override
        public NetworkStatsHistory getHistoryIntervalForUid(NetworkTemplate template, int uid, int set, int tag, int fields, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (tag == TAG_NONE) {
                return getUidComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
            } else if (uid == Binder.getCallingUid()) {
                return getUidTagComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
            } else {
                throw new SecurityException("Calling package " + mCallingPackage + " cannot access tag information from a different uid");
            }
        }

        @Override
        public void close() {
            mUidComplete = null;
            mUidTagComplete = null;
        }
    };
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkStats(android.net.NetworkStats)

Example 35 with NetworkTemplate

use of android.net.NetworkTemplate in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerService method readPolicyAL.

private void readPolicyAL() {
    if (LOGV)
        Slog.v(TAG, "readPolicyAL()");
    // clear any existing policy and read from disk
    mNetworkPolicy.clear();
    mUidPolicy.clear();
    FileInputStream fis = null;
    try {
        fis = mPolicyFile.openRead();
        final XmlPullParser in = Xml.newPullParser();
        in.setInput(fis, StandardCharsets.UTF_8.name());
        int type;
        int version = VERSION_INIT;
        boolean insideWhitelist = false;
        while ((type = in.next()) != END_DOCUMENT) {
            final String tag = in.getName();
            if (type == START_TAG) {
                if (TAG_POLICY_LIST.equals(tag)) {
                    final boolean oldValue = mRestrictBackground;
                    version = readIntAttribute(in, ATTR_VERSION);
                    if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) {
                        mRestrictBackground = readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                    } else {
                        mRestrictBackground = false;
                    }
                    if (mRestrictBackground != oldValue) {
                        // Some early services may have read the default value,
                        // so notify them that it's changed
                        mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0).sendToTarget();
                    }
                } else if (TAG_NETWORK_POLICY.equals(tag)) {
                    final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE);
                    final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID);
                    final String networkId;
                    if (version >= VERSION_ADDED_NETWORK_ID) {
                        networkId = in.getAttributeValue(null, ATTR_NETWORK_ID);
                    } else {
                        networkId = null;
                    }
                    final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY);
                    final String cycleTimezone;
                    if (version >= VERSION_ADDED_TIMEZONE) {
                        cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE);
                    } else {
                        cycleTimezone = Time.TIMEZONE_UTC;
                    }
                    final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES);
                    final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES);
                    final long lastLimitSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE);
                    } else if (version >= VERSION_ADDED_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE);
                    } else {
                        lastLimitSnooze = SNOOZE_NEVER;
                    }
                    final boolean metered;
                    if (version >= VERSION_ADDED_METERED) {
                        metered = readBooleanAttribute(in, ATTR_METERED);
                    } else {
                        switch(networkTemplate) {
                            case MATCH_MOBILE_3G_LOWER:
                            case MATCH_MOBILE_4G:
                            case MATCH_MOBILE_ALL:
                                metered = true;
                                break;
                            default:
                                metered = false;
                        }
                    }
                    final long lastWarningSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE);
                    } else {
                        lastWarningSnooze = SNOOZE_NEVER;
                    }
                    final boolean inferred;
                    if (version >= VERSION_ADDED_INFERRED) {
                        inferred = readBooleanAttribute(in, ATTR_INFERRED);
                    } else {
                        inferred = false;
                    }
                    final NetworkTemplate template = new NetworkTemplate(networkTemplate, subscriberId, networkId);
                    if (template.isPersistable()) {
                        mNetworkPolicy.put(template, new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, limitBytes, lastWarningSnooze, lastLimitSnooze, metered, inferred));
                    }
                } else if (TAG_UID_POLICY.equals(tag)) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUncheckedUL(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                } else if (TAG_APP_POLICY.equals(tag)) {
                    final int appId = readIntAttribute(in, ATTR_APP_ID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    // TODO: set for other users during upgrade
                    // app policy is deprecated so this is only used in pre system user split.
                    final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUncheckedUL(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                } else if (TAG_WHITELIST.equals(tag)) {
                    insideWhitelist = true;
                } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    mRestrictBackgroundWhitelistUids.put(uid, true);
                } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    mRestrictBackgroundWhitelistRevokedUids.put(uid, true);
                }
            } else if (type == END_TAG) {
                if (TAG_WHITELIST.equals(tag)) {
                    insideWhitelist = false;
                }
            }
        }
    } catch (FileNotFoundException e) {
        // missing policy is okay, probably first boot
        upgradeLegacyBackgroundDataUL();
    } catch (IOException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } catch (XmlPullParserException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

NetworkTemplate (android.net.NetworkTemplate)111 NetworkPolicy (android.net.NetworkPolicy)33 RemoteException (android.os.RemoteException)21 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)15 NetworkStats (android.net.NetworkStats)12 IOException (java.io.IOException)12 AlertDialog (android.app.AlertDialog)6 Dialog (android.app.Dialog)6 DialogInterface (android.content.DialogInterface)6 NetworkIdentity (android.net.NetworkIdentity)6 Time (android.text.format.Time)6 NtpTrustedTime (android.util.NtpTrustedTime)6 TrustedTime (android.util.TrustedTime)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 AppItem (com.android.settingslib.AppItem)6 FileInputStream (java.io.FileInputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 Bucket (android.app.usage.NetworkStats.Bucket)5