• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceViewHolder;
28 import android.text.format.Formatter;
29 import android.util.Log;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.widget.ImageView;
33 import android.widget.ProgressBar;
34 
35 import com.android.settings.R;
36 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
37 import com.android.settingslib.Utils;
38 
39 import java.io.IOException;
40 import java.io.File;
41 
42 /**
43  * Preference line representing a single {@link VolumeInfo}, possibly including
44  * quick actions like unmounting.
45  */
46 public class StorageVolumePreference extends Preference {
47     private static final String TAG = StorageVolumePreference.class.getSimpleName();
48 
49     private final StorageManager mStorageManager;
50     private final VolumeInfo mVolume;
51 
52     private int mColor;
53     private int mUsedPercent = -1;
54 
55     // TODO: ideally, VolumeInfo should have a total physical size.
StorageVolumePreference(Context context, VolumeInfo volume, int color, long totalBytes)56     public StorageVolumePreference(Context context, VolumeInfo volume, int color, long totalBytes) {
57         super(context);
58 
59         mStorageManager = context.getSystemService(StorageManager.class);
60         mVolume = volume;
61         mColor = color;
62 
63         setLayoutResource(R.layout.storage_volume);
64 
65         setKey(volume.getId());
66         setTitle(mStorageManager.getBestVolumeDescription(volume));
67 
68         Drawable icon;
69         if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(volume.getId())) {
70             icon = context.getDrawable(R.drawable.ic_storage);
71         } else {
72             icon = context.getDrawable(R.drawable.ic_sim_sd);
73         }
74 
75         if (volume.isMountedReadable()) {
76             // TODO: move statfs() to background thread
77             final File path = volume.getPath();
78 
79             long freeBytes = 0;
80             long usedBytes = 0;
81             if (volume.getType() == VolumeInfo.TYPE_PRIVATE) {
82                 final StorageStatsManager stats =
83                         context.getSystemService(StorageStatsManager.class);
84                 try {
85                     totalBytes = stats.getTotalBytes(volume.getFsUuid());
86                     freeBytes = stats.getFreeBytes(volume.getFsUuid());
87                     usedBytes = totalBytes - freeBytes;
88                 } catch (IOException e) {
89                     Log.w(TAG, e);
90                 }
91             } else {
92                 // StorageStatsManager can only query private volumes.
93                 // Default to previous storage calculation for public volumes.
94                 if (totalBytes <= 0) {
95                     totalBytes = path.getTotalSpace();
96                 }
97                 freeBytes = path.getFreeSpace();
98                 usedBytes = totalBytes - freeBytes;
99             }
100 
101             final String used = Formatter.formatFileSize(context, usedBytes);
102             final String total = Formatter.formatFileSize(context, totalBytes);
103             setSummary(context.getString(R.string.storage_volume_summary, used, total));
104             if (totalBytes > 0) {
105                 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
106             }
107 
108             if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
109                 mColor = Utils.getColorAttr(context, android.R.attr.colorError);
110                 icon = context.getDrawable(R.drawable.ic_warning_24dp);
111             }
112 
113         } else {
114             setSummary(volume.getStateDescription());
115             mUsedPercent = -1;
116         }
117 
118         icon.mutate();
119         icon.setTint(mColor);
120         setIcon(icon);
121 
122         if (volume.getType() == VolumeInfo.TYPE_PUBLIC
123                 && volume.isMountedReadable()) {
124             setWidgetLayoutResource(R.layout.preference_storage_action);
125         }
126     }
127 
128     @Override
onBindViewHolder(PreferenceViewHolder view)129     public void onBindViewHolder(PreferenceViewHolder view) {
130         final ImageView unmount = (ImageView) view.findViewById(R.id.unmount);
131         if (unmount != null) {
132             unmount.setImageTintList(ColorStateList.valueOf(Color.parseColor("#8a000000")));
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(ColorStateList.valueOf(mColor));
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