1 /* 2 * Copyright (C) 2015 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.settings.deviceinfo; 18 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; 32 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceViewHolder; 35 36 import com.android.settings.R; 37 import com.android.settings.deviceinfo.StorageSettings.UnmountTask; 38 import com.android.settingslib.Utils; 39 40 import java.io.File; 41 import java.io.IOException; 42 43 /** 44 * Preference line representing a single {@link VolumeInfo}, possibly including 45 * quick actions like unmounting. 46 */ 47 public class StorageVolumePreference extends Preference { 48 private static final String TAG = StorageVolumePreference.class.getSimpleName(); 49 50 private final StorageManager mStorageManager; 51 private final VolumeInfo mVolume; 52 53 private int mUsedPercent = -1; 54 private ColorStateList mColorTintList; 55 56 // TODO: ideally, VolumeInfo should have a total physical size. StorageVolumePreference(Context context, VolumeInfo volume, long totalBytes)57 public StorageVolumePreference(Context context, VolumeInfo volume, long totalBytes) { 58 super(context); 59 60 mStorageManager = context.getSystemService(StorageManager.class); 61 mVolume = volume; 62 mColorTintList = Utils.getColorAttr(context, android.R.attr.colorControlNormal); 63 64 setLayoutResource(R.layout.storage_volume); 65 66 setKey(volume.getId()); 67 setTitle(mStorageManager.getBestVolumeDescription(volume)); 68 69 Drawable icon; 70 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) { 71 icon = context.getDrawable(R.drawable.ic_storage); 72 } else { 73 icon = context.getDrawable(R.drawable.ic_sim_sd); 74 } 75 76 if (volume.isMountedReadable()) { 77 // TODO: move statfs() to background thread 78 final File path = volume.getPath(); 79 80 long freeBytes = 0; 81 long usedBytes = 0; 82 if (volume.getType() == VolumeInfo.TYPE_PRIVATE) { 83 final StorageStatsManager stats = 84 context.getSystemService(StorageStatsManager.class); 85 try { 86 totalBytes = stats.getTotalBytes(volume.getFsUuid()); 87 freeBytes = stats.getFreeBytes(volume.getFsUuid()); 88 usedBytes = totalBytes - freeBytes; 89 } catch (IOException e) { 90 Log.w(TAG, e); 91 } 92 } else { 93 // StorageStatsManager can only query private volumes. 94 // Default to previous storage calculation for public volumes. 95 if (totalBytes <= 0) { 96 totalBytes = path.getTotalSpace(); 97 } 98 freeBytes = path.getFreeSpace(); 99 usedBytes = totalBytes - freeBytes; 100 } 101 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); 107 } 108 109 if (freeBytes < mStorageManager.getStorageLowBytes(path)) { 110 mColorTintList = Utils.getColorAttr(context, android.R.attr.colorError); 111 icon = context.getDrawable(R.drawable.ic_warning_24dp); 112 icon.mutate(); 113 icon.setTintList(mColorTintList); 114 } 115 116 } else { 117 setSummary(volume.getStateDescription()); 118 mUsedPercent = -1; 119 } 120 121 setIcon(icon); 122 123 if (volume.getType() == VolumeInfo.TYPE_PUBLIC 124 && volume.isMountedReadable()) { 125 setWidgetLayoutResource(R.layout.preference_storage_action); 126 } 127 } 128 129 @Override onBindViewHolder(PreferenceViewHolder view)130 public void onBindViewHolder(PreferenceViewHolder view) { 131 final ImageView unmount = (ImageView) view.findViewById(R.id.unmount); 132 if (unmount != null) { 133 unmount.setOnClickListener(mUnmountListener); 134 } 135 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); 141 } else { 142 progress.setVisibility(View.GONE); 143 } 144 145 super.onBindViewHolder(view); 146 } 147 148 private final View.OnClickListener mUnmountListener = new OnClickListener() { 149 @Override 150 public void onClick(View v) { 151 new UnmountTask(getContext(), mVolume).execute(); 152 } 153 }; 154 } 155