1/*
2 * Copyright (C) 2011 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.Resources;
21import android.graphics.Color;
22import android.graphics.drawable.ShapeDrawable;
23import android.graphics.drawable.shapes.RectShape;
24import android.os.UserHandle;
25import android.preference.Preference;
26
27import com.android.settings.R;
28
29public class StorageItemPreference extends Preference {
30    public final int color;
31    public final int userHandle;
32
33    public StorageItemPreference(Context context, int titleRes, int colorRes) {
34        this(context, context.getText(titleRes), colorRes, UserHandle.USER_NULL);
35    }
36
37    public StorageItemPreference(
38            Context context, CharSequence title, int colorRes, int userHandle) {
39        super(context);
40
41        if (colorRes != 0) {
42            this.color = context.getResources().getColor(colorRes);
43
44            final Resources res = context.getResources();
45            final int width = res.getDimensionPixelSize(R.dimen.device_memory_usage_button_width);
46            final int height = res.getDimensionPixelSize(R.dimen.device_memory_usage_button_height);
47            setIcon(createRectShape(width, height, this.color));
48        } else {
49            this.color = Color.MAGENTA;
50        }
51
52        setTitle(title);
53        setSummary(R.string.memory_calculating_size);
54
55        this.userHandle = userHandle;
56    }
57
58    private static ShapeDrawable createRectShape(int width, int height, int color) {
59        ShapeDrawable shape = new ShapeDrawable(new RectShape());
60        shape.setIntrinsicHeight(height);
61        shape.setIntrinsicWidth(width);
62        shape.getPaint().setColor(color);
63        return shape;
64    }
65}
66