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.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; 32 33 import com.android.settings.R; 34 import com.android.settings.deviceinfo.StorageSettings.UnmountTask; 35 36 import java.io.File; 37 38 /** 39 * Preference line representing a single {@link VolumeInfo}, possibly including 40 * quick actions like unmounting. 41 */ 42 public class StorageVolumePreference extends Preference { 43 private final StorageManager mStorageManager; 44 private final VolumeInfo mVolume; 45 46 private int mColor; 47 private int mUsedPercent = -1; 48 StorageVolumePreference(Context context, VolumeInfo volume, int color)49 public StorageVolumePreference(Context context, VolumeInfo volume, int color) { 50 super(context); 51 52 mStorageManager = context.getSystemService(StorageManager.class); 53 mVolume = volume; 54 mColor = color; 55 56 setLayoutResource(R.layout.storage_volume); 57 58 setKey(volume.getId()); 59 setTitle(mStorageManager.getBestVolumeDescription(volume)); 60 61 Drawable icon; 62 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) { 63 icon = context.getDrawable(R.drawable.ic_settings_storage); 64 } else { 65 icon = context.getDrawable(R.drawable.ic_sim_sd); 66 } 67 68 if (volume.isMountedReadable()) { 69 // TODO: move statfs() to background thread 70 final File path = volume.getPath(); 71 final long freeBytes = path.getFreeSpace(); 72 final long totalBytes = path.getTotalSpace(); 73 final long usedBytes = totalBytes - freeBytes; 74 75 final String used = Formatter.formatFileSize(context, usedBytes); 76 final String total = Formatter.formatFileSize(context, totalBytes); 77 setSummary(context.getString(R.string.storage_volume_summary, used, total)); 78 if (totalBytes > 0) { 79 mUsedPercent = (int) ((usedBytes * 100) / totalBytes); 80 } 81 82 if (freeBytes < mStorageManager.getStorageLowBytes(path)) { 83 mColor = StorageSettings.COLOR_WARNING; 84 icon = context.getDrawable(R.drawable.ic_warning_24dp); 85 } 86 87 } else { 88 setSummary(volume.getStateDescription()); 89 mUsedPercent = -1; 90 } 91 92 icon.mutate(); 93 icon.setTint(mColor); 94 setIcon(icon); 95 96 if (volume.getType() == VolumeInfo.TYPE_PUBLIC 97 && volume.isMountedReadable()) { 98 setWidgetLayoutResource(R.layout.preference_storage_action); 99 } 100 } 101 102 @Override onBindViewHolder(PreferenceViewHolder view)103 public void onBindViewHolder(PreferenceViewHolder view) { 104 final ImageView unmount = (ImageView) view.findViewById(R.id.unmount); 105 if (unmount != null) { 106 unmount.setImageTintList(ColorStateList.valueOf(Color.parseColor("#8a000000"))); 107 unmount.setOnClickListener(mUnmountListener); 108 } 109 110 final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress); 111 if (mVolume.getType() == VolumeInfo.TYPE_PRIVATE && mUsedPercent != -1) { 112 progress.setVisibility(View.VISIBLE); 113 progress.setProgress(mUsedPercent); 114 progress.setProgressTintList(ColorStateList.valueOf(mColor)); 115 } else { 116 progress.setVisibility(View.GONE); 117 } 118 119 super.onBindViewHolder(view); 120 } 121 122 private final View.OnClickListener mUnmountListener = new OnClickListener() { 123 @Override 124 public void onClick(View v) { 125 new UnmountTask(getContext(), mVolume).execute(); 126 } 127 }; 128 } 129