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