History log of /frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
Revision Date Author Comments (<<< Hide modified files) (Show modified files >>>)
20b4d846edafeaacf0da2c810f511c236fdbd187 18-May-2018 Annie Meng <anniemeng@google.com> Fix NPE in restoring network policies

When backing up network policies, we write null for policies that are
null or are inferred. When restoring, this sends null policies to be set
which results in a NPE.

Bug: 79961560
Test: 1) atest NetworkPolicyManagerServiceTest
2) Manual: adb restore using backup set with null policies does not
crash

Change-Id: I450685e38acae0658ea984b86ca8b17ca27a71a6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
146bb336ecb36bfa27529828172ec7593d4fb2f5 18-Apr-2018 Jeff Sharkey <jsharkey@android.com> Never interact with "phone" while holding locks.

We've seen devices where heavy communication between "system_server"
and the "phone" process can exhuast Binder threads, especially when
calling while holding locks. To mitigate this, we now interact with
the "phone" process before acquiring any locks.

Update our internal data structures either when we see a connectivity
change, or when SubscriptionManager tells us something changed.

Fix bug in resolveSubscriptionPlan() that always picked the 0'th
SubscriptionPlan instead of looking for the currently active plan;
we now use the same logic for both NSS and NPMS.

Bug: 77908520, 77154412
Test: atest com.android.server.NetworkPolicyManagerServiceTest
Test: atest com.android.server.net.NetworkStatsServiceTest
Change-Id: I177d3fa6cddc78d745b35a9ede12451d458b892c
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
00072395e2fbc196cc781f3072b61f6ba11cf022 12-Apr-2018 Jeff Sharkey <jsharkey@android.com> Add tracing for network statistics and policy.

These trace points surround areas of heavy lifting to help us debug
code that might be taking longer than expected. (For example, slow
getting data from eBPF, slow recording into data structures, slow
writing to disk, or slow evaluating policy.)

Typical timings on a taimen:

performPollLocked: 25.5ms
snapshotUid: 4.4ms
snapshotXt: 1.4ms
[persisting]: 13.4ms
updateNotifications: 3.3ms

Bug: 77908520, 77808546, 77853238, 77154412
Test: builds, boots
Change-Id: I072bfecffee01eeec7e4cbad439bd1cdc166febc
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
050151e0529c16fc878b7273fbf2b951f8039fd7 13-Apr-2018 Jeff Sharkey <jsharkey@android.com> Don't process broadcasts until really ready.

Someone started setting mSystemReady too early, when we really want
to know SystemServer's real ready state. Add a variable to track
that, and don't process broadcasts until that's set.

Bug: 78020762
Test: builds, boots
Change-Id: I65213e46044c95fb0a8a4b09b9aa463bb15c1844
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a26e7559003b4f83553bf1d6c3b0d9558ec20005 12-Apr-2018 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Add stats logging to ServiceManager and NPMS" into pi-dev
49392d335af8559169238bb9cb4fc2911192adea 11-Apr-2018 Makoto Onuki <omakoto@google.com> Add stats logging to ServiceManager and NPMS

ServiceManager:
- Do an event log every N getService() calls with total time spent
in getService().
where N = 100 for core UIDs and 200 for other apps.

- Do an event log if getService() takes longer than N ms.
where N = 10 for core UIDs and 50 for other apps.

... with some extra throttling.

NPMS:
- Do the basic "stats logger" log for updateNetworkEnabledNL() and
isUidNetworkingBlocked()

This CL also enhances StatsLogegr so it now can show the slowest call
and the max # of calls per-second.

Bug: 77853238
Test: Manual test:
- Insert a SIM card
- Set data limit
- toggle airplane mode
- toggle wifi
- toggle mobile data

Then
- "dumpsys netpolicy" and "dumpsys activity processes" and check the stats
- also check "adb logcat -b all | grep ' service_manager'"

Change-Id: I5789541063f95d0eac501189816c8604a4571ba0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0a5570d49df7d438b618616e439192f3c216cfe8 10-Apr-2018 Jeff Sharkey <jsharkey@android.com> APIs to disable data usage notifications.

Certain carriers have requested the ability to disable the warning,
limit, and rapid data usage notifications shown by the OS, so give
them that ability on a per-subId basis. (The notifications are still
enabled by default.)

Unhide NOT_CONGESTED to match setSubscriptionOverrideCongested().

Fix bug in getPrimarySubscriptionPlanLocked() to find the first
currently active plan; the first non-recurring plan may be no longer
active. Fix bug in SubscriptionPlan which allowed invalid negative
or zero-length Period objects.

Bug: 77851691, 77506882, 77662747
Test: atest com.android.server.NetworkPolicyManagerServiceTest
Test: atest android.telephony.cts.SubscriptionManagerTest
Exempt-From-Owner-Approval: SDK deadline, responding to API feedback
Change-Id: Ib6e6dbadd5022937c3f0661ab4b66aa617f0b5af
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
ed6d2ca369a03fd7a357405eb36d6182e187973b 04-Apr-2018 Remi NGUYEN VAN <reminv@google.com> Clamp multipath quota to 0 if roaming.

Also adding tests for getSubscriptionOppportunisticQuota.

Bug: b/72631572
Change-Id: Iafc1ae1e0c0d58c027b5a42fa04b841a2739cff9
Fixes: b/72631572
Test: atest com.android.server.NetworkPolicyManagerServiceTest
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5e0d36041172a6393e8028184854d3c175f48051 10-Apr-2018 Jeff Sharkey <jsharkey@google.com> Merge "API council requested tweaks to SubscriptionPlan." into pi-dev
70df72232148340a5f3ea85f0892272901939f49 09-Apr-2018 TreeHugger Robot <treehugger-gerrit@google.com> Merge "More knobs for connectivity experiments." into pi-dev
3f1bf5fbd2339419395c8207899ef298ea974f10 05-Apr-2018 Makoto Onuki <omakoto@google.com> Don't call into phone process with lock held.

Bug: 77456094
Fixes: 77456094

Test: Manual test:
- Insert SIM card to device
- Set break point to setNetworkTemplateEnabledInner
- Change connectivity and make sure setNetworkTemplateEnabledInner()
is called with right arguments.

Change-Id: I6ff3c5639b48cb95419b431a85c498e214ee8e0d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
36b414bc2de065ead88a384a8e877788a5db32f1 30-Mar-2018 Jeff Sharkey <jsharkey@android.com> More knobs for connectivity experiments.

Keep the same default values in place for calculating opportunistic
quotas, but now allow experiments to override these values.

Tests to verify behavior of defaults.

Bug: 72353440
Test: atest com.android.server.NetworkPolicyManagerServiceTest
Change-Id: I38c225fd141b2e94085ca4cce17ecd51fee3f3aa
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0fc6d03b628c8cbe80e3c2c14aaf8c6944b32d1e 31-Mar-2018 Jeff Sharkey <jsharkey@android.com> API council requested tweaks to SubscriptionPlan.

Return new shiny Range<> object instead of Pair<>. Tell developers
what permission to check on refresh broadcast. Describe what
exceptions might be thrown, and that an empty list is okay.

Allow creation of plans with richer Period object, instead of forcing
them into rigid week or day options.

Protect SubscriptionPlan broadcast.

Test: atest android.util.RecurrenceRuleTest
Test: atest com.android.server.NetworkPolicyManagerServiceTest
Bug: 74945820
Change-Id: I7e555798e0cfaa214ca93d9df627c6443fc5d986
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
93e11759d69e0d79ab1d2e57b072b075fc8cd614 02-Apr-2018 TreeHugger Robot <treehugger-gerrit@google.com> Merge changes from topic "planz" into pi-dev

* changes:
Tests for SubscriptionManager APIs.
Obtain NPMS through helper method.
8303baf4d0b1e41f6a4eba41a6c49f4e04157ff7 02-Apr-2018 Sudheer Shanka <sudheersai@google.com> Merge "Update uid state used for bucketizing data in fg vs bg states." into pi-dev
07daad2dbffa9ce81a5c66adf7f12715bce5516a 02-Apr-2018 Remi NGUYEN VAN <reminv@google.com> Merge "Calculate multipath quota excluding current day." into pi-dev
bed7b970e731ce7c5e91f1ec8e11a042ece72233 02-Apr-2018 Remi NGUYEN VAN <reminv@google.com> Calculate multipath quota excluding current day.

The current calculation includes data usage for the current day,
which causes the returned daily quota to change during the day.
This makes it difficult to use for callers that need a fixed
data limit for the day.

Current usage in MultipathPolicyTracker (the only caller) is
incorrect because of this, as it assumes that quota is not constantly
changing.

Test: Tests in go/ag/3803612 pass
Bug: b/72631572
Bug: b/72877610
Change-Id: I9b3edde2dee7c7479d428e2bf2b73297afed3e6b
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
dfa8f0dbcef180d3a9cdeb7311146cc1c0432962 02-Apr-2018 Remi NGUYEN VAN <reminv@google.com> Merge "Differentiate depleted and unset multipath quota" into pi-dev
003d3e61e6f6ed47e1d6301af302ce69557ba5fa 30-Mar-2018 Jeff Sharkey <jsharkey@android.com> Tests for SubscriptionManager APIs.

Ensure that new SubscriptionManager APIs behave as documented. Test
based on device claiming FEATURE_TELEPHONY, and require that we have
a valid getDefaultDataSubscriptionId() to run our tests against.

Test: atest android.telephony.cts.SubscriptionManagerTest
Bug: 77327990, 71816837
Change-Id: Ib90ceeacf546c9b36ecf69694442cb661a62d353
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5a89f94096186302e1defc2ecd85356ed80cf777 30-Mar-2018 Remi NGUYEN VAN <reminv@google.com> Differentiate depleted and unset multipath quota

NetworkPolicyManagerInternalImpl#getSubscriptionOpportunisticQuota
would return 0 both for unset and depleted quota, which cannot be
interpreted properly by MultipathPolicyTracker (the only caller),
to use a fallback policy.

Return OPPORTUNISTIC_QUOTA_UNKNOWN (-1) instead for unknown quota.

Test: verified with CTS tests in ag/3811166
Bug: b/72631572
Bug: b/72877610
Change-Id: Ic087f7bfa9f10cc376b5ccd68a4685bd5242c586
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d993dcfe13866d5e7bf4f957d64830bfe9b1c823 11-Feb-2018 Sudheer Shanka <sudheersai@google.com> Update uid state used for bucketizing data in fg vs bg states.

Currently we are using PROCESS_STATE_BOUND_FOREGROUND_SERVICE to
decide whether to allow network access for an uid or not but
PROCESS_STATE_TOP is used for bucketizing data in fg vs bg states.
It's possible that even though user restricts background data uasge
for an app, the bg data usage amount displayed to the user is > 0.
As this could be confusing, use PROCESS_STATE_BOUND_FOREGROUND_SERVICE
for bucketizing in fg vs bg states too.

Bug: 63907204
Test: atest com.android.server.NetworkPolicyManagerServiceTest
Test: manual
Change-Id: Ib506e421043fbe1052b6d0068ebf01d288faba21
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5723ccb7ff9c29f39c43ef16f61c109264d75134 13-Feb-2018 Sudheer Shanka <sudheersai@google.com> Save internal state to disk after updating whitelist revoked uids.

Fixes: 71817859
Test: manual
Change-Id: I555ec29ddf7821972556fda65faa3553a17244a1
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d37154efb5af243cb19ba4775b6c9041c96e3a48 27-Mar-2018 Jeff Sharkey <jsharkey@android.com> Fix NPE when UID doesn't exist.

This can happen when the usage was performed by a synthetic UID,
such as TrafficStats.UID_TETHERING or UID_REMOVED.

Test: none
Bug: 74195808
Change-Id: I2859a8f5f4a1611c48bb56386ad4b2335a87f611
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
09e0f5741dff75c00674f61eb5677908b7ea028b 15-Mar-2018 Sundeep Ghuman <sghuman@google.com> Add fake plan with data overuse.

Bug: 70950124
Test: 1. adb shell setprop fw.fake_plan month_over
2. Visit data usage page

Change-Id: I44b474c8a668ca15eca733878057ab0931c349b1
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
119be9a5fc4033eba570ec94b94862401ee84570 19-Feb-2018 Amith Yamasani <yamasani@google.com> Improve reporting of bucketing reason

Keep track of main and sub reason for bucket change

Bug: 73178753
Test: atest AppIdleHistoryTests
Change-Id: I4936281ac06046bb5ffed9f3306efa24c7fd47ab
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e0c2995d0f16246336d4385d5ebe631a60e12e86 21-Feb-2018 Jeff Sharkey <jsharkey@android.com> Name specific app for rapid data usage.

When a single app is responsible for more than half of the data usage
that caused us to trigger a "rapid usage" alert, name that app in the
notification. Tests to verify.

Move NPMS->NSS direct calls to "Internal" pattern, following
best-practices to avoid unnecessary AIDL exposure.

Remove 3G/4G split mobile plan support, which has been deprecated for
years and was never supported in a shipping product.

Move MultipathPolicyTracker in tree to reflect its package name.

Test: bit FrameworksNetTests:*
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 69263587, 64221505, 73431080, 72746951
Exempt-From-Owner-Approval: approved in previous PS
Change-Id: I3e4ec1ae2222d51b232f76f32faca93d4f8cd272
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9911a28adb3b1196fe912ff5b65b8d5cbc7321ec 15-Feb-2018 Jeff Sharkey <jsharkey@android.com> Better handling of NTP-based clocks.

Now that we have a nice Clock abstraction, we can use it to represent
a clock backed by an NTP fix. (This makes testing logic much easier
to write.)

We now rely completely on NetworkTimeUpdateService to keep our NTP
fix up to date, instead of trying to refresh in the middle of
critical paths which could trigger random ANRs.

Add internal FallbackClock to make it easier to handle missing NTP
fixes. Add internal SimpleClock to let implementers focus on single
millis() method.

Test: bit FrameworksNetTests:com.android.server.net.NetworkStatsServiceTest
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 69714690, 72320957
Change-Id: Ic32cdcbe093d08b73b0e4b23d6910b23ea8e1968
Exempt-From-Owner-Approval: approved in previous PS
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a7f504662f428422cb1b0542814527aef6abf763 14-Feb-2018 Jeff Sharkey <jsharkey@android.com> Let debug builds set SubscriptionPlan owners.

Examine system property to decide if a test app should be granted
the ability to manage SubscriptionPlan details for a specific subId.

This doesn't give the testing app any access to existing plans
defined by other carrier apps, it only allows the testing app to
provide plans for testing purposes. Purposefully ignored on
end-user builds.

Test: builds, boots
Bug: 73173096
Change-Id: I926ec0f88b7c2c282a1ec4410eaab92e1e1090dd
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a36dc621ef0b86a0bd46f1d4f5a719a466ba1800 06-Feb-2018 Andreas Gampe <agampe@google.com> Frameworks: Annotate trivial @GuardedBy in services/core

Add @GuardedBy for simple functions that require a single lock
and are named XYZLocked.

Derived by errorprone.

Bug: 73000847
Test: m
Test: m javac-check-framework RUN_ERROR_PRONE=true
Change-Id: I6993325b11c71a4ec27c21935fb54a954d95455f
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f46dc156cfcb31da8714f29338614c2cd4fc61e7 07-Feb-2018 Jeff Sharkey <jsharkey@android.com> Let's not divide by zero.

Test: sure
Bug: 73060623
Change-Id: Ic9d60bb99e43a05c4131f642563d792de665e592
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
bfb43ea9e684e62644cecf7f037c4087bfaa1e0f 03-Feb-2018 Jeff Sharkey <jsharkey@android.com> Polished rapid data usage alerting.

Switch to reading limit information from NetworkPolicy, which is
typically populated from SubscriptionPlan. This lets users have
direct control over the limits we're using to trigger rapid usage
alerts, and makes the feature work without requiring that the carrier
wire up SubscriptionPlan information.

Let the user "snooze" the rapid usage alerting for a day at a time,
so we're less annoying to them. Send the snooze broadcasts as
foreground, so that we don't re-post notifications while working
through a long background broadcast queue.

Fix notifications to use the "ALERTS" channel, since these alerts
really are higher priority than simple "STATUS" updates; this also
gives us HUN behavior when in full-screen apps.

Update both service and unit tests to work directly with
NotificationManager, instead of the raw AIDL.

Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 72444638, 72436702
Change-Id: I8d9138522a7779cc68eb9fa4777b50facb6567b7
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e17b445b6c813f6f9bc93a5e3811128a197ef50b 10-Jan-2018 Dianne Hackborn <hackbod@google.com> Reduce pss collection amount, improve logging.

Tuned rates that we collect PSS, to reduce how much we do
that heavy operation. Added a new way to determine
whether a process has changed to a state for the
"first" time -- now this is when it has gone to that
state for the first time since it was in a lower state.
This will reduce the amount of time we consider a
process to be first to only when it has previously
gone into a higher state than it had before.

Keep track of more fine-grained information about why we
collect a PSS sample (not just internal, but for a single
process, all processes because of a mem state change, all
processes because of a poll).

Started collecting RSS in various places, so we can start
looking at that w.r.t. PSS and see about transitioning to
it is a new primary metric.

Added logging for many of the places where the system
writes its configuration files, so we can more easily
see any bad behavior going on in those areas.

Added some currently disabled code to read smaps directly
instead of using fgets(). Probably won't help, but want
tot test.

Bug: 70859548
Test: atest CtsAppTestCases
Change-Id: I400dba0f3ae9c024df51c946cfa592561028b598
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
04d61ae6c3ea1ddba22a8557f2fa372cd13b26ef 17-Jan-2018 Sudheer Shanka <sudheersai@google.com> Add DPM API to block apps from using metered data.

Bug: 63700027
Test: cts-tradefed run singleCommand cts-dev -m CtsDevicePolicyManagerTestCases -t \
com.android.cts.devicepolicy.MixedDeviceOwnerTest#testSetMeteredDataDisabled
Test: cts-tradefed run singleCommand cts-dev -m CtsDevicePolicyManagerTestCases -t \
com.android.cts.devicepolicy.MixedProfileOwnerTest#testSetMeteredDataDisabled
Test: atest com.android.server.devicepolicy.DevicePolicyManagerTest#testSetGetMeteredDataDisabled
Change-Id: I147399d316f375c68b415dc6ede837c53cd1aad0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
c53c47fa3a2fa0650ffe6c3f7eba8008004f6aa4 16-Jan-2018 Sudheer Shanka <sudheersai@google.com> Load admin data in DPMS asynchronously during boot.

