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