Commit Graph

290 Commits

Author SHA1 Message Date
Varun Shah
9c6f72bae3 Removed AppUsageLimit#isGroupLimit API.
Bug: 123354775
Test: atest FrameworksServicesTests:AppTimeLimitControllerTests
Change-Id: I7082aaac0071be8d5bc444c6f8521576bf513cff
2019-01-25 21:13:56 -08:00
Varun Shah
2546cef56c Added APIs for App Usage Limits.
Added a new AppUsageLimit group observer which follows the same pattern as
other UsageGroups. This specific observer allows the launcher to query
for the AppUsageLimit, available via the new LauncherApps API below. The
observer can be registered and unregistered via the respective new APIs in
UsageStats.

LauncherApps has a new API which allows it to get the AppUsageLimit for
a specified package and user, initally set via the API in UsageStats.
This new API allows the launcher to query specifics about the limit such
as how much usage time the limit has, and how much total usage time is
remaining.

Bug: 117409586
Test: atest FrameworksServicesTests:AppTimeLimitControllerTests
Test: atest android.app.usage.cts.UsageStatsTest#testObserveUsagePermissionForRegisterObserver
Test: atest android.app.usage.cts.UsageStatsTest#testObserveUsagePermissionForUnregisterObserver
Test: manual (mmma frameworks/base/tests/UsageStatsTest/)
Change-Id: Ifaffab629409e9191e40404a949c8df70bd3f7cb
2019-01-23 20:30:25 -08:00
Michael Wachenschwanz
0b4ab1f171 Add Task Root package info to UsageEvent
Bug: 113094946
Test: manual (use "adb shell dumpsys usagestats apptimelimit" to verify
apps at the root of tasks are considered active)
Test: atest cts/tests/tests/app.usage/src/android/app/usage/cts/UsageStatsTest.java#testTaskRootEventField
Test: atest cts/tests/tests/app.usage/src/android/app/usage/cts/UsageStatsTest.java#testUsageSourceAttribution
Test: atest UsageStatsDatabaseTest

Change-Id: I40f86743d33c13892de0e59ae02c9ebddb606ee7
2019-01-22 22:19:21 -08:00
Hui Yu
b1d243a51b UsageStats DEVICE_SHUTDOWN event.
DEVICE_SHUTDOWN event is used to close all open usage events that do
not have matching closing event when device is shut down. For example,
ACTIVITY_RESUMED or FOREGROUND_SERVICE_START are open events, the
DEVICE_SHUTDOWN event will close the usage session of the open events.

At orderly shutdown like selecting Power Off or Restart after pressing
power button, a DEVICE_SHUTDOWN event is sent to UsageStats.
UsageStats persists UsageStatsDatabase to disk immediately.

When power button is pressed for 3.5 seconds (configured by
config_veryLongPressTimeout in config.xml). A DEVICE_SHUTDOWN
event is sent to UsageStats. UsageStats persists UsageStatsDatabase
to disk immediately.

This is the mechanism that we do not lose UsageStats data when the
device is shut down.

When the device boots up, if the last event is not
DEVICE_SHUTDOWN, we add a DEVICE_SHUTDOWN with timestamp set to be the last
time database file is persisted. This is to handle the case device
shutdown abruptly due to power drained or cold temperature.

Bug: 111464278
Test: atest UsageStatsTest.java
Change-Id: I1e88063ba71d09042d02c6deb9f07d8581a15c30
2019-01-16 11:13:58 -08:00
Michael Wachenschwanz
266dd3bfd7 Merge "Add Usage Reporting Api to UsageStatsManager" 2019-01-08 06:50:24 +00:00
Michael Wachenschwanz
36778525ba Add Usage Reporting Api to UsageStatsManager
The Usage Reporting Api allows apps to report usage within the app to
platform. Apps with the the OBSERVE_APP_USAGE permission may register
observers that use the reported in-app usage.