Bug: 71902030
Bug: 71710099
Test: atest services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
Test: Ran boot tests - go/run-boottest
Test: manual
Change-Id: I34970c6f41877c7e3ece4843d47831374d455067
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7f0087be4804a9f8fb1f5fb68036015918c3b37f 19-Jan-2018 Lorenzo Colitti <lorenzo@google.com> Merge changes Id8776d14,Ifa291c62,I5ea9d200 am: c48f4cb7f1 am: b36b423230
am: cfbf873dd1

Change-Id: I12108d96e29bfe71406106c2af950a6d6a9f8351
d3e4a1e250194bbf59b1e04b60711f97001f61f8 18-Jan-2018 Lorenzo Colitti <lorenzo@google.com> Track and persist in stats whether traffic is on the default network.

This change adds the defaultNetwork to the ident based on current
system networking state, and persists that value to flash.

Bug: 35142602
Test: runtest frameworks-net
Test: downloaded files using default/non-default networks while watching dumpsys netstats
Change-Id: Id8776d149b2977548a9eb455ad4926af55e25aba
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2e4714575ff7aac65a0e107cbf2dd03fa3affe95 19-Jan-2018 Jeff Sharkey <jsharkey@android.com> Alert user on rapid/heavy data usage.

Now that we have accurate information about a user's carrier data
plan, we can alert them if the current usage patterns would end up
with a nasty surprise towards the end of the current billing cycle.

For example, a single abusive app could use 90% of the user's budget
within the first few days of a billing cycle, leaving the user to
limp along for the remainder of the month.

The simple algorithm here extrapolates to see if the average usage
over the last 4 days would be more than 150% of the data limit for
the full billing cycle. This period is short enough to catch rapid
recent usage, but long enough to smooth over short-term habit
changes, such as a weekend getaway. This was chosen after
backtesting the proposed algorithm against real-world data usage
from a handful of internal users.

Fix NPMS unit tests, and write new ones, but leave the existing
@Ignored annotation intact for now.

Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 64133169
Change-Id: I0d394b133257e8569a9aa2631b57638839d870ce
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9252b34065809731ea2f6d3ffad91f678f809c93 18-Jan-2018 Jeff Sharkey <jsharkey@android.com> Use data plans for better job scheduling.

Now that we have data plan information from the carrier, we can start
using it to influence when we schedule jobs. As a first pass
algorithm:

-- If the network is congested, and a job is less than 50% through
its runnable window, then we'll defer it for awhile.
-- If the network has a surplus of data, we'll consider using some
of it to improve the user experience by running prefetching jobs.

Provider APIs for carrier apps to override their connections to be
temporarily marked as either "unmetered" or "congested", along with
automatic timeouts if desired.

Flag for developers to indicate which jobs will have a material
positive impact on end users. (We don't want to promote jobs that
are simply doing logs upload; for example.) Glue code to quickly
return targetSdk of a specific package.

More tweaking to the exact algorithms will come in future CLs.

Test: bit FrameworksServicesTests:com.android.server.job.
Bug: 64133169
Change-Id: Iabb9f90a7a65958ad648b091edec378fc3bf785a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4df1c3dac7f9564a0f0a5d4621dd15e0aef41288 11-Jan-2018 Jeff Sharkey <jsharkey@google.com> Merge "Define broadcasts for refreshing data plans."
e92ed6fbb3d3c0761da6faced69f968495c8fa09 11-Jan-2018 Jeff Sharkey <jsharkey@android.com> Define broadcasts for refreshing data plans.

We don't hand the Intent out to the Settings app, since we need to
carefully set whitelisting to allow the carrier app to wake up and
respond if it's dozing.

Define new "changed" broadcast that we'll send out whenever plans
are changed internally.

Test: builds, boots
Bug: 64133169
Change-Id: Iacdd06d8e273fd52c8fc83edab13c9c8453b28eb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
10fc4fdd133c89347c38155475a3863bd12b94c4 20-Dec-2017 Dianne Hackborn <hackbod@google.com> Swap "foreground service" and "bound foreground service" proc states.

It has become apparent that apps running foreground services is
a much more important (and less common) state than all the various
ways apps run in the foreground because of the system being bound
to them or whatever else. So push this up about the bound foreground
service state, right after the explicit TOP state. Then we can
keep these together as "the app is explicitly running in the
foreground for some reason", etc.

Bug: 70808931
Test: CtsAppTestCases
Change-Id: Icfc1905b8e3eb60ddf26118c61ec1ef095659436
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
717f52f2dd8920d387c4d5143c7646787cec13bf 05-Jan-2018 Jeff Sharkey <jsharkey@android.com> Intent to manage subscription plans.

Define an API for carrier apps to provide a deep-link into their app
for the user to see more details about their billing relationship,
such as upgrading plans.

The createManageSubscriptionIntent() method is ready for OS
components to use when deciding if they should show a "MANAGE"
button in their UI, and the returned Intent is fully constructed
and ready to roll.

Test: builds, boots
Bug: 64133169
Change-Id: I9b2775b7cba5313f517996870135eb4682082873
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
352dc57186ff796dbb7a095c609d1a0bae5d3fca 23-Sep-2017 Sudheer Shanka <sudheersai@google.com> Track some event history and include it in netpolicy dump.

Bug: 66921847
Test: manual
Change-Id: I0c473790f83076def807308fe44db9cb9365769e
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
84cd7b7a9e5ad6a604c075bc620f6bd9ab6b1486 07-Nov-2017 Amith Yamasani <yamasani@google.com> Allow standby timeouts to occur after usage

And inform listeners when the bucket changes, not just when
going in and out of RARE bucket.
Avoid redundant callbacks when informing listeners.

Bug: 63527785
Test: runtest -x
frameworks/base/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java
Change-Id: Icd98d59f597147fbf8ea4bf44edf4b3b3d5c8e14
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2eccd0262827259d15ceaa0fca975d3243c3a699 01-Nov-2017 Makoto Onuki <omakoto@google.com> Make the battery saver knobs hidden public,

so other components (system UI, settings, etc) can use them.

Bug: 68769804
Test: Build & presubmit
Change-Id: I30fe78e49d2187bee7c7aeba735cd9c26fc332a5
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5435ff295826ca56b3da4480709af1a70243feb7 04-Oct-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update NPMS to inform NMS of the changes in fw rules synchronously." into oc-mr1-dev am: 7a349d7288
am: 99ae0aed0d

Change-Id: If264a7fc7e0300961d8f76c37c74ef2f4ae51088
addebccc56c3c21385a219c9d4e7450cfdb3c3bb 03-Oct-2017 Sudheer Shanka <sudheersai@google.com> Update NPMS to inform NMS of the changes in fw rules synchronously.

Currently, NPMS informs NMS synchronously at some events and
asynchronously at some others. If these sync and async calls get
interleaved, then NMS will end up in an inconsistent state.

In N-MR1, NPMS was updated to inform NMS asynchronously to avoid some
lock contentions during screen unlock. This shouldn't be a problem any more
since NPMS no longer updates its internal state on ui thread and also
netd calls have been optimized from ~50ms to ~7ms since then.

Bug: 66015813
Test: cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \
com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: I24ccb5819430f19014630e7b90cba1f0b993e430
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
22efe78a9048e941643422e157b4b7cd3bfcd9ff 15-Sep-2017 Jeff Sharkey <jsharkey@google.com> Merge "Update network rules on meteredness changes." into oc-mr1-dev am: d50ef281ba
am: 45af8e9ac5

Change-Id: I3857e5133cd452cdad439449c59fe4a66ddc5ac2
b43a2921c6164641c8585ec019dbb90e665a13db 14-Sep-2017 Jeff Sharkey <jsharkey@android.com> Update network rules on meteredness changes.

The CONFIGURED_NETWORKS_CHANGED_ACTION broadcast can be dispatched
before WifiStateMachine has a chance to update NetworkCapabilities,
resulting in a race condition. To fix this, update network rules
whenever we actually notice a meteredness change take place.

Test: builds, boots
Bug: 64274313
Change-Id: I490eaa789fc0754e4c1c413b6606378ef6f90662
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9b1570cc07cd73a37a472bc2a80372092be58f06 02-Sep-2017 Jeff Sharkey <jsharkey@android.com> Merge "Don't over-acquire NPMS locks." into oc-mr1-dev am: 4576171c33
am: ad8690266e

Change-Id: I4a975059ceb64dd6ce143645c00a314a2041ad2c
4635f10b789810d2726d5936f921dcec557078da 01-Sep-2017 Jeff Sharkey <jsharkey@android.com> Don't over-acquire NPMS locks.

We only need to hold mNetworkPoliciesSecondLock when working with
subscription plans; before this CL we could end up acquiring the two
NPMS locks out of order, resulting in a deadlock.

Also annotate objects in NSS that require mStatsLock to be held.

Test: builds, boots
Bug: 65268076
Change-Id: I06497564424316ef895dc8dceba72ae784781dc3
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d9d2b3e9eccaea0eb8369228e97b5b0316d70b22 31-Aug-2017 Jeff Sharkey <jsharkey@google.com> Merge "Merge "Fix meteredness bugs in updateNetworkRulesNL()." into oc-mr1-dev am: 03a5cb51c8" into oc-mr1-dev-plus-aosp
am: f16c85de94

Change-Id: I0db230a2dcd633a782a7b5c9e341c84088970cfd
64c96ecd1bddb34d345f10ff74073e89010d60a6 31-Aug-2017 Jeff Sharkey <jsharkey@android.com> Fix meteredness bugs in updateNetworkRulesNL().

The logic in that method appears to have become broken at some
point; refactor it to apply all known network policies, and then
perform one last pass to handle any metered networks that didn't
have policies; typically Wi-Fi networks.

Test: cts-tradefed run commandAndExit cts-dev -m CtsHostsideNetworkTests
Bug: 64274313
Change-Id: Id9cbcc6fdfe3793e058f52505c9edd250bed7089
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
45affe7edb7f6c1c1b38ae73d320bcb8d4c827bf 17-Aug-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update standby firewall rules when an app came out of standby mode." into oc-mr1-dev am: f523c630e4
am: 2c6b1400ea

Change-Id: I93d9e01a58c3bafb0b38d7e6655d806681e048f6
9e77d23fd3e3c9324d01d253b4e7289e77f3a729 14-Aug-2017 Sudheer Shanka <sudheersai@google.com> Update standby firewall rules when an app came out of standby mode.

Fixes: 64690559
Test: cts-tradefed run singleCommand cts-dev --module CtsHostsideNetworkTests -t \
com.android.cts.net.HostsideRestrictBackgroundNetworkTests
Test: bit FrameworksServicesTests:com.android.server.net.ConnOnActivityStartTest

Change-Id: If437047a3b9e37ca7c587460d39f93ce4d4febcd
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8518991995204f1ab64a6f86a5f4e5a2eb57bbea 11-Aug-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update ConnOnActivityStartTest to be hermetic." into oc-mr1-dev am: 06668b5f73
am: d732136ed1

Change-Id: Iff9111b3d1e3af576de8327e7fe0fdddf7bcadb1
6ebc3c8da14207b040cbd043d37e609d46a2572e 11-Aug-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update NPMS.setRestrictBackgroundUL to notify listeners."
99f76da6f74c26da5cd6436cc9ff2a45b639f739 11-Aug-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update network rules only for the added/removed appId in the whitelist."
b8f2316fb457861ca6cb31575eaa2e985358dd8b 04-Aug-2017 Sudheer Shanka <sudheersai@google.com> Update ConnOnActivityStartTest to be hermetic.

Bug: 38432755
Test: runtest -x services/tests/servicestests/src/com/android/server/net/ConnOnActivityStartTest.java
Change-Id: Ie847ec0a202021a2b2cf16bb2d720650c9ee847d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f34f3ec1c53188fe48f1adb500c5c8d8bdc3de46 03-Aug-2017 Sudheer Shanka <sudheersai@google.com> Update network rules only for the added/removed appId in the whitelist.

Bug: 30568426
Test: bit CtsHostsideNetworkTests:com.android.cts.net.HostsideRestrictBackgroundNetworkTests
Change-Id: I2dccb7419b7398c0db4e6fba0d28550a0708b8e6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2aa79aa73d9653331d7bb68774e15092626a375b 01-Aug-2017 Jeff Sharkey <jsharkey@google.com> Merge "Update inferred policies from SubscriptionPlan." into oc-mr1-dev
0f2910c3dc20bb9667f9592a3af18a474ad319c3 31-Jul-2017 Jeff Sharkey <jsharkey@android.com> Update inferred policies from SubscriptionPlan.

When creating or updating inferred network policies, consult any
SubscriptionPlan information first, then fallback to CarrierService
values, before finally falling back to system defaults.

Dump known SubscriptionPlan values in dumpsys output for debugging.

Fix bug where updateNetworkRulesNL() tried calculating cycles when
non existed.

Test: builds, boots, subscription plans update
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 63925474, 64124815
Change-Id: I12257cada14f72890d5346e366643aac88de278e
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
543339fe6b01aaaf112b6b093bc34a7a7aa79a7e 29-Jul-2017 Sudheer Shanka <sudheersai@google.com> Update NPMS.setRestrictBackgroundUL to notify listeners.

- Move the notifying listeners part to setRestrictBackgroundUL so
that we can avoid notifying if the restrict background update fails.
- Add tracing code for setRestrictBackgroundUL.
- Remove any existing restrict_background_changed messages in the queue
before posting a new one so that listeners will be notified only for
the latest update.

Fixes: 36328002
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Change-Id: I83c6270aa9f2d38cd418d2d88bfe59394b111692
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a20bf80aa3b5b504ebf68577ed7d8135c7753e16 30-Jul-2017 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Add more variations of subscription plan to the fake subscription plan list." into oc-mr1-dev
b7479988d248add39ba6ce1fe71dea6f810fd54f 29-Jul-2017 Jeff Sharkey <jsharkey@android.com> Migrate plans to @SystemApi, evolve permissions.

We're not yet ready to commit to SubscriptionPlan as public API, so
relax to be @SystemApi instead. Add a new MANAGE_SUBSCRIPTION_PLANS
permission that we require apps to hold, unless they've been
delegated access via a trusted CarrierService.

Since several apps have the ability to provide plans for a single
subId, we now remember the "owner" who set the current plan
information, and we refuse to leak plan information beyond the app
that originally set it.

Relax permissions check to not require READ_PHONE_STATE, since we're
only returning data that an app provided to us earlier. Also fix
NPE when SubscriptionInfo is missing.

Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Bug: 63997177, 63928277, 64156138, 63903381
Change-Id: If503378ef406dcaec438c9b41e837e0a821a3ef4
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4701beb984541281b7d87bfb4ad9b9faba406d47 27-Jul-2017 Rajeev Kumar <rajekumar@google.com> Add more variations of subscription plan to the fake subscription plan list.

Bug: 62349208
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Change-Id: I343ba1dc43d5319963fbac9455cc7b8c9c769ff0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
17bebd2fd7d3af0bc648172da6ae36f8b55ad7af 20-Jul-2017 Jeff Sharkey <jsharkey@android.com> Expose SubscriptionPlan API.

Clean up SubscriptionPlan, add docs, and expose as public API.

Deprecate older constants in CarrierConfigManager, and point users
towards new API. (The goal of CarrierConfigManager is to express
key/value pairs that are identical for all users on a particular
carrier, not to configure details that vary between users.)

Since a CarrierService may itself not have access to the plan
details, allow them to "delegate" the capability to configure plans
to another package name.

Factor out new RecurrenceRule class which is used by both
NetworkPolicy and SubscriptionPlan internally. Polish it to support
arbitrary periods, and continue to test it through NPMST.

Bug: 63391323
Test: bit FrameworksCoreTests:android.util.RecurrenceRuleTest
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Exempt-From-Owner-Approval: Bug 63673347
Change-Id: I93fae4a43e58e9e4c32f8bf345a257dcd607f088
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
94bcdbca224188c067c651e034e187def082e8dd 17-Jul-2017 Narayan Kamath <narayan@google.com> NetworkPolicyManagerService: Allow data saver to be on by default.

This value is read from the global settings provider, and can be
configured via an overlay.

Bug: 63112872
Test: NetworkPolicyManagerServiceTest

Change-Id: If55ea08df1dd8440189f9b46a5606118de99c930
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
53313d7e8153982f842910118371e1dc061248cd 14-Jul-2017 Jeff Sharkey <jsharkey@android.com> Iterate NetworkPolicy towards SubscriptionPlan.

Add new SubscriptionPlan API to describe the various types of carrier
data plans. Internally the OS will only use the first plan for
driving policy, but it will blindly plumb through the details for
Settings to display any secondary plans.

As part of getting Settings ready to roll towards SubscriptionPlan,
reduce references to NetworkPolicy internal fields. All usage cycle
details are now obtained from an Iterator which converts to
SubscriptionPlan under the hood.

Replace all data usage cycle calculation with new SubscriptionPlan
implementation, and retrofit large suite of existing tests to
exercise and verify the new logic.

Offer a debugging property that can be used to return "fake" plan
examples for testing.

Bug: 63391323
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Exempt-From-Owner-Approval: Bug 63673347
Change-Id: I889c653980eeb7887abdfa4f5b6986f35855ee6d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
43d2a1700b6eb1d804924c6a1e5e0161a13a5348 12-Jul-2017 Jeff Sharkey <jsharkey@android.com> Move "metered" persistence to WifiConfiguration.

For a long time we've had a nasty tangled dependency between Wi-Fi
and NPMS, since they both persisted different details for configured
networks. As part of preparing for new carrier data plan APIs, move
the tracking of meteredness over to WifiConfiguration.

This also cleans up how meteredness is communicated through
NetworkAgents to rely completely on NET_CAPABILITY_NOT_METERED by
removing the metered flag on NetworkInfo, which has caused confusion
and staleness.

