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