| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.usage; |
| 18 |
|
| 19 |
import static android.os.storage.StorageManager.convert; |
| 20 |
|
| 21 |
import android.annotation.BytesLong; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.RequiresPermission; |
| 24 |
import android.annotation.SystemService; |
| 25 |
import android.annotation.TestApi; |
| 26 |
import android.annotation.WorkerThread; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.pm.ApplicationInfo; |
| 29 |
import android.content.pm.PackageInfo; |
| 30 |
import android.content.pm.PackageManager; |
| 31 |
import android.content.pm.ParceledListSlice; |
| 32 |
import android.os.ParcelableException; |
| 33 |
import android.os.RemoteException; |
| 34 |
import android.os.UserHandle; |
| 35 |
import android.os.storage.CrateInfo; |
| 36 |
import android.os.storage.StorageManager; |
| 37 |
|
| 38 |
import com.android.internal.util.Preconditions; |
| 39 |
|
| 40 |
import java.io.File; |
| 41 |
import java.io.IOException; |
| 42 |
import java.util.Collection; |
| 43 |
import java.util.Objects; |
| 44 |
import java.util.UUID; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@SystemService(Context.STORAGE_STATS_SERVICE) |
| 60 |
public class StorageStatsManager { |
| 61 |
private final Context mContext; |
| 62 |
private final IStorageStatsManager mService; |
| 63 |
|
| 64 |
|
| 65 |
public StorageStatsManager(Context context, IStorageStatsManager service) { |
| 66 |
mContext = Objects.requireNonNull(context); |
| 67 |
mService = Objects.requireNonNull(service); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
@TestApi |
| 72 |
public boolean isQuotaSupported(@NonNull UUID storageUuid) { |
| 73 |
try { |
| 74 |
return mService.isQuotaSupported(convert(storageUuid), mContext.getOpPackageName()); |
| 75 |
} catch (RemoteException e) { |
| 76 |
throw e.rethrowFromSystemServer(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
@Deprecated |
| 82 |
public boolean isQuotaSupported(String uuid) { |
| 83 |
return isQuotaSupported(convert(uuid)); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
@TestApi |
| 88 |
public boolean isReservedSupported(@NonNull UUID storageUuid) { |
| 89 |
try { |
| 90 |
return mService.isReservedSupported(convert(storageUuid), mContext.getOpPackageName()); |
| 91 |
} catch (RemoteException e) { |
| 92 |
throw e.rethrowFromSystemServer(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@WorkerThread |
| 112 |
public @BytesLong long getTotalBytes(@NonNull UUID storageUuid) throws IOException { |
| 113 |
try { |
| 114 |
return mService.getTotalBytes(convert(storageUuid), mContext.getOpPackageName()); |
| 115 |
} catch (ParcelableException e) { |
| 116 |
e.maybeRethrow(IOException.class); |
| 117 |
throw new RuntimeException(e); |
| 118 |
} catch (RemoteException e) { |
| 119 |
throw e.rethrowFromSystemServer(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
@Deprecated |
| 125 |
public long getTotalBytes(String uuid) throws IOException { |
| 126 |
return getTotalBytes(convert(uuid)); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@WorkerThread |
| 144 |
public @BytesLong long getFreeBytes(@NonNull UUID storageUuid) throws IOException { |
| 145 |
try { |
| 146 |
return mService.getFreeBytes(convert(storageUuid), mContext.getOpPackageName()); |
| 147 |
} catch (ParcelableException e) { |
| 148 |
e.maybeRethrow(IOException.class); |
| 149 |
throw new RuntimeException(e); |
| 150 |
} catch (RemoteException e) { |
| 151 |
throw e.rethrowFromSystemServer(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
@Deprecated |
| 157 |
public long getFreeBytes(String uuid) throws IOException { |
| 158 |
return getFreeBytes(convert(uuid)); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
public @BytesLong long getCacheBytes(@NonNull UUID storageUuid) throws IOException { |
| 163 |
try { |
| 164 |
return mService.getCacheBytes(convert(storageUuid), mContext.getOpPackageName()); |
| 165 |
} catch (ParcelableException e) { |
| 166 |
e.maybeRethrow(IOException.class); |
| 167 |
throw new RuntimeException(e); |
| 168 |
} catch (RemoteException e) { |
| 169 |
throw e.rethrowFromSystemServer(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
@Deprecated |
| 175 |
public long getCacheBytes(String uuid) throws IOException { |
| 176 |
return getCacheBytes(convert(uuid)); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@WorkerThread |
| 209 |
public @NonNull StorageStats queryStatsForPackage(@NonNull UUID storageUuid, |
| 210 |
@NonNull String packageName, @NonNull UserHandle user) |
| 211 |
throws PackageManager.NameNotFoundException, IOException { |
| 212 |
try { |
| 213 |
return mService.queryStatsForPackage(convert(storageUuid), packageName, |
| 214 |
user.getIdentifier(), mContext.getOpPackageName()); |
| 215 |
} catch (ParcelableException e) { |
| 216 |
e.maybeRethrow(PackageManager.NameNotFoundException.class); |
| 217 |
e.maybeRethrow(IOException.class); |
| 218 |
throw new RuntimeException(e); |
| 219 |
} catch (RemoteException e) { |
| 220 |
throw e.rethrowFromSystemServer(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
@Deprecated |
| 226 |
public StorageStats queryStatsForPackage(String uuid, String packageName, |
| 227 |
UserHandle user) throws PackageManager.NameNotFoundException, IOException { |
| 228 |
return queryStatsForPackage(convert(uuid), packageName, user); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@WorkerThread |
| 252 |
public @NonNull StorageStats queryStatsForUid(@NonNull UUID storageUuid, int uid) |
| 253 |
throws IOException { |
| 254 |
try { |
| 255 |
return mService.queryStatsForUid(convert(storageUuid), uid, |
| 256 |
mContext.getOpPackageName()); |
| 257 |
} catch (ParcelableException e) { |
| 258 |
e.maybeRethrow(IOException.class); |
| 259 |
throw new RuntimeException(e); |
| 260 |
} catch (RemoteException e) { |
| 261 |
throw e.rethrowFromSystemServer(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
@Deprecated |
| 267 |
public StorageStats queryStatsForUid(String uuid, int uid) throws IOException { |
| 268 |
return queryStatsForUid(convert(uuid), uid); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
@WorkerThread |
| 290 |
public @NonNull StorageStats queryStatsForUser(@NonNull UUID storageUuid, |
| 291 |
@NonNull UserHandle user) throws IOException { |
| 292 |
try { |
| 293 |
return mService.queryStatsForUser(convert(storageUuid), user.getIdentifier(), |
| 294 |
mContext.getOpPackageName()); |
| 295 |
} catch (ParcelableException e) { |
| 296 |
e.maybeRethrow(IOException.class); |
| 297 |
throw new RuntimeException(e); |
| 298 |
} catch (RemoteException e) { |
| 299 |
throw e.rethrowFromSystemServer(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
@Deprecated |
| 305 |
public StorageStats queryStatsForUser(String uuid, UserHandle user) throws IOException { |
| 306 |
return queryStatsForUser(convert(uuid), user); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@WorkerThread |
| 327 |
public @NonNull ExternalStorageStats queryExternalStatsForUser(@NonNull UUID storageUuid, |
| 328 |
@NonNull UserHandle user) throws IOException { |
| 329 |
try { |
| 330 |
return mService.queryExternalStatsForUser(convert(storageUuid), user.getIdentifier(), |
| 331 |
mContext.getOpPackageName()); |
| 332 |
} catch (ParcelableException e) { |
| 333 |
e.maybeRethrow(IOException.class); |
| 334 |
throw new RuntimeException(e); |
| 335 |
} catch (RemoteException e) { |
| 336 |
throw e.rethrowFromSystemServer(); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
@Deprecated |
| 342 |
public ExternalStorageStats queryExternalStatsForUser(String uuid, UserHandle user) |
| 343 |
throws IOException { |
| 344 |
return queryExternalStatsForUser(convert(uuid), user); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
public long getCacheQuotaBytes(String volumeUuid, int uid) { |
| 349 |
try { |
| 350 |
return mService.getCacheQuotaBytes(volumeUuid, uid, mContext.getOpPackageName()); |
| 351 |
} catch (RemoteException e) { |
| 352 |
throw e.rethrowFromSystemServer(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
@TestApi |
| 369 |
@WorkerThread |
| 370 |
@NonNull |
| 371 |
public Collection<CrateInfo> queryCratesForUid(@NonNull UUID storageUuid, |
| 372 |
int uid) throws IOException, PackageManager.NameNotFoundException { |
| 373 |
try { |
| 374 |
ParceledListSlice<CrateInfo> crateInfoList = |
| 375 |
mService.queryCratesForUid(convert(storageUuid), uid, |
| 376 |
mContext.getOpPackageName()); |
| 377 |
return Objects.requireNonNull(crateInfoList).getList(); |
| 378 |
} catch (ParcelableException e) { |
| 379 |
e.maybeRethrow(PackageManager.NameNotFoundException.class); |
| 380 |
e.maybeRethrow(IOException.class); |
| 381 |
throw new RuntimeException(e); |
| 382 |
} catch (RemoteException e) { |
| 383 |
throw e.rethrowFromSystemServer(); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
@WorkerThread |
| 401 |
@TestApi |
| 402 |
@NonNull |
| 403 |
public Collection<CrateInfo> queryCratesForPackage(@NonNull UUID storageUuid, |
| 404 |
@NonNull String packageName, @NonNull UserHandle user) |
| 405 |
throws PackageManager.NameNotFoundException, IOException { |
| 406 |
try { |
| 407 |
ParceledListSlice<CrateInfo> crateInfoList = |
| 408 |
mService.queryCratesForPackage(convert(storageUuid), packageName, |
| 409 |
user.getIdentifier(), mContext.getOpPackageName()); |
| 410 |
return Objects.requireNonNull(crateInfoList).getList(); |
| 411 |
} catch (ParcelableException e) { |
| 412 |
e.maybeRethrow(PackageManager.NameNotFoundException.class); |
| 413 |
e.maybeRethrow(IOException.class); |
| 414 |
throw new RuntimeException(e); |
| 415 |
} catch (RemoteException e) { |
| 416 |
throw e.rethrowFromSystemServer(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
@WorkerThread |
| 433 |
@TestApi |
| 434 |
@RequiresPermission(android.Manifest.permission.MANAGE_CRATES) |
| 435 |
@NonNull |
| 436 |
public Collection<CrateInfo> queryCratesForUser(@NonNull UUID storageUuid, |
| 437 |
@NonNull UserHandle user) throws PackageManager.NameNotFoundException, IOException { |
| 438 |
try { |
| 439 |
ParceledListSlice<CrateInfo> crateInfoList = |
| 440 |
mService.queryCratesForUser(convert(storageUuid), user.getIdentifier(), |
| 441 |
mContext.getOpPackageName()); |
| 442 |
return Objects.requireNonNull(crateInfoList).getList(); |
| 443 |
} catch (ParcelableException e) { |
| 444 |
e.maybeRethrow(PackageManager.NameNotFoundException.class); |
| 445 |
e.maybeRethrow(IOException.class); |
| 446 |
throw new RuntimeException(e); |
| 447 |
} catch (RemoteException e) { |
| 448 |
throw e.rethrowFromSystemServer(); |
| 449 |
} |
| 450 |
} |
| 451 |
} |