OSDN Git Service

Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / StorageVolumePreference.java
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             if (totalBytes > 0) {
78                 mUsedPercent = (int) ((usedBytes * 100) / totalBytes);
79             }
80
81             if (freeBytes < mStorageManager.getStorageLowBytes(path)) {
82                 mColor = StorageSettings.COLOR_WARNING;
83                 icon = context.getDrawable(R.drawable.ic_warning_24dp);
84             }
85
86         } else {
87             setSummary(volume.getStateDescription());
88             mUsedPercent = -1;
89         }
90
91         icon.mutate();
92         icon.setTint(mColor);
93         setIcon(icon);
94
95         if (volume.getType() == VolumeInfo.TYPE_PUBLIC
96                 && volume.isMountedReadable()) {
97             setWidgetLayoutResource(R.layout.preference_storage_action);
98         }
99     }
100
101     @Override
102     protected void onBindView(View view) {
103         final ImageView unmount = (ImageView) view.findViewById(R.id.unmount);
104         if (unmount != null) {
105             unmount.setImageTintList(ColorStateList.valueOf(Color.parseColor("#8a000000")));
106             unmount.setOnClickListener(mUnmountListener);
107         }
108
109         final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
110         if (mVolume.getType() == VolumeInfo.TYPE_PRIVATE && mUsedPercent != -1) {
111             progress.setVisibility(View.VISIBLE);
112             progress.setProgress(mUsedPercent);
113             progress.setProgressTintList(ColorStateList.valueOf(mColor));
114         } else {
115             progress.setVisibility(View.GONE);
116         }
117
118         super.onBindView(view);
119     }
120
121     private final View.OnClickListener mUnmountListener = new OnClickListener() {
122         @Override
123         public void onClick(View v) {
124             new UnmountTask(getContext(), mVolume).execute();
125         }
126     };
127 }