Home | History | Annotate | Download | only in ui
      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 
     17 package com.android.gallery3d.ui;
     18 
     19 import android.content.Context;
     20 
     21 import com.android.gallery3d.app.AlbumDataLoader;
     22 import com.android.gallery3d.app.GalleryActivity;
     23 import com.android.gallery3d.data.MediaObject;
     24 import com.android.gallery3d.data.Path;
     25 
     26 public class AlbumSlotRenderer extends AbstractSlotRenderer {
     27     @SuppressWarnings("unused")
     28     private static final String TAG = "AlbumView";
     29 
     30     public interface SlotFilter {
     31         public boolean acceptSlot(int index);
     32     }
     33 
     34     private static final int PLACEHOLDER_COLOR = 0xFF222222;
     35     private static final int CACHE_SIZE = 96;
     36 
     37     private AlbumSlidingWindow mDataWindow;
     38     private final GalleryActivity mActivity;
     39     private final ColorTexture mWaitLoadingTexture;
     40     private final SlotView mSlotView;
     41     private final SelectionManager mSelectionManager;
     42 
     43     private int mPressedIndex = -1;
     44     private boolean mAnimatePressedUp;
     45     private Path mHighlightItemPath = null;
     46     private boolean mInSelectionMode;
     47 
     48     private SlotFilter mSlotFilter;
     49 
     50     public AlbumSlotRenderer(GalleryActivity activity, SlotView slotView,
     51             SelectionManager selectionManager) {
     52         super((Context) activity);
     53         mActivity = activity;
     54         mSlotView = slotView;
     55         mSelectionManager = selectionManager;
     56 
     57         mWaitLoadingTexture = new ColorTexture(PLACEHOLDER_COLOR);
     58         mWaitLoadingTexture.setSize(1, 1);
     59     }
     60 
     61     public void setPressedIndex(int index) {
     62         if (mPressedIndex == index) return;
     63         mPressedIndex = index;
     64         mSlotView.invalidate();
     65     }
     66 
     67     public void setPressedUp() {
     68         if (mPressedIndex == -1) return;
     69         mAnimatePressedUp = true;
     70         mSlotView.invalidate();
     71     }
     72 
     73     public void setHighlightItemPath(Path path) {
     74         if (mHighlightItemPath == path) return;
     75         mHighlightItemPath = path;
     76         mSlotView.invalidate();
     77     }
     78 
     79     public void setModel(AlbumDataLoader model) {
     80         if (mDataWindow != null) {
     81             mDataWindow.setListener(null);
     82             mSlotView.setSlotCount(0);
     83             mDataWindow = null;
     84         }
     85         if (model != null) {
     86             mDataWindow = new AlbumSlidingWindow(mActivity, model, CACHE_SIZE);
     87             mDataWindow.setListener(new MyDataModelListener());
     88             mSlotView.setSlotCount(model.size());
     89         }
     90     }
     91 
     92     private static Texture checkTexture(Texture texture) {
     93         return (texture instanceof UploadedTexture)
     94                 && ((UploadedTexture) texture).isUploading()
     95                 ? null
     96                 : texture;
     97     }
     98 
     99     @Override
    100     public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
    101         if (mSlotFilter != null && !mSlotFilter.acceptSlot(index)) return 0;
    102 
    103         AlbumSlidingWindow.AlbumEntry entry = mDataWindow.get(index);
    104 
    105         int renderRequestFlags = 0;
    106 
    107         Texture content = checkTexture(entry.content);
    108         if (content == null) {
    109             content = mWaitLoadingTexture;
    110             entry.isWaitDisplayed = true;
    111         } else if (entry.isWaitDisplayed) {
    112             entry.isWaitDisplayed = false;
    113             content = new FadeInTexture(PLACEHOLDER_COLOR, entry.bitmapTexture);
    114             entry.content = content;
    115         }
    116         drawContent(canvas, content, width, height, entry.rotation);
    117         if ((content instanceof FadeInTexture) &&
    118                 ((FadeInTexture) content).isAnimating()) {
    119             renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
    120         }
    121 
    122         if (entry.mediaType == MediaObject.MEDIA_TYPE_VIDEO) {
    123             drawVideoOverlay(canvas, width, height);
    124         }
    125 
    126         if (entry.isPanorama) {
    127             drawPanoramaBorder(canvas, width, height);
    128         }
    129 
    130         renderRequestFlags |= renderOverlay(canvas, index, entry, width, height);
    131 
    132         return renderRequestFlags;
    133     }
    134 
    135     private int renderOverlay(GLCanvas canvas, int index,
    136             AlbumSlidingWindow.AlbumEntry entry, int width, int height) {
    137         int renderRequestFlags = 0;
    138         if (mPressedIndex == index) {
    139             if (mAnimatePressedUp) {
    140                 drawPressedUpFrame(canvas, width, height);
    141                 renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
    142                 if (isPressedUpFrameFinished()) {
    143                     mAnimatePressedUp = false;
    144                     mPressedIndex = -1;
    145                 }
    146             } else {
    147                 drawPressedFrame(canvas, width, height);
    148             }
    149         } else if ((entry.path != null) && (mHighlightItemPath == entry.path)) {
    150             drawSelectedFrame(canvas, width, height);
    151         } else if (mInSelectionMode && mSelectionManager.isItemSelected(entry.path)) {
    152             drawSelectedFrame(canvas, width, height);
    153         }
    154         return renderRequestFlags;
    155     }
    156 
    157     private class MyDataModelListener implements AlbumSlidingWindow.Listener {
    158         @Override
    159         public void onContentChanged() {
    160             mSlotView.invalidate();
    161         }
    162 
    163         @Override
    164         public void onSizeChanged(int size) {
    165             mSlotView.setSlotCount(size);
    166         }
    167     }
    168 
    169     public void resume() {
    170         mDataWindow.resume();
    171     }
    172 
    173     public void pause() {
    174         mDataWindow.pause();
    175     }
    176 
    177     @Override
    178     public void prepareDrawing() {
    179         mInSelectionMode = mSelectionManager.inSelectionMode();
    180     }
    181 
    182     @Override
    183     public void onVisibleRangeChanged(int visibleStart, int visibleEnd) {
    184         if (mDataWindow != null) {
    185             mDataWindow.setActiveWindow(visibleStart, visibleEnd);
    186         }
    187     }
    188 
    189     @Override
    190     public void onSlotSizeChanged(int width, int height) {
    191         // Do nothing
    192     }
    193 
    194     public void setSlotFilter(SlotFilter slotFilter) {
    195         mSlotFilter = slotFilter;
    196     }
    197 }
    198