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.settings.applications;
18 
19 
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.os.UserHandle;
24 import android.util.Log;
25 
26 import androidx.annotation.NonNull;
27 
28 import com.android.internal.util.Preconditions;
29 import com.android.settingslib.applications.StorageStatsSource;
30 import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
31 import com.android.settingslib.utils.AsyncLoaderCompat;
32 
33 import java.io.IOException;
34 
35 /**
36  * Fetches the storage stats using the StorageStatsManager for a given package and user tuple.
37  */
38 public class FetchPackageStorageAsyncLoader extends AsyncLoaderCompat<AppStorageStats> {
39     private static final String TAG = "FetchPackageStorage";
40     private final StorageStatsSource mSource;
41     private final ApplicationInfo mInfo;
42     private final UserHandle mUser;
43 
FetchPackageStorageAsyncLoader(Context context, @NonNull StorageStatsSource source, @NonNull ApplicationInfo info, @NonNull UserHandle user)44     public FetchPackageStorageAsyncLoader(Context context, @NonNull StorageStatsSource source,
45             @NonNull ApplicationInfo info, @NonNull UserHandle user) {
46         super(context);
47         mSource = Preconditions.checkNotNull(source);
48         mInfo = info;
49         mUser = user;
50     }
51 
52     @Override
loadInBackground()53     public AppStorageStats loadInBackground() {
54         AppStorageStats result = null;
55         try {
56             result = mSource.getStatsForPackage(mInfo.volumeUuid, mInfo.packageName, mUser);
57         } catch (NameNotFoundException | IOException e) {
58             Log.w(TAG, "Package may have been removed during query, failing gracefully", e);
59         }
60         return result;
61     }
62 
63     @Override
onDiscardResult(AppStorageStats result)64     protected void onDiscardResult(AppStorageStats result) {
65     }
66 }
67