Home | History | Annotate | Download | only in deviceinfo
      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 
     49     // TODO: ideally, VolumeInfo should have a total physical size.
     50     public StorageVolumePreference(Context context, VolumeInfo volume, int color, long totalBytes) {
     51         super(context);
     52 
     53         mStorageManager = context.getSystemService(StorageManager.class);
     54         mVolume = volume;
     55         mColor = color;
     56 
     57         setLayoutResource(R.layout.storage_volume);
     58 
     59         setKey(volume.getId());
     60         setTitle(mStorageManager.getBestVolumeDescription(volume));
     61 
     62         Drawable icon;
     63         if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) {
     64             icon = context.getDrawable(R.drawable.ic_settings_storage);
     65         } else {
     66             icon = context.getDrawable(R.drawable.ic_sim_sd);
     67         }
     68 
     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();
     74             }
     75             final long freeBytes = path.getFreeSpace();
     76             final long usedBytes = totalBytes - freeBytes;
     77 
     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));
     81             if (totalBytes > 0) {
     82                 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
     83             }
     84 
     85             if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
     86                 mColor = StorageSettings.COLOR_WARNING;
     87                 icon = context.getDrawable(R.drawable.ic_warning_24dp);
     88             }
     89 
     90         } else {
     91             setSummary(volume.getStateDescription());
     92             mUsedPercent = -1;
     93         }
     94 
     95         icon.mutate();
     96         icon.setTint(mColor);
     97         setIcon(icon);
     98 
     99         if (volume.getType() == VolumeInfo.TYPE_PUBLIC
    100                 && volume.isMountedReadable()) {
    101             setWidgetLayoutResource(R.layout.preference_storage_action);
    102         }
    103     }
    104 
    105     @Override
    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);
    111         }
    112 
    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));
    118         } else {
    119             progress.setVisibility(View.GONE);
    120         }
    121 
    122         super.onBindViewHolder(view);
    123     }
    124 
    125     private final View.OnClickListener mUnmountListener = new OnClickListener() {
    126         @Override
    127         public void onClick(View v) {
    128             new UnmountTask(getContext(), mVolume).execute();
    129         }
    130     };
    131 }
    132