2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.deviceinfo;
19 import android.app.usage.StorageStatsManager;
20 import android.content.Context;
21 import android.content.res.ColorStateList;
22 import android.graphics.Color;
23 import android.graphics.drawable.Drawable;
24 import android.os.storage.StorageManager;
25 import android.os.storage.VolumeInfo;
26 import android.text.format.Formatter;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.ImageView;
31 import android.widget.ProgressBar;
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceViewHolder;
36 import com.android.settings.R;
37 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
38 import com.android.settingslib.Utils;
41 import java.io.IOException;
44 * Preference line representing a single {@link VolumeInfo}, possibly including
45 * quick actions like unmounting.
47 public class StorageVolumePreference extends Preference {
48 private static final String TAG = StorageVolumePreference.class.getSimpleName();
50 private final StorageManager mStorageManager;
51 private final VolumeInfo mVolume;
53 private int mUsedPercent = -1;
54 private ColorStateList mColorTintList;
56 // TODO: ideally, VolumeInfo should have a total physical size.
57 public StorageVolumePreference(Context context, VolumeInfo volume, long totalBytes) {
60 mStorageManager = context.getSystemService(StorageManager.class);
62 mColorTintList = Utils.getColorAttr(context, android.R.attr.colorControlNormal);
64 setLayoutResource(R.layout.storage_volume);
66 setKey(volume.getId());
67 setTitle(mStorageManager.getBestVolumeDescription(volume));
70 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) {
71 icon = context.getDrawable(R.drawable.ic_storage);
73 icon = context.getDrawable(R.drawable.ic_sim_sd);
76 if (volume.isMountedReadable()) {
77 // TODO: move statfs() to background thread
78 final File path = volume.getPath();
82 if (volume.getType() == VolumeInfo.TYPE_PRIVATE) {
83 final StorageStatsManager stats =
84 context.getSystemService(StorageStatsManager.class);
86 totalBytes = stats.getTotalBytes(volume.getFsUuid());
87 freeBytes = stats.getFreeBytes(volume.getFsUuid());
88 usedBytes = totalBytes - freeBytes;
89 } catch (IOException e) {
93 // StorageStatsManager can only query private volumes.
94 // Default to previous storage calculation for public volumes.
95 if (totalBytes <= 0) {
96 totalBytes = path.getTotalSpace();
98 freeBytes = path.getFreeSpace();
99 usedBytes = totalBytes - freeBytes;
102 final String used = Formatter.formatFileSize(context, usedBytes);
103 final String total = Formatter.formatFileSize(context, totalBytes);
104 setSummary(context.getString(R.string.storage_volume_summary, used, total));
105 if (totalBytes > 0) {
106 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
109 if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
110 mColorTintList = Utils.getColorAttr(context, android.R.attr.colorError);
111 icon = context.getDrawable(R.drawable.ic_warning_24dp);
113 icon.setTintList(mColorTintList);
117 setSummary(volume.getStateDescription());
123 if (volume.getType() == VolumeInfo.TYPE_PUBLIC
124 && volume.isMountedReadable()) {
125 setWidgetLayoutResource(R.layout.preference_storage_action);
130 public void onBindViewHolder(PreferenceViewHolder view) {
131 final ImageView unmount = (ImageView) view.findViewById(R.id.unmount);
132 if (unmount != null) {
133 unmount.setOnClickListener(mUnmountListener);
136 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
137 if (mVolume.getType() == VolumeInfo.TYPE_PRIVATE && mUsedPercent != -1) {
138 progress.setVisibility(View.VISIBLE);
139 progress.setProgress(mUsedPercent);
140 progress.setProgressTintList(mColorTintList);
142 progress.setVisibility(View.GONE);
145 super.onBindViewHolder(view);
148 private final View.OnClickListener mUnmountListener = new OnClickListener() {
150 public void onClick(View v) {
151 new UnmountTask(getContext(), mVolume).execute();