1 /* 2 * Copyright (C) 2013 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.ingest.adapter; 18 19 import com.android.gallery3d.R; 20 import com.android.gallery3d.ingest.data.IngestObjectInfo; 21 import com.android.gallery3d.ingest.data.MtpDeviceIndex; 22 import com.android.gallery3d.ingest.data.MtpDeviceIndex.SortOrder; 23 import com.android.gallery3d.ingest.data.SimpleDate; 24 import com.android.gallery3d.ingest.ui.DateTileView; 25 import com.android.gallery3d.ingest.ui.MtpThumbnailTileView; 26 27 import android.annotation.TargetApi; 28 import android.app.Activity; 29 import android.content.Context; 30 import android.os.Build; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.view.ViewGroup; 34 import android.widget.BaseAdapter; 35 import android.widget.SectionIndexer; 36 37 /** 38 * Adapter for MTP thumbnail grid. 39 */ 40 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) 41 public class MtpAdapter extends BaseAdapter implements SectionIndexer { 42 public static final int ITEM_TYPE_MEDIA = 0; 43 public static final int ITEM_TYPE_BUCKET = 1; 44 45 @SuppressWarnings("unused") 46 private Context mContext; 47 private MtpDeviceIndex mModel; 48 private SortOrder mSortOrder = SortOrder.DESCENDING; 49 private LayoutInflater mInflater; 50 private int mGeneration = 0; 51 MtpAdapter(Activity context)52 public MtpAdapter(Activity context) { 53 super(); 54 mContext = context; 55 mInflater = LayoutInflater.from(context); 56 } 57 setMtpDeviceIndex(MtpDeviceIndex index)58 public void setMtpDeviceIndex(MtpDeviceIndex index) { 59 mModel = index; 60 notifyDataSetChanged(); 61 } 62 getMtpDeviceIndex()63 public MtpDeviceIndex getMtpDeviceIndex() { 64 return mModel; 65 } 66 67 @Override notifyDataSetChanged()68 public void notifyDataSetChanged() { 69 mGeneration++; 70 super.notifyDataSetChanged(); 71 } 72 73 @Override notifyDataSetInvalidated()74 public void notifyDataSetInvalidated() { 75 mGeneration++; 76 super.notifyDataSetInvalidated(); 77 } 78 deviceConnected()79 public boolean deviceConnected() { 80 return (mModel != null) && mModel.isDeviceConnected(); 81 } 82 indexReady()83 public boolean indexReady() { 84 return (mModel != null) && mModel.isIndexReady(); 85 } 86 87 @Override getCount()88 public int getCount() { 89 return mModel != null ? mModel.size() : 0; 90 } 91 92 @Override getItem(int position)93 public Object getItem(int position) { 94 return mModel.get(position, mSortOrder); 95 } 96 97 @Override areAllItemsEnabled()98 public boolean areAllItemsEnabled() { 99 return true; 100 } 101 102 @Override isEnabled(int position)103 public boolean isEnabled(int position) { 104 return true; 105 } 106 107 @Override getItemId(int position)108 public long getItemId(int position) { 109 return position; 110 } 111 112 @Override getViewTypeCount()113 public int getViewTypeCount() { 114 return 2; 115 } 116 117 @Override getItemViewType(int position)118 public int getItemViewType(int position) { 119 // If the position is the first in its section, then it corresponds to 120 // a title tile, if not it's a media tile 121 if (position == getPositionForSection(getSectionForPosition(position))) { 122 return ITEM_TYPE_BUCKET; 123 } else { 124 return ITEM_TYPE_MEDIA; 125 } 126 } 127 itemAtPositionIsBucket(int position)128 public boolean itemAtPositionIsBucket(int position) { 129 return getItemViewType(position) == ITEM_TYPE_BUCKET; 130 } 131 itemAtPositionIsMedia(int position)132 public boolean itemAtPositionIsMedia(int position) { 133 return getItemViewType(position) == ITEM_TYPE_MEDIA; 134 } 135 136 @Override getView(int position, View convertView, ViewGroup parent)137 public View getView(int position, View convertView, ViewGroup parent) { 138 int type = getItemViewType(position); 139 if (type == ITEM_TYPE_MEDIA) { 140 MtpThumbnailTileView imageView; 141 if (convertView == null) { 142 imageView = (MtpThumbnailTileView) mInflater.inflate( 143 R.layout.ingest_thumbnail, parent, false); 144 } else { 145 imageView = (MtpThumbnailTileView) convertView; 146 } 147 imageView.setMtpDeviceAndObjectInfo(mModel.getDevice(), 148 (IngestObjectInfo) getItem(position), mGeneration); 149 return imageView; 150 } else { 151 DateTileView dateTile; 152 if (convertView == null) { 153 dateTile = (DateTileView) mInflater.inflate( 154 R.layout.ingest_date_tile, parent, false); 155 } else { 156 dateTile = (DateTileView) convertView; 157 } 158 dateTile.setDate((SimpleDate) getItem(position)); 159 return dateTile; 160 } 161 } 162 163 @Override getPositionForSection(int section)164 public int getPositionForSection(int section) { 165 if (getCount() == 0) { 166 return 0; 167 } 168 int numSections = getSections().length; 169 if (section >= numSections) { 170 section = numSections - 1; 171 } 172 return mModel.getFirstPositionForBucketNumber(section, mSortOrder); 173 } 174 175 @Override getSectionForPosition(int position)176 public int getSectionForPosition(int position) { 177 int count = getCount(); 178 if (count == 0) { 179 return 0; 180 } 181 if (position >= count) { 182 position = count - 1; 183 } 184 return mModel.getBucketNumberForPosition(position, mSortOrder); 185 } 186 187 @Override getSections()188 public Object[] getSections() { 189 return getCount() > 0 ? mModel.getBuckets(mSortOrder) : null; 190 } 191 getSortOrder()192 public SortOrder getSortOrder() { 193 return mSortOrder; 194 } 195 translatePositionWithoutLabels(int position)196 public int translatePositionWithoutLabels(int position) { 197 if (mModel == null) { 198 return -1; 199 } 200 return mModel.getPositionFromPositionWithoutLabels(position, mSortOrder); 201 } 202 } 203