Migrates any existing user-configured metered networks over to
WifiConfiguration once the device finishes booting.

Remove support for NetworkQuotaInfo, since this information can no
longer be made available to apps. Frustratingly, some apps are
using it, so keep the object around returning stub values, and shame
them in the logs.

Bug: 63391323
Test: builds, boots, Wi-Fi policy is upgraded
Exempt-From-Owner-Approval: Bug 63673347
Change-Id: I64f865ddeb65cfcd330f8d2a847368abdf960a07
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3e99f654fbf56d3b7ef7130658b8ef83d2d0cdbd 06-Jul-2017 Dianne Hackborn <hackbod@google.com> Fix issue #34471029: Don't allow audio use from background apps.

This is becoming a common pattern (keeping track of which processes
are cached and not allowing them to do stuff in that state), so I
am turning this in to a general mechanism for monitoring this state
through the activity manager's IUidObserver. Now we can just have
AudoService implement its own IUidObserver to get this state and
update which uids it is blocking.

This required making some changes to uid change reports so that
the integer is now a bit mask instead of an enumerations, but that
is what it was already turning in to anyway. (This gets rid of
the crazy GONE_IDLE constant that we'd needed to add before because
it wasn't a bit mask).

Eventually the power manager should be changed to be told about
these changes to cached state instead of listening to every proc
state change, but we'll do that later, it is more disruption than
I want to take for now. However, while working on this, I noticed
that the power manager had regressed in the cached uids it would
actually block, because the activity manager was no longer telling
it about all uids that are idle. (I think this happened when I had
to change the default idle state of UidRecord to true.) So I am
adding a bit of new code to keep track of what idle state we last
reported to observers, to make sure we tell it about newly created
uids that are idle but have never actually become active.

Test: runtest -c com.android.server.am.ActivityManagerServiceTest frameworks-services

Change-Id: I7bfd46bacadd4cab2a69f40e6e52afb4e67b456a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
db9aaf0322044786a27d85aeb97c39b768f89a49 20-Jun-2017 Sudheer Shanka <sudheersai@google.com> Shorten the log in the netpolicy dump.

Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Change-Id: I105326e733f2498e68b0552b45fec1ed0e8e3179
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
bf216a963cf277fd007957e039e2f3debff3d64d 28-Apr-2017 Sudheer Shanka <sudheersai@google.com> Merge "Consider allow-in-power-save-except-idle whitelist while updating rules." into oc-dev am: 5ee9a1dfa3
am: 4074687cd2

Change-Id: I86bcdb4e905586f9fc19c365c82b4103b316d87a
5ee9a1dfa389f5f078b530b13dac01fe52b8a4b2 28-Apr-2017 Sudheer Shanka <sudheersai@google.com> Merge "Consider allow-in-power-save-except-idle whitelist while updating rules." into oc-dev
d6247427ac3137555055fda2bed511db4941ed30 27-Apr-2017 Julia Reynolds <juliacr@google.com> Merge "Api updates" into oc-dev am: f84467e19f
am: 7ee513c27b

Change-Id: Iafbf2f2ca92b288de7bded40772cd15eca2012ee
54a92fdd4fc18ecbadf784879dd000c03d29dd77 26-Apr-2017 Sudheer Shanka <sudheersai@google.com> Consider allow-in-power-save-except-idle whitelist while updating rules.

Fixes: 37670935
Test: manual &&
runtest -c com.android.server.NetworkPolicyManagerServiceTest frameworks-services
cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \
com.android.cts.net.HostsideRestrictBackgroundNetworkTests
Change-Id: I3520ed24147b9b6dd9e7b19aa9a90dd8c0b371ad
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
bad42976009ffb61010136ed8055f9e0f6d2e416 25-Apr-2017 Julia Reynolds <juliacr@google.com> Api updates

- Better documentation on DEFAULT_CHANNEL_ID
- Renaming getChannel/setChannel/getTimeout/setTimeout
- Add documentation to getShortcutId
- @hide parcel constructors

Test: make, make cts
Bug: 37672218

Change-Id: I695b2620c51638a84930e9d1c5fbfd6d85699b55
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
758635c288845a202426778907cc401cee00b47f 21-Apr-2017 Julia Reynolds <juliacr@google.com> Merge "Remove long standing debug code" into oc-dev am: f74a736add
am: ce822b2fb7

Change-Id: Ib9702a224dae742c6a6f1418567e537d8e857e84
fea6f7b799225f6f0211d7114c779183adbf7b6b 19-Apr-2017 Julia Reynolds <juliacr@google.com> Remove long standing debug code

Because it stops real errors from bubbling up to the calling app.

Test: make, post notifications
Change-Id: I0e59c61b0a60302a5fd023b7ad909e84b97e5df5
Fixes: 37494547
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
911abd9bcf46b4d430d5df0410489ce31cc54d47 13-Apr-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update NPMS internal state when app idle parole state is changed." into oc-dev am: 67e802dd5e
am: 012ddf15c7

Change-Id: Ic491c87920519ac0fc04353f62f02a45dc1ba5fc
3af0294c36e432914f07dbc6f209ac75321ca0cf 12-Apr-2017 Sudheer Shanka <sudheersai@google.com> Update NPMS internal state when app idle parole state is changed.

Fixes: 37250313
Test: 1. cts-tradefed run singleCommand cts-dev --module CtsHostsideNetworkTests -t \
com.android.cts.net.HostsideRestrictBackgroundNetworkTests
2. runtest -c com.android.server.NetworkPolicyManagerServiceTest frameworks-services
Change-Id: I5fcac397116a07fe9015b7dabf5bca57b0b23176
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9cd99a08dcbc1bb3a2cb1a8ab4d685508d7f9f72 10-Apr-2017 Sudheer Shanka <sudheersai@google.com> Merge "NetworkPolicyManagerService: fix deadlock" into oc-dev am: 73edcd7328
am: 1bbe822a5c

Change-Id: I36464a2ce5370579b1b051f2eaa0a5c23de51fc7
73edcd7328db1a1466e786baaae9657b7e6d5083 10-Apr-2017 Sudheer Shanka <sudheersai@google.com> Merge "NetworkPolicyManagerService: fix deadlock" into oc-dev
446c9c9172684c4653ef33a8eb8b769ba0331807 10-Apr-2017 Hugo Benichi <hugobenichi@google.com> NetworkPolicyManagerService: fix deadlock

Callers of addNetworkPolicy() were not taking locks in the correct order
inside NetworkPolicyManagerService:
- addNetworkPolicy() is an internal method that calls
setNetworkPolicies which takes both mUidRulesFirstLock and
mNetworkPoliciesSecondLock in order.
- both callers of addNetworkPolicy, mWifiStateReceiver and
mConnReceiver via ensureActiveMobilePolicy, were taking
mNetworkPoliciesSecondLock before calling addNetworkPolicy.
- this causes the order of locking to be reversed, which can cause a
deadlock when another concurrent codepath in
NetworkPolicyManagerService tries to take both locks in the correct
order.

This patch fixes this issue by wrapping both problematic codepaths into
addNetworkPolicy() with a lock on mUidRulesFirstLock.

Test: build, flashed, NetworkPolicyManagerServiceTest passes
Bug: 36972283
Change-Id: If7888c11aef8b628e1b013224075c4c75eae0022
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2966c187978a23af7899004c5d62b86d0d77e06f 28-Mar-2017 Hugo Benichi <hugobenichi@google.com> NetworkPolicyManagerService: use hasRule()

Change-Id: I74c62967b9565ec92f84ed19af14c46d4df14808
Test: no functional change.
Bug: 32069544
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5d2f2b51f99d637e0451675ce5e1331c8b5ce69f 07-Apr-2017 Hugo Benichi <hugobenichi@google.com> NetworkPolicyManagerService: turn off rule debugging

Left over from Change-Id Ic75d4f7a8853e6be20e51262c4b59805ec35093a.

Test: no functional changes
Bug: 37084734
Change-Id: Ifb10b4fe146bb6bea6260d8cd723a8e7f5b853a9
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
df9cf5450cec9bc8b92d25a66c1f348cdc4b3498 04-Apr-2017 Hugo Benichi <hugobenichi@google.com> Merge "Move back networking policy logic into NetworkPolicyManagerService" into oc-dev
ef3a28cb92039c198ed8e78a69d00bd9c8322dab 03-Apr-2017 Chris Wren <cwren@android.com> Merge "standardize network policy notification IDs" into oc-dev
fe9a53bc45fd0124a876dc0a49680aaf86641d3e 31-Mar-2017 Jeff Sharkey <jsharkey@android.com> Consistent dump() permission checking.

This change introduces new methods on DumpUtils that can check if the
caller has DUMP and/or PACKAGE_USAGE_STATS access. It then moves all
existing dump() methods to use these checks so that we emit
consistent error messages.

Test: cts-tradefed run commandAndExit cts-dev -m CtsSecurityTestCases -t android.security.cts.ServicePermissionsTest
Bug: 32806790
Change-Id: Iaff6b9506818ee082b1e169c89ebe1001b3bfeca
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
193ae6bc785170516a8c67174ddab2cc254c57a2 31-Mar-2017 Chris Wren <cwren@android.com> standardize network policy notification IDs

Bug: 36811253
Test: make
Change-Id: I8cb2c0a4be549f6e600b4d75b8113a2fd6d7cd64
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
938ab4fa3942398e942d98aa7b16dd87dfb7ff11 11-Feb-2017 Hugo Benichi <hugobenichi@google.com> Move back networking policy logic into NetworkPolicyManagerService

This patch removes from ConnectivityService the logic involved in
deciding if a uid has access to networking based on networking policies.

This logic is moved into NetworkPolicyManagerService which is the source
of truth with regards to the state of networking policie, both for
existing networks and uids.

Instead ConnectivityService directly queries NetworkPolicyManagerService
in a synchronous fashion for a specific uid or a (uid, network) pair.

This eliminates the need to keep a copy of the uid policy rules inside
ConnectivityService and ensures that ConnectivityService takes
networking decisions based on the correct state of networking policies,
and therefore eliminates certain data races in ConnectivityManager API
that applications are exposed to.

Test: $ runtest frameworks-net
$ runtest -x frameworks/base/services/tests/../NetworkPolicyManagerServiceTest.java
$ runtest -c com.android.server.net.ConnOnActivityStartTest frameworks-services
Bug: 32069544, 30919851
Change-Id: Ic75d4f7a8853e6be20e51262c4b59805ec35093a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
ed25ce6d598d7ed14f9b9859cdd2219bcc907f4c 30-Mar-2017 Sudheer Shanka <sudheersai@google.com> Fix flaky tests in NetworkPolicyManagerServiceTest.

Test: runtest -c com.android.server.NetworkPolicyManagerServiceTest frameworks-services
Change-Id: I0a6f0d11e3922bfeb2bd3784f23e10ae45e5e40c
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
6ce48e25e510aed187c8b2a84377e0676449767a 29-Mar-2017 Ammar Aijazi <aaijazi@google.com> NetworkPolicy gets updated due to CarrierConfigManager updates.

Bug: b/34385985
Test: Added unit tests to NetworkPolicyManagerServiceTest
Change-Id: I1d8249081c478e6484c8011d3eab73a53f105fac
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8e77733656c8d566b8fdbb259adb042e03be006a 28-Mar-2017 Makoto Onuki <omakoto@google.com> Don't do heavy work on UiThread.

Bug 34961340
Test: manual test
Test: adb shell am instrument -e class com.android.server.NetworkPolicyManagerServiceTest -w com.android.frameworks.servicestests
(except for flakey ones)

Change-Id: I9895b4e8f5f20b680051173995a8410f08a1f4d0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
29c82ab242d50000d22e593c27153751c8c6b44a 10-Mar-2017 jackqdyulei <jackqdyulei@google.com> Hook up data saver to battery saver.

1. Add globalBatterySaverEnabled in PowerSaveState, which stores
the real battery saver mode, not the one for specific service.
2. Add "mRestrictBackgroundChangedInBsm" and
"mRestrictBackgroundBeforeBsm" to restore the mRestrictBackground
if necessary.
3. If user toggles the data saver when battery saver is on, don't
restore the data saver when battery saver is off.

Bug: 34693888
Test: FrameworksServicesTests
Change-Id: Ic50c1cae3fb110a228e59e5d1fd04c164e4b7274
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4c66f8b20d860bb99bf7a49b9302fe8bafff8bd9 21-Mar-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update NPMS to notify AMS when network policy rules are updated."
e736185ee90e0f19a7092542cadab4cf561a5703 07-Mar-2017 Sudheer Shanka <sudheersai@google.com> Update NPMS to notify AMS when network policy rules are updated.

Bug: 27803922
Test: runtest -c com.android.server.am.ActivityManagerInternalTest frameworks-services
runtest -c com.android.server.NetworkPolicyManagerServiceTest frameworks-services

Change-Id: I357fd5c80b7e6d3e63df95397e328c52f233958b
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0e6e3bd1dc210d6e949046d62be80b01a5f10de6 20-Mar-2017 Makoto Onuki <omakoto@google.com> Fine-grained systrace for NetworkPolicyManagerService

Bug 34961340
Test: manual test with systrace.py

Change-Id: I6b0e53758d80db1ed9e522f82fff9ccc16301fb0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
c37cc6030146f7179d658cc1969a5353dad5fd97 11-Mar-2017 Sudheer Shanka <sudheersai@google.com> Merge "Update IUidObserver.onUidStateChanged to include procStateSeq."
8025580449081ddd21f449f8719bc87d1cc9fc7b 04-Mar-2017 Sudheer Shanka <sudheersai@google.com> Update IUidObserver.onUidStateChanged to include procStateSeq.

This procStateSeq is associated with the process state change of
an uid and will help in identifying if network policy rules have been
updated for a process state change.

Bug: 27803922
Test: runtest -c com.android.server.am.ActivityManagerServiceTest
frameworks-services
Change-Id: Iaab8d004f476b96f28d08c886c7b0a6b585b9fef
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
455e90add22835d0744fc0f5c2feb5fcaf03b28f 10-Feb-2017 jackqdyulei <jackqdyulei@google.com> Add BatterySaverPolicy for power save mode

The BatterySaverPolicy is designed to consolidate all battery saver
knobs into a central location. Usually it is consistent to
mLowPowerModeEnabled unless it gets different data for specific
service. By adding these knobs, we can effectively tune the battery
saver.

This cl sets up the framework for BatterySaverPolicy and updates
following service to get battery saver data from BatterySaverPolicy

1. GnssLocationProvider
2. VibratorService
3. WindowManagerService
4. BackupManagerService
5. SoundTriggerService
6. NetworkPolicyManagerService

Screen brightness will come in a following cl.

Bug: 34693888
Test: FrameworksServicesTests

Change-Id: I6b040e93391614b44d136a485faa4a332c396e51
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
af759c52ce01fe6b5144957e38da956af01a217b 15-Feb-2017 Geoffrey Pitsch <gpitsch@google.com> Channels for Frameworks notifications

Adapts all notifications used by system services to use channels.
Channels are initialized by SystemServer after the NotificationService
has started.

Test: runtest systemui-notification
Change-Id: I25c45293b786adb57787aeab4c2613c9d7c89dab
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
983552d03e5d81080a180121c3200c76550bd22d 01-Mar-2017 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Move some static methods from NPMS to NPM class."
12d2922a6e9c63ca890f8c9e44bb8f4a50024044 25-Feb-2017 Sudheer Shanka <sudheersai@google.com> Fix default value for mUidState.get.

In updateRulesForWhitelistedPowerSaveUL, we are using
mUidState.get(uid). So if the uid didn't already exist, it will
give 0 which represents PROCESS_STATE_PERSISTENT and this is
inconsistent with it's other usages.

Bug: 35766310
Test: verified that apps coming to foreground, while the battery saver is on,
have access to network.
Change-Id: I42a8455862e3a15e14825d9cf74e05a6f1dbe298
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e359c3db336e6a7735da06b1ae086832d36e0992 23-Feb-2017 Sudheer Shanka <sudheersai@google.com> Move some static methods from NPMS to NPM class.

Moved isProcStateAllowedWhileIdleOrPowerSaveMode and
isProcStateAllowedWhileOnRestrictBackground to
NetworkPolicyManager from NetworkPolicyManagerService
so that these can be accessed from other classes.

Bug: 27803922
Test: Builds without any errors.
Change-Id: I4736652d546cdc84de3cdea9b6524854e4773419
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
c9d9407e8ec10a56a4caa3c84122d1c6a1fedaab 22-Feb-2017 Sudheer Shanka <sudheersai@google.com> Revert "Ensure network connectivity on app start."

This reverts commit 27e693158398059d00ff20e60640cb105e255625.

Change-Id: I4c41ce7c1839d3b91931bc8a8f117f32bf6cd381
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
27e693158398059d00ff20e60640cb105e255625 23-Jan-2017 Sudheer Shanka <sudheersai@google.com> Ensure network connectivity on app start.

Bug: 27803922
Test: cts-tradefed run singleCommand cts-dev --module CtsHostsideNetworkTests
Change-Id: Ifd7a52924cd6bbc809c9a3f92d52fe2df09b7fe2
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
311b9fac00c9298479340244ffedf4825e0a7756 03-Dec-2016 Fyodor Kupolov <fkupolov@google.com> 2-phase initialization of NetworkPolicyManagerService

Split systemReady() into 2 callbacks:
1) networkScoreAndNetworkManagementServiceReady - called when required
services are ready. NetworkPolicyManagerService starts its initialization
on the worker thread
2) systemReady - wait for phase (1) to finish so that the service is fully
initialized

NetworkPolicyManagerService is now prepared in parallel with
ConnectivityService and NetworkStatsService

Test: device boots without errors
Test: HostsideRestrictBackgroundNetworkTests and NetworkPolicyManagerServiceTest pass
Bug: 32315581
Change-Id: Ic9755ed096900247fa0bbb43f396d8bcb97ae6db
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0d1fd8d09163566d2c7eb72037f63b6404ada642 12-Oct-2016 Amith Yamasani <yamasani@google.com> Secure MATCH_UNINSTALLED_PACKAGES across users

