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