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 com.android.gallery3d.R;
     20 import com.android.gallery3d.app.AbstractGalleryActivity;
     21 import com.android.gallery3d.app.AlbumSetDataLoader;
     22 import com.android.gallery3d.data.MediaObject;
     23 import com.android.gallery3d.data.Path;
     24 import com.android.gallery3d.ui.AlbumSetSlidingWindow.AlbumSetEntry;
     25 
     26 public class AlbumSetSlotRenderer extends AbstractSlotRenderer {
     27     @SuppressWarnings("unused")
     28     private static final String TAG = "AlbumSetView";
     29     private static final int CACHE_SIZE = 96;
     30     private final int mPlaceholderColor;
     31 
     32     private final ColorTexture mWaitLoadingTexture;
     33     private final ResourceTexture mCameraOverlay;
     34     private final AbstractGalleryActivity mActivity;
     35     private final SelectionManager mSelectionManager;
     36     protected final LabelSpec mLabelSpec;
     37 
     38     protected AlbumSetSlidingWindow mDataWindow;
     39     private SlotView mSlotView;
     40 
     41     private int mPressedIndex = -1;
     42     private boolean mAnimatePressedUp;
     43     private Path mHighlightItemPath = null;
     44     private boolean mInSelectionMode;
     45 
     46     public static class LabelSpec {
     47         public int labelBackgroundHeight;
     48         public int titleOffset;
     49         public int countOffset;
     50         public int titleFontSize;
     51         public int countFontSize;
     52         public int leftMargin;
     53         public int iconSize;
     54         public int titleRightMargin;
     55         public int backgroundColor;
     56         public int titleColor;
     57         public int countColor;
     58         public int borderSize;
     59     }
     60 
     61     public AlbumSetSlotRenderer(AbstractGalleryActivity activity,
     62             SelectionManager selectionManager,
     63             SlotView slotView, LabelSpec labelSpec, int placeholderColor) {
     64         super (activity);
     65         mActivity = activity;
     66         mSelectionManager = selectionManager;
     67         mSlotView = slotView;
     68         mLabelSpec = labelSpec;
     69         mPlaceholderColor = placeholderColor;
     70 
     71         mWaitLoadingTexture = new ColorTexture(mPlaceholderColor);
     72         mWaitLoadingTexture.setSize(1, 1);
     73         mCameraOverlay = new ResourceTexture(activity,
     74                 R.drawable.ic_cameraalbum_overlay);
     75     }
     76 
     77     public void setPressedIndex(int index) {
     78         if (mPressedIndex == index) return;
     79         mPressedIndex = index;
     80         mSlotView.invalidate();
     81     }
     82 
     83     public void setPressedUp() {
     84         if (mPressedIndex == -1) return;
     85         mAnimatePressedUp = true;
     86         mSlotView.invalidate();
     87     }
     88 
     89     public void setHighlightItemPath(Path path) {
     90         if (mHighlightItemPath == path) return;
     91         mHighlightItemPath = path;
     92         mSlotView.invalidate();
     93     }
     94 
     95     public void setModel(AlbumSetDataLoader model) {
     96         if (mDataWindow != null) {
     97             mDataWindow.setListener(null);
     98             mDataWindow = null;
     99             mSlotView.setSlotCount(0);
    100         }
    101         if (model != null) {
    102             mDataWindow = new AlbumSetSlidingWindow(
    103                     mActivity, model, mLabelSpec, CACHE_SIZE);
    104             mDataWindow.setListener(new MyCacheListener());
    105             mSlotView.setSlotCount(mDataWindow.size());
    106         }
    107     }
    108 
    109     private static Texture checkLabelTexture(Texture texture) {
    110         return ((texture instanceof UploadedTexture)
    111                 && ((UploadedTexture) texture).isUploading())
    112                 ? null
    113                 : texture;
    114     }
    115 
    116     private static Texture checkContentTexture(Texture texture) {
    117         return ((texture instanceof TiledTexture)
    118                 && !((TiledTexture) texture).isReady())
    119                 ? null
    120                 : texture;
    121     }
    122 
    123     @Override
    124     public int renderSlot(GLCanvas canvas, int index, int pass, int width, int height) {
    125         AlbumSetEntry entry = mDataWindow.get(index);
    126         int renderRequestFlags = 0;
    127         renderRequestFlags |= renderContent(canvas, entry, width, height);
    128         renderRequestFlags |= renderLabel(canvas, entry, width, height);
    129         renderRequestFlags |= renderOverlay(canvas, index, entry, width, height);
    130         return renderRequestFlags;
    131     }
    132 
    133     protected int renderOverlay(
    134             GLCanvas canvas, int index, AlbumSetEntry entry, int width, int height) {
    135         int renderRequestFlags = 0;
    136         if (entry.album != null && entry.album.isCameraRoll()) {
    137             int uncoveredHeight = height - mLabelSpec.labelBackgroundHeight;
    138             int dim = uncoveredHeight / 2;
    139             mCameraOverlay.draw(canvas, (width - dim) / 2,
    140                     (uncoveredHeight - dim) / 2, dim, dim);
    141         }
    142         if (mPressedIndex == index) {
    143             if (mAnimatePressedUp) {
    144                 drawPressedUpFrame(canvas, width, height);
    145                 renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
    146                 if (isPressedUpFrameFinished()) {
    147                     mAnimatePressedUp = false;
    148                     mPressedIndex = -1;
    149                 }
    150             } else {
    151                 drawPressedFrame(canvas, width, height);
    152             }
    153         } else if ((mHighlightItemPath != null) && (mHighlightItemPath == entry.setPath)) {
    154             drawSelectedFrame(canvas, width, height);
    155         } else if (mInSelectionMode && mSelectionManager.isItemSelected(entry.setPath)) {
    156             drawSelectedFrame(canvas, width, height);
    157         }
    158         return renderRequestFlags;
    159     }
    160 
    161     protected int renderContent(
    162             GLCanvas canvas, AlbumSetEntry entry, int width, int height) {
    163         int renderRequestFlags = 0;
    164 
    165         Texture content = checkContentTexture(entry.content);
    166         if (content == null) {
    167             content = mWaitLoadingTexture;
    168             entry.isWaitLoadingDisplayed = true;
    169         } else if (entry.isWaitLoadingDisplayed) {
    170             entry.isWaitLoadingDisplayed = false;
    171             content = new FadeInTexture(mPlaceholderColor, entry.bitmapTexture);
    172             entry.content = content;
    173         }
    174         drawContent(canvas, content, width, height, entry.rotation);
    175         if ((content instanceof FadeInTexture) &&
    176                 ((FadeInTexture) content).isAnimating()) {
    177             renderRequestFlags |= SlotView.RENDER_MORE_FRAME;
    178         }
    179 
    180         return renderRequestFlags;
    181     }
    182 
    183     protected int renderLabel(
    184             GLCanvas canvas, AlbumSetEntry entry, int width, int height) {
    185         Texture content = checkLabelTexture(entry.labelTexture);
    186         if (content == null) {
    187             content = mWaitLoadingTexture;
    188         }
    189         int b = AlbumLabelMaker.getBorderSize();
    190         int h = mLabelSpec.labelBackgroundHeight;
    191         content.draw(canvas, -b, height - h + b, width + b + b, h);
    192 
    193         return 0;
    194     }
    195 
    196     @Override
    197     public void prepareDrawing() {
    198         mInSelectionMode = mSelectionManager.inSelectionMode();
    199     }
    200 
    201     private class MyCacheListener implements AlbumSetSlidingWindow.Listener {
    202 
    203         @Override
    204         public void onSizeChanged(int size) {
    205             mSlotView.setSlotCount(size);
    206         }
    207 
    208         @Override
    209         public void onContentChanged() {
    210             mSlotView.invalidate();
    211         }
    212     }
    213 
    214     public void pause() {
    215         mDataWindow.pause();
    216     }
    217 
    218     public void resume() {
    219         mDataWindow.resume();
    220     }
    221 
    222     @Override
    223     public void onVisibleRangeChanged(int visibleStart, int visibleEnd) {
    224         if (mDataWindow != null) {
    225             mDataWindow.setActiveWindow(visibleStart, visibleEnd);
    226         }
    227     }
    228 
    229     @Override
    230     public void onSlotSizeChanged(int width, int height) {
    231         if (mDataWindow != null) {
    232             mDataWindow.onSlotSizeChanged(width, height);
    233         }
    234     }
    235 }
    236