Introduce a new internal flag MATCH_ANY_USER for genuine uses
of searching through all apps on the device.

Some temporary accommodations for Launchers that reach across
to the work profile until we have a new LauncherApps API to do
that officially.

Bug: 31000380
Test: CTS tests added
Change-Id: I2e43dc49d6c2e11814a8f8d1eb07ef557f31af34
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e07641d4fbdd0528c18305213e861a6e1aff4a3b 10-Nov-2016 Dianne Hackborn <hackbod@google.com> Start implementing background restrictions for eph apps.

This implements the additional intended path for checking
allowed background operations, APP_START_MODE_DISABLED, which
doesn't allow an app to launch in the background at all.

Also change the semantics of delivering broadcasts to manifest
receivers to always restrict those, not changing based on
whether the app is currently idle. This is the desired intended
behavior for apps as they explicitly update to work with
bg check.

And now that we have ephemerality associated with the uid state
in the activity manager, we can propagate this through the
relevant callbacks in IUidObserver so things watching these
changes can immediately determine whether they should do their
more aggressive shut down work for the uid rather than having
to walk through all their state looking for package associated
with that uid and whether they should be shut down.

Also remove the "lenient" bg check mode, since that was
just an early experiment that we won't actually use.

Add a new "make-idle" activity manager command to immediately
put a uid into the idle state (if possible) to make it easier
to test.

Test: manually against an eph app
Change-Id: I43a138ff281f69a9251d3f29ab6e13f48cff8ad6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5614bf5a1ae4522dfc1a041f003cebc9b25c8b93 08-Nov-2016 Dianne Hackborn <hackbod@google.com> Move code for handling uid obs "cutoff" to activity manager

To do this, I had to fix the PROCESS_STATE_NONEXISTENT constant
to be the last value (instead of the special magical -1 value)
so it semantically matches the public importance constants. I
think this is better anyway.

Also this fixes a big problem in the implementation, where we
weren't keeping track of the last proc state per uid...! Duh.

Test: manually ran testUidImportanceListener
Change-Id: Ie3008f824446089840f896885e6033472abb065e
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
058f1e4468bd54d0ac39fdf81b4df7221b28721a 03-Nov-2016 Dianne Hackborn <hackbod@google.com> Implement system API for monitoring uid importance changes.

Changed the permission for registerUidObserver to be
PACKAGE_USAGE_STATS, which better matches the semantics of this
API and is accessible to system apps.

Test: Added testUidImportanceListener()

Change-Id: Ib47d02bd654fbe399a9c92550c89ecbd38f928dc
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4306608707ad24af2b9a7c5764412e429329eab3 05-Oct-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Added trace on Data Saver-related methods."
354736e196ff79962b3ddb52619a674044d773e2 23-Aug-2016 Dianne Hackborn <hackbod@google.com> New infrastructure to switch remaining commands to "cmd" calls.

This introduces a new feature of the IBinder command protocol
to allow the shell command implementation to call back into
its caller to ask it to open files in the calling context. This
is needed so that commands that have arguments specifying files
can open those files as the calling shell, not the system (or
whatever) process.

To test this all out, move the "am start" implementation over
to ActivityManagerShellCommand, in particular along with its
option to specify a file in which to write profiling data.

Test: Manual

Change-Id: I0c1e3857defefbd19a2ac29413aafbb34b1e48a3
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
29e72ea036a4ec9b68d05296a95d84a4ab7ad935 08-Sep-2016 Felipe Leme <felipeal@google.com> Added trace on Data Saver-related methods.

BUG: 31389946
Test: m -j32 cts && cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: I4a4299371a529121438db166fe03de8263812323
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
03e95e2f436278672e6b087e03239114ebe6b80c 09-Sep-2016 Felipe Leme <felipeal@google.com> NPMS: Cleaned up UID removal code.

When an UID is removed, NPMS needs to perform 2 tasks:

- Removed internal state associated with the UID.
- Call NMS to reset the iptables rules associated with the UID.

Currently, the UID removal logic is intermingled with code used used
during the UID state path, which has 3 drawbacks:

- Adds unnecessary comparisons.
- Makes the code more complicated.
- Is not handling all scenarios (like removing UID from fw_standby).

This CL simplifies how these 2 tasks are achieved.

Test: m -j32 cts && cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: I12e9a6777b0d7168751571889fd7bb2d13934745
Fixes: 31377000
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
b3fe65004127a2487da0195409cc0722ed712156 22-Sep-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Fixed mRestrictBackgroundWhitelistRevokedUids usage."
9918bd550be785474d038a58ae9fb6c7831be016 20-Sep-2016 Amith Yamasani <yamasani@google.com> Fix reporting of network connectivity when parole changes am: 0938f2fa98 am: 94a3a21bf7
am: 620d47560b

Change-Id: I45f10dfdfb27f99a6e25be8061763d5a8555c4e9
620d47560b05ea25f95129d6fc6e16b548a550bd 20-Sep-2016 Amith Yamasani <yamasani@google.com> Fix reporting of network connectivity when parole changes am: 0938f2fa98
am: 94a3a21bf7

Change-Id: I9dff65c4a4dabeff0dbc28ca0fb523dd3f2e0851
94a3a21bf7e5c29dad2d934667559f9e74f03630 20-Sep-2016 Amith Yamasani <yamasani@google.com> Fix reporting of network connectivity when parole changes
am: 0938f2fa98

Change-Id: I8c3326a4afeeb3cc8d22d846518a866ee3fad3e5
0938f2fa989414dcb42b3c654805a535be80ecdb 16-Sep-2016 Amith Yamasani <yamasani@google.com> Fix reporting of network connectivity when parole changes

When parole state changes, compute the uids that have been
impacted by the change of idle state and inform listeners.

Bug: 31399882
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleNonMetered_whenCharging
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleMetered_whenCharging
Change-Id: I5cc49464a2d359890b5c48033149e569a5b021ca
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
03f9029bed18338eaca0741eb95649cffb0f1874 09-Sep-2016 Felipe Leme <felipeal@google.com> Fixed mRestrictBackgroundWhitelistRevokedUids usage.

mRestrictBackgroundWhitelistRevokedUids used to be set on
removeRestrictBackgroundWhitelistedUidUL(), which has been refactored
into setUidPolicy().

Fixes: 28791717

Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

Change-Id: I097fddd236bf279890a8f466927fdc330360477f
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4eeadef9e7749617cf943346d91f9700c70c03de 15-Sep-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Listen to ACTION_UID_REMOVED only."
2ba7e2184c03762ddfe0e65fcadbdd6f9062f023 15-Sep-2016 Fan Zhang <zhfan@google.com> Parse -1 as WARNING_DISABLED in NetworkPolicyManager. am: da71ca08ef am: 96320f6aca
am: a4b723663a

Change-Id: I3a8e889ea92df42fa57835476906286b2e8fe359
a4b723663a1f6a18945c64366f9fd0e8a18fa45b 14-Sep-2016 Fan Zhang <zhfan@google.com> Parse -1 as WARNING_DISABLED in NetworkPolicyManager. am: da71ca08ef
am: 96320f6aca

Change-Id: I1c54431f6d669b33c52a1cf75de74638b01d6c1d
96320f6aca33927731a73412587bae1822e76572 14-Sep-2016 Fan Zhang <zhfan@google.com> Parse -1 as WARNING_DISABLED in NetworkPolicyManager.
am: da71ca08ef

Change-Id: I148f7edb93a29e5e8f0c26693b5b21a744c18797
da71ca08ef8f4aaaa5cbe25b49d166968e816e46 13-Sep-2016 Fan Zhang <zhfan@google.com> Parse -1 as WARNING_DISABLED in NetworkPolicyManager.

Bug: 26934313
Bug: 31437693
Test: Flashed phone with new config (-1) and verified data warning is
disabled out of box.
Change-Id: Iabdfb9d0625698d525d7537a93b936df505bd0ab
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
fdebbea401dc9d38b0cbea0fdfbd758e16805358 09-Sep-2016 Felipe Leme <felipeal@google.com> Listen to ACTION_UID_REMOVED only.

NMPS was listening to both ACTION_UID_REMOVED and
ACTION_PACKAGE_REMOVED, which not only was redundant but would also
cause problems when a package with shared UIDs was removed.

Fixes: 31375684

Change-Id: Iec006dbbec0e6298f1e568cbf81ed3672f61b7d2
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
dd53dbfcd0200b26135f56bd8651287bd26d778e 09-Sep-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Calls setUidFirewallRules() and enableFirewallChain() asynchronously."
bc853dd7f659f82da7b33393b2fa37772aab2155 08-Sep-2016 Felipe Leme <felipeal@google.com> Calls setUidFirewallRules() and enableFirewallChain() asynchronously.

The updateRulesForRestrictPowerUL() method is used to set the
power-related restrictions for all apps and it ends up by calling
NetworkManagerService twice (and each call spawns an iptables process,
which takes ~50ms to complete). This method is called on some critical
paths, like when the device leaves Doze Light mode.

This change makes these calls asynchronously, hence reducing the delay
on such critical paths in the O(100ms).

BUG: 31281543

Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t
com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: If720a7112c72a18193ea9614ae187b7ed9c741ec
(cherry picked from commit 84d3973d854aef6678624aa8773c5b0c12f24419)
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
1cedf00b09515c8027d99b1dc287a4b069183647 09-Sep-2016 Felipe Leme <felipeal@google.com> Move EventLogTags.writeDeviceIdle calls outside synchronized block. am: ea01439ef2 am: c2df6afdec
am: e892b57e02

Change-Id: I26248764113b52deb10a14a7989cb4538016ae95
e892b57e025cf5becf630e7b588df785753a745e 09-Sep-2016 Felipe Leme <felipeal@google.com> Move EventLogTags.writeDeviceIdle calls outside synchronized block. am: ea01439ef2
am: c2df6afdec

Change-Id: I70335305b9accb01eb09f002668bbb7dbc1eba9d
10b18a66b34ca7e4babe6449dd165a517d06f8f0 09-Sep-2016 Felipe Leme <felipeal@google.com> Calls setUidFirewallRules() and enableFirewallChain() asynchronously. am: 84d3973d85
am: f1963ae227

Change-Id: Ia99446070b7601fabea110a3116e91c4f33b4117
c2df6afdec609795cfe747ebcda167f1ef5b91d3 09-Sep-2016 Felipe Leme <felipeal@google.com> Move EventLogTags.writeDeviceIdle calls outside synchronized block.
am: ea01439ef2

Change-Id: I61753c52e3a4d8d2cc8f44921ee657fbcc572b6b
b0e4d9e97bcbf55690a3eff0e7b62dd48ec77645 09-Sep-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Move EventLogTags.writeDeviceIdle calls outside synchronized block." into nyc-mr1-dev
f1963ae227a7b80d5aa16c6e54fd9055307cfc6c 09-Sep-2016 Felipe Leme <felipeal@google.com> Calls setUidFirewallRules() and enableFirewallChain() asynchronously.
am: 84d3973d85

Change-Id: Iff72b7a770f6b3a7ff7167d8d0c56547e6d657dc
ea01439ef2c6eb278b1b92fcdedc9e3d78a323b2 06-Sep-2016 Felipe Leme <felipeal@google.com> Move EventLogTags.writeDeviceIdle calls outside synchronized block.

BUG: 31281543

Change-Id: I1cf5f80dda7ef0801a9fa9d0b5a5b207fc684cd5
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
27da814a1a7e9b239e54eeea448035474abf2de5 09-Sep-2016 Felipe Leme <felipeal@google.com> Don't update app idle rules on updateRulesForRestrictPowerUL(). am: 0970046adf am: fe8e64da79
am: b0d291f3a7

Change-Id: I5f375bbc98dcdaae6f4b67c6b76745e579b827ba
b0d291f3a72c52303b99997955ed613ee1db48aa 09-Sep-2016 Felipe Leme <felipeal@google.com> Don't update app idle rules on updateRulesForRestrictPowerUL(). am: 0970046adf
am: fe8e64da79

Change-Id: I3c553daf35ea540671bc782ef1165c2e356495af
fe8e64da798298ea485b5eda441295d1e8b96540 09-Sep-2016 Felipe Leme <felipeal@google.com> Don't update app idle rules on updateRulesForRestrictPowerUL().
am: 0970046adf

Change-Id: I59d5daec570117f53e31837b61d8bb2886d35780
84d3973d854aef6678624aa8773c5b0c12f24419 08-Sep-2016 Felipe Leme <felipeal@google.com> Calls setUidFirewallRules() and enableFirewallChain() asynchronously.

The updateRulesForRestrictPowerUL() method is used to set the
power-related restrictions for all apps and it ends up by calling
NetworkManagerService twice (and each call spawns an iptables process,
which takes ~50ms to complete). This method is called on some critical
paths, like when the device leaves Doze Light mode.

This change makes these calls asynchronously, hence reducing the delay
on such critical paths in the O(100ms).

Fixes: 31281543

Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t
com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: If720a7112c72a18193ea9614ae187b7ed9c741ec
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0970046adf578c0003d5bfbd19a0ca58c18d96cc 08-Sep-2016 Felipe Leme <felipeal@google.com> Don't update app idle rules on updateRulesForRestrictPowerUL().

updateRulesForRestrictPowerUL() should only affect the global
rules (Doze and Battery Saver), while app idle is an app-specific
rule. Such call was delaying setDeviceIdle() in O(100ms).

BUG: 31281543

Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleNonMetered_enabled
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleNonMetered_whitelisted
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleNonMetered_blacklisted
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleMetered_enabled
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleMetered_whitelisted
Test: cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testAppIdleMetered_disabled

Change-Id: I32ad6853a6d88e2709538d00cd1e77b96d331b2d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
66bf79760a5ed0beaa5ce2f4f99ed986ef0310ac 08-Sep-2016 Felipe Leme <felipeal@google.com> Created a NETWORK trace tag. am: 873a83af3f am: e9bac30add
am: 68dcfcc22f

Change-Id: I4d89a5e4720bf639800999819d69dbedae19a1fd
68dcfcc22f2e97504d07ff5cd15cbfc46a8930d3 08-Sep-2016 Felipe Leme <felipeal@google.com> Created a NETWORK trace tag. am: 873a83af3f
am: e9bac30add

Change-Id: Ibc8c156d3b3c6c893ee55eca4f6b92767fefa741
e9bac30add5d2a7b8d7018eb7ac9c1cbadc27119 08-Sep-2016 Felipe Leme <felipeal@google.com> Created a NETWORK trace tag.
am: 873a83af3f

Change-Id: Iebfc57a542d2f78fa41fda053ae731966476cd56
873a83af3fff6b0ac85489ebc0b3106d11e97ca6 07-Sep-2016 Felipe Leme <felipeal@google.com> Created a NETWORK trace tag.

Also applied it to a few critical paths on NetworkPolicyManagerService.

BUG: 31281543

Change-Id: If27240d86c21e5b49b9595bfcbc90214126cf489
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0ecfcd1e3a158ab5ee54d822d1a1166b15ba7a3c 06-Sep-2016 Felipe Leme <felipeal@google.com> Refactored INetworkPolicyListener callbacks.

Methods onRestrictBackgroundWhitelistChanged() and
onRestrictBackgroundBlacklistChanged() were replaced by a more generic
onUidPoliciesChanged().

Test: m javac-check RUN_ERROR_PRONE=true
Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

BUG: 28791717
Change-Id: I18b2ee4af06802e0d23822b153d1be9b429d5811
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
1de7186c927716ce16b8d383ce2da3b3d32e04dc 03-Sep-2016 TreeHugger Robot <treehugger-gerrit@google.com> Merge "Removed deprecated methods from INetworkPolicyManager."
57e3d310913ff61ae63c66fcf3d7b64ab9d741a6 23-Aug-2016 Felipe Leme <felipeal@google.com> Removed deprecated methods from INetworkPolicyManager.

Removed the following methods:

- addRestrictBackgroundWhitelistedUid(int uid)
- removeRestrictBackgroundWhitelistedUid(int uid);
- getRestrictBackgroundWhitelistedUids();

They've been replaced by getUidsWithPolicy() and setUidPolicy().

Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

BUG: 28791717

Change-Id: I8f6a569cef84c2be60e3277756283026c0fb5b29
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a1252b2fc67ce7732cd09c601cae4122b53023e8 31-Aug-2016 Felipe Leme <felipeal@google.com> Remove wtf log when optional apps are not found.

Test: none
Change-Id: I9f94c2ab47eb78d05754c85aafc13e1e238ead04
Fixes: 31177447
(cherry picked from commit 6fa02e95ba41578d8e8d16f8201f90ca37634183)
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
24c27754c5f1d23dfa679ade368977936519a58d 01-Sep-2016 Svetoslav Ganov <svetoslavganov@google.com> resolve merge conflicts of a2c1196 to master - continued

Change-Id: I1b3c0e1a42b4cbb025de049e95d5377a162d58d1
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
388d08b6e3f6eab30e90ac22b151862b3b4ab352 01-Sep-2016 Svetoslav Ganov <svetoslavganov@google.com> resolve merge conflicts of a2c1196 to master

Change-Id: I221f03e2cda9e677199698d492d16788c25c9e96
0f148aac582221f675743cebd67940cc90bb04c6 31-Aug-2016 Felipe Leme <felipeal@google.com> Remove wtf log when optional apps are not found. am: 6fa02e95ba
am: e20c47686b

Change-Id: I038a1c3782800040c946da50417f4afde362cdd9
e20c47686b56002e63181b2d1f8fa25727e224bd 31-Aug-2016 Felipe Leme <felipeal@google.com> Remove wtf log when optional apps are not found.
am: 6fa02e95ba

Change-Id: Ie8359e68f31979c18b2dd2b0dbad627c2045a293
6fa02e95ba41578d8e8d16f8201f90ca37634183 31-Aug-2016 Felipe Leme <felipeal@google.com> Remove wtf log when optional apps are not found.

Test: none
Change-Id: I9f94c2ab47eb78d05754c85aafc13e1e238ead04
Fixes: 31177447
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3d3308d3e476f62664a9c3c31e6bc95bc879e49e 24-Aug-2016 Felipe Leme <felipeal@google.com> Moar test cases for NetworkPolicyManagerServiceTest.

