ManageCacheDrawer.java revision 1a4bd273afe5dd11592f7625c2f19853b6f174e9
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 android.content.Context;
20
21import com.android.gallery3d.R;
22import com.android.gallery3d.data.Path;
23
24public class ManageCacheDrawer extends IconDrawer {
25    private final ResourceTexture mCheckedItem;
26    private final ResourceTexture mUnCheckedItem;
27    private final SelectionManager mSelectionManager;
28
29    private final ResourceTexture mLocalAlbumIcon;
30    private final StringTexture mCachingText;
31
32    private final int mCachePinSize;
33    private final int mCachePinMargin;
34
35    public ManageCacheDrawer(Context context, SelectionManager selectionManager,
36            int cachePinSize, int cachePinMargin) {
37        super(context);
38        mCheckedItem = new ResourceTexture(context, R.drawable.btn_make_offline_normal_on_holo_dark);
39        mUnCheckedItem = new ResourceTexture(context, R.drawable.btn_make_offline_normal_off_holo_dark);
40        mLocalAlbumIcon = new ResourceTexture(context, R.drawable.btn_make_offline_disabled_on_holo_dark);
41        String cachingLabel = context.getString(R.string.caching_label);
42        mCachingText = StringTexture.newInstance(cachingLabel, 12, 0xffffffff);
43        mSelectionManager = selectionManager;
44        mCachePinSize = cachePinSize;
45        mCachePinMargin = cachePinMargin;
46    }
47
48    @Override
49    public void prepareDrawing() {
50    }
51
52    private static boolean isLocal(int dataSourceType) {
53        return dataSourceType != DATASOURCE_TYPE_PICASA;
54    }
55
56    @Override
57    public void draw(GLCanvas canvas, Texture content, int width,
58            int height, int rotation, Path path,
59            int dataSourceType, int mediaType, boolean isPanorama,
60            int labelBackgroundHeight, boolean wantCache, boolean isCaching) {
61
62        boolean selected = mSelectionManager.isItemSelected(path);
63        boolean chooseToCache = wantCache ^ selected;
64        boolean available = isLocal(dataSourceType) || chooseToCache;
65
66        int x = -width / 2;
67        int y = -height / 2;
68
69        if (!available) {
70            canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
71            canvas.multiplyAlpha(0.6f);
72        }
73
74        drawWithRotation(canvas, content, x, y, width, height, rotation);
75
76        if (!available) {
77            canvas.restore();
78        }
79
80        if (((rotation / 90) & 0x01) == 1) {
81            int temp = width;
82            width = height;
83            height = temp;
84            x = -width / 2;
85            y = -height / 2;
86        }
87
88        drawMediaTypeOverlay(canvas, mediaType, isPanorama, x, y, width, height);
89        drawCachingPin(canvas, path, dataSourceType, isCaching, chooseToCache,
90                width, height);
91
92        if (mSelectionManager.isPressedPath(path)) {
93            drawPressedFrame(canvas, x, y, width, height);
94        }
95    }
96
97    private void drawCachingPin(GLCanvas canvas, Path path, int dataSourceType,
98            boolean isCaching, boolean chooseToCache, int width, int height) {
99
100        ResourceTexture icon = null;
101        if (isLocal(dataSourceType)) {
102            icon = mLocalAlbumIcon;
103        } else if (chooseToCache) {
104            icon = mCheckedItem;
105        } else {
106            icon = mUnCheckedItem;
107        }
108
109        int w = mCachePinSize;
110        int h = mCachePinSize;
111        int right = (width + 1) / 2;
112        int bottom = (height + 1) / 2;
113        int x = right - w - mCachePinMargin;
114        int y = bottom - h - mCachePinMargin;
115
116        icon.draw(canvas, x, y, w, h);
117
118        if (isCaching) {
119            int textWidth = mCachingText.getWidth();
120            int textHeight = mCachingText.getHeight();
121            // Align the center of the text to the center of the pin icon
122            x = right - mCachePinMargin - (textWidth + mCachePinSize) / 2;
123            y = bottom - textHeight;
124            mCachingText.draw(canvas, x, y);
125        }
126    }
127
128    @Override
129    public void drawFocus(GLCanvas canvas, int width, int height) {
130    }
131}
132