Search in sources :

Example 71 with NetworkTemplate

use of android.net.NetworkTemplate in project platform_frameworks_base by android.

the class NetworkStatsService method performSampleLocked.

/**
     * Sample recent statistics summary into {@link EventLog}.
     */
private void performSampleLocked() {
    // TODO: migrate trustedtime fixes to separate binary log events
    final long trustedTime = mTime.hasCache() ? mTime.currentTimeMillis() : -1;
    NetworkTemplate template;
    NetworkStats.Entry devTotal;
    NetworkStats.Entry xtTotal;
    NetworkStats.Entry uidTotal;
    // collect mobile sample
    template = buildTemplateMobileWildcard();
    devTotal = mDevRecorder.getTotalSinceBootLocked(template);
    xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
    uidTotal = mUidRecorder.getTotalSinceBootLocked(template);
    EventLogTags.writeNetstatsMobileSample(devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, trustedTime);
    // collect wifi sample
    template = buildTemplateWifiWildcard();
    devTotal = mDevRecorder.getTotalSinceBootLocked(template);
    xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
    uidTotal = mUidRecorder.getTotalSinceBootLocked(template);
    EventLogTags.writeNetstatsWifiSample(devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, trustedTime);
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkStats(android.net.NetworkStats)

Example 72 with NetworkTemplate

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

the class NetworkOverLimitActivity method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    final NetworkTemplate template = getIntent().getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getLimitedDialogTitleForTemplate(template));
    builder.setMessage(R.string.data_usage_disabled_dialog);
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(R.string.data_usage_disabled_dialog_enable, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            snoozePolicy(template);
        }
    });
    final Dialog dialog = builder.create();
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        public void onDismiss(DialogInterface dialog) {
            finish();
        }
    });
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) NetworkTemplate(android.net.NetworkTemplate) DialogInterface(android.content.DialogInterface) Dialog(android.app.Dialog) AlertDialog(android.app.AlertDialog)

Example 73 with NetworkTemplate

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

the class NetworkPolicyManagerService method writePolicyLocked.

private void writePolicyLocked() {
    if (LOGV)
        Slog.v(TAG, "writePolicyLocked()");
    FileOutputStream fos = null;
    try {
        fos = mPolicyFile.startWrite();
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(fos, "utf-8");
        out.startDocument(null, true);
        out.startTag(null, TAG_POLICY_LIST);
        writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST);
        writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground);
        // write all known network policies
        for (NetworkPolicy policy : mNetworkPolicy.values()) {
            final NetworkTemplate template = policy.template;
            out.startTag(null, TAG_NETWORK_POLICY);
            writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule());
            final String subscriberId = template.getSubscriberId();
            if (subscriberId != null) {
                out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId);
            }
            final String networkId = template.getNetworkId();
            if (networkId != null) {
                out.attribute(null, ATTR_NETWORK_ID, networkId);
            }
            writeIntAttribute(out, ATTR_CYCLE_DAY, policy.cycleDay);
            out.attribute(null, ATTR_CYCLE_TIMEZONE, policy.cycleTimezone);
            writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes);
            writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes);
            writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze);
            writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze);
            writeBooleanAttribute(out, ATTR_METERED, policy.metered);
            writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred);
            out.endTag(null, TAG_NETWORK_POLICY);
        }
        // write all known uid policies
        for (int i = 0; i < mUidPolicy.size(); i++) {
            final int uid = mUidPolicy.keyAt(i);
            final int policy = mUidPolicy.valueAt(i);
            // skip writing empty policies
            if (policy == POLICY_NONE)
                continue;
            out.startTag(null, TAG_UID_POLICY);
            writeIntAttribute(out, ATTR_UID, uid);
            writeIntAttribute(out, ATTR_POLICY, policy);
            out.endTag(null, TAG_UID_POLICY);
        }
        out.endTag(null, TAG_POLICY_LIST);
        out.endDocument();
        mPolicyFile.finishWrite(fos);
    } catch (IOException e) {
        if (fos != null) {
            mPolicyFile.failWrite(fos);
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 74 with NetworkTemplate

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

the class NetworkPolicyManagerService method ensureActiveMobilePolicyLocked.

/**
     * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we
     * have at least a default mobile policy defined.
     */
private void ensureActiveMobilePolicyLocked() {
    if (LOGV)
        Slog.v(TAG, "ensureActiveMobilePolicyLocked()");
    if (mSuppressDefaultPolicy)
        return;
    final TelephonyManager tele = TelephonyManager.from(mContext);
    // avoid creating policy when SIM isn't ready
    if (tele.getSimState() != SIM_STATE_READY)
        return;
    final String subscriberId = tele.getSubscriberId();
    final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false);
    // examine to see if any policy is defined for active mobile
    boolean mobileDefined = false;
    for (NetworkPolicy policy : mNetworkPolicy.values()) {
        if (policy.template.matches(probeIdent)) {
            mobileDefined = true;
        }
    }
    if (!mobileDefined) {
        Slog.i(TAG, "no policy for active mobile network; generating default policy");
        // build default mobile policy, and assume usage cycle starts today
        final long warningBytes = mContext.getResources().getInteger(com.android.internal.R.integer.config_networkPolicyDefaultWarning) * MB_IN_BYTES;
        final Time time = new Time();
        time.setToNow();
        final int cycleDay = time.monthDay;
        final String cycleTimezone = time.timezone;
        final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
        final NetworkPolicy policy = new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, true);
        addNetworkPolicyLocked(policy);
    }
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkIdentity(android.net.NetworkIdentity) TelephonyManager(android.telephony.TelephonyManager) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) TrustedTime(android.util.TrustedTime) NtpTrustedTime(android.util.NtpTrustedTime)

Example 75 with NetworkTemplate

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

the class NetworkPolicyManagerService method readPolicyLocked.

private void readPolicyLocked() {
    if (LOGV)
        Slog.v(TAG, "readPolicyLocked()");
    // 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, null);
        int type;
        int version = VERSION_INIT;
        while ((type = in.next()) != END_DOCUMENT) {
            final String tag = in.getName();
            if (type == START_TAG) {
                if (TAG_POLICY_LIST.equals(tag)) {
                    version = readIntAttribute(in, ATTR_VERSION);
                    if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) {
                        mRestrictBackground = readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                    } else {
                        mRestrictBackground = false;
                    }
                } 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);
                    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)) {
                        setUidPolicyUnchecked(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
                    final int uid = UserHandle.getUid(UserHandle.USER_OWNER, appId);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUnchecked(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
        // missing policy is okay, probably first boot
        upgradeLegacyBackgroundData();
    } 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) 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