Before refactoring restrict-background into a UID policy, it's important
to make sure apps receive the proper ACTION_RESTRICT_BACKGROUND_CHANGED
intents.

BUG: 28791717

Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

Change-Id: I32b2e36750ce4640b57d9b1d29dc53ec641456fa
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
6f51a0afe9237d36c374d5c202842292b9ba2a46 25-Aug-2016 Felipe Leme <felipeal@google.com> Fixed getUidsWithPolicy().

UID polices are stored as flag, but getUidsWithPolicy() was checking for
their exact values.

Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

BUG: 28791717
Change-Id: I923b4f63636a32e42cadda5bbb59268267e899c0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
46b451fa7da6c0323e80616409cf467d5b1fd01f 19-Aug-2016 Felipe Leme <felipeal@google.com> Refactored whitelist restrict background uids.

On Android N, the list of UIDs whitelisted was kept in a separate array
and XML tag, while in reality it should be a new UID
policy (POLICY_ALLOW_METERED_BACKGROUND).

This change refactors NetworkPolicyManagerService to use the new UID
policy, although without removing any of its existing methods (but
marking them as deprecated).

Test: m -j32 FrameworksServicesTests && adb install -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner"

Test: cts-tradefed run commandAndExit cts --skip-device-info --skip-system-status-check com.android.compatibility.common.tradefed.targetprep.NetworkConnectivityChecker --skip-preconditions -m CtsHostsideNetworkTests --abi armeabi-v7a -t com.android.cts.net.HostsideRestrictBackgroundNetworkTests

BUG: 28791717

Change-Id: I39869efda554ca0b736dd2380e439474f91dfbe6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
bb10d26ba67c1f8fbd6927d5b15b2864f0defef5 20-Aug-2016 Felipe Leme <felipeal@google.com> Removed screen on/off callbacks from NPMS. am: f8dd7b4e8d
am: 6a3706a08c

Change-Id: I025326d7b89a60920c0db3aa6e363541974cd204
6a3706a08c47bdbfe0c427f110d5d1bf79457209 20-Aug-2016 Felipe Leme <felipeal@google.com> Removed screen on/off callbacks from NPMS.
am: f8dd7b4e8d

Change-Id: I0cbf6cbbbfe8a30b6257f934b76c4e88631c0f7b
b146f7678f525d19dc3fb38790ad23a4d276113f 19-Aug-2016 Felipe Leme <felipeal@google.com> Added a uidPoliciesToString() method.

BUG: 28791717
Change-Id: Ib26e08332d011ca5414406810cec4b4936841068
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f8dd7b4e8d548274c680644a2225951b97e94a4f 10-Aug-2016 Felipe Leme <felipeal@google.com> Removed screen on/off callbacks from NPMS.

NetworkPolicyManagerService (NPMS) used to depend on screen on/off
changes to determine if a foreground activity should have network
restrictions, but such check is now redundant since ActivityManager
already changes the proper UID state (like going from TOP to
TOP_SLEEPING) when the screen status is changed.

Removing such code decreases the NPMS lock contention when the screen is
turned on in about 3-5ms.

Change-Id: I2853443efedbf14961ae9a5b2e72689d4d1a646c
BUG: 30785671
(cherry picked from commit 88f40ad9a721ee30708be82f66fb58c64f1d36b5)
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
340ca9f1530e2da0a9fd89339331821404b9e202 12-Aug-2016 Chong Zhang <chz@google.com> Merge "Allow to keep screen on only if window can be seen" into nyc-mr1-dev am: 305d610fd8 am: 74670684d4
am: 541d028da4

Change-Id: I095b6af71821585064f51f3a992293b21fabc742
541d028da4446b5ab10463c46b896e796105960d 12-Aug-2016 Sanket Padawe <sanketpadawe@google.com> Make data warning notification heads up. am: c015e1c63c
am: 73bcc6e0f3

Change-Id: I4331b1ee94d1f8e871aaebec874959c650d2d10c
73bcc6e0f32202c1cb0014ec35693799b3fefd38 12-Aug-2016 Sanket Padawe <sanketpadawe@google.com> Make data warning notification heads up.
am: c015e1c63c

Change-Id: I826fadc6fdda8707ec280b11379201f72b861362
c015e1c63c9d8dac7715a2f6c2133926ca1d70a4 12-Aug-2016 Sanket Padawe <sanketpadawe@google.com> Make data warning notification heads up.

Bug: 30760683
Change-Id: Id3fed0a62936dddabcb169536a4a917d750259d7
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
88f40ad9a721ee30708be82f66fb58c64f1d36b5 10-Aug-2016 Felipe Leme <felipeal@google.com> Removed screen on/off callbacks from NPMS.

NetworkPolicyManagerService (NPMS) used to depend on screen on/off
changes to determine if a foreground activity should have network
restrictions, but such check is now redundant since ActivityManager
already changes the proper UID state (like going from TOP to
TOP_SLEEPING) when the screen status is changed.

Removing such code decreases the NPMS lock contention when the screen is
turned on in about 3-5ms.

Change-Id: I2853443efedbf14961ae9a5b2e72689d4d1a646c
BUG: 30785671
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d43d8ce8199243e8691cc1c2a0f0d2ad24503da0 03-Aug-2016 Felipe Leme <felipeal@google.com> Removed redundant call to updateRulesForRestrictPowerUL(). am: 7e6d8e2236 am: 893c6bd832
am: 2735eda600

Change-Id: I86debb83a5ef670e75d93ee3ac6413e0da16dedb
5ca3e861fbb47785afa308b99fdf10bf3c6ea8c7 02-Aug-2016 Wei Liu <luciferleo@google.com> Merge commit '1080bc108d2a2dd48b16e6280a1df21556af06e8' into manual_merge_1080bc1

Change-Id: Ida1d99be96e5e637d454d68906fe8bebeb21ed09
2735eda600bdf3039239a3f9792be23547fc96bd 02-Aug-2016 Felipe Leme <felipeal@google.com> Removed redundant call to updateRulesForRestrictPowerUL(). am: 7e6d8e2236
am: 893c6bd832

Change-Id: I679ce9027274f46657548550e4eaf48b3232d7a2
893c6bd8320b3e9b186cbaca4ac0da3d6f6522ea 02-Aug-2016 Felipe Leme <felipeal@google.com> Removed redundant call to updateRulesForRestrictPowerUL().
am: 7e6d8e2236

Change-Id: Iaab5f2eaf7b5764ab0e3146d3eefdfe69820bd85
1080bc108d2a2dd48b16e6280a1df21556af06e8 02-Aug-2016 Wei Liu <luciferleo@google.com> Make NetworkOverLimitActivity and DataUsageSummaryActivity configurable
am: 546cb77123

Change-Id: I76d1bdd1b4d4a880bc550945db65168dc189f0df
546cb7712364618490f508659a9ce557276b6d1b 22-Jul-2016 Wei Liu <luciferleo@google.com> Make NetworkOverLimitActivity and DataUsageSummaryActivity configurable

b/23120366

Change-Id: I9176482ea15ee1fd0baa0958c582cb342dcd1b79
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7e6d8e223698184ca170f901d3000457055107ae 30-Jul-2016 Felipe Leme <felipeal@google.com> Removed redundant call to updateRulesForRestrictPowerUL().

That method scans all UIDs, and is redundantly called on
updateRulesForTempWhitelistChangeUL() for each UID.

BUG: 30374782
BUG: 30568426

Change-Id: I56670578eff0789b1923cee0a1b316ffdbc3aa79
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8a3d56ce127b2d4531e94177c4355458ba4adb0f 01-Aug-2016 Chris Wren <cwren@android.com> support long text descriptions in the data use notifications.

We previously removed the character limit for translations, so it
would be good to make sure any translations that are very long
can be expanded, although it's probably unlikely to be an issue.

Bug: 28545220
Change-Id: Ibd5b689808ec97f3584627038bd5d486c184caea
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
37506e246dfc78b9e7bb9263249a28ddc0af26ab 14-Jul-2016 Jack Yu <jackyu@google.com> Merge \"Disable VT when users turn off data or hit data limit\" into nyc-mr1-dev
am: 92a07ae79d

Change-Id: Ia53f350beda8d9671cb67c2a01ed0071b65c4b76
8781b68e2bd6c0080e9c91544557b64783e7dffa 08-Jul-2016 Jack Yu <jackyu@google.com> Disable VT when users turn off data or hit data limit

Disable VT when users turn off data or hit data limit. If
there are ongoing VT calls, we'll need to downgrade them
to VoLTE calls.

bug: 27316521
Change-Id: I89fe42f6cf6c419c21c4f2006e9cb43cf7e13503
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
92944f3e0a12a69ec6e5738c71565588feff43ef 30-Jun-2016 Jack Yu <jackyu@google.com> Merge \"Added a virtual interface for Video Telephony call usage counting\" into nyc-mr1-dev
am: 83e96c1957

Change-Id: Ia70a43a4df982cf09bb89b00650ce25aff7a43f4
b6587eac06c00c964b6f541e18d307df8c4a9a50 22-Jun-2016 Jack Yu <jackyu@google.com> Added a virtual interface for Video Telephony call usage counting

1. Added a virtual VT interface.
2. Qeury VT usage from telephony service.

bug: 20888836
Change-Id: I3df0c22922f8394554230b53f8ae091e17c95595
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
80f0d0f2a49e3f1651af5852b2a824c5261519fc 21-Jun-2016 Felipe Leme <felipeal@google.com> resolve merge conflicts of a3d3da8 to nyc-mr1-dev-plus-aosp

Change-Id: Ic75a6926e4bd2b8199e4feb7af315e84659d03a2
a1540899f7c5a5e7b9c117baa2a87710e9d881e6 21-Jun-2016 Felipe Leme <felipeal@google.com> resolve merge conflicts of fa2715e to nyc-mr1-dev

Change-Id: If0a1e7e70aa33e99f05694a37c2a54c567449507
a3d3da8de206c7f7b9a6c35c8525ea5c1e0cd62f 21-Jun-2016 Felipe Leme <felipeal@google.com> Merge \"Ignore system apps on enforceMeteredApnPolicy().\" into nyc-dev
am: fa2715e03b

Change-Id: I98440b2ae3371661ed8b1b2f29b0ba18e1b6e6c3
ee27cabe343a665bc2224cdb598857d143b567b2 21-Jun-2016 Felipe Leme <felipeal@google.com> Ignore system apps on enforceMeteredApnPolicy().

BUG: 29514913
Fixes: 29448383
Change-Id: I19eb004c937e1c872bbf82caa7a919e43d48a259
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
5a3842b4ba392c9cd011dbe17c3f4833f4382d8b 18-Jun-2016 Felipe Leme <felipeal@google.com> Split mRulesLock in 2 distinct locks.
am: f0823858e5

Change-Id: If5cd09f47268b70e63c4c4c6ecae57f9b38c3ed2
f0823858e502d36c01a39afd1fc9384a1fe9a3f7 08-Jun-2016 Felipe Leme <felipeal@google.com> Split mRulesLock in 2 distinct locks.

mRulesLock was used to lock Life, the Universe, and Everything. Or more
specifically, UID rules, network policies, and other state.

As such, it was frequently a cause of slowness in the device due to lock
contention. For example, isNetworkMetered() is a method called by
ConnectivityService and it only needs to lock the network policies info,
but because it required the full lock, it would be often on hold when
the UID state is changed by onUidStateChanged().

BUG: 29397827

Change-Id: I7aac8f33e150c52cf7a682f0290a91a2c350b8de
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f5b666cb1b6731df2afe98f2e3f1082308ea1691 18-Jun-2016 Felipe Leme <felipeal@google.com> Merge \"Refactored unnecessary calls to updateRulesForGlobalChangeLocked().\" into nyc-mr1-dev
am: a87bbe85a9

Change-Id: I98c03686c99b44c640b627b7cf3ec16381e76c7e
f3e40642369fac771c707ee2153976295e41be03 08-Jun-2016 Felipe Leme <felipeal@google.com> Refactored unnecessary calls to updateRulesForGlobalChangeLocked().

BUG: 29397827
Change-Id: I165eb5b4bbd44a0f3e115bbe63bc883863874608
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4cb2741230cc29ccf36664bd6a4ff103c9f1f280 06-Jun-2016 Xin Li <delphij@google.com> resolve merge conflicts of b9d266f to nyc-dev-plus-aosp

Change-Id: I1580d3c108d1f0b57f75b27d67e229f8b314a72a
0655edd611c10a587d5fc5a1236cfa27b45a4247 18-May-2016 songjinshi <songjinshi@xiaomi.com> Fixes the system server crash issues caused by null pointer in
NetworkPolicyManagerService.

if the multi user occurs switch and remove then to getPackagesForUid
the return value will got null,because the user remove is not synced
with the getPackagesForUid,so we must check the return value for
getPackagesForUid to avoid the crash.

https://code.google.com/p/android/issues/detail?id=211972

Change-Id: If921582da86dbe862f2dad337b0a8d1f20593c99
Signed-off-by: songjinshi <songjinshi@xiaomi.com>
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
99d5d3d7759883119665d928d110edb14df44ecc 16-May-2016 Felipe Leme <felipeal@google.com> Added a onRestrictBackgroundBlacklistChanged() callback.

When an UID is added / removed to the Data Saver blacklist, it's
necessary to notify internal components such as the Settings UI (which
was erroneously listening to UID rules changes instead).

BUG: 28743623
BUG: 28791717

Change-Id: I11c85e141dfe074ad390fd324309d2412bfbbd45
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
781ba14af2a8c8e85f684208055dba0ebfd5db30 10-May-2016 Felipe Leme <felipeal@google.com> Fixed connectivity state in some power saving scenarios.

NetworkPolicyManagerService (NPMS) manages 4 type of network restriction
when apps are running on background:

- Data Saver Mode (data usage restriction on metered-networks)
- Battery Saver Mode (power restriction on all networks)
- Doze Mode (power restriction on all networks)
- App Idle (power restriction on all networks)

These restrictions affects 2 parts of the system:

- Internal framework state on NPMS which is propagated to other internal
classes.
- External firewall rules (managed by netd).

Although each of the power-related restrictions have their own external firewall
rules, internally apps are whitelisted to them through the same
whitelist, and the current code is only updating the internal state (and
notifying the internal listeners) when Battery Saver Mode is on.

As a consequence of this problem, there are scenarios where an app
correctly does not have internet access (because the firewall rules are
properly set), but the NetworkInfo state returns the wrong state (like
CONNECTED / CONNECTED).

This CL fixes this problem by splitting the power-related logic from
updateRulesForRestrictBackgroundLocked() into its own
method (updateRulesForPowerRestrictionsLocked()), and making sure such
method is called whenever the firewall rules are updated.

Externally to this change, the CTS tests were also improved to verify
the apps get the proper connection state; it can be verified by running:

cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests \
-t com.android.cts.net.HostsideRestrictBackgroundNetworkTests

BUG: 28521946
Change-Id: Id5187eb7a59c549ef30e2b17627ae2d734afa789
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d31a97fd83468d27d0f4c6e1455c2f6f59d5a7c9 06-May-2016 Felipe Leme <felipeal@google.com> Fixed connectivity state in some restricted network scenarios.

NetworkPolicyManagerService (NMPS) keeps an internal list of uid
rules (mUidRules) for network restrictions, and when these rules
changes it needs to notify external listeners (such as
ConnectivityService / CS).

Prior to Android N, both Data Saver mode (the feature previously known
as "Restrict Baground Data") and Battery Save mode used the same set of
firewall rules to implement their restrictions: when Battery Saver mode
NPMS would mark all networks as metered and set the proper firewall
rules externally.

Recently, these 2 modes were split in 2 distinct firewall rules and
NMPS.updateRuleForRestrictBackgroundLocked() was changed to update
the mUidRules logic based on the Data Saver firewall (since the Battery
Saver firewall changes are handled externally, on
updateRuleForRestrictPowerLocked()). As such, CS was not notified when
the power-related changes were made, which would cause apps to get a
state of CONNECTED / CONNECTED when querying its active connection.

Another scenario that is not properly handled is when a UID whitelisted
for Data Saver is brought back to foreground: although the proper
firewall rules are set, CS is not notified, and the apps state would be
DISCONNECTED / BLOCKED.

This CL introduces many changes that fix this issue:

- Fixed updateRuleForRestrictBackgroundLocked() to invoke
onUidRulesChanged() when the Battery Saver status changed.
- Fixed updateRuleForRestrictBackgroundLocked() to invoke
onUidRulesChanged() when an app whitelisted for Data Saver is brought
back to the foreground.
- Added a new API (onRestrictPowerChanged() and getRestrictPower())
to notify external services about Battery Saver mode changes.
- Fixed CS logic to properly handle the Battery Saver changes.

Externally to this change, the CTS tests were also improved to verify
the apps get the proper connection state; they can be verified running:

cts-tradefed run commandAndExit cts -m CtsHostsideNetworkTests \
-t com.android.cts.net.HostsideRestrictBackgroundNetworkTests

BUG: 28521946

Change-Id: I8eaccd39968eb4b8c6b34f462fbc541e5daf55f1
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
46c4fc357ce8e686b6e7c2adda573e9870a8221f 04-May-2016 Felipe Leme <felipeal@google.com> Refactored NetworkPolicyManagerService mUidRules.

NetworkPolicyManagerService (NMPS) keeps an internal list of uid
rules (mUidRules) for network restrictions, and when these rules
changes it needs to notify external listeners (such as
ConnectivityService / CS).

Prior to Android N, both Data Saver mode (the feature previously known
as "Restrict Baground Data") and Battery Save mode used the same set of
firewall rules to implement their restrictions: when Battery Saver mode
NPMS would mark all networks as metered and set the proper firewall
rules externally.

Recently, these 2 modes were split in 2 distinct firewall rules and
NMPS.updateRuleForRestrictBackgroundLocked() was changed to update
the mUidRules logic based on the Data Saver firewall (since the Battery
Saver firewall changes are handled externally, on
updateRuleForRestrictPowerLocked()). As such, CS was not notified when
the power-related changes were made, which would cause apps to get a
state of CONNECTED / CONNECTED when querying its active connection.

