1/*
2 * Copyright (C) 2010 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.preference.Preference;
21import android.util.AttributeSet;
22import android.view.View;
23
24import com.android.settings.R;
25import com.google.android.collect.Lists;
26
27import java.util.Collections;
28import java.util.List;
29
30/**
31 * Creates a percentage bar chart inside a preference.
32 */
33public class UsageBarPreference extends Preference {
34    private PercentageBarChart mChart = null;
35
36    private final List<PercentageBarChart.Entry> mEntries = Lists.newArrayList();
37
38    public UsageBarPreference(Context context) {
39        this(context, null);
40    }
41
42    public UsageBarPreference(Context context, AttributeSet attrs) {
43        this(context, attrs, 0);
44    }
45
46    public UsageBarPreference(Context context, AttributeSet attrs, int defStyle) {
47        this(context, attrs, defStyle, 0);
48    }
49
50    public UsageBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
51            int defStyleRes) {
52        super(context, attrs, defStyleAttr, defStyleRes);
53        setLayoutResource(R.layout.preference_memoryusage);
54    }
55
56    public void addEntry(int order, float percentage, int color) {
57        mEntries.add(PercentageBarChart.createEntry(order, percentage, color));
58        Collections.sort(mEntries);
59    }
60
61    @Override
62    protected void onBindView(View view) {
63        super.onBindView(view);
64
65        mChart = (PercentageBarChart) view.findViewById(R.id.percentage_bar_chart);
66        mChart.setEntries(mEntries);
67    }
68
69    public void commit() {
70        if (mChart != null) {
71            mChart.invalidate();
72        }
73    }
74
75    public void clear() {
76        mEntries.clear();
77    }
78}
79