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.List; 28 29 /** 30 * This class provides an API surface for system apps to manipulate the app hibernation 31 * state of a package for the user provided in the context. 32 * @hide 33 */ 34 @SystemApi 35 @SystemService(Context.APP_HIBERNATION_SERVICE) 36 public class AppHibernationManager { 37 private static final String TAG = "AppHibernationManager"; 38 private final Context mContext; 39 private final IAppHibernationService mIAppHibernationService; 40 41 /** 42 * Creates a new instance. 43 * 44 * @param context The current context associated with the user 45 * 46 * @hide 47 */ AppHibernationManager(@onNull Context context)48 public AppHibernationManager(@NonNull Context context) { 49 mContext = context; 50 mIAppHibernationService = IAppHibernationService.Stub.asInterface( 51 ServiceManager.getService(Context.APP_HIBERNATION_SERVICE)); 52 } 53 54 /** 55 * Returns true if the package is hibernating for this context's user, false otherwise. 56 * 57 * @hide 58 */ 59 @SystemApi 60 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) isHibernatingForUser(@onNull String packageName)61 public boolean isHibernatingForUser(@NonNull String packageName) { 62 try { 63 return mIAppHibernationService.isHibernatingForUser(packageName, mContext.getUserId()); 64 } catch (RemoteException e) { 65 throw e.rethrowFromSystemServer(); 66 } 67 } 68 69 /** 70 * Set whether the package is hibernating for this context's user. 71 * 72 * @hide 73 */ 74 @SystemApi 75 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) setHibernatingForUser(@onNull String packageName, boolean isHibernating)76 public void setHibernatingForUser(@NonNull String packageName, boolean isHibernating) { 77 try { 78 mIAppHibernationService.setHibernatingForUser(packageName, mContext.getUserId(), 79 isHibernating); 80 } catch (RemoteException e) { 81 throw e.rethrowFromSystemServer(); 82 } 83 } 84 85 /** 86 * Returns true if app is hibernating globally / at the package level. 87 * 88 * @hide 89 */ 90 @SystemApi 91 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) isHibernatingGlobally(@onNull String packageName)92 public boolean isHibernatingGlobally(@NonNull String packageName) { 93 try { 94 return mIAppHibernationService.isHibernatingGlobally(packageName); 95 } catch (RemoteException e) { 96 throw e.rethrowFromSystemServer(); 97 } 98 } 99 100 /** 101 * Set whether a package should be globally hibernating. This hibernates the package at a 102 * package level. User-level hibernation (e.g.. {@link #isHibernatingForUser} is independent 103 * from global hibernation. 104 * 105 * @hide 106 */ 107 @SystemApi 108 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) setHibernatingGlobally(@onNull String packageName, boolean isHibernating)109 public void setHibernatingGlobally(@NonNull String packageName, boolean isHibernating) { 110 try { 111 mIAppHibernationService.setHibernatingGlobally(packageName, isHibernating); 112 } catch (RemoteException e) { 113 throw e.rethrowFromSystemServer(); 114 } 115 } 116 117 /** 118 * Get the hibernating packages for the user. This is equivalent to the list of packages for 119 * the user that return true for {@link #isHibernatingForUser}. 120 * 121 * @hide 122 */ 123 @SystemApi 124 @RequiresPermission(value = android.Manifest.permission.MANAGE_APP_HIBERNATION) getHibernatingPackagesForUser()125 public @NonNull List<String> getHibernatingPackagesForUser() { 126 try { 127 return mIAppHibernationService.getHibernatingPackagesForUser(mContext.getUserId()); 128 } catch (RemoteException e) { 129 throw e.rethrowFromSystemServer(); 130 } 131 } 132 } 133