This change refactores the mUidRules to use bitmasks, in preparation for
another change that will fix the issue.

It also fixes a minor bug that was preventing removed packages to be
removed from the whitelist.

BUG: 28521946
Change-Id: I9f0e1509a6192cad403f740c1cd76a6b7dab7d26
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d17fda40cdcf875002086fbe98bf34ba7cbdb03c 29-Apr-2016 Felipe Leme <felipeal@google.com> Added option to reset network policies for a given user.

BUG: 28429571
Change-Id: I675e4ee5484a865f6819d7a0eef34133be11306f
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a110eec77f226965cf14d2a2385dae6b22565ec4 29-Apr-2016 Felipe Leme <felipeal@google.com> Multi-user Data Saver fixes:

- When adding a new user, adds its default whitelist apps.
- When removing an user, clears its revoked default whitelist apps.

BUG: 28451218

Change-Id: Ic30a3d6d30c7575620edd3ad32c3221203626f8d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
1b6519bd87a59a10e9928e5772a17976692611da 28-Apr-2016 Jeff Sharkey <jsharkey@android.com> Flag to mark foreground jobs, fix data saver.

When a job will eventually run in the foreground, the internal
scheduling needs to ignore any background network restrictions when
satisfying constraints. This also means the job should ignore the
current device doze state, since the requesting app could get the
same behavior by starting their own foreground service.

Always dispatch network policy changes to ConnectivityService first
to ensure that it has up-to-date information. Fix bugs around data
saver that were causing networks to not be marked as BLOCKED for
background apps; before this fix apps would have been spinning in
internal connectivity loops, thinking that the network was actually
connected when the kernel was actually blocking their traffic.

Offer new ConnectivityService method overloads to ignore the blocked
state for a specific UID.

Print unsatisfied job constraints to aid debugging.

Bug: 26571724
Change-Id: Iaaa17933e6dc1bf6d3dff26d0bfc12222e51e241
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
61731854329a6331ae055bb20d98caf6a71ea5b6 29-Apr-2016 Felipe Leme <felipeal@google.com> Fixed wrong loop variable (AKA: I, Y U NO J?)

BUG: 27366993
BUG: 28451218
Change-Id: I8455ada4194e7fc44ae7448e5cd763d22065fd2e
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
70c8b9b66027409f9dec91872cbe8ae8756d5ea4 25-Apr-2016 Felipe Leme <felipeal@google.com> Tethering and Data Saver: There Can Be Only One!

If tethering is on when Data Saver mode is turned on, tethering should
be disabled, and vice-versa.

BUG: 28313291
Change-Id: I7f3dadbd97ea1492ab76b41043dea7487c1db02d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f07c7b9fd0a640bff4bf7690373613da217fe69b 22-Apr-2016 Jeff Sharkey <jsharkey@android.com> API changes for DownloadManager and JobScheduler.

To support moving DownloadManager, add new JobScheduler network type
constraint that matches "any network except roaming." Also add an
API to get a specific JobInfo by ID.

Since the default network can be different on a per-app basis, and
individual apps may be blocked due to app standby, evaluate job
connectivity constraints on a per-UID basis. To implement this
cleanly, add NetworkInfo.isMetered() to match the isRoaming() API.

Add new DownloadManager APIs to support charging and device idle
constraints, which are plumbed through to JobScheduler under the
hood when scheduled.

Add filtering to JobScheduler dumpsys to omit noisy details for
packages the caller isn't interested in.

Bug: 28098882, 26571724, 19821935
Change-Id: I09ca7184ef7ce6adba399f579d415a5fb2ea6110
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
019fcd2bec414fbb730c0bb0fa62995ee692c5bf 19-Apr-2016 Felipe Leme <felipeal@google.com> Added onRestrictBackgroundWhitelistChanged callback.

When users add or remove an app to the Data Saver Mode whitelist, the
app is notified through a
ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED. But besides this
broadcast, it's also necessary to notify internal apps such Settings,
hence a new method is being added to INetworkPolicyListener.

BUG: 27481520
Change-Id: I1537a77becf6b7da1535ed5faabdc894fd9f7084
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3646cbdfe77ff37475259b39171aa49686c5501a 13-Apr-2016 Amith Yamasani <yamasani@google.com> Update interface quota on the handler

This avoids making expensive netd calls while holding the mRulesLock

Doesn't fix the problem of turning on hotspot while WiFi was connected.
It is no longer blocked on isNetworkMetered() call though.

Partial fix for following bugs...
Bug: 27857665
Bug: 28201280
Change-Id: I62f3c0b0571292cc1e156b48ce3329def41cdd07
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
66a6be3a4c955e52ea61e0d35f338983f632abf8 30-Mar-2016 Jack Yu <jackyu@google.com> Excluded certain APNs (e.g. IMS) from mobile data usage.

Added not_metered capability to a mobile network if none
of its associated APN types are metered. Also used not_metered
capability to determine if a network should be accounted for
data usage or not instead of using network type, which is
always MOBILE after refactoring. Will add VT usage support
in next phase.

bug: 20888836
Change-Id: Id692cb856be9a47d0e918371112630128965b1bb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
01e05e741e2d0b4ec40a54fe50e8c90fdaaa0b46 31-Mar-2016 Felipe Leme <felipeal@google.com> Save whitelist status of all apps.

When app is asked to be whitelisted (or removed from the whitelist),
NMPS checks if the app has internet permission, since if it doesn't the
firewall rules do not need to be changed.

But even if the optimization kicks in, it should still mark the app as
whitelisted, otherwise it will leave to inconsistencies like the UI
never displaying it as whitelisted.

BUG: 26685616
Change-Id: I6e287092d7bf8b7aa3a1bda707b58b128a22bffa
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
ef89c9097893123c5eebe464e6650fbd249cd837 31-Mar-2016 Felipe Leme <felipeal@google.com> Data Saver Mode should automatically allow foreground services.

BUG: 27324964

Change-Id: I8f09b9c9673f3ed697daca14902123a81ce41085
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
70c57c2810eb5004ac6487921bfdf0880f818e3c 29-Mar-2016 Felipe Leme <felipeal@google.com> Uses bw_happy_box and bw_penalty_box for Data Saver.

bw_penalty_box is a blacklist-based firewall chain used to determine
which UIDs do not have access to metered interfaces. It can be used
alone or with bw_happy_box, which is a whitelist-based chain: when
bw_happy_box is enabled, it's included in the bw_penalty_box chain.

Currently, NMPS and NMS uses just bw_penalty_box for both blacklist and
whitelist, so when Data Saver is turned on, it has to build a extensive
list of UIDs to be blacklisted, which can take seconds (since it makes
dozens of iptables, and which forks an iptables process)

This CL changes this behavior so it uses both chain (plus a new
bw_data_saver chain, which is added to the end of bw_happy_box), in
which case the Data Saver switch is much faster (around 120-160ms),
since it requires just 1 or 2 iptables calls (one to switch the
bw_data_saver rule, and another to whitelist the foreground app if it's
not whitelisted yet).

BUG: 27127112
BUG: 26685616

Change-Id: If10222aef9f49a924b07b978d4bdccdd92f9acdb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f28983d3a071d4b420886306853617c0264ca342 25-Mar-2016 Felipe Leme <felipeal@google.com> More Data Saver / Battery Saver modes cleanup

When Battery Saver mode was first implemented, there were no firewall rules
on netd, so the solution was to make all network interface metered and
re-use the bw_penalty_box chain.

Recently, a fw_powersave chain was created with the purpose of
separating these 2 concerns, but some scenarios were no properly
handled.

This change further cleans up the code by:

- Creating a updateRestrictionRulesForUidLocked() to be used when both
Battery Saver and Power Save rules need to be re-evaluated.
- Renaming some methods to be more consistent with others.
- Disabling rules for apps that do not have internet permissions.
- Updating network stats only when process status changes.
- Only updating Battery Saver rules when they changed.

BUG: 27127112
BUG: 26685616

Change-Id: I75fbc9733b8989fadcb10ebc154e214e3d256fe9
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
65be3025fd07d53137f5434794d1d1b3a0933aab 22-Mar-2016 Felipe Leme <felipeal@google.com> Refactored NetworkManagerService to support Data Saver.

Netd provides 2 bandwidth control rules to restrict which uids can use
metered networks:

- bw_penalty_box is a blacklist-based firewall chain used to determine
which uids do not have access to metered interfaces.

- bw_happy_box is whitelist-based firewall chain used to determine which
uids have access to metered interfaces.

Currently, both NetworkManagerService (NMS) and
NetworkPolicyManagerService (NPMS) uses just the bw_penalty_box rule,
which makes turning Data Saver mode on / off too slow (since NPMS needs
to build the bw_penalty_box on demand); this CL adds support for both
rules on NMS, although NPMS doesn't take advantage of it yet (it will be
refactored in a separate CL).

BUG: 27127112
BUG: 26685616
Change-Id: Ib954574f7c86269fc9b4cf8ce4ba72ba5878c23d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7474fe7b421dcc190c4602389ca0f9c910382260 21-Mar-2016 Jeff Sharkey <jsharkey@android.com> Ignore wildcard network policies.

They never should have been persisted to disk in the first place.

Bug: 24972775
Change-Id: If9254f74455823f9fda9545fdf543fc7250a4016
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
71c56189682d22472a1564897dfb55080718f87b 18-Mar-2016 Felipe Leme <felipeal@google.com> Merge "Cleaned up dependency to data saver mode." into nyc-dev
76010a33025fea69f0c1daf0436bd967e47a69a9 17-Mar-2016 Felipe Leme <felipeal@google.com> Cleaned up dependency to data saver mode.

When power-save mode was first implemented, there were no firewall rules
on netd, so the solution was to make all network interface metered and
re-use the bw_penalty_box chain.

Recently, a fw_powersave chain was created with the purpose of
separating these 2 concerns, but the initial implementation left a lot
of dead code around; this change cleans up that code.

Changes:

- Renamed updateRulesForUidLocked() to
updateRestrictDataRulesForUidLocked().
- Simplified updateRestrictDataRulesForUidLocked() implementation.
- Removed calls to updateRestrictDataRulesForUidLocked() from places
that don't touch the data saver rules.
- Refactored the data saver part of updateRulesForGlobalChangeLocked()
into a updateRulesForRestrictDataLocked(), which is similar to the
existing methods for iddle and restricted power.
- Changed setRestrictBackground() to use
updateRulesForRestrictDataLocked() instead of
updateRulesForGlobalChangeLocked().
- Removed redundant calls to updateRestrictDataRulesForUidLocked() and
updateRulesForGlobalChangeLocked() when an user id deleted.

BUG: 27127112
BUG: 26685616
Change-Id: I621d20139742cc0dd3bbf6fe9cbfe889ddcc1fd7
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8a372a0a280127743ce9a7ce4b6198c7a02d2a4f 16-Mar-2016 Jeff Sharkey <jsharkey@android.com> Refactoring FBE APIs based on council feedback.

Mostly consists of removing the word "encryption" from most APIs,
since we can't actually make promises about the data being encrypted.

Bug: 27531029
Change-Id: Iace9d7c4e64716abf86ed11847c40f3947e1d625
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4f3fc215050bb52d470fc7d6b1fd2eeb39c2119b 04-Mar-2016 Rakesh Iyer <rni@google.com> Fix ActivityManager watchdog kill.

Typo caused an infinite loop causing a watchdog kill.

Change-Id: If9a8973c28e284d993d81ba7d6724f9575a30c92
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7af0be288e3bce8c97e8e830025e011f9686bb42 04-Mar-2016 Felipe Leme <felipeal@google.com> Merge "Handles blacklist on getRestrictBackgroundStatus()." into nyc-dev
923845fdaa03a3f807a759d0eef3d0decca107d6 02-Mar-2016 Felipe Leme <felipeal@google.com> Handles blacklist on getRestrictBackgroundStatus().

Initial implementation of getRestrictBackgroundStatus() was just
handling the whitelist status of apps when data saver is turned on, but
when an app is blacklisted, it should return
RESTRICT_BACKGROUND_STATUS_ENABLED regardless of the data saver status.

BUG: 27432317
Change-Id: I5a027d8b7d17a13075510ff8f29d41eaa5a6beb8
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3ce000e647da42c0f39b70dceabbaa52b8ef5ca3 04-Mar-2016 Felipe Leme <felipeal@google.com> Merge "Code cleanup on NMPS and NMS." into nyc-dev
03e689db51284d37145b8af2e750d6bbd22312b2 03-Mar-2016 Felipe Leme <felipeal@google.com> Code cleanup on NMPS and NMS.

- Always check for DBG before using Slog.d.
- Logs duration of NMS.systemReady() when debugging.
- Logs duration of NMPS.updateRulesForGlobalChangeLocked() when debugging.
- Removed redundant toString() calls.
- Catch multiple exceptions.
- Replaced enhanced for on nested Iterable iterations.

BUG: 21725996
Change-Id: Ia775a394b59de7a0570ad4954d1fe3a2698c66d6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
011b98f93a4e0f609f9f3a83be784021c8b593a6 11-Feb-2016 Felipe Leme <felipeal@google.com> Created a firewall chain for power save mode.

When power-save mode was first implemented, there were no firewall rules
on netd, so the solution was to make all network interface metered and
re-use the bw_penalty_box chain.

This change removes that workaround by creating a explicit fw_powersave
chain, whose behavior is similar to fw_dozable (in fact, it reuses some
of its code); such change not only makes network restrictions on
power-save mode simpler, but it also allows to optimze how the restrict
network rules are changed (which will be done in a separate change).

BUG: 27127112
BUG: 26685616
Change-Id: I7f7a7b1c1855e916c6651ad90da29fe187a7bea2
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a9505cc39b770a1ee4524d05efb859a9500d892f 26-Feb-2016 Felipe Leme <felipeal@google.com> Add pre-defined apps to the restrict background whitelist.

System config can defined apps to be automatically whitelisted for
restricted background data, but the user can remove the whitelist.

Implementation-wise, NPMS now keeps a list of
<revoked-restrict-background> UIDs in the netpolicy.xml file, and when
it starts it compares the UIDs returned by SystemConfig against this
list, and only whitelist them if they are not revoked. The
revoked-restrict-background is then updated as users change the
whitelist status of UIDs.

BUG: 27366993

Change-Id: I427024fd058924fc9831e409da6636e1bf8e4219
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f23aa99270606b227d007b8f7c1619f564c55b0f 22-Jan-2016 Jason Monk <jmonk@google.com> Move background data notif to data saver status icon

Bug: 26796370
Change-Id: Ibf53c3affa63100b603e362b58ca6d2958791321
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
6a05eee2bdbabb844f1d63741a0680477ec63546 19-Feb-2016 Felipe Leme <felipeal@google.com> Fixed permissions so Shell can call netpolicy methods.

BUG: 27127112
Change-Id: I5548aba70a1c25be139462fbabe9a854fa96d2a6
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2528fcdfbd5d674c00387ed4ce93546ea5581ede 17-Feb-2016 Bill Yi <byi@google.com> Merge commit '26ada7e78993241d702a908eae93d174cbb3c8b3' into HEAD

Change-Id: Ifc45930160cf01ee78ecfbfdc48697055f82e0a7
2b5087b381ee6417e9c6a146c75931c154129a57 17-Feb-2016 Felipe Leme <felipeal@google.com> Merge "Added extra check to avoid NPE if package does not exist." into nyc-dev
86e5a01708256901cb81d5daeeb24fe946436cf2 17-Feb-2016 Felipe Leme <felipeal@google.com> Added extra check to avoid NPE if package does not exist.

BUG: 26685616

Change-Id: I244e479fd9668bc4f07b8797ce2fc1e1df7a4d28
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2a4ac4e5ef2a755e74f62a2974c6069a87520d05 12-Feb-2016 Amith Yamasani <yamasani@google.com> Use hasInternetPermission method

Followup to Bug: 27165396

Change-Id: Ibdf6f5dd15197d4e19d7c7729612c13a02cdbf2b
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
b8849e6891dd91bc62e90b5ccd78621dbf6e4093 12-Feb-2016 Amith Yamasani <yamasani@google.com> Merge "When the device boots up, netd works more than required" into nyc-dev
a065da1703406c7e45acf7b3f16feed3b6a558a6 21-Dec-2015 Soi, Yoshinari <yoshinari.x.soi@sonymobile.com> When the device boots up, netd works more than required

When the device boots up, netd adds rules for applications
which do not have the NETWORK permission to iptables.
Therefore, optimize NetworkPolicyManagerService to not touch
uids that do not have the NETWORK permission.
This modification is similar to Google commit 88e98dfa5.

Cherry picked from AOSP commit to master.

Bug: 27165396
Change-Id: Ic8bb837143b9e349859210654248195d62b73d17
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
47585ba14352d20f8e8633f5409f1fe954cc8511 10-Feb-2016 Felipe Leme <felipeal@google.com> Optimized add|removeRestrictBackgroundWhitelistedUid().

The initial implementation of these methods was calling
updateRulesForGlobalChangeLocked(), which is expensive.

The new implementation is optimized by:

- quitting earlier if there is no change
- only calling setUidNetworkRules
- only broadcasting a ACTION_RESTRICT_BACKGROUND_CHANGED it the status
of the app changed.

This change also removed the unnecessary onPackageRemovedAllUsers()
callback (just onPackageRemoved() is enough).

BUG: 26685616

Change-Id: If38b8b870e992c48e40b4362e8170c64ff8b2a9d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
b1a65ee161d2dd1bb5fee752659350d43102fd0c 08-Feb-2016 Felipe Leme <felipeal@google.com> Changed list metered-networks so it returns all networks.

Prior to this change it was relying on NMPS.getNetworkPolicies(), which
would not return networks that never had any policy attribute (like
metered) set.

This change fix this problem by also querying the WifiManager for the
list of saved wi-fi networks. In fact, the new logic is very similar to
what's done on SettingLib's NetworkPolicyEditor.

BUG: 26685616
Change-Id: I42f37de3a7367718036f45b15c6b5d37b67e7d01
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
95f6280bf13272b0dd345ef8f07f956710c02791 04-Feb-2016 Felipe Leme <felipeal@google.com> Merge "Propagate changes when uids are added or removed to the restricted background whitelist."
3f52cd5f2a0fb29b9dd65b7dee6d84b6fc9e2e88 03-Feb-2016 Felipe Leme <felipeal@google.com> Propagate changes when uids are added or removed to the restricted
background whitelist.

