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 android.apphibernation; 18 19 import android.annotation.NonNull; 20 import android.annotation.RequiresPermission; 21 import android.annotation.SystemApi; 22 import android.annotation.SystemService; 23 import android.content.Context; 24 import android.os.RemoteException; 25 import android.os.ServiceManager; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Set; 31 32 /** 33 * This class provides an API surface for system apps to manipulate the app hibernation 34 * state of a package for the user provided in the context. 35 * @hide 36 */ 37 @SystemApi 38 @SystemService(Context.APP_HIBERNATION_SERVICE) 39 public class AppHibernationManager { 40 private static final String TAG = "AppHibernationManager"; 41 private final Context mContext; 42 private final IAppHibernationService mIAppHibernationService; 43 44 /** 45 * Creates a new instance. 46 * 47 * @param context The current context associated with the user 48 * 49 * @hide 50 */ AppHibernationManager(@onNull Context context)51 public AppHibernationManager(@NonNull Context context) { 52 mContext = context; 53 mIAppHibernationService = IAppHibernationService.Stub.asInterface( 54 ServiceManager.getService(Context.APP_HIBERNATION_SERVICE)); 55 } 56 57 /** 58 * Returns true if the package is hibernating for this context's user, false otherwise. 59 * 60 * @hide 61 */ 62 @SystemApi 63 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) isHibernatingForUser(@onNull String packageName)64 public boolean isHibernatingForUser(@NonNull String packageName) { 65 try { 66 return mIAppHibernationService.isHibernatingForUser(packageName, mContext.getUserId()); 67 } catch (RemoteException e) { 68 throw e.rethrowFromSystemServer(); 69 } 70 } 71 72 /** 73 * Set whether the package is hibernating for this context's user. 74 * 75 * @hide 76 */ 77 @SystemApi 78 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) setHibernatingForUser(@onNull String packageName, boolean isHibernating)79 public void setHibernatingForUser(@NonNull String packageName, boolean isHibernating) { 80 try { 81 mIAppHibernationService.setHibernatingForUser(packageName, mContext.getUserId(), 82 isHibernating); 83 } catch (RemoteException e) { 84 throw e.rethrowFromSystemServer(); 85 } 86 } 87 88 /** 89 * Returns true if app is hibernating globally / at the package level. 90 * 91 * @hide 92 */ 93 @SystemApi 94 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) isHibernatingGlobally(@onNull String packageName)95 public boolean isHibernatingGlobally(@NonNull String packageName) { 96 try { 97 return mIAppHibernationService.isHibernatingGlobally(packageName); 98 } catch (RemoteException e) { 99 throw e.rethrowFromSystemServer(); 100 } 101 } 102 103 /** 104 * Set whether a package should be globally hibernating. This hibernates the package at a 105 * package level. User-level hibernation (e.g.. {@link #isHibernatingForUser} is independent 106 * from global hibernation. 107 * 108 * @hide 109 */ 110 @SystemApi 111 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) setHibernatingGlobally(@onNull String packageName, boolean isHibernating)112 public void setHibernatingGlobally(@NonNull String packageName, boolean isHibernating) { 113 try { 114 mIAppHibernationService.setHibernatingGlobally(packageName, isHibernating); 115 } catch (RemoteException e) { 116 throw e.rethrowFromSystemServer(); 117 } 118 } 119 120 /** 121 * Get the hibernating packages for the user. This is equivalent to the list of packages for 122 * the user that return true for {@link #isHibernatingForUser}. 123 * 124 * @hide 125 */ 126 @SystemApi 127 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) getHibernatingPackagesForUser()128 public @NonNull List<String> getHibernatingPackagesForUser() { 129 try { 130 return mIAppHibernationService.getHibernatingPackagesForUser(mContext.getUserId()); 131 } catch (RemoteException e) { 132 throw e.rethrowFromSystemServer(); 133 } 134 } 135 136 /** 137 * Returns the stats from app hibernation for each package provided. 138 * 139 * @param packageNames the set of packages to return stats for 140 * @hide 141 */ 142 @SystemApi 143 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) getHibernationStatsForUser( @onNull Set<String> packageNames)144 public @NonNull Map<String, HibernationStats> getHibernationStatsForUser( 145 @NonNull Set<String> packageNames) { 146 try { 147 return mIAppHibernationService.getHibernationStatsForUser( 148 new ArrayList(packageNames), mContext.getUserId()); 149 } catch (RemoteException e) { 150 throw e.rethrowFromSystemServer(); 151 } 152 } 153 154 /** 155 * Returns the stats from app hibernation for all packages for the user 156 * 157 * @hide 158 */ 159 @SystemApi 160 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) getHibernationStatsForUser()161 public @NonNull Map<String, HibernationStats> getHibernationStatsForUser() { 162 try { 163 return mIAppHibernationService.getHibernationStatsForUser( 164 null /* packageNames */, mContext.getUserId()); 165 } catch (RemoteException e) { 166 throw e.rethrowFromSystemServer(); 167 } 168 } 169 170 /** 171 * Whether global hibernation should delete ART ahead-of-time compilation artifacts 172 * and prevent package manager from re-optimizing the APK. 173 * @hide 174 */ 175 @SystemApi 176 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) isOatArtifactDeletionEnabled()177 public boolean isOatArtifactDeletionEnabled() { 178 try { 179 return mIAppHibernationService.isOatArtifactDeletionEnabled(); 180 } catch (RemoteException e) { 181 throw e.rethrowFromSystemServer(); 182 } 183 } 184 } 185