• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.hibernation
18 
19 import android.app.usage.UsageStatsManager
20 import android.apphibernation.AppHibernationManager
21 import android.content.Context
22 import android.content.Context.APP_HIBERNATION_SERVICE
23 import android.content.Context.USAGE_STATS_SERVICE
24 import android.os.Build
25 import android.os.UserHandle
26 import com.android.permissioncontroller.DumpableLog
27 import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
28 
29 /**
30  * Hibernation controller that handles modifying hibernation state.
31  */
32 class HibernationController(
33     val context: Context,
34     val unusedThreshold: Long,
35     val targetsPreS: Boolean
36 ) {
37 
38     companion object {
39         private const val LOG_TAG = "HibernationController"
40         private const val DEBUG_HIBERNATION = true
41     }
42 
43     /**
44      * Hibernates the apps provided for each user.
45      *
46      * @param apps map of each user to a list of packages that should be hibernated for the user
47      * @return list of apps that were successfully hibernated
48      */
hibernateAppsnull49     fun hibernateApps(
50         apps: Map<UserHandle, List<LightPackageInfo>>
51     ): Set<Pair<String, UserHandle>> {
52         val hibernatedApps = mutableSetOf<Pair<String, UserHandle>>()
53         for ((user, userApps) in apps) {
54             val userContext = context.createContextAsUser(user, 0 /* flags */)
55             val hibernationManager =
56                 userContext.getSystemService(APP_HIBERNATION_SERVICE) as AppHibernationManager
57             for (pkg in userApps) {
58                 try {
59                     if (hibernationManager.isHibernatingForUser(pkg.packageName)) {
60                         continue
61                     }
62                     if (!targetsPreS &&
63                         pkg.targetSdkVersion < Build.VERSION_CODES.S) {
64                         // Only apps targeting S or above can be truly hibernated.
65                         continue
66                     }
67                     hibernationManager.setHibernatingForUser(pkg.packageName, true)
68                     hibernatedApps.add(pkg.packageName to user)
69                 } catch (e: Exception) {
70                     DumpableLog.e(LOG_TAG, "Failed to hibernate package: ${pkg.packageName}", e)
71                 }
72             }
73         }
74 
75         // Globally hibernate any of the hibernated apps that are unused by any user
76         val usageStatsManager = context.getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager
77         val hibernationManager =
78             context.getSystemService(APP_HIBERNATION_SERVICE) as AppHibernationManager
79         val globallyHibernatedApps = mutableSetOf<String>()
80         for ((pkgName, _) in hibernatedApps) {
81             if (globallyHibernatedApps.contains(pkgName) ||
82                 hibernationManager.isHibernatingGlobally(pkgName)) {
83                 continue
84             }
85 
86             val now = System.currentTimeMillis()
87             val lastUsedGlobally = usageStatsManager.getLastTimeAnyComponentUsed(pkgName)
88             if (now - lastUsedGlobally < unusedThreshold) {
89                 continue
90             }
91 
92             hibernationManager.setHibernatingGlobally(pkgName, true)
93             globallyHibernatedApps.add(pkgName)
94         }
95         if (DEBUG_HIBERNATION) {
96             DumpableLog.i(LOG_TAG,
97                 "Done hibernating apps $hibernatedApps \n " +
98                 "Globally hibernating apps $globallyHibernatedApps")
99         }
100 
101         return hibernatedApps
102     }
103 }
104