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