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