1 /*
<lambda>null2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.permission.data
18 
19 import android.app.Application
20 import android.app.usage.UsageStats
21 import android.app.usage.UsageStatsManager
22 import android.app.usage.UsageStatsManager.INTERVAL_MONTHLY
23 import android.os.UserHandle
24 import com.android.permissioncontroller.PermissionControllerApplication
25 import com.android.permissioncontroller.permission.utils.Utils
26 import kotlinx.coroutines.Job
27 
28 /**
29  * A livedata which tracks the usage stats for all packages for all users in a given length of time.
30  *
31  * @param app The current application
32  * @param searchTimeMs The length of time, in milliseconds, that this LiveData will track. The time
33  *   will start when the liveData is loaded, and extend backwards searchTimeMs milliseconds.
34  * @param interval The interval to measure in. Default is monthly.
35  */
36 class UsageStatsLiveData
37 private constructor(
38     private val app: Application,
39     private val searchTimeMs: Long,
40     private val interval: Int = INTERVAL_MONTHLY
41 ) : SmartAsyncMediatorLiveData<Map<UserHandle, List<UsageStats>>>() {
42 
43     init {
44         addSource(UsersLiveData) { update() }
45     }
46 
47     override suspend fun loadDataAndPostValue(job: Job) {
48         if (!UsersLiveData.isInitialized) {
49             return
50         }
51 
52         val now = System.currentTimeMillis()
53         val userMap = mutableMapOf<UserHandle, List<UsageStats>>()
54         for (user in UsersLiveData.value!!) {
55             // If the user is not enabled, or if the user is a managed profile, and this is not an
56             // android TV (where parental control accounts are managed profiles), do not get stats.
57             if (Utils.isUserDisabledOrWorkProfile(user)) {
58                 continue
59             }
60             val statsManager =
61                 Utils.getUserContext(app, user).getSystemService(UsageStatsManager::class.java)!!
62             statsManager.queryUsageStats(interval, now - searchTimeMs, now)?.let { stats ->
63                 userMap[user] = stats
64             }
65         }
66 
67         postValue(userMap)
68     }
69 
70     companion object : DataRepository<Pair<Long, Int>, UsageStatsLiveData>() {
71         override fun newValue(key: Pair<Long, Int>): UsageStatsLiveData {
72             return UsageStatsLiveData(PermissionControllerApplication.get(), key.first, key.second)
73         }
74 
75         operator fun get(interval: Long): UsageStatsLiveData {
76             return get(interval to INTERVAL_MONTHLY)
77         }
78     }
79 }
80