1 /*
2  * Copyright (C) 2016 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.settingslib.deviceinfo;
18 
19 import android.app.AppGlobals;
20 import android.app.usage.StorageStatsManager;
21 import android.content.Context;
22 import android.os.storage.VolumeInfo;
23 import android.util.Log;
24 
25 import java.io.IOException;
26 
27 /**
28  * PrivateStorageInfo provides information about the total and free storage on the device.
29  */
30 public class PrivateStorageInfo {
31     private static final String TAG = "PrivateStorageInfo";
32 
33     public final long freeBytes;
34     public final long totalBytes;
35 
PrivateStorageInfo(long freeBytes, long totalBytes)36     public PrivateStorageInfo(long freeBytes, long totalBytes) {
37         this.freeBytes = freeBytes;
38         this.totalBytes = totalBytes;
39     }
40 
getPrivateStorageInfo(StorageVolumeProvider sm)41     public static PrivateStorageInfo getPrivateStorageInfo(StorageVolumeProvider sm) {
42         final Context context = AppGlobals.getInitialApplication();
43         final StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
44 
45         long privateFreeBytes = 0;
46         long privateTotalBytes = 0;
47         for (VolumeInfo info : sm.getVolumes()) {
48             if (info.getType() == VolumeInfo.TYPE_PRIVATE && info.isMountedReadable()) {
49                 try {
50                     privateTotalBytes += sm.getTotalBytes(stats, info);
51                     privateFreeBytes += sm.getFreeBytes(stats, info);
52                 } catch (IOException e) {
53                     Log.w(TAG, e);
54                 }
55             }
56         }
57         return new PrivateStorageInfo(privateFreeBytes, privateTotalBytes);
58     }
59 
getTotalSize(VolumeInfo info, long totalInternalStorage)60     public static long getTotalSize(VolumeInfo info, long totalInternalStorage) {
61         final Context context = AppGlobals.getInitialApplication();
62         final StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
63         try {
64             return stats.getTotalBytes(info.getFsUuid());
65         } catch (IOException e) {
66             Log.w(TAG, e);
67             return 0;
68         }
69     }
70 }
71