Search in sources :

Example 51 with NetworkIdentity

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

the class NetworkPolicyManagerService method ensureActiveMobilePolicyNL.

private void ensureActiveMobilePolicyNL(String subscriberId) {
    // Poke around to see if we already have a policy
    final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true);
    for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) {
        final NetworkTemplate template = mNetworkPolicy.keyAt(i);
        if (template.matches(probeIdent)) {
            if (LOGD) {
                Slog.d(TAG, "Found template " + template + " which matches subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId));
            }
            return;
        }
    }
    Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) + "; generating default policy");
    // Build default mobile policy, and assume usage cycle starts today
    final int dataWarningConfig = mContext.getResources().getInteger(com.android.internal.R.integer.config_networkPolicyDefaultWarning);
    final long warningBytes;
    if (dataWarningConfig == WARNING_DISABLED) {
        warningBytes = WARNING_DISABLED;
    } else {
        warningBytes = dataWarningConfig * 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);
    addNetworkPolicyNL(policy);
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkIdentity(android.net.NetworkIdentity) NetworkPolicy(android.net.NetworkPolicy) Time(android.text.format.Time) TrustedTime(android.util.TrustedTime) NtpTrustedTime(android.util.NtpTrustedTime) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString)

Example 52 with NetworkIdentity

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

the class NetworkIdentitySet method compareTo.

@Override
public int compareTo(NetworkIdentitySet another) {
    if (isEmpty())
        return -1;
    if (another.isEmpty())
        return 1;
    final NetworkIdentity ident = iterator().next();
    final NetworkIdentity anotherIdent = another.iterator().next();
    return ident.compareTo(anotherIdent);
}
Also used : NetworkIdentity(android.net.NetworkIdentity)

Example 53 with NetworkIdentity

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

the class NetworkIdentitySet method writeToStream.

public void writeToStream(DataOutputStream out) throws IOException {
    out.writeInt(VERSION_ADD_METERED);
    out.writeInt(size());
    for (NetworkIdentity ident : this) {
        out.writeInt(ident.getType());
        out.writeInt(ident.getSubType());
        writeOptionalString(out, ident.getSubscriberId());
        writeOptionalString(out, ident.getNetworkId());
        out.writeBoolean(ident.getRoaming());
        out.writeBoolean(ident.getMetered());
    }
}
Also used : NetworkIdentity(android.net.NetworkIdentity)

Example 54 with NetworkIdentity

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

the class NetworkStatsCollectionTest method testAccessLevels.

public void testAccessLevels() throws Exception {
    final NetworkStatsCollection collection = new NetworkStatsCollection(HOUR_IN_MILLIS);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    final NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, TEST_IMSI, null, false, true));
    int myUid = Process.myUid();
    int otherUidInSameUser = Process.myUid() + 1;
    int uidInDifferentUser = Process.myUid() + UserHandle.PER_USER_RANGE;
    // Record one entry for the current UID.
    entry.rxBytes = 32;
    collection.recordData(identSet, myUid, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for another UID in this user.
    entry.rxBytes = 64;
    collection.recordData(identSet, otherUidInSameUser, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for the system UID.
    entry.rxBytes = 128;
    collection.recordData(identSet, Process.SYSTEM_UID, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for a UID in a different user.
    entry.rxBytes = 256;
    collection.recordData(identSet, uidInDifferentUser, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Verify the set of relevant UIDs for each access level.
    MoreAsserts.assertEquals(new int[] { myUid }, collection.getRelevantUids(NetworkStatsAccess.Level.DEFAULT));
    MoreAsserts.assertEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser }, collection.getRelevantUids(NetworkStatsAccess.Level.USER));
    MoreAsserts.assertEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser, uidInDifferentUser }, collection.getRelevantUids(NetworkStatsAccess.Level.DEVICE));
    // Verify security check in getHistory.
    assertNotNull(collection.getHistory(buildTemplateMobileAll(TEST_IMSI), myUid, SET_DEFAULT, TAG_NONE, 0, NetworkStatsAccess.Level.DEFAULT));
    try {
        collection.getHistory(buildTemplateMobileAll(TEST_IMSI), otherUidInSameUser, SET_DEFAULT, TAG_NONE, 0, NetworkStatsAccess.Level.DEFAULT);
        fail("Should have thrown SecurityException for accessing different UID");
    } catch (SecurityException e) {
    // expected
    }
    // Verify appropriate aggregation in getSummary.
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32, 0, 0, 0, NetworkStatsAccess.Level.DEFAULT);
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32 + 64 + 128, 0, 0, 0, NetworkStatsAccess.Level.USER);
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32 + 64 + 128 + 256, 0, 0, 0, NetworkStatsAccess.Level.DEVICE);
}
Also used : NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 55 with NetworkIdentity

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

the class NetworkStatsObserversTest method testUpdateStats_userAccess_usageSameUser_notifies.

public void testUpdateStats_userAccess_usageSameUser_notifies() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_BLUE, NetworkStatsAccess.Level.USER);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveUidIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = null;
    NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Aggregations

NetworkIdentity (android.net.NetworkIdentity)82 NetworkStats (android.net.NetworkStats)32 DataUsageRequest (android.net.DataUsageRequest)28 NetworkPolicy (android.net.NetworkPolicy)24 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)20 NetworkState (android.net.NetworkState)11 RemoteException (android.os.RemoteException)11 TelephonyManager (android.telephony.TelephonyManager)11 LinkProperties (android.net.LinkProperties)10 SubscriptionManager (android.telephony.SubscriptionManager)10 ArraySet (android.util.ArraySet)10 NetworkQuotaInfo (android.net.NetworkQuotaInfo)6 NetworkTemplate (android.net.NetworkTemplate)6 Time (android.text.format.Time)6 NtpTrustedTime (android.util.NtpTrustedTime)6 TrustedTime (android.util.TrustedTime)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)1 Map (java.util.Map)1