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.camera.data;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.AsyncTask;
22 import android.view.View;
23 
24 import com.android.camera.data.FilmstripItem.VideoClickedCallback;
25 import com.google.common.base.Optional;
26 
27 /**
28  * A {@link LocalFilmstripDataAdapter} which puts a {@link FilmstripItem} fixed at the last
29  * position. It's done by combining a {@link FilmstripItem} and another
30  * {@link LocalFilmstripDataAdapter}.
31  */
32 public class FixedLastProxyAdapter extends FilmstripDataAdapterProxy {
33     private FilmstripItem mLastData;
34     private Listener mListener;
35 
36     /**
37      * Constructor.
38      *
39      * @param context A valid Android context.
40      * @param wrappedAdapter The {@link LocalFilmstripDataAdapter} to be wrapped.
41      * @param lastData The {@link FilmstripItem} to be placed at the last position.
42      */
FixedLastProxyAdapter( Context context, LocalFilmstripDataAdapter wrappedAdapter, FilmstripItem lastData)43     public FixedLastProxyAdapter(
44           Context context,
45           LocalFilmstripDataAdapter wrappedAdapter,
46           FilmstripItem lastData) {
47         super(context, wrappedAdapter);
48         if (lastData == null) {
49             throw new AssertionError("data is null");
50         }
51         mLastData = lastData;
52     }
53 
54     @Override
setListener(Listener listener)55     public void setListener(Listener listener) {
56         super.setListener(listener);
57         mListener = listener;
58     }
59 
60     @Override
getItemAt(int index)61     public FilmstripItem getItemAt(int index) {
62         int totalNumber = mAdapter.getTotalNumber();
63 
64         if (index < totalNumber) {
65             return mAdapter.getItemAt(index);
66         } else if (index == totalNumber) {
67             return mLastData;
68         }
69         return null;
70     }
71 
72     @Override
removeAt(int index)73     public void removeAt(int index) {
74         if (index < mAdapter.getTotalNumber()) {
75             mAdapter.removeAt(index);
76         }
77     }
78 
79     @Override
findByContentUri(Uri uri)80     public int findByContentUri(Uri uri) {
81         return mAdapter.findByContentUri(uri);
82     }
83 
84     @Override
updateItemAt(final int pos, FilmstripItem item)85     public void updateItemAt(final int pos, FilmstripItem item) {
86         int totalNumber = mAdapter.getTotalNumber();
87 
88         if (pos < totalNumber) {
89             mAdapter.updateItemAt(pos, item);
90         } else if (pos == totalNumber) {
91             mLastData = item;
92             if (mListener != null) {
93                 mListener.onFilmstripItemUpdated(new UpdateReporter() {
94                     @Override
95                     public boolean isDataRemoved(int index) {
96                         return false;
97                     }
98 
99                     @Override
100                     public boolean isDataUpdated(int index) {
101                         return (index == pos);
102                     }
103                 });
104             }
105         }
106     }
107 
108     @Override
getTotalNumber()109     public int getTotalNumber() {
110         return mAdapter.getTotalNumber() + 1;
111     }
112 
113     @Override
getView(View recycled, int index, VideoClickedCallback videoClickedCallback)114     public View getView(View recycled, int index, VideoClickedCallback videoClickedCallback) {
115         int totalNumber = mAdapter.getTotalNumber();
116 
117         if (index < totalNumber) {
118             return mAdapter.getView(recycled, index, videoClickedCallback);
119         } else if (index == totalNumber) {
120             mLastData.setSuggestedSize(mSuggestedWidth, mSuggestedHeight);
121             return mLastData.getView(Optional.fromNullable(recycled), null, false,
122                   videoClickedCallback);
123         }
124         return null;
125     }
126 
127     @Override
getItemViewType(int index)128     public int getItemViewType(int index) {
129         int totalNumber = mAdapter.getTotalNumber();
130 
131         if (index < totalNumber) {
132             return mAdapter.getItemViewType(index);
133         } else if (index == totalNumber) {
134             return mLastData.getItemViewType().ordinal();
135         }
136         return -1;
137    }
138 
139     @Override
getFilmstripItemAt(int index)140     public FilmstripItem getFilmstripItemAt(int index) {
141         int totalNumber = mAdapter.getTotalNumber();
142 
143         if (index < totalNumber) {
144             return mAdapter.getFilmstripItemAt(index);
145         } else if (index == totalNumber) {
146             return mLastData;
147         }
148         return null;
149     }
150 
151     @Override
updateMetadataAt(int index)152     public AsyncTask updateMetadataAt(int index) {
153         if (index < mAdapter.getTotalNumber()) {
154             return mAdapter.updateMetadataAt(index);
155         } else {
156             MetadataLoader.loadMetadata(mContext, mLastData);
157         }
158         return null;
159     }
160 
161     @Override
isMetadataUpdatedAt(int index)162     public boolean isMetadataUpdatedAt(int index) {
163         if (index < mAdapter.getTotalNumber()) {
164             return mAdapter.isMetadataUpdatedAt(index);
165         } else {
166             return mLastData.getMetadata().isLoaded();
167         }
168     }
169 }
170