BUG: 26451391
Change-Id: I21eb4959d9f60e83ef96d8d7d1c92e38ddeb2b78
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
de4e8e3a8eee1fb86ccef9e4e3966db4089f9b2e 03-Feb-2016 Felipe Leme <felipeal@google.com> Created commands to manage metered networks.

These commands will be used primarily in the CTS tests:

- list metered-networks [BOOLEAN]
- get metered-network ID
- set metered-network ID BOOLEAN

For example, assuming the device has 2 wi-fi networks, and neither is metered:

// list all non-mobile networks
$ adb shell cmd netpolicy list metered-networks
wifi-A;false
wifi-B;false

// set wifi-A metered
$ adb shell cmd netpolicy set metered-network wifi-A true

// get wifi-A status
$ adb shell cmd netpolicy get metered-network wifi-A
true

// list only metered non-mobile networks
$ adb shell cmd netpolicy list metered-networks true
wifi-A;true

BUG: 26685616
Change-Id: I1ec8be4579477448fcb7f0d6cfa6b9cc6887fa97
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
22173bc1808905090672e5fb51ce5207b46fadf7 21-Dec-2015 Soi, Yoshinari <yoshinari.x.soi@sonymobile.com> When the device boots up, netd works more than required

When the device boots up, netd adds rules for applications
which do not have the NETWORK permission to iptables.
Therefore, optimize NetworkPolicyManagerService to not touch
uids that do not have the NETWORK permission.
This modification is similar to Google commit 88e98dfa5.

Change-Id: Ic8bb837143b9e349859210654248195d62b73d17
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f933871976b4dc4c8e71a364b66f5208cefad3d3 03-Feb-2016 Felipe Leme <felipeal@google.com> Merge "Created a ACTION_RESTRICT_BACKGROUND_CHANGED intent."
a31c591f257c942f22de6dac4c981837179b10de 22-Jan-2016 Fyodor Kupolov <fkupolov@google.com> Purge all state for a removed user

Bug: 26687570
Change-Id: Icd516a871c274ce8986129654081b6dec3850856
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9778f761237b2533903bacc091b1597990110a7e 27-Jan-2016 Felipe Leme <felipeal@google.com> Created a ACTION_RESTRICT_BACKGROUND_CHANGED intent.

This intent will be broadcasted when:

- Global restrict background setting is changed (sent to all packages)
- An individual uid is added to or removed from the whitelist (sent just
to the packages belonging to that uid).

This intent is only sent to registered receivers.

BUG: 26451391
Change-Id: Ic0a5771f88baa52076ad04764f29098a386463cc
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8ab775956720c10277203105d697de771ad6d9ca 26-Jan-2016 Wei Liu <luciferleo@google.com> Add a null check before reference networkInfo. am: b8eaf45e1d am: dca8f10031 am: a12d383117
am: 21e0927b47

* commit '21e0927b474a686d5827e059476291370c0b306e':
Add a null check before reference networkInfo.
b8eaf45e1df007fc4f5a11bb58491abaef39cd57 25-Jan-2016 Wei Liu <luciferleo@google.com> Add a null check before reference networkInfo.

b/26758858

Change-Id: If335ece223114dbfb6b08391f18a09467f377cf1
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
50a235e6c94733659a8cede9ddce8fe8e1cf7124 16-Jan-2016 Felipe Leme <felipeal@google.com> Initial implementation of onShellCommand().

Only the commands related to restrict background whitelisting were
initially added, so they can be used in the CTS test for the new APIs.

BUG: 26451391
Change-Id: I5479073d85c44b99368497448f1f8e36cd99e63c
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
1b103238b90ca0aa7a2dcaaf0912361d583367a9 22-Jan-2016 Felipe Leme <felipeal@google.com> Created new public API so applications can customize its metered network
usage while running in background.

The new API is 'int getRestrictBackgroundStatus()' and returns the
following values:

- RESTRICT_BACKGROUND_STATUS_DISABLED: no restrictions
- RESTRICT_BACKGROUND_STATUS_WHITELISTED: restriction but app is
whitelisted
- RESTRICT_BACKGROUND_STATUS_ENABLED: full restriction

The proper way to interprete these values for using metered networks
while running on background is:

- When disabled, there is no restriction and the application could use the
metered networks freely.

- When whitelisted, the application can use the metered network, but should try
to minimize the usage.

- When enabled, the application should not try to use metered networks at
all, since the usage will be denied.

BUG: 26451391
Change-Id: If07d42bb88e4c02802df0234861f38aef2cfead7
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
b85a637eac7714dcbbcf179ef38e5edc11557b78 15-Jan-2016 Felipe Leme <felipeal@google.com> Added API to whitelist apps for background restriction.

The new APIs are:

void addRestrictBackgroundWhitelistedUid(int uid);
void removeRestrictBackgroundWhitelistedUid(int uid);
int[] getRestrictBackgroundWhitelistedUids();

The whitelist is persisted in the same XML used for the other policies,
using the new 'whitelist and 'restrict-background' tags. Examples:

3 whitelisted apps:

<whitelist>
<restrict-background uid="10001" />
<restrict-background uid="10009" />
<restrict-background uid="10086" />
</whitelist>

No whitelisted app:
<whitelist />

BUG: 26451391

Change-Id: I01b114c346fd8abc05433ad43d010379384f8c2b
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
377ded0fce449f77cd7efae35f97722cdab52693 10-Jan-2016 Jeff Sharkey <jsharkey@android.com> Fix NPE when UID has no packages.

Bug: 25224723
Change-Id: I637214666a1f02bd23474c27a285eea9152cfa9d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
c5967e9862489024c932b0c7fcb84ed0af2a7fd7 08-Jan-2016 Jeff Sharkey <jsharkey@android.com> More progress on triaging PackageManager callers.

Catch a bunch of simple cases where the PackageManager flags are
obvious. Add the ability to use the MATCH_SYSTEM_ONLY flag on
PackageInfo and ApplicationInfo queries.

Re-examine recent tasks after a user is unlocked, since some of the
activities may now be available and runnable.

Bug: 26471205, 26253870
Change-Id: I989d9f8409070e5cae13202b47e2c7de85bf4a5b
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e06b4d1d9f718b9fe02980fea794a36831a16db2 06-Jan-2016 Jeff Sharkey <jsharkey@android.com> Consistent naming for PackageManager methods.

When hidden PackageManager methods take a userId argument, they
should be named explicitly with the "AsUser" suffix. This fixes
several lagging examples so that we can pave the way to safely
start passing flags to new methods without scary overloading.

Also fix spacing issues in various logging statements.

Change-Id: I1e42f7f66427410275df713bea04f6e0445fba28
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
2a9e3f8e6813716ab88ca54fd04ae047dc9aaaeb 18-Dec-2015 Jeff Sharkey <jsharkey@android.com> Better named encryption flags, start triaging.

Create distinct flags for encryption aware, unaware, and both, and
name them like the other MATCH_ flags.

Start adding logic to help triage all system internal callers to
verify that they've done their homework and thought about how to
handle apps while locked. Call sites in the system should either
ask for explicit matching behavior, or explicitly use the DEFAULT
match flag to indicate that they've been triaged to use the
default state-based matching.

Bug: 26250295
Change-Id: I86214e5c4f71a6dc72f06930800388713aecd107
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
bef28feba57be7fd6a4d14a85a8229154338b2ed 30-Oct-2015 Dianne Hackborn <hackbod@google.com> Initial stab at background check.

Actually, this implementation is more what we want for ephemeral
apps. I am realizing the two are not really the same thing. :(

For this implementation, we now keep track of how long a uid has
been in the background, and after a certain amount of time
(currently 1 minute) we mark it as "idle". Any packages associated
with that uid are then no longer allowed to run in the background.
This means, until the app next goes in the foreground:

- No manifest broadcast receivers in the app will execute.
- No services can be started (binding services is still okay,
as this is outside dependencies on the app that should still
be represented).
- All alarms for the app are cancelled and no more can be set.
- All jobs for the app are cancelled and no more can be scheduled.
- All syncs for the app are cancelled and no more can be requested.

Change-Id: If53714ca4beed35faf2e89f916ce9eaaabd9290d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
24937d235da1648ece1866ce326da539c0ccb4bf 03-Nov-2015 Dianne Hackborn <hackbod@google.com> resolve merge conflicts of 3aee38c86e to master.

Change-Id: I9ff3f92b37e24f5f0527a0034b159920f6d7ffe1
9a13cc69926c90fe75b624a9a781dc56706a6091 03-Nov-2015 Dianne Hackborn <hackbod@google.com> Merge "Fix issue #25357209: Could not send SMS or MMS messages, had to reboot" into mnc-dr-dev
am: 71e4984505

* commit '71e49845054a3a7dacc51efc4fe658818caec048':
Fix issue #25357209: Could not send SMS or MMS messages, had to reboot
fcc95a6d2c749d6b77eca14bf301d665d858a840 02-Nov-2015 Dianne Hackborn <hackbod@google.com> Fix issue #25357209: Could not send SMS or MMS messages, had to reboot

I think what probably happened is that since we only report an app
going in to the "interaction" state as an interaction event to usage
stats, apps that sit around in that state forever will only see one
interaction at the start and never again. So usage stats could start
thinking they are idle.

Fix this by having the activity manager report an interaction event
for such long running applications at least once a day.

Also, because it is correct and for paranoia by protected us another
way, system uids should never go in to standby.

Change-Id: I8a3805bfca86cbe78560488a649ecd07427da99a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d166333eb5d3886a18a3a951d833a6d01cb8340d 21-Oct-2015 Kevin Cernekee <cernekee@google.com> Merge "ignore initial request to disable iptable chain" into mnc-dr-dev
am: 3554d62ca0

* commit '3554d62ca02bf6bc7777d1b22699876ec388fe6c':
ignore initial request to disable iptable chain
690f18a5648e292c29f17b248644e7b887c1f52c 20-Oct-2015 Xiaohui Chen <xiaohuic@google.com> ignore initial request to disable iptable chain

iptable child chains are disabled by default. Ignore the initial
disable request so netd won't throw errors when it cannot detach chains
that do not exist.

Bug: 22800189
Change-Id: I70b4b8b9e6a5dbffe32841941922b7fd92dcd3d6
Original-Change-Id: I9312dd558ba6d59dc6aa394afeb66a222daafcf9
[cernekee: backported from master branch]
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7c3e44265147073e5949c42c27134274472990a1 13-Oct-2015 Jeff Sharkey <jsharkey@android.com> Maybe we can build?

Change-Id: Id0ea9da94f8672cc18430903cd8e9c8a6b8ef8e7
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
75f47866066d5b06924530417c7cb72296753ccb 13-Oct-2015 Jeff Sharkey <jsharkey@android.com> resolved conflicts for f20a5700 to master

Change-Id: I1f13a63c3680901fac58ef709118c9242032b11f
7f6d8754e6d509bf17fe0c46130c884b16706b12 12-Oct-2015 Jeff Sharkey <jsharkey@google.com> am a03ecf7d: Merge "Push firewall rules up to ConnectivityService." into mnc-dr-dev

* commit 'a03ecf7d7c8ff2844caf5feecc6d29f1f7346ff9':
Push firewall rules up to ConnectivityService.
dc988061ac145c86d6871e249d65ca326aaff99f 14-Sep-2015 Jeff Sharkey <jsharkey@android.com> Push firewall rules up to ConnectivityService.

getActiveNetworkInfo() and friends already know how to augment their
results to help apps detect when network access is blocked. This
change wires up the new app-idle and device-idle firewall rules to
be reported through these APIs.

This also causes other platform tools like DownloadManager and
SyncManager to respect these new policies.

Bug: 24050462
Change-Id: Id9517b0b70be7e3ca2ab27bed8049db916e4d829
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
be3b06766a1ed0e8cecb78c98f08dc2c2703a6d8 02-Sep-2015 Xiaohui Chen <xiaohuic@google.com> Clean up USER_OWNER in NetworkPolicyManagerSvc

Removed some TODOs about network usage warnings going to OWNER only. It
is updated to send the warning to all users.

Bug: 19913735
Change-Id: Ie49703626a10a530ede28062090d39a3d23dff48
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7619dba79e3aa422962898920547ac18573ac9c9 13-Aug-2015 Jeff Sharkey <jsharkey@google.com> am 59a18530: am b4ac3781: am d913d78a: am ea68c4ce: am c97509c0: Merge "Update ifaces when global restriction changes." into mnc-dev

* commit '59a18530f5fc0152b7a7609bd2ed2128fa8e7fc7':
Update ifaces when global restriction changes.
d913d78a331de5e488b27810d8dd18399f50de48 13-Aug-2015 Jeff Sharkey <jsharkey@google.com> am ea68c4ce: am c97509c0: Merge "Update ifaces when global restriction changes." into mnc-dev

* commit 'ea68c4ceacbe235c4d8688437010d855ef02e1fb':
Update ifaces when global restriction changes.
9d14206594c0b50a539ee73e37804542694937d6 13-Aug-2015 Jeff Sharkey <jsharkey@android.com> Update ifaces when global restriction changes.

Global restriction of background data only applies to metered
interfaces, but battery saver applies to all interfaces. In the
very specific case where global background had been turned on while
battery saver was enabled, we'd end up with a stale battery saver
rule floating around.

This change triggers an update of iface rules when the global
restriction changes, giving us consistent behavior.

Bug: 23098198
Change-Id: I454dc71cf11d50a2e9e6122e8a801ff17039b43a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
143ebdb94e13f7a9fee2c9f3801bda1df17844c1 11-Aug-2015 Zach Johnson <zachoverflow@google.com> DO NOT MERGE Allow a custom component to handle network policy notifications

Broadcasts information about network policy notifications
to a custom component, if specified, so it can handle them.

Also exposes an API to snooze warning notifications.

Change-Id: Ic89478fa437861e2d4b6a9443858bcaf12122fba
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
908109c1f909cbeeb766f551c16121ddd1a3adba 08-Aug-2015 Dianne Hackborn <hackbod@google.com> am 0385e3e9: am 8e63bbfd: am efa4dee1: am 977d421a: am f0e5501e: Merge "Fix issue #22989030: Separate battery whitelists" into mnc-dev

* commit '0385e3e908fb9988bb2d2d0d15e32cb6a819b34e':
Fix issue #22989030: Separate battery whitelists
4a503b1ece485d44c15eb02ec2bcd464b46e6f7f 07-Aug-2015 Dianne Hackborn <hackbod@google.com> Fix issue #22989030: Separate battery whitelists

We now have a new whitelist you can put apps in, which
opts them out of the old battery saver mode and new app idle,
but doesn't keep them from going in to doze. This is for a few
special cases that we had previously whitelisted for battery saver,
and inherited to the new modes... ultimately we should figure out
how to get these apps out of the whitelist completely, but this
will help for now.

Apps in this new whitelist are not shown in the UI, because they
are still significantly restricted by not being able to operate
normally in doze. This also means they are still visible in the
list of all apps for the user to be able to put them on/off the
complete whitelist if that is what they really want.

In the course of doing this, I needed to clean up code in the
network policy manager to better separate management of the
two firewall rules that now have different whitelists applied
to them. This also hopefully just generally simplifies and cleans
up that code. Hopefully!

Change-Id: I92e15f2f85899571dd8b049b5e3eb1354f55f353
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
896b49ad02b20cf0723241f1f28dd9bee43f2b99 29-Jul-2015 Xiaohui Chen <xiaohuic@google.com> ignore initial request to disable iptable chain

iptable child chains are disabled by default. Ignore the initial
request so netd won't throw errors when it cannot detach chains that
does not exist.

Bug: 22800189
Change-Id: I9312dd558ba6d59dc6aa394afeb66a222daafcf9
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
a9e72a7653d25ae1974dac3f1156d126c807e40c 31-Jul-2015 Amit Mahajan <amitmahajan@google.com> Add OrSelf to privileged permission check.

Bug: 22857263
Change-Id: Ide7105dfd653e3c271330822fb8cee7cf620e35d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
72f83d6a05ddecd516787cedb3b49839bb287c6d 27-Jul-2015 Julia Reynolds <juliacr@google.com> Allow Settings to set background data restrict on secondary users.

Bug: 22721091
Change-Id: Iadfbd25f75f233f2c3c080e0de05b0244572b6cd
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
fd854ee58c5d56f84047007ead9f88a767ae956f 14-Jul-2015 Dianne Hackborn <hackbod@google.com> Fix issue #21626564: MMS should be receivied while Dozing

We now place whoever is receiving the MMS on the temporary
whitelist while doing so, so they can get network access to
download it.

There was also an issue that needed to be fixed where we
were no longer updating the list of allowed uids while
dozing based on their proc states... we now do that.

Also did a bit of optimization of the temp white list update
path do the network policy manager, instead of going through
a broadcast we now directly call in to the network policy
manager. This also allows us to have a synchronous version
of updating the list, so we can know the app has network access
before we tell it to do anything.

Finally added battery stats events for things going on and off
the whitelist so we can diagnose the behavior there.

Change-Id: Ic7fe010af680034d9f8cb014bb135b2addef7455
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
7c5befa4a7ccd6e7e91ffdd93264b717259ec863 14-Jul-2015 Amit Mahajan <amitmahajan@google.com> Add checks for READ_PRIVILEGED_PHONE_STATE.

Callers will have either PRIVILEGED or regular READ_PHONE_STATE.

Bug: 22468536
Change-Id: I60251f61250a975f4147b027d2539824a728e3b5
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3b16cf4f47142a845bf0ede54ef8ef956506c6c3 02-Jul-2015 Dianne Hackborn <hackbod@google.com> Device idle fixes: issue #22209630 and issue #22225665

Issue #22209630: Only allow whitelisted apps to put apps on the temp whitelist

We now check whether the calling app is a system uid or in the whitelist
and, if not, throw an exception.

Issue #22225665: Alarm still goes off in idle mode (doze)

Fix a bug where we were not clearing the calling identity when coming
through the dump command to the service, and as a result when we would
eventually call out to the alarm manager it wouldn't do what we want.
This was only broken when being controlled by the shell.

Also adjust the network policy manager service's handling of device
idle transitions to only toggle the device idle state, which gets rid
of the long delay we have coming out of idle mode. And add in a bit
of logging around going in/out of idle mode to try to understand where
things may be slow in the future.

Change-Id: I4a41f790e9b0bb31330314b94111557d479f2ba5
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
c915eebbf8782f64c5d7167a72c4c4f1d35f6e83 27-Jun-2015 Xiaohui Chen <xiaohuic@google.com> fix temp white list update in device idle

It appears firewall rules for temp white list is updated differently
from the non temp one. The non temp one will trigger a global update
but the temp one is updating only the uids in the list one by one.
The individual uid firewall rule update does not work well with dozable
chain. This cl moves the dozable chain update logic out and put it where
the white list is updated.

BUG:22094135
Change-Id: I425c82da6231b1ef119664907705d0880365f339
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
8dca36dc8a5d17315775ce216689addc5bd9be00 19-Jun-2015 Xiaohui Chen <xiaohuic@google.com> system_server: optimize app idle parole state change

Currently when app idle parole state changes, all idle apps' states
are updated one by one including firewall modifications which are
very expensive. This optimization gets rid of individual firewall
rule changes and makes sure we only modify the firewall once at child
chain level.

BUG: 21446713
Change-Id: Iafc415fe0bc127826fe17894d4fedcf1755cb17d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
802ece61399e94bbe98cafaa277c71bee73c03ba 18-Jun-2015 Xiaohui Chen <xiaohuic@google.com> Merge "system_server: add two child chains to firewall" into mnc-dev
b41c9f7f39939cee8d226eb5e506c3f0573f44f5 18-Jun-2015 Xiaohui Chen <xiaohuic@google.com> system_server: add two child chains to firewall

This is an attempt to speed up getting out of device idle. It groups
uid firewall rules in these child chains so we can attach/detach a whole
chain instead of individual uid rules.

BUG:21446713
Change-Id: Ie8f392da2deabe7cc86a9ecf4ed080163861d41e
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
1958e5e7870579337f1d1d3e6c6fae096ba3abb9 13-Jun-2015 Dianne Hackborn <hackbod@google.com> Fix issue #21813831: Need API for asking to be added to power whitelist

Add the API. Clean up a few related things.

Change-Id: I190adad1812f36f6095b98a1001fedb94874e8b5
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
06f0806bd54e907f3d6d2069d263c700bd91260c 12-Jun-2015 Amith Yamasani <yamasani@google.com> Update network state of apps leaving the whitelist

Bug: 21787562
Change-Id: I9f45acc58ba0b4b6cfed08aecbf081a5f57eb217
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
af575b9f8e1b59be9c8862b6a65c0dcb88145a23 30-May-2015 Amith Yamasani <yamasani@google.com> Temporarily whitelist an app for network during doze

API to allow an app to be whitelisted for network and wakelock
access for a short period. So even if the device is in idle
mode, such apps can be given a chance to download the payload
related to a high priority cloud-to-device message.

This API is meant for system apps only.

A new permission CHANGE_DEVICE_IDLE_TEMP_WHITELIST is required
to make this call.

Bug: 21525864
Change-Id: Id7a761a664f21af5d7ff55aa56e8df98d15511ca
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
e3e314df4d52881225326d426a76e3e7f1bc40d3 20-Apr-2015 Stuart Scott <stuartscott@google.com> Network Reset should have a lockdown like Factory Reset.

bug:20332322
Change-Id: I7c61a011d11e89513757f112abf320bb2a785edb
(cherry picked from commit 94b038bbb291431a7b39611d72f206b07e839891)
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
d23e0d6901935588f9472bd7073fea0009581e9b 16-May-2015 Dianne Hackborn <hackbod@google.com> Update power manager to track uid state like netstats.

To follow the correct semantics for when restricts due to
device idle can be applied, power manager need to know about
uid process states like net policy so that it can allow
wake locks from apps that are in the foreground.

Since this is being added to a second place, I reworked things
so that the activity manager now keeps track of per-uid process
states and allows apps to register to listen to those, rather
than having to track lower-level process states and transform
them into an overall uid state. Both net policy and power
manager use this new facility.

Change-Id: I77359164c40d0f36fe1ef296dd9f9c3062431148
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9e9e2e73c6ec7bece20268196dc89ad0c8bafad4 08-May-2015 Wojciech Staszkiewicz <staszkiewicz@google.com> Pass charset to XmlPullParser.setInput instead of null

Passing null to XmlPullParser.setInput forces it to do additional
work, which can be easily avoided if we know the charset beforehand.

bug: b/20849543

Change-Id: Iaff97be9df2d0f99d7af8f19f65934439c9658e2
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f7b795ca1a552909d333ed3f2d5ec881a8b23144 30-Apr-2015 Dianne Hackborn <hackbod@google.com> Merge "Update use of procstate for services." into mnc-dev
d69e4c1460017062e7c36be55801cb434ad19d97 24-Apr-2015 Dianne Hackborn <hackbod@google.com> Update use of procstate for services.

Now that we have a separate foreground service proc state
(above a sleeping top app), update various system services
to put their bindings into this state when appropriate.

There are two new bind flags for this -- one that just always
makes it a foreground service, another that only does it when
the device is awake (useful for things like the wallpaper).

And with all of that, tweak network policy manager to only
include apps that are at least foreground service state when
in power save and device idle modes. This will allow us to
further reduce the set of apps that have network access
(in particular not giving access to the current top app when
the screen is off), hopefully leading to even better battery
life.

Change-Id: I91d85a5c5ed64e856149e9a5d94a634a7925ec7f
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
15e47235c055495ec0ccc24768a6746a960d3a61 25-Apr-2015 Amith Yamasani <yamasani@google.com> Remove network access for idle apps

Track apps going in and out of idle in the NetworkPolicyManagerService.
Apply DROP rules in firewall controller if app is to be blacklisted
for network access.

Firewall can now be in whitelist (old) or blacklist mode. When in
blacklist, it allows all by default and we can selectively DENY
some uids.

Track app idle in UsageStats and update periodically.
Track charging/discharging states.

TODO: Check for appidle temporary parole state

Bug: 20066058
Change-Id: Ia65d7544204b3bcb78a517310ef4adcc05aac6fb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
0b4daca9ba54b7252ea8c159218391380eb00c8a 27-Apr-2015 Dianne Hackborn <hackbod@google.com> Implement user-settable power save whitelist.

The whitelist is now maintained by DeviceIdleController,
which is moving out into its own independent system service.
Network stats now queries it for the whitelist, instead of
collecting that itself.

Also did a few improvements in alarm manager -- made the
code for moving alarms out of the pending list more robust,
and fixed the debug output to always print the contents of
the pending list even if we aren't in a pending state. (That
would have helped me identify the problem much earlier.)

