Daniel Nishi | cf9d19e | 2017-01-23 14:33:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.storage; |
| 18 | |
| 19 | import android.annotation.MainThread; |
| 20 | import android.app.usage.CacheQuotaHint; |
| 21 | import android.app.usage.CacheQuotaService; |
| 22 | import android.app.usage.ICacheQuotaService; |
| 23 | import android.app.usage.UsageStats; |
| 24 | import android.app.usage.UsageStatsManager; |
| 25 | import android.app.usage.UsageStatsManagerInternal; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.ServiceConnection; |
| 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.pm.ServiceInfo; |
| 34 | import android.content.pm.UserInfo; |
| 35 | import android.os.AsyncTask; |
| 36 | import android.os.Bundle; |
| 37 | import android.os.IBinder; |
| 38 | import android.os.RemoteCallback; |
| 39 | import android.os.RemoteException; |
| 40 | import android.os.UserHandle; |
| 41 | import android.os.UserManager; |
| 42 | import android.text.format.DateUtils; |
| 43 | import android.util.Slog; |
| 44 | |
| 45 | import com.android.internal.util.Preconditions; |
| 46 | import com.android.server.pm.Installer; |
| 47 | |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.List; |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * CacheQuotaStrategy is a strategy for determining cache quotas using usage stats and foreground |
| 54 | * time using the calculation as defined in the refuel rocket. |
| 55 | */ |
| 56 | public class CacheQuotaStrategy implements RemoteCallback.OnResultListener { |
| 57 | private static final String TAG = "CacheQuotaStrategy"; |
| 58 | |
| 59 | private final Object mLock = new Object(); |
| 60 | |
| 61 | private final Context mContext; |
| 62 | private final UsageStatsManagerInternal mUsageStats; |
| 63 | private final Installer mInstaller; |
| 64 | private ServiceConnection mServiceConnection; |
| 65 | private ICacheQuotaService mRemoteService; |
| 66 | |
| 67 | public CacheQuotaStrategy( |
| 68 | Context context, UsageStatsManagerInternal usageStatsManager, Installer installer) { |
| 69 | mContext = Preconditions.checkNotNull(context); |
| 70 | mUsageStats = Preconditions.checkNotNull(usageStatsManager); |
| 71 | mInstaller = Preconditions.checkNotNull(installer); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Recalculates the quotas and stores them to installd. |
| 76 | */ |
| 77 | public void recalculateQuotas() { |
| 78 | createServiceConnection(); |
| 79 | |
| 80 | ComponentName component = getServiceComponentName(); |
| 81 | if (component != null) { |
| 82 | Intent intent = new Intent(); |
| 83 | intent.setComponent(component); |
| 84 | mContext.bindServiceAsUser( |
| 85 | intent, mServiceConnection, Context.BIND_AUTO_CREATE, UserHandle.CURRENT); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private void createServiceConnection() { |
| 90 | // If we're already connected, don't create a new connection. |
| 91 | if (mServiceConnection != null) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | mServiceConnection = new ServiceConnection() { |
| 96 | @Override |
| 97 | @MainThread |
| 98 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 99 | Runnable runnable = new Runnable() { |
| 100 | @Override |
| 101 | public void run() { |
| 102 | synchronized (mLock) { |
| 103 | mRemoteService = ICacheQuotaService.Stub.asInterface(service); |
| 104 | List<CacheQuotaHint> requests = getUnfulfilledRequests(); |
| 105 | final RemoteCallback remoteCallback = |
| 106 | new RemoteCallback(CacheQuotaStrategy.this); |
| 107 | try { |
| 108 | mRemoteService.computeCacheQuotaHints(remoteCallback, requests); |
| 109 | } catch (RemoteException ex) { |
| 110 | Slog.w(TAG, |
| 111 | "Remote exception occurred while trying to get cache quota", |
| 112 | ex); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | AsyncTask.execute(runnable); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | @MainThread |
| 122 | public void onServiceDisconnected(ComponentName name) { |
| 123 | synchronized (mLock) { |
| 124 | mRemoteService = null; |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns a list of CacheQuotaRequests which do not have their quotas filled out for apps |
| 132 | * which have been used in the last year. |
| 133 | */ |
| 134 | private List<CacheQuotaHint> getUnfulfilledRequests() { |
| 135 | long timeNow = System.currentTimeMillis(); |
| 136 | long oneYearAgo = timeNow - DateUtils.YEAR_IN_MILLIS; |
| 137 | |
| 138 | List<CacheQuotaHint> requests = new ArrayList<>(); |
| 139 | UserManager um = mContext.getSystemService(UserManager.class); |
| 140 | final List<UserInfo> users = um.getUsers(); |
| 141 | final int userCount = users.size(); |
| 142 | final PackageManager packageManager = mContext.getPackageManager(); |
| 143 | for (int i = 0; i < userCount; i++) { |
| 144 | UserInfo info = users.get(i); |
| 145 | List<UsageStats> stats = |
| 146 | mUsageStats.queryUsageStatsForUser(info.id, UsageStatsManager.INTERVAL_BEST, |
| 147 | oneYearAgo, timeNow); |
| 148 | if (stats == null) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | for (UsageStats stat : stats) { |
| 153 | String packageName = stat.getPackageName(); |
| 154 | try { |
| 155 | // We need the app info to determine the uid and the uuid of the volume |
| 156 | // where the app is installed. |
| 157 | ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, 0); |
| 158 | requests.add( |
| 159 | new CacheQuotaHint.Builder() |
| 160 | .setVolumeUuid(appInfo.volumeUuid) |
| 161 | .setUid(appInfo.uid) |
| 162 | .setUsageStats(stat) |
| 163 | .setQuota(CacheQuotaHint.QUOTA_NOT_SET) |
| 164 | .build()); |
| 165 | } catch (PackageManager.NameNotFoundException e) { |
| 166 | Slog.w(TAG, "Unable to find package for quota calculation", e); |
| 167 | continue; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return requests; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onResult(Bundle data) { |
| 176 | final List<CacheQuotaHint> processedRequests = |
| 177 | data.getParcelableArrayList( |
| 178 | CacheQuotaService.REQUEST_LIST_KEY); |
| 179 | final int requestSize = processedRequests.size(); |
| 180 | for (int i = 0; i < requestSize; i++) { |
| 181 | CacheQuotaHint request = processedRequests.get(i); |
| 182 | long proposedQuota = request.getQuota(); |
| 183 | if (proposedQuota == CacheQuotaHint.QUOTA_NOT_SET) { |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | int uid = request.getUid(); |
| 189 | mInstaller.setAppQuota(request.getVolumeUuid(), |
| 190 | UserHandle.getUserId(uid), |
| 191 | UserHandle.getAppId(uid), proposedQuota); |
| 192 | } catch (Installer.InstallerException ex) { |
| 193 | Slog.w(TAG, |
| 194 | "Failed to set cache quota for " + request.getUid(), |
| 195 | ex); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | disconnectService(); |
| 200 | } |
| 201 | |
| 202 | private void disconnectService() { |
| 203 | mContext.unbindService(mServiceConnection); |
| 204 | mServiceConnection = null; |
| 205 | } |
| 206 | |
| 207 | private ComponentName getServiceComponentName() { |
| 208 | String packageName = |
| 209 | mContext.getPackageManager().getServicesSystemSharedLibraryPackageName(); |
| 210 | if (packageName == null) { |
| 211 | Slog.w(TAG, "could not access the cache quota service: no package!"); |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | Intent intent = new Intent(CacheQuotaService.SERVICE_INTERFACE); |
| 216 | intent.setPackage(packageName); |
| 217 | ResolveInfo resolveInfo = mContext.getPackageManager().resolveService(intent, |
| 218 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA); |
| 219 | if (resolveInfo == null || resolveInfo.serviceInfo == null) { |
| 220 | Slog.w(TAG, "No valid components found."); |
| 221 | return null; |
| 222 | } |
| 223 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 224 | return new ComponentName(serviceInfo.packageName, serviceInfo.name); |
| 225 | } |
| 226 | } |