blob: 39dfe0ef662017fa22c1b9a681f6f20dc486f79e [file] [log] [blame]
Daniel Nishi8d911532016-09-19 12:05:45 -07001/*
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
17package com.android.settingslib.deviceinfo;
18
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060019import android.app.AppGlobals;
20import android.app.usage.StorageStatsManager;
21import android.content.Context;
Daniel Nishi8d911532016-09-19 12:05:45 -070022import android.os.storage.VolumeInfo;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060023import android.util.Log;
24
25import java.io.IOException;
Daniel Nishi8d911532016-09-19 12:05:45 -070026
27/**
28 * PrivateStorageInfo provides information about the total and free storage on the device.
29 */
30public class PrivateStorageInfo {
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060031 private static final String TAG = "PrivateStorageInfo";
32
Daniel Nishi8d911532016-09-19 12:05:45 -070033 public final long freeBytes;
34 public final long totalBytes;
35
Daniel Nishi580ae012017-04-26 10:49:50 -070036 public PrivateStorageInfo(long freeBytes, long totalBytes) {
Daniel Nishi8d911532016-09-19 12:05:45 -070037 this.freeBytes = freeBytes;
38 this.totalBytes = totalBytes;
39 }
40
41 public static PrivateStorageInfo getPrivateStorageInfo(StorageVolumeProvider sm) {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060042 final Context context = AppGlobals.getInitialApplication();
43 final StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
44
Daniel Nishi8d911532016-09-19 12:05:45 -070045 long privateFreeBytes = 0;
46 long privateTotalBytes = 0;
47 for (VolumeInfo info : sm.getVolumes()) {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060048 if (info.getType() == VolumeInfo.TYPE_PRIVATE && info.isMountedReadable()) {
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060049 try {
50 privateTotalBytes += sm.getTotalBytes(stats, info);
51 privateFreeBytes += sm.getFreeBytes(stats, info);
52 } catch (IOException e) {
53 Log.w(TAG, e);
54 }
Daniel Nishi8d911532016-09-19 12:05:45 -070055 }
Daniel Nishi8d911532016-09-19 12:05:45 -070056 }
57 return new PrivateStorageInfo(privateFreeBytes, privateTotalBytes);
58 }
59
Daniel Nishi8d911532016-09-19 12:05:45 -070060 public static long getTotalSize(VolumeInfo info, long totalInternalStorage) {
Jeff Sharkeydafb17e2017-04-02 23:24:40 -060061 final Context context = AppGlobals.getInitialApplication();
62 final StorageStatsManager stats = context.getSystemService(StorageStatsManager.class);
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060063 try {
64 return stats.getTotalBytes(info.getFsUuid());
65 } catch (IOException e) {
66 Log.w(TAG, e);
67 return 0;
68 }
Daniel Nishi8d911532016-09-19 12:05:45 -070069 }
Daniel Nishi8d911532016-09-19 12:05:45 -070070}