190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey/*
290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey *
490c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * you may not use this file except in compliance with the License.
690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * You may obtain a copy of the License at
790c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey *
890c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
990c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey *
1090c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
1190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * See the License for the specific language governing permissions and
1490c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey * limitations under the License.
1590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey */
1690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
1790c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeypackage com.android.settings.deviceinfo;
1890c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
1990c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.content.Context;
2090c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.content.res.Resources;
2190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.graphics.Color;
2290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.graphics.drawable.ShapeDrawable;
2390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.graphics.drawable.shapes.RectShape;
24da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkeyimport android.os.UserHandle;
2590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport android.preference.Preference;
2690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
2790c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeyimport com.android.settings.R;
2890c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
2990c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkeypublic class StorageItemPreference extends Preference {
30da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey    public final int color;
31da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey    public final int userHandle;
3290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
33da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey    public StorageItemPreference(Context context, int titleRes, int colorRes) {
34da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey        this(context, context.getText(titleRes), colorRes, UserHandle.USER_NULL);
3590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey    }
3690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
37da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey    public StorageItemPreference(
38da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey            Context context, CharSequence title, int colorRes, int userHandle) {
3990c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        super(context);
4090c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
4190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        if (colorRes != 0) {
42da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey            this.color = context.getResources().getColor(colorRes);
4390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
4490c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey            final Resources res = context.getResources();
4590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey            final int width = res.getDimensionPixelSize(R.dimen.device_memory_usage_button_width);
4690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey            final int height = res.getDimensionPixelSize(R.dimen.device_memory_usage_button_height);
47da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey            setIcon(createRectShape(width, height, this.color));
48da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey        } else {
49da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey            this.color = Color.MAGENTA;
5090c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        }
5190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
5290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        setTitle(title);
5390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        setSummary(R.string.memory_calculating_size);
54da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey
55da13ec0cb454f1aadada2acf12291cfaf0a7b176Jeff Sharkey        this.userHandle = userHandle;
5690c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey    }
5790c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey
5890c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey    private static ShapeDrawable createRectShape(int width, int height, int color) {
5990c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        ShapeDrawable shape = new ShapeDrawable(new RectShape());
6090c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        shape.setIntrinsicHeight(height);
6190c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        shape.setIntrinsicWidth(width);
6290c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        shape.getPaint().setColor(color);
6390c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey        return shape;
6490c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey    }
6590c8b20ccfd8f2e877bfac7cf3e883631af7a3e3Jeff Sharkey}
66