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