2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ingest.adapter;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.mtp.MtpObjectInfo;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.BaseAdapter;
26 import android.widget.SectionIndexer;
28 import com.android.gallery3d.R;
29 import com.android.gallery3d.ingest.MtpDeviceIndex;
30 import com.android.gallery3d.ingest.MtpDeviceIndex.SortOrder;
31 import com.android.gallery3d.ingest.SimpleDate;
32 import com.android.gallery3d.ingest.ui.DateTileView;
33 import com.android.gallery3d.ingest.ui.MtpThumbnailTileView;
35 public class MtpAdapter extends BaseAdapter implements SectionIndexer {
36 public static final int ITEM_TYPE_MEDIA = 0;
37 public static final int ITEM_TYPE_BUCKET = 1;
39 private Context mContext;
40 private MtpDeviceIndex mModel;
41 private SortOrder mSortOrder = SortOrder.Descending;
42 private LayoutInflater mInflater;
43 private int mGeneration = 0;
45 public MtpAdapter(Activity context) {
48 mInflater = LayoutInflater.from(context);
51 public void setMtpDeviceIndex(MtpDeviceIndex index) {
53 notifyDataSetChanged();
56 public MtpDeviceIndex getMtpDeviceIndex() {
61 public void notifyDataSetChanged() {
63 super.notifyDataSetChanged();
67 public void notifyDataSetInvalidated() {
69 super.notifyDataSetInvalidated();
72 public boolean deviceConnected() {
73 return (mModel != null) && (mModel.getDevice() != null);
76 public boolean indexReady() {
77 return (mModel != null) && mModel.indexReady();
81 public int getCount() {
82 return mModel != null ? mModel.size() : 0;
86 public Object getItem(int position) {
87 return mModel.get(position, mSortOrder);
91 public boolean areAllItemsEnabled() {
96 public boolean isEnabled(int position) {
101 public long getItemId(int position) {
106 public int getViewTypeCount() {
111 public int getItemViewType(int position) {
112 // If the position is the first in its section, then it corresponds to
113 // a title tile, if not it's a media tile
114 if (position == getPositionForSection(getSectionForPosition(position))) {
115 return ITEM_TYPE_BUCKET;
117 return ITEM_TYPE_MEDIA;
121 public boolean itemAtPositionIsBucket(int position) {
122 return getItemViewType(position) == ITEM_TYPE_BUCKET;
125 public boolean itemAtPositionIsMedia(int position) {
126 return getItemViewType(position) == ITEM_TYPE_MEDIA;
130 public View getView(int position, View convertView, ViewGroup parent) {
131 int type = getItemViewType(position);
132 if (type == ITEM_TYPE_MEDIA) {
133 MtpThumbnailTileView imageView;
134 if (convertView == null) {
135 imageView = (MtpThumbnailTileView) mInflater.inflate(
136 R.layout.ingest_thumbnail, parent, false);
138 imageView = (MtpThumbnailTileView) convertView;
140 imageView.setMtpDeviceAndObjectInfo(mModel.getDevice(), (MtpObjectInfo)getItem(position), mGeneration);
143 DateTileView dateTile;
144 if (convertView == null) {
145 dateTile = (DateTileView) mInflater.inflate(
146 R.layout.ingest_date_tile, parent, false);
148 dateTile = (DateTileView) convertView;
150 dateTile.setDate((SimpleDate)getItem(position));
156 public int getPositionForSection(int section) {
157 if (getCount() == 0) {
160 int numSections = getSections().length;
161 if (section >= numSections) {
162 section = numSections - 1;
164 return mModel.getFirstPositionForBucketNumber(section, mSortOrder);
168 public int getSectionForPosition(int position) {
169 int count = getCount();
173 if (position >= count) {
174 position = count - 1;
176 return mModel.getBucketNumberForPosition(position, mSortOrder);
180 public Object[] getSections() {
181 return getCount() > 0 ? mModel.getBuckets(mSortOrder) : null;
184 public SortOrder getSortOrder() {
188 public int translatePositionWithoutLabels(int position) {
189 if (mModel == null) return -1;
190 return mModel.getPositionFromPositionWithoutLabels(position, mSortOrder);