Change-Id: I0f7119d4c553c3af4d77b2f71246fa6e2c13c561
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9a9a1d923d476ab21624fa9c5da69e0a4daf51fc 20-Apr-2015 Stuart Scott <stuartscott@google.com> Fixing build break

Change-Id: I7ab3673627d1fda2d96a17c7e1823417fcb5cfef
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f1fb39763d87dabe254b6ed64ac8f20145035bc0 03-Apr-2015 Stuart Scott <stuartscott@google.com> Move factoryReset to service and protect.

bug:16161518
Change-Id: I02d1bbae1887c62ee426e6f03e8bc1f18c6666bf
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
f851d6dd2a15b74f63c85403afad0ed747f7311c 20-Apr-2015 Erik Kline <ek@google.com> CONNECTIVITY_ACTION is the new _IMMEDIATE

Bug: 20013379
Change-Id: I3e904dc0a0ddb30c7bb8cdb24d0762052ddc59eb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
16a16899505ec0a9ede5b76650bfb8817b3227c7 16-Apr-2015 Svet Ganov <svetoslavganov@google.com> Add OP_READ_PHONE_STATE app op - framework

The READ_PHONE_STATE permission protects PII information and
is in the Phone group. This change is adding the corrseponding
app op for gating access to the API guarded by READ_POHNE state
which will be used instead as an access control for legacy apps.

Change-Id: I2ff895a5a0e529f26ec0ad706266a30d829268ba
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
88e98dfa59e68a860d8c3b462ec03bc0b06d6b5c 23-Mar-2015 Dianne Hackborn <hackbod@google.com> More work on device idle mode.

- There is now an API for people to find out about
its state.
- Moved DeviceIdleController to be closer to the
power manager implementation, since they are
closely related.
- Job scheduler now knows about idle state and doesn't
run jobs while the device is idle.
- Battery stats now keeps track of "idling" vs "idle mode".
Idling is when we consider the device to be idle,
independent of whether we are actually in deep idle mode.
This allows us to keep track of longer-term changes
independently of cycling in and out of idle mode.
- Battery stats also now keeps track of package changes in
its daily stats.
- Small optimization to network policy manager service to
not touch uids that do not have the NETWORK permission.

Change-Id: I0b3304fb3722c78cdfdd0c1eada7369ece7cbcf9
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
79ec42e6a3c6a6f6a00ef3ad92221a5ad2149aa9 20-Mar-2015 Dianne Hackborn <hackbod@google.com> Merge "First stab at device idle mode."
8ad2af7e5b28f71e7bdfc1bb097937c6c1d22b18 18-Mar-2015 Dianne Hackborn <hackbod@google.com> First stab at device idle mode.

Introduce a new device idle controller service that
monitor's the device state and determines when to go
in to idle mode. When in idle mode, all we do right
now is turn off network access the same as we do for
power save mode. Many more things should come in the
future -- stopping the alarm manager from scheduling
(most) alarms, telling GmsCore for it to stop doing
stuff, etc.

Battery stats now has state tracking for devie idle
mode, as well as events for the reasons we can come
out of idle mode (significant motion or the device
becoming active). Also added new events noting when
packages are installed.

Renamed the "low power" event in battery stats to
"power save" because the former was just way too
confusing.

Finally, fix buffer size reading kernel wake locks.
(Stupidly, just increasing the buffer size. Ideally
we should try to be smarter and grow our buffer to
fit the data available, but I'll leave that for
another time.)

Change-Id: I0be2062466c83ee9d890c6cf5a228d9cc4090eca
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
4a357cd2e55293402d7172766f7f9419815fc1e8 19-Mar-2015 Alan Viverette <alanv@google.com> Replace usages of deprecated Resources.getColor() and getColorStateList()

Change-Id: I8f64fe6c4c44a92ff6d07250223ba590a1d691b0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
08c7116ab9cd04ad6dd3c04aa1017237e7f409ac 28-Feb-2015 John Spurlock <jspurlock@google.com> Remove unused imports in frameworks/base.

Change-Id: I031443de83f93eb57a98863001826671b18f3b17
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
3256601f5e4d94713f59e97b9d4912875c1bdcaf 03-Dec-2014 Jeff Sharkey <jsharkey@android.com> Offer to "merge" subscribers for data usage.

There are some cases where multiple subscriber identities (IMSI)
should be treated as "merged together" from a data usage
perspective. This is done by extending the template used for
matching purposes to support multiple subscribers.

Then, when we query historical usage or set network policies, we
normalize the matching template to merge to any other identities
that should be included. When normalizing, the "lowest" identity
is always used for equality and storage purposes, which allows
identities to come and go over time.

This change also fixes data usage recording for multi-SIM devices
by passing along the concrete subscriber identity for each network
interface. Also correctly create default policies for multi-SIM
devices. This change also drops setPolicyDataEnable() until it can
be wired up to the right underlying NetworkAgent. (This means we
still bring up the network, and then rely on iptables rules to block
traffic when over the limit, instead of proactively disabling the
connection.)

Bug: 18012787
Change-Id: If6acf32009fdfea2b836f5aff8e2f3e5e0248b4a
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
92d141fbc2f6bfc85dca1056e40512cba6d1dddb 11-Sep-2014 Vinit Deshpande <vinitd@google.com> Don't require CONNECTIVITY_INTERNAL check for protected broadcasts

This change removes requirement that sender has this permission for
protected broadcasts (since they can only come from framework)

Bug: 17409667

Change-Id: I3431c20a4ed28b3ba2bfc3cf53772e63a3424a2c
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
255dd04271088590fedc46c8e22b2fd4ab142d39 19-Aug-2014 Selim Cinek <cinek@google.com> Added notification color to all system notifications

Bug: 17128331
Change-Id: I81a94510ef51b99916f314c0dd65852426a1fbeb
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9da2f1e007268a42248a1778aa9b560e3ce3b704 14-Aug-2014 Jeff Sharkey <jsharkey@android.com> Some networks may have null ifaces, I guess?

Seems like a bug further upstream, but guard ourselves against it
for now.

Bug: 17033841
Change-Id: Id0e5895c3389b63d4e34b1fb064efef8b72cd609
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
eb2c2c790c4b86c9c09245e0b87a38972713434a 12-Aug-2014 Jeff Sharkey <jsharkey@android.com> Hack and ship: NetworkStats edition.

Some devices use clatd for catching raw IPv4 traffic when running on
a pure-IPv6 carrier network. In those situations, the per-UID
stats are accounted against the clat iface, so framework users need
to combine both the "base" and "stacked" iface usage together.

This also means that policy rules (like restricting background data
or battery saver) need to apply to the stacked ifaces.

Finally, we need to massage stats data slightly:

-- Currently xt_qtaguid double-counts the clatd traffic *leaving*
the device; both against the original UID on the clat iface, and
against UID 0 on the final egress interface.

-- All clatd traffic *arriving* at the device is missing the extra
IPv6 packet header overhead when accounted against the final UID.

Bug: 12249687, 15459248, 16296564
Change-Id: I0ee59d96831f52782de7a980e4cce9b061902fff
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
aedebda776ee3a68eda498a79bf14855e5f5d62e 14-Jul-2014 John Spurlock <jspurlock@google.com> Update data limit notification icon + strings.

- Use a more specific disabled cellular data icon.
- Reword framework strings to use "Cellular" instead of "Mobile"
and avoid "enabled/disabled"
- Update SystemUI's network limit dialog to follow suit.

Bug:16185238
Change-Id: Ie9191990fab17cd7dc04395fad39f84bfff8349d
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
497175beffe26336c092ee11a67b90f79dcdaca7 01-Jul-2014 Dianne Hackborn <hackbod@google.com> Rework network stats to use proc state for fg/bg.

Switch to using the process state to determine whether a
process should be foreground or background, instead of the
boolean foreground given by the activity manager.

This is for battery save mode, where we can now allow more apps
to havenetwork access: everything whose process state is at
least IMPORTANT_FOREGROUND, which allows music playback
and other use-visible things to continue to have network
access.

Note this also impact the traditional background data disabled
state, where now we allow anything top or better to have
network access. This automatically includes all persistent
processes, the current top activity, and any other processes
hosting the top activity or being used by the top activity.
So it broadens the set of apps that get network access, but I
think this increases it to a reasonable set of things that may
actually be needed for the foreground app to work correctly.

Change-Id: Icb609a2cea280dc3fa3e83417f478ed77f3685aa
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
be7c50e0a14e91330ce13161bc14a33d34ff6aca 30-Jun-2014 Dianne Hackborn <hackbod@google.com> Add network access blocking when in battery save mode.

The network policy manager now monitors battery save mode and,
when in battery save, uses its facility to block access to metered
networks to block access to all networks. That is, it tells the
network management service that all networks have an (infinite)
quota, and puts various app uids to be restricted under quota
interfaces as appropriate.

This new network blocking needs a new facility to be able to white
list apps, such as GmsCore. To do this, I refactored the package
manager's permission configuration stuff into a separate SystemConfig
class that can be used by others, and it now has a new tag to
specify package names that should be white-listed for power save
mode. These are retrieved by the network policy manager and used
to build a whitelist of uids.

The new general config files can now go in system/etc/config,
though currently everything still remains in the permissions dir.

Still left to be done is changing the semantics of what uids are
allowed in this mode, to include all perceptable uids. (So that we
can still do things like background music playback.) This will be
done in a follow-on CL.

Change-Id: I9bb7029f61dae62e6236da5ca60765439f8d76d2
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
684bf34ee8acc41931fac23762b13e14a22011db 30-Apr-2014 Dianne Hackborn <hackbod@google.com> Switch IProcessObserver to report process state

When IProcessObserver was created, the only information
we had for the state of a process was its "importance".
Now we have the process state, which is much more useful.
Switch to reporting that.

Change-Id: Icdb3eea8cf96f4eff7ed3d584f940a1bd9cc3884
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
037c33eae74bee2774897d969d48947f9abe254f 09-Apr-2014 Jeff Brown <jeffbrown@google.com> Plumb display power state through display manager.

Declare a new method, Display.getState() to retrieve the actual
power state of a display.

Improved documentation for Intent.ACTION_SCREEN_ON and
Intent.ACTION_SCREEN_OFF to clarify what they really mean in
terms of the interactive state of the device.

Deprecated PowerManager.isScreenOn() and replaced it with
PowerManager.isInteractive() with a more suggestive name and
better documentation.

Redirect display power state changes to go through the display
manager first and only then head over to the power manager for
legacy compatibility.

Eliminated the bright here and woke here policy flags since they
were unused. Simplified the input dispatch policy somewhat.

Ensure that screen wake locks are respected up until the point
when dozing really begins.

Fixed a regression in DreamService where onDreamingStarted
might be called before onWindowAttached.

Bug: 13133142
Bug: 13472578
Bug: 13929355
Bug: 13760290
Change-Id: Iabef96921dd554ce3768fb18619cefc3230b5fb0
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
9158825f9c41869689d6b1786d7c7aa8bdd524ce 22-Nov-2013 Amith Yamasani <yamasani@google.com> Move some system services to separate directories

Refactored the directory structure so that services can be optionally
excluded. This is step 1. Will be followed by another change that makes
it possible to remove services from the build.

Change-Id: Ideacedfd34b5e213217ad3ff4ebb21c4a8e73f85
/frameworks/base/services/core/java/com/android/server/net/NetworkPolicyManagerService.java