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.content.Context;
20 import android.content.res.ColorStateList;
21 import android.graphics.Color;
22 import android.graphics.drawable.Drawable;
23 import android.os.storage.StorageManager;
24 import android.os.storage.VolumeInfo;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceViewHolder;
27 import android.text.format.Formatter;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.ImageView;
31 import android.widget.ProgressBar;
33 import com.android.settings.R;
34 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
39 * Preference line representing a single {@link VolumeInfo}, possibly including
40 * quick actions like unmounting.
42 public class StorageVolumePreference extends Preference {
43 private final StorageManager mStorageManager;
44 private final VolumeInfo mVolume;
47 private int mUsedPercent = -1;
49 // TODO: ideally, VolumeInfo should have a total physical size.
50 public StorageVolumePreference(Context context, VolumeInfo volume, int color, long totalBytes) {
53 mStorageManager = context.getSystemService(StorageManager.class);
57 setLayoutResource(R.layout.storage_volume);
59 setKey(volume.getId());
60 setTitle(mStorageManager.getBestVolumeDescription(volume));
63 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) {
64 icon = context.getDrawable(R.drawable.ic_settings_storage);
66 icon = context.getDrawable(R.drawable.ic_sim_sd);
69 if (volume.isMountedReadable()) {
70 // TODO: move statfs() to background thread
71 final File path = volume.getPath();
72 if (totalBytes <= 0) {
73 totalBytes = path.getTotalSpace();
75 final long freeBytes = path.getFreeSpace();
76 final long usedBytes = totalBytes - freeBytes;
78 final String used = Formatter.formatFileSize(context, usedBytes);
79 final String total = Formatter.formatFileSize(context, totalBytes);
80 setSummary(context.getString(R.string.storage_volume_summary, used, total));
82 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
85 if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
86 mColor = StorageSettings.COLOR_WARNING;
87 icon = context.getDrawable(R.drawable.ic_warning_24dp);
91 setSummary(volume.getStateDescription());
99 if (volume.getType() == VolumeInfo.TYPE_PUBLIC
100 && volume.isMountedReadable()) {
101 setWidgetLayoutResource(R.layout.preference_storage_action);
106 public void onBindViewHolder(PreferenceViewHolder view) {
107 final ImageView unmount = (ImageView) view.findViewById(R.id.unmount);
108 if (unmount != null) {
109 unmount.setImageTintList(ColorStateList.valueOf(Color.parseColor("#8a000000")));
110 unmount.setOnClickListener(mUnmountListener);
113 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
114 if (mVolume.getType() == VolumeInfo.TYPE_PRIVATE && mUsedPercent != -1) {
115 progress.setVisibility(View.VISIBLE);
116 progress.setProgress(mUsedPercent);
117 progress.setProgressTintList(ColorStateList.valueOf(mColor));
119 progress.setVisibility(View.GONE);
122 super.onBindViewHolder(view);
125 private final View.OnClickListener mUnmountListener = new OnClickListener() {
127 public void onClick(View v) {
128 new UnmountTask(getContext(), mVolume).execute();