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 android.content.Context; 20 import android.mtp.MtpObjectInfo; 21 import android.support.v4.view.PagerAdapter; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import com.android.gallery3d.R; 27 import com.android.gallery3d.ingest.MtpDeviceIndex; 28 import com.android.gallery3d.ingest.MtpDeviceIndex.SortOrder; 29 import com.android.gallery3d.ingest.ui.MtpFullscreenView; 30 31 public class MtpPagerAdapter extends PagerAdapter { 32 33 private LayoutInflater mInflater; 34 private int mGeneration = 0; 35 private CheckBroker mBroker; 36 private MtpDeviceIndex mModel; 37 private SortOrder mSortOrder = SortOrder.Descending; 38 39 private MtpFullscreenView mReusableView = null; 40 MtpPagerAdapter(Context context, CheckBroker broker)41 public MtpPagerAdapter(Context context, CheckBroker broker) { 42 super(); 43 mInflater = LayoutInflater.from(context); 44 mBroker = broker; 45 } 46 setMtpDeviceIndex(MtpDeviceIndex index)47 public void setMtpDeviceIndex(MtpDeviceIndex index) { 48 mModel = index; 49 notifyDataSetChanged(); 50 } 51 52 @Override getCount()53 public int getCount() { 54 return mModel != null ? mModel.sizeWithoutLabels() : 0; 55 } 56 57 @Override notifyDataSetChanged()58 public void notifyDataSetChanged() { 59 mGeneration++; 60 super.notifyDataSetChanged(); 61 } 62 translatePositionWithLabels(int position)63 public int translatePositionWithLabels(int position) { 64 if (mModel == null) return -1; 65 return mModel.getPositionWithoutLabelsFromPosition(position, mSortOrder); 66 } 67 68 @Override finishUpdate(ViewGroup container)69 public void finishUpdate(ViewGroup container) { 70 mReusableView = null; 71 super.finishUpdate(container); 72 } 73 74 @Override isViewFromObject(View view, Object object)75 public boolean isViewFromObject(View view, Object object) { 76 return view == object; 77 } 78 79 @Override destroyItem(ViewGroup container, int position, Object object)80 public void destroyItem(ViewGroup container, int position, Object object) { 81 MtpFullscreenView v = (MtpFullscreenView)object; 82 container.removeView(v); 83 mBroker.unregisterOnCheckedChangeListener(v); 84 mReusableView = v; 85 } 86 87 @Override instantiateItem(ViewGroup container, int position)88 public Object instantiateItem(ViewGroup container, int position) { 89 MtpFullscreenView v; 90 if (mReusableView != null) { 91 v = mReusableView; 92 mReusableView = null; 93 } else { 94 v = (MtpFullscreenView) mInflater.inflate(R.layout.ingest_fullsize, container, false); 95 } 96 MtpObjectInfo i = mModel.getWithoutLabels(position, mSortOrder); 97 v.getImageView().setMtpDeviceAndObjectInfo(mModel.getDevice(), i, mGeneration); 98 v.setPositionAndBroker(position, mBroker); 99 container.addView(v); 100 return v; 101 } 102 } 103