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.app.AbstractGalleryActivity;
23import com.android.gallery3d.data.DataSourceType;
24import com.android.gallery3d.data.MediaSet;
25import com.android.gallery3d.data.Path;
26import com.android.gallery3d.glrenderer.GLCanvas;
27import com.android.gallery3d.glrenderer.ResourceTexture;
28import com.android.gallery3d.glrenderer.StringTexture;
29import com.android.gallery3d.ui.AlbumSetSlidingWindow.AlbumSetEntry;
30
31public class ManageCacheDrawer extends AlbumSetSlotRenderer {
32    private final ResourceTexture mCheckedItem;
33    private final ResourceTexture mUnCheckedItem;
34    private final SelectionManager mSelectionManager;
35
36    private final ResourceTexture mLocalAlbumIcon;
37    private final StringTexture mCachingText;
38
39    private final int mCachePinSize;
40    private final int mCachePinMargin;
41
42    public ManageCacheDrawer(AbstractGalleryActivity activity, SelectionManager selectionManager,
43            SlotView slotView, LabelSpec labelSpec, int cachePinSize, int cachePinMargin) {
44        super(activity, selectionManager, slotView, labelSpec,
45                activity.getResources().getColor(R.color.cache_placeholder));
46        Context context = activity;
47        mCheckedItem = new ResourceTexture(
48                context, R.drawable.btn_make_offline_normal_on_holo_dark);
49        mUnCheckedItem = new ResourceTexture(
50                context, R.drawable.btn_make_offline_normal_off_holo_dark);
51        mLocalAlbumIcon = new ResourceTexture(
52                context, R.drawable.btn_make_offline_disabled_on_holo_dark);
53        String cachingLabel = context.getString(R.string.caching_label);
54        mCachingText = StringTexture.newInstance(cachingLabel, 12, 0xffffffff);
55        mSelectionManager = selectionManager;
56        mCachePinSize = cachePinSize;
57        mCachePinMargin = cachePinMargin;
58    }
59
60    private static boolean isLocal(int dataSourceType) {
61        return dataSourceType != DataSourceType.TYPE_PICASA;
62    }
63
64    @Override
65    public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
66        AlbumSetEntry entry = mDataWindow.get(index);
67
68        boolean wantCache = entry.cacheFlag == MediaSet.CACHE_FLAG_FULL;
69        boolean isCaching = wantCache && (
70                entry.cacheStatus != MediaSet.CACHE_STATUS_CACHED_FULL);
71        boolean selected = mSelectionManager.isItemSelected(entry.setPath);
72        boolean chooseToCache = wantCache ^ selected;
73        boolean available = isLocal(entry.sourceType) || chooseToCache;
74
75        int renderRequestFlags = 0;
76
77        if (!available) {
78            canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
79            canvas.multiplyAlpha(0.6f);
80        }
81        renderRequestFlags |= renderContent(canvas, entry, width, height);
82        if (!available) canvas.restore();
83
84        renderRequestFlags |= renderLabel(canvas, entry, width, height);
85
86        drawCachingPin(canvas, entry.setPath,
87                entry.sourceType, isCaching, chooseToCache, width, height);
88
89        renderRequestFlags |= renderOverlay(canvas, index, entry, width, height);
90        return renderRequestFlags;
91    }
92
93    private void drawCachingPin(GLCanvas canvas, Path path, int dataSourceType,
94            boolean isCaching, boolean chooseToCache, int width, int height) {
95        ResourceTexture icon;
96        if (isLocal(dataSourceType)) {
97            icon = mLocalAlbumIcon;
98        } else if (chooseToCache) {
99            icon = mCheckedItem;
100        } else {
101            icon = mUnCheckedItem;
102        }
103
104        // show the icon in right bottom
105        int s = mCachePinSize;
106        int m = mCachePinMargin;
107        icon.draw(canvas, width - m - s, height - s, s, s);
108
109        if (isCaching) {
110            int w = mCachingText.getWidth();
111            int h = mCachingText.getHeight();
112            // Show the caching text in bottom center
113            mCachingText.draw(canvas, (width - w) / 2, height - h);
114        }
115    }
116}
117