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

Commit 2f37fabe authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by junyulai
Browse files

Remove @hide usage and fix NetworkPolicy backup

Remove usage of hidden symbols for network stats APIs, replacing them
with public API alternatives.

Also fix backup of NetworkPolicy when the set of subscriber IDs is
empty. This as not caught because the test used a hidden API builder
that creates a non-empty set containing null.

Bug: 197717846
Test: atest FrameworksCoreTests
  (cherry-picked and fixed merged conflict from ag/16781267)
Change-Id: I45a98795c571ac97bd6025988da156bd06d57e45
Merged-In: I45a98795c571ac97bd6025988da156bd06d57e45
parent ba28bb57
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -338,7 +338,9 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
        out.writeInt(TEMPLATE_BACKUP_VERSION_LATEST);

        out.writeInt(template.getMatchRule());
        BackupUtils.writeString(out, template.getSubscriberIds().iterator().next());
        final Set<String> subscriberIds = template.getSubscriberIds();
        BackupUtils.writeString(out, subscriberIds.isEmpty()
                ? null : subscriberIds.iterator().next());
        BackupUtils.writeString(out, template.getWifiNetworkKeys().isEmpty()
                ? null : template.getWifiNetworkKeys().iterator().next());
        out.writeInt(template.getMeteredness());
+13 −3
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package android.net

import android.net.NetworkStats.METERED_YES
import android.net.NetworkTemplate.MATCH_BLUETOOTH
import android.net.NetworkTemplate.MATCH_CARRIER
import android.net.NetworkTemplate.MATCH_ETHERNET
import android.net.NetworkTemplate.MATCH_MOBILE
import android.net.NetworkTemplate.MATCH_WIFI
@@ -39,11 +41,19 @@ class NetworkPolicyTest {
    @Test
    fun testTemplateBackupRestore() {
        assertPolicyBackupRestore(createTestPolicyForTemplate(
                NetworkTemplate.buildTemplateWifi(TEST_WIFI_NETWORK_KEY1)))
                NetworkTemplate.Builder(MATCH_WIFI)
                    .setWifiNetworkKeys(setOf(TEST_WIFI_NETWORK_KEY1))
                    .build()))
        assertPolicyBackupRestore(createTestPolicyForTemplate(
                NetworkTemplate.buildTemplateMobileAll(TEST_IMSI1)))
                NetworkTemplate.Builder(MATCH_MOBILE)
                    .setSubscriberIds(setOf(TEST_IMSI1))
                    .setMeteredness(METERED_YES)
                    .build()))
        assertPolicyBackupRestore(createTestPolicyForTemplate(
                NetworkTemplate.buildTemplateCarrierMetered(TEST_IMSI1)))
                NetworkTemplate.Builder(MATCH_CARRIER)
                    .setSubscriberIds(setOf(TEST_IMSI1))
                    .setMeteredness(METERED_YES)
                    .build()))
    }

    private fun createTestPolicyForTemplate(template: NetworkTemplate): NetworkPolicy {
+7 −2
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.internal.os;

import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.ROAMING_NO;
import static android.os.BatteryStats.POWER_DATA_UNAVAILABLE;

import static com.google.common.truth.Truth.assertThat;
@@ -89,7 +92,8 @@ public class MobileRadioPowerCalculatorTest {

        // Note application network activity
        NetworkStats networkStats = new NetworkStats(10000, 1)
                .insertEntry("cellular", APP_UID, 0, 0, 1000, 100, 2000, 20, 100);
                .addEntry(new NetworkStats.Entry("cellular", APP_UID, 0, 0,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 1000, 100, 2000, 20, 100));
        mStatsRule.setNetworkStats(networkStats);

        ModemActivityInfo mai = new ModemActivityInfo(10000, 2000, 3000,
@@ -150,7 +154,8 @@ public class MobileRadioPowerCalculatorTest {

        // Note application network activity
        NetworkStats networkStats = new NetworkStats(10000, 1)
                .insertEntry("cellular", APP_UID, 0, 0, 1000, 100, 2000, 20, 100);
                .addEntry(new NetworkStats.Entry("cellular", APP_UID, 0, 0,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 1000, 100, 2000, 20, 100));
        mStatsRule.setNetworkStats(networkStats);

        ModemActivityInfo mai = new ModemActivityInfo(10000, 2000, 3000,
+7 −3
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package com.android.internal.os;


import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.ROAMING_NO;
import static android.os.BatteryStats.POWER_DATA_UNAVAILABLE;

import static com.google.common.truth.Truth.assertThat;
@@ -67,8 +69,10 @@ public class WifiPowerCalculatorTest {
                new int[]{NetworkCapabilities.TRANSPORT_WIFI});

        NetworkStats networkStats = new NetworkStats(10000, 1)
                .insertEntry("wifi", APP_UID, 0, 0, 1000, 100, 2000, 20, 100)
                .insertEntry("wifi", Process.WIFI_UID, 0, 0, 1111, 111, 2222, 22, 111);
                .addEntry(new NetworkStats.Entry("wifi", APP_UID, 0, 0, METERED_NO,
                        ROAMING_NO, DEFAULT_NETWORK_NO, 1000, 100, 2000, 20, 100))
                .addEntry(new NetworkStats.Entry("wifi", Process.WIFI_UID, 0, 0, METERED_NO,
                        ROAMING_NO, DEFAULT_NETWORK_NO, 1111, 111, 2222, 22, 111));
        mStatsRule.setNetworkStats(networkStats);

        return batteryStats;