Test: manual (using the included Usage Reporter App)
Test: atest CtsUsageStatsTestCases:UsageReportingTest
Test: atest FrameworksServicesTests:AppTimeLimitControllerTests
Bug: 112486938

Change-Id: Iddd6f0993bbbf68a2032b34d473ef8d67da7747a
2019-01-07 14:56:06 -08:00
Hui Yu
63c0ad4dbb Reduce UsageStatsXmlV1 log messages.
When parsing newly added fields from Xml database, must catch
the IOException because the new fields do not exist in old Xml database.
This is not really an error. Change Log.e to Log.i, do not print the
full stack.

Change-Id: I5ba86790f7c8eadf4f5dd20d28f9348a23201f87
Fix: 121085733
Test: atest UsageStatsDatabaseTest.java
2018-12-17 10:13:17 -08:00
Hui Yu
03d1240ef3 UsageStats tracking of screen usage.
1. Add UsageStats Event types:
ACTIVITY_RESUMED is synonym to existing MOVE_TO_FOREGROUND.
ACTIVITY_PAUSED is synonym to existing MOVE_TO_BACKGROUND.
ACTIVITY_STOPPED when an activity becomes invisible on the UI.
2. In UsageStats.java, add API getLastTimeVisible() to report last time the
app is visible (ACTIVITY_RESUMED or ACTIVITY_PAUSED), add API getTotalTimeVisible()
to report total time the app is visible.
The existing API getLastTimeUsed() can report last time the app is in
foreground (AKA have focus).
The existing API getTotalTimeInForeground() can report total time the
app is in foreground (AKA have focus).
3. UsageStats.getTotalTimeVisible() can report screen usage for
split-screen mode and picture-in-picture mode.
4. Because in the same package, activity can be instantiated multiple times,
In UsageEvents.Event class, add a member mInstaceId for activity's
instance ID, add interface getInstanceId() to retrieve the instance ID.

Bug: 112002260
Test: frameworks/base/services/tests/servicestests/src/com/android/server/usage/UsageStatsDatabaseTest.java
atest frameworks/base/core/tests/coretests/src/android/app/usage/UsageStatsTest.java

Change-Id: Ibcef2488e9620804c9f9220b027f976e8fa0c98b
2018-12-12 10:07:14 -08:00
TreeHugger Robot
1ca78d6198 Merge "Catch IOException when failed to parse new fields." 2018-11-27 01:00:35 +00:00
Hui Yu
746b82aa9d Catch IOException when failed to parse new fields.
When parsing from a XML file written by previous code, the new fields
will get an IOException, catch the IOException and let the new fields
to have default value.

Change-Id: I2b2a494bf17aaf1383a6cc469d9c2addd6cac9c3
Fix: 119822293
Test: test com.google.android.gts.backup.UsageStatsRestoreHostSideTest#testUsageStatsRestore
2018-11-26 22:40:43 +00:00
Hui Yu
7e5bf5c54f Do not update AppStandbyController on foreground service events
Change-Id: I3ce067c9e7aae50aaf9784f1a63618c6cbaea2cc
Fix: 119781710
Test: atest HostsideRestrictBackgroundNetworkTests#testAppIdleMetered_enabled
2018-11-26 11:14:40 -08:00
Esteban Talavera
917a71d4f9 Expand notification generates USER_INTERACTION event
Bug: 118376048
Test: runtest systemui-notification
Change-Id: Idbfac239783aa6371bdef4127125f30c27b74e3d
2018-11-15 10:46:08 +00:00
Hui Yu
81f977753a Merge "Add UsageStats events for foreground service start/stop." 2018-11-13 18:16:19 +00:00
TreeHugger Robot
a66a53194c Merge "Add null checks to removing AppTimeLimit observers" 2018-11-13 03:29:21 +00:00
Hui Yu
e361a23bba Add UsageStats events for foreground service start/stop.
1. Send FOREGROUND_SERVICE_START event when foreground service starts.
Send FOREGROUND_SERVICE_STOP event when foreground service stops.
2. One app can multiple foreground services and multiple services can be
started. Because this, in UsageStats, change mLastForegroundEvent to
className to event map, do this for both activity and foreground
service. Change UsageStatsProto and UsageStatsXmlV1 to support this
change.
3. Add more test cases in UsageStatsTest.java.

