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.preference.Preference;
26 import android.text.format.Formatter;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.widget.ImageView;
30 import android.widget.ProgressBar;
32 import com.android.settings.R;
33 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
38 * Preference line representing a single {@link VolumeInfo}, possibly including
39 * quick actions like unmounting.
41 public class StorageVolumePreference extends Preference {
42 private final StorageManager mStorageManager;
43 private final VolumeInfo mVolume;
46 private int mUsedPercent = -1;
48 public StorageVolumePreference(Context context, VolumeInfo volume, int color) {
51 mStorageManager = context.getSystemService(StorageManager.class);
55 setLayoutResource(R.layout.storage_volume);
57 setKey(volume.getId());
58 setTitle(mStorageManager.getBestVolumeDescription(volume));
61 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) {
62 icon = context.getDrawable(R.drawable.ic_settings_storage);
64 icon = context.getDrawable(R.drawable.ic_sim_sd);
67 if (volume.isMountedReadable()) {
68 // TODO: move statfs() to background thread
69 final File path = volume.getPath();
70 final long freeBytes = path.getFreeSpace();
71 final long totalBytes = path.getTotalSpace();
72 final long usedBytes = totalBytes - freeBytes;
74 final String used = Formatter.formatFileSize(context, usedBytes);
75 final String total = Formatter.formatFileSize(context, totalBytes);
76 setSummary(context.getString(R.string.storage_volume_summary, used, total));
78 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
81 if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
82 mColor = StorageSettings.COLOR_WARNING;
83 icon = context.getDrawable(R.drawable.ic_warning_24dp);
87 setSummary(volume.getStateDescription());
95 if (volume.getType() == VolumeInfo.TYPE_PUBLIC
96 && volume.isMountedReadable()) {
97 setWidgetLayoutResource(R.layout.preference_storage_action);
102 protected void onBindView(View view) {
103 final ImageView unmount = (ImageView) view.findViewById(R.id.unmount);
104 if (unmount != null) {
105 unmount.setImageTintList(ColorStateList.valueOf(Color.parseColor("#8a000000")));
106 unmount.setOnClickListener(mUnmountListener);
109 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
110 if (mVolume.getType() == VolumeInfo.TYPE_PRIVATE && mUsedPercent != -1) {
111 progress.setVisibility(View.VISIBLE);
112 progress.setProgress(mUsedPercent);
113 progress.setProgressTintList(ColorStateList.valueOf(mColor));
115 progress.setVisibility(View.GONE);
118 super.onBindView(view);
121 private final View.OnClickListener mUnmountListener = new OnClickListener() {
123 public void onClick(View v) {
124 new UnmountTask(getContext(), mVolume).execute();