ManageCacheDrawer.java revision 9201679ed1c485767f2e334aa618bd733024af03
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.gallery3d.ui;
18
19import com.android.gallery3d.R;
20import com.android.gallery3d.common.Utils;
21import com.android.gallery3d.data.Path;
22
23import android.content.Context;
24
25public class ManageCacheDrawer extends IconDrawer {
26    private static final int COLOR_CACHING_BACKGROUND = 0x7F000000;
27    private static final int ICON_SIZE = 36;
28    private final ResourceTexture mCheckedItem;
29    private final ResourceTexture mUnCheckedItem;
30    private final SelectionManager mSelectionManager;
31
32    private final ResourceTexture mLocalAlbumIcon;
33    private final StringTexture mCaching;
34
35    public ManageCacheDrawer(Context context, SelectionManager selectionManager) {
36        super(context);
37        mCheckedItem = new ResourceTexture(context, R.drawable.btn_make_offline_normal_on_holo_dark);
38        mUnCheckedItem = new ResourceTexture(context, R.drawable.btn_make_offline_normal_off_holo_dark);
39        mLocalAlbumIcon = new ResourceTexture(context, R.drawable.btn_make_offline_disabled_on_holo_dark);
40        String cachingLabel = context.getString(R.string.caching_label);
41        mCaching = StringTexture.newInstance(cachingLabel, 12, 0xffffffff);
42        mSelectionManager = selectionManager;
43    }
44
45    @Override
46    public void prepareDrawing() {
47    }
48
49    private static boolean isLocal(int dataSourceType) {
50        return dataSourceType != DATASOURCE_TYPE_PICASA;
51    }
52
53    @Override
54    public void draw(GLCanvas canvas, Texture content, int width, int height,
55            int rotation, Path path, int topIndex, int dataSourceType,
56            int mediaType, int darkStripHeight, boolean wantCache,
57            boolean isCaching) {
58
59        boolean selected = mSelectionManager.isItemSelected(path);
60        boolean chooseToCache = wantCache ^ selected;
61
62        int x = -width / 2;
63        int y = -height / 2;
64
65        drawWithRotationAndGray(canvas, content, x, y, width, height, rotation,
66                topIndex);
67
68        if (((rotation / 90) & 0x01) == 1) {
69            int temp = width;
70            width = height;
71            height = temp;
72            x = -width / 2;
73            y = -height / 2;
74        }
75
76        drawVideoOverlay(canvas, mediaType, x, y, width, height, topIndex);
77
78        if (topIndex == 0) {
79            drawDarkStrip(canvas, width, height, darkStripHeight);
80            drawIcon(canvas, width, height, dataSourceType);
81        }
82
83        if (topIndex == 0) {
84            ResourceTexture icon = null;
85            if (isLocal(dataSourceType)) {
86                icon = mLocalAlbumIcon;
87            } else if (chooseToCache) {
88                icon = mCheckedItem;
89            } else {
90                icon = mUnCheckedItem;
91            }
92
93            int w = ICON_SIZE;
94            int h = ICON_SIZE;
95            x = width / 2 - w;
96            y = -height / 2;
97
98            icon.draw(canvas, x, y, w, h);
99
100            if (isCaching) {
101                int textWidth = mCaching.getWidth();
102                int textHeight = mCaching.getHeight();
103                x = -textWidth / 2;
104                y = height / 2 - textHeight;
105
106                // Leave a few pixels of margin in the background rect.
107                float sideMargin = Utils.clamp(textWidth * 0.1f, 2.0f,
108                        6.0f);
109                float clearance = Utils.clamp(textHeight * 0.1f, 2.0f,
110                        6.0f);
111
112                // Overlay the "Caching" wording at the bottom-center of the content.
113                canvas.fillRect(x - sideMargin, y - clearance,
114                        textWidth + sideMargin * 2, textHeight + clearance,
115                        COLOR_CACHING_BACKGROUND);
116                mCaching.draw(canvas, x, y);
117            }
118        }
119    }
120
121    @Override
122    public void drawFocus(GLCanvas canvas, int width, int height) {
123    }
124}
125