Test: start music player which is foreground service, observce these
two events when start play and pause play.

Change-Id: I3dc14f5b73cc114a53b8c51f90d3011d9ace35ac
Bug: 112002260
Test: atest UsageStatsTest#testForegroundService
atest frameworks/base/services/tests/servicestests/src/com/android/server/usage/UsageStatsDatabaseTest.java
atest frameworks/base/core/tests/coretests/src/android/app/usage/UsageStatsTest.java
2018-11-12 17:41:03 -08:00
Michael Wachenschwanz
ae9811d44f Add null checks to removing AppTimeLimit observers
Also some minor touch up

Test: atest FrameworksServicesTests:AppTimeLimitControllerTests
Change-Id: Ibcecb483bc28ba6b1dcd0158e946137abb0e54e4
Fixes: 119371427
2018-11-12 16:55:34 -08:00
Kweku Adams
1e8947c5da Informing app idle listeners on enabled state changes.
Currently, if AppStandbyController is enabled after some listeners have
already registered, the listeners will be told that the system is in a
state of parole even though AppStandbyController thinks it's not in
parole. This change informs AppIdleStateChangeListeners when
AppStandbyController's enabled state changes as well so that they can be
in the correct state.

I also removed the call to setAppIdleEnabled() in onBootPhase() since
updateSettings() is called right afterwards and that also calls
setAppIdleEnabled().

Bug: 112329453
Test: atest AppStandbyControllerTests
also check logs to confirm that NetworkPolicyManagerService and
JobSchedulerService's listeners switch from ON to OFF when the system is
ready.

Change-Id: I88bc293c70b459f54f75f92126ad306d6ab8d9b7
2018-11-08 13:58:50 -08:00
Risan
5287e1fe48 Merge "isReservedSupported=true when running as container" am: e0076e4145 am: a228f69e07
am: d9231a0984

Change-Id: Id2f4862faa57099d6b15f06567bdb53fd2f33b03
2018-10-31 14:08:54 -07:00
Risan
12adf3a985 isReservedSupported=true when running as container
Test: Remove first_sdk_check from StorageHostTest#testVerify, log
Build.IS_CONTAINER inside isReservedSupported function, run on sailfish.
It shows that Build.IS_CONTAINER=false and the test still passes (i.e.,
isReservedSupported is correctly true).
Also checked that Build.IS_CONTAINER in isReservedSupported is true in
ARC++.
Bug: 111287848

Change-Id: I9af1f6cce5bab75ef941989bf279cf40c52e62b0
2018-10-29 20:43:43 -06:00
Michael Wachenschwanz
0f47284af3 Refactor AppTimeLimitController for Session Observers
Introducing the concept of Usage Session Observers to UsageStats. A
session observer monitors usage within individual "continuous" sessions
(brief gaps of non usage may be allowed in a session and still be
considered continuous)

The new session observer in AppTimeLimitController are both similar and
different enough from the current app usage observer to warrant
refactoring TimeLimitGroup into an OOP friendly abstract base class.

Added some Observer App handling to avoid clash between registered
observers from multiple apps.

Reworded packages to observed and usage entities to accomodate future
changes, where usage may come from more than just app usage.

Reworded moveToForeground/Background to generic usage and allow multiple
usage entities to be active at the same time to accomodate future
changes, where more than just the foreground app can be considered used.

Test: atest FrameworksServicesTests:AppTimeLimitControllerTests
Bug: 111465038
Change-Id: I63aebf8b0aa5516111bd6d5e142525d0bee6ef58
2018-10-29 16:21:24 -07:00
Michael Wachenschwanz
0e1ce140a2 Merge changes from topic "UsageStatsDatabaseUpgradeAttempt2"
* changes:
  Add ProtoInputStream and UsageStatsProto to art profiles
  Upgrade UsageStatsDatabase to version 4 (attempt 2)
  Move UsageStatsDatabase upgrade backup to seperate folder
