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