2018-10-24 02:17:00 +00:00
Varun Shah
7609b7535f Prevents uninstalled packages from being added to the DB.
Apps trying to set the standby bucket for a package that was uninstalled
will now be blocked to prevent uninstalled package entries from
appearing in the App Standby DB.

Bug: 116190365
Test: atest com.android.server.usage.AppIdleHistoryTests
Test: atest com.android.server.usage.AppStandbyControllerTests
Test: atest cts/tests/tests/app.usage/src/android/app/usage/cts/UsageStatsTest.java
Test: atest vendor/xts/gts-tests/tests/usagestats/src/com/google/android/usagestats/gts/UsageStatsTests.java
Change-Id: If421c46f3459eced78ae260e88749022ed5cf7a2
2018-10-19 16:48:55 -07:00
Michael Wachenschwanz
d083f6b875 Upgrade UsageStatsDatabase to version 4 (attempt 2)
If version 4 causes any regression, this CL should be reverted.
The other UsageStatsDatabase changes are not safe to revert.

Bug: 111422946
Test: atest UsageStatsDatabase
Change-Id: I8af08a25c596397ec319d457bfa5c51b73beb4df
2018-10-12 14:09:31 -07:00
Michael Wachenschwanz
dc46632382 Move UsageStatsDatabase upgrade backup to seperate folder
During a UsageStatsDatabase version upgrade, move backup files to a
seperate folder to reduce the chance of accidentally picking up the
backup file.

Change-Id: I0052cb25388703c328cd7160f523d8542cf424d7
Fixes: 117224541
Test: atest UsageStatsDatabaseTest
2018-10-12 14:09:31 -07:00
Michael Wachenschwanz
3efa16d29f Revert UsageStatsDatabase to version 3
Change-Id: I650caeaed954ff2d0921b88a000c8f2be37df078
Fixes: 117177943
Bug: 117224541
Test: run google/template/local --template:map test
google/continuous/boot-successive  --no-first-boot --successive-boot
--boot-count 2 --granular-boot-info  -f
SystemServerTiming_StartUsageService    (See go/run-boottest)
2018-10-03 18:52:36 +00:00
Michael Wachenschwanz
c90bc15dc8 Pool Package and Class names when writing UsageStats to disk
Write the package and class names at the top of the proto file and
replace each instance of those strings in the rest of the protobuf with
the index of said string.

Some rough number on the impact of this change plus the previous proto
change:
File size on disk reduces to ~13% of XML file size!!!
File read time reduces to ~32-45% of XML read time!
File write time clock at around ~102-107% of XML write time.

Bug: 111422946
Fixes: 111449191
Test: atest UsageStatsDatabaseTest
Change-Id: I6bcce54a2431a964bda2c03bd3be1f3d4b4156e1
2018-09-28 16:48:35 -07:00
Michael Wachenschwanz
c8c26365a4 Upgrade UsageStatsDatabase from XML to Protobuf
Add the relevant methods to read from ProtoInputStream to
various classes.

Also add some framework to handle version changes in
UsageStatsDatabase. There is some risk of users losing all their current
UsageStats data, if something goes horribly wrong. The debug flag and a
keep backup files flag are temporarily set in UsageStatsDatabase with
this change. They will both be unset in the future before the Q release.

Some rough number on the impact of this change:
Proto file size on disk reduces to ~47% of XML file size :)
Proto file read time reduces to ~55% of XML file read :)
Proto file write time increases ~17% over the XML file write :(

There will be a follow up CL to address the file write time regression

Bug: 111422946
Fixes: 111449927
Test: atest UsageStatsDatabaseTest
Change-Id: I084aea796ed2163c42947d52396a36cc7c5562a2
2018-09-28 16:48:23 -07:00
Andreas Gampe
8ce7ed95a0 Frameworks: Annotate trivial @GuardedBy in services
Add @GuardedBy for simple functions that require locks and have a name in
one of the frameworks naming styles for locks ("^.*(Locked|LPw|LPr|L[a-zA-Z]|UL|AL|NL)$").

Derived by errorprone.

Bug: 73000847
Test: m
Change-Id: If70bb03313388af34d547efca20fb5115de95bf1
2018-09-05 17:02:08 -07:00
Michael Wachenschwanz
ddcac2c78d Set AppStandbyController charging state on init
am: 2795cca1cf

Change-Id: I67b2d6bd5a6f1a5bbc5f8c6e70d6d6516d67a791
2018-08-31 17:22:07 -07:00
Amith Yamasani
8b7725b77a Show the diff instead of current time for last parole time.
So the time shown will be how long ago the last parole happened.
This is easier to read than when the last parole happened, in
time since epoch.

... mChargingStable=false mLastAppIdleParoledTime=+15s666ms


Bug: 112880101
Test: adb shell dumpsys usagestats
Change-Id: Ieddf752e811a95a95e63d621e9cd9f26957b51a8
2018-08-29 21:20:00 +00:00
Michael Wachenschwanz
2795cca1cf Set AppStandbyController charging state on init
Test: manual (restart device and "adb shell dumpsys usagestats | grep
mCharging=")
Test: CtsHostsideNetworkTests
Fixes:80545083

Change-Id: I0592622b83525159eeca611b3cc1021347bc53ca
Merged-In: I0592622b83525159eeca611b3cc1021347bc53ca
(cherry-picked from 13b1e1774c)
(cherry picked from commit 4d5b4b03c8065fc6421456a6135f6d247d2d1dd5)
2018-08-29 15:00:30 +08:00
Jeff Sharkey
5518cf584b No permissions needed for static disk attributes.
Detecting if a storage device has quota or resgid support isn't
sensitive, so we're okay letting anyone ask about it.

Bug: 112175169
Test: atest android.appsecurity.cts.StorageHostTest#testFullDisk
Change-Id: I1e45eeade0d0d8ea242afca274b45a68972e6b57
2018-08-24 17:38:02 -06:00
TreeHugger Robot
06ea043275 Merge "Add missing app standby parameters to controller dump" 2018-08-08 23:50:42 +00:00
TreeHugger Robot
8c8a9a1e33 Merge "Add null check for LastEvent in UserUsageStatsService" 2018-07-20 05:50:28 +00:00
Michael Wachenschwanz
bc9abc6589 Add null check for LastEvent in UserUsageStatsService
Change-Id: Ie57233ccb23f1399234b1dbb6ae490de9595e71f
Fixes: 111630625
Test: manual
2018-07-19 12:02:19 -07:00
TreeHugger Robot
766d24e06c Merge "Log Wtf when UsageStats missing over a day of events" 2018-07-17 02:45:52 +00:00
Michael Wachenschwanz
e4c818f440 Log Wtf when UsageStats missing over a day of events
Bug: 110930764
Test: manual (hardcode future time in printLast24HrEvents)
Change-Id: Ia254f7512bfa549966dda3674140772504dbbffd
2018-07-16 16:26:32 -07:00
Amith Yamasani
a4dd6efdc1 Merge "Fix crash on calling getAppStandbyBucket()" into pi-dev am: 632c036d8c
am: 9d629c85fd

Change-Id: I9760ced42479a795c03ecefd24ed6b7597294421
2018-07-16 14:00:20 -07:00
Amith Yamasani
99e1424e0a Fix crash on calling getAppStandbyBucket()
MATCH_ANY_USER, which was locked down recently, is not
really needed in this context, so removed it.

Bug: 111145886
Test: cts CtsAppUsageHostTestCases
Change-Id: I1bb9df0c7370015365bea414599b2b83084a1846
2018-07-11 10:44:17 -07:00
Makoto Onuki
3ab7781d1f Clean up sync manager and fix the lock screen blocked sync issue.
- Fix the issue where when a sync starts when the user is locked, the sync
is enqueued and waits for the 10 minute timeout, which drains the battery.

Now, in this case, we finished the job right away, and if it's a non-periodic
sync, we ask the job scheduler to reschedule.

- Clean up all the mess and unnecessary code.

Fixes: 79433653
Test: manual: Add an account, remove it, make sure all the sync operations are
gone from dumpsys content.
Test: manual: Use the requestsync command while the user is locked and
make sure the sync won't stuck.

Change-Id: I8cfd6a80715336ebea3793b2ed10b59d90cd8e52
2018-07-10 14:11:34 -07:00
Esteban Talavera
5b79bfa763 Add missing app standby parameters to controller dump
Test: adb shell dumpsys usagestats

Change-Id: Iacbbbab8608ebd871ab9fcf56b20be51dc847d52
2018-07-05 15:15:44 +01:00
TreeHugger Robot
565aa50952 Merge "Initial UsageStatsDatabase Perf tests" 2018-07-03 21:15:15 +00:00
Beverly
f7cc6b28c1 Ignore user portion when checking for system uid
In UsageStatsService, the calling uid is considered
from the system regardeless of the curret user id

Change-Id: I5c33db37c585ac918a09ad9014bb819b1e24d7f8
Fixes: 110527624
Test: manual
2018-06-28 19:41:11 +00:00
Michael Wachenschwanz
e717e0cb4e Initial UsageStatsDatabase Perf tests
A suite of simple performance test for UsageStatsDatabase. They measure
the time it take to write and read UsageEvents to and from a file.

Bug: 110428559
Test: atest UsageStatsPerfTests
Change-Id: If1558515e1da9e22fb56bc13f8e89c10c51a1625
2018-06-21 15:59:49 -07:00
Julia Reynolds
5dc1edfe16 Fix notification interruption stats
- Write channelid to xml
- Fix issue querying packages by user

Test: manual
Bug: 79607096
Change-Id: I77b1a532d364fcf97a4dba9d83ce97b93650a88c
2018-06-14 11:16:30 -04:00
TreeHugger Robot
0ebb10d596 Merge "Set AppStandbyController charging state on init" 2018-06-08 02:27:26 +00:00
Michael Wachenschwanz
13b1e1774c Set AppStandbyController charging state on init
Test: manual (restart device and "adb shell dumpsys usagestats | grep
mCharging=")
Test: CtsHostsideNetworkTests
Fixes:80545083

Change-Id: I0592622b83525159eeca611b3cc1021347bc53ca
2018-06-07 18:33:22 +00:00
Amith Yamasani
82d3041f30 DO NOT MERGE Fix security exception due to incorrect flags
Caught by test UsageStatsTest due to stricter checks for
MATCH_ANY_USER.

Change-Id: Ie884c7007a8588a222773fba8d7967bcdf2a528a
Fixes: 109700742
Test: atest CtsUsageStatsTestCases:UsageStatsTest
2018-06-06 19:51:24 +00:00
Julia Reynolds
1b61d018ec Allow cross user usage event queries
For multipackage requests, caller must be system uid or hold
interact_across_users if not requesting fate for the same user.

For single package requests, caller must be system uid or the
same package and hold interact_across_users if not requesting data
for the current user.

In both cases the caller must have the usagestats permission.

Test: couldn't find automated tests for this file; manual
Bug: 79142791
Change-Id: I8d650f8e875e0e4578cb48c9ba42e821342852a0
2018-06-06 14:26:49 -04:00
Makoto Onuki
da4db58021 Merge "Tweak exemption for sync requests made by FG apps" into pi-dev am: 621fd20f55
am: d8cf91c5f8

Change-Id: Icac177c8d404ce20d5e894d85df2b72a001d6d9a
2018-05-24 22:14:27 -07:00