Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 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.ContentResolver; |
| 20 | import android.content.Context; |
Angus Kong | 885367c | 2013-05-31 16:30:32 -0700 | [diff] [blame] | 21 | import android.net.Uri; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 22 | import android.os.AsyncTask; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 23 | import android.view.View; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 24 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 25 | import com.android.camera.debug.Log; |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 26 | import com.android.camera.filmstrip.ImageData; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 27 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 29 | import java.util.Comparator; |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 30 | import java.util.List; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 31 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 32 | /** |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 33 | * A {@link LocalDataAdapter} that provides data in the camera folder. |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 34 | */ |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 35 | public class CameraDataAdapter implements LocalDataAdapter { |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 36 | private static final Log.Tag TAG = new Log.Tag("CameraDataAdapter"); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 37 | |
Angus Kong | 653c43b | 2013-08-21 18:28:43 -0700 | [diff] [blame] | 38 | private static final int DEFAULT_DECODE_SIZE = 1600; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 39 | |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 40 | private final Context mContext; |
| 41 | |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 42 | private LocalDataList mImages; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 43 | |
| 44 | private Listener mListener; |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 45 | private LocalDataListener mLocalDataListener; |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 46 | private final int mPlaceHolderResourceId; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 47 | |
| 48 | private int mSuggestedWidth = DEFAULT_DECODE_SIZE; |
| 49 | private int mSuggestedHeight = DEFAULT_DECODE_SIZE; |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 50 | private long mLastPhotoId = LocalMediaData.QUERY_ALL_MEDIA_ID; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 51 | |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 52 | private LocalData mLocalDataToDelete; |
Angus Kong | 49448f8 | 2013-06-05 13:12:39 -0700 | [diff] [blame] | 53 | |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 54 | public CameraDataAdapter(Context context, int placeholderResource) { |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 55 | mContext = context; |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 56 | mImages = new LocalDataList(); |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 57 | mPlaceHolderResourceId = placeholderResource; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 60 | @Override |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 61 | public void setLocalDataListener(LocalDataListener listener) { |
| 62 | mLocalDataListener = listener; |
| 63 | } |
| 64 | |
| 65 | @Override |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 66 | public void requestLoadNewPhotos() { |
| 67 | LoadNewPhotosTask ltask = new LoadNewPhotosTask(mLastPhotoId); |
| 68 | ltask.execute(mContext.getContentResolver()); |
| 69 | } |
| 70 | |
| 71 | @Override |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 72 | public void requestLoad() { |
| 73 | QueryTask qtask = new QueryTask(); |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 74 | qtask.execute(mContext); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 77 | @Override |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 78 | public AsyncTask updateMetadata(int dataId) { |
| 79 | MetadataUpdateTask result = new MetadataUpdateTask(); |
| 80 | result.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataId); |
| 81 | return result; |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean isMetadataUpdated(int dataId) { |
| 86 | if (dataId < 0 || dataId >= mImages.size()) { |
| 87 | return true; |
| 88 | } |
| 89 | return mImages.get(dataId).isMetadataUpdated(); |
| 90 | } |
| 91 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 92 | @Override |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 93 | public int getItemViewType(int dataId) { |
| 94 | if (dataId > mImages.size() || dataId < 0) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return mImages.get(dataId).getItemViewType().ordinal(); |
| 99 | } |
| 100 | |
| 101 | @Override |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 102 | public LocalData getLocalData(int dataID) { |
Angus Kong | 81c5992 | 2013-08-09 17:09:35 -0700 | [diff] [blame] | 103 | if (dataID < 0 || dataID >= mImages.size()) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 104 | return null; |
| 105 | } |
| 106 | |
| 107 | return mImages.get(dataID); |
| 108 | } |
| 109 | |
| 110 | @Override |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 111 | public int getTotalNumber() { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 112 | return mImages.size(); |
| 113 | } |
| 114 | |
| 115 | @Override |
Angus Kong | 01054e9 | 2013-12-10 11:06:18 -0800 | [diff] [blame] | 116 | public ImageData getImageData(int id) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 117 | return getLocalData(id); |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 120 | @Override |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 121 | public void suggestViewSizeBound(int w, int h) { |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 122 | mSuggestedWidth = w; |
| 123 | mSuggestedHeight = h; |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 127 | public View getView(Context context, View recycled, int dataID) { |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 128 | if (dataID >= mImages.size() || dataID < 0) { |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | return mImages.get(dataID).getView( |
Sam Judd | 43bf03f | 2014-03-17 11:27:03 -0700 | [diff] [blame] | 133 | context, recycled, mSuggestedWidth, mSuggestedHeight, |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 134 | mPlaceHolderResourceId, this, /* inProgress */ false); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 138 | public void resizeView(Context context, int dataID, View view, int w, int h) { |
| 139 | if (dataID >= mImages.size() || dataID < 0) { |
| 140 | return; |
| 141 | } |
Sam Judd | 4021c89 | 2014-03-17 12:57:50 -0700 | [diff] [blame] | 142 | mImages.get(dataID).loadFullImage(context, w, h, view, this); |
Andy Huibers | caca8c7 | 2013-12-13 15:53:43 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 146 | public void setListener(Listener listener) { |
| 147 | mListener = listener; |
Angus Kong | cb8942b | 2013-12-16 14:32:49 -0800 | [diff] [blame] | 148 | if (mImages.size() != 0) { |
Angus Kong | 750e8ec | 2013-05-06 10:42:28 -0700 | [diff] [blame] | 149 | mListener.onDataLoaded(); |
| 150 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Angus Kong | 49448f8 | 2013-06-05 13:12:39 -0700 | [diff] [blame] | 153 | @Override |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 154 | public boolean canSwipeInFullScreen(int dataID) { |
| 155 | if (dataID < mImages.size() && dataID > 0) { |
| 156 | return mImages.get(dataID).canSwipeInFullScreen(); |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | @Override |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 162 | public void removeData(int dataID) { |
Sascha Haeberling | 14ff6c8 | 2013-12-13 13:29:58 -0800 | [diff] [blame] | 163 | if (dataID >= mImages.size()) { |
| 164 | return; |
| 165 | } |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 166 | LocalData d = mImages.remove(dataID); |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 167 | // Delete previously removed data first. |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 168 | executeDeletion(); |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 169 | mLocalDataToDelete = d; |
Angus Kong | 87f9a62 | 2013-05-16 16:59:39 -0700 | [diff] [blame] | 170 | mListener.onDataRemoved(dataID, d); |
| 171 | } |
| 172 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 173 | @Override |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 174 | public boolean addData(LocalData newData) { |
| 175 | final Uri uri = newData.getUri(); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 176 | int pos = findDataByContentUri(uri); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 177 | if (pos != -1) { |
| 178 | // a duplicate one, just do a substitute. |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 179 | Log.v(TAG, "found duplicate data"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 180 | updateData(pos, newData); |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 181 | return false; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 182 | } else { |
| 183 | // a new data. |
| 184 | insertData(newData); |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 185 | return true; |
Angus Kong | 885367c | 2013-05-31 16:30:32 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public int findDataByContentUri(Uri uri) { |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 191 | // LocalDataList will return in O(1) if the uri is not contained. |
| 192 | // Otherwise the performance is O(n), but this is acceptable as we will |
| 193 | // most often call this to find an element at the beginning of the list. |
| 194 | return mImages.indexOf(uri); |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 198 | public boolean undoDataRemoval() { |
| 199 | if (mLocalDataToDelete == null) return false; |
| 200 | LocalData d = mLocalDataToDelete; |
| 201 | mLocalDataToDelete = null; |
| 202 | insertData(d); |
| 203 | return true; |
| 204 | } |
| 205 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 206 | @Override |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 207 | public boolean executeDeletion() { |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 208 | if (mLocalDataToDelete == null) return false; |
| 209 | |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 210 | DeletionTask task = new DeletionTask(); |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 211 | task.execute(mLocalDataToDelete); |
| 212 | mLocalDataToDelete = null; |
| 213 | return true; |
| 214 | } |
| 215 | |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 216 | @Override |
| 217 | public void flush() { |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 218 | replaceData(new LocalDataList()); |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 221 | @Override |
Seth Raphael | 455ba5a | 2014-02-13 15:10:06 -0800 | [diff] [blame] | 222 | public void refresh(Uri uri) { |
| 223 | final int pos = findDataByContentUri(uri); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 224 | if (pos == -1) { |
| 225 | return; |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 226 | } |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 227 | |
| 228 | LocalData data = mImages.get(pos); |
Sascha Haeberling | 44c1afb | 2013-12-20 11:59:35 -0800 | [diff] [blame] | 229 | LocalData refreshedData = data.refresh(mContext); |
Sascha Haeberling | 14ff6c8 | 2013-12-13 13:29:58 -0800 | [diff] [blame] | 230 | |
Angus Kong | 2520b50 | 2014-01-10 17:45:27 -0800 | [diff] [blame] | 231 | // Refresh failed. Probably removed already. |
| 232 | if (refreshedData == null && mListener != null) { |
| 233 | mListener.onDataRemoved(pos, data); |
| 234 | return; |
| 235 | } |
Angus Kong | 2520b50 | 2014-01-10 17:45:27 -0800 | [diff] [blame] | 236 | updateData(pos, refreshedData); |
Angus Kong | 8e5e4ee | 2013-07-30 11:36:00 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void updateData(final int pos, LocalData data) { |
| 241 | mImages.set(pos, data); |
| 242 | if (mListener != null) { |
| 243 | mListener.onDataUpdated(new UpdateReporter() { |
| 244 | @Override |
| 245 | public boolean isDataRemoved(int dataID) { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public boolean isDataUpdated(int dataID) { |
| 251 | return (dataID == pos); |
| 252 | } |
| 253 | }); |
| 254 | } |
| 255 | } |
| 256 | |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 257 | private void insertData(LocalData data) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 258 | // Since this function is mostly for adding the newest data, |
| 259 | // a simple linear search should yield the best performance over a |
| 260 | // binary search. |
| 261 | int pos = 0; |
| 262 | Comparator<LocalData> comp = new LocalData.NewestFirstComparator(); |
| 263 | for (; pos < mImages.size() |
| 264 | && comp.compare(data, mImages.get(pos)) > 0; pos++); |
| 265 | mImages.add(pos, data); |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 266 | updateMetadata(pos); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 267 | if (mListener != null) { |
| 268 | mListener.onDataInserted(pos, data); |
| 269 | } |
| 270 | } |
Angus Kong | a06b4e2 | 2013-06-03 11:10:11 -0700 | [diff] [blame] | 271 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 272 | /** Update all the data */ |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 273 | private void replaceData(LocalDataList list) { |
Angus Kong | d806e65 | 2013-08-12 15:54:46 -0700 | [diff] [blame] | 274 | if (list.size() == 0 && mImages.size() == 0) { |
| 275 | return; |
| 276 | } |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 277 | mImages = list; |
Angus Kong | d806e65 | 2013-08-12 15:54:46 -0700 | [diff] [blame] | 278 | if (mListener != null) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 279 | mListener.onDataLoaded(); |
Angus Kong | a06b4e2 | 2013-06-03 11:10:11 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 283 | @Override |
| 284 | public List<AsyncTask> preloadItems(List<Integer> items) { |
| 285 | List<AsyncTask> result = new ArrayList<AsyncTask>(); |
| 286 | for (Integer id : items) { |
| 287 | if (!isMetadataUpdated(id)) { |
| 288 | result.add(updateMetadata(id)); |
| 289 | } |
| 290 | } |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void cancelItems(List<AsyncTask> loadTokens) { |
| 296 | for (AsyncTask asyncTask : loadTokens) { |
| 297 | if (asyncTask != null) { |
| 298 | asyncTask.cancel(false); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public List<Integer> getItemsInRange(int startPosition, int endPosition) { |
| 305 | List<Integer> result = new ArrayList<Integer>(); |
| 306 | for (int i = Math.max(0, startPosition); i < endPosition; i++) { |
| 307 | result.add(i); |
| 308 | } |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public int getCount() { |
| 314 | return getTotalNumber(); |
| 315 | } |
| 316 | |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 317 | private class LoadNewPhotosTask extends AsyncTask<ContentResolver, Void, List<LocalData>> { |
| 318 | |
| 319 | private long mMinPhotoId; |
| 320 | |
| 321 | public LoadNewPhotosTask(long lastPhotoId) { |
| 322 | mMinPhotoId = lastPhotoId; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Loads any new photos added to our storage directory since our last query. |
| 327 | * @param contentResolvers {@link android.content.ContentResolver} to load data. |
| 328 | * @return An {@link java.util.ArrayList} containing any new data. |
| 329 | */ |
| 330 | @Override |
| 331 | protected List<LocalData> doInBackground(ContentResolver... contentResolvers) { |
| 332 | final ContentResolver cr = contentResolvers[0]; |
| 333 | return LocalMediaData.PhotoData.query(cr, LocalMediaData.PhotoData.CONTENT_URI, |
| 334 | mMinPhotoId); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | protected void onPostExecute(List<LocalData> newPhotoData) { |
| 339 | if (!newPhotoData.isEmpty()) { |
| 340 | LocalData newestPhoto = newPhotoData.get(0); |
| 341 | // We may overlap with another load task or a query task, in which case we want |
| 342 | // to be sure we never decrement the oldest seen id. |
| 343 | mLastPhotoId = Math.max(mLastPhotoId, newestPhoto.getContentId()); |
| 344 | } |
| 345 | // We may add data that is already present, but if we do, it will be deduped in addData. |
| 346 | for (LocalData localData : newPhotoData) { |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 347 | addData(localData); |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | private class QueryTaskResult { |
| 353 | public LocalDataList mLocalDataList; |
| 354 | public long mLastPhotoId; |
| 355 | |
| 356 | public QueryTaskResult(LocalDataList localDataList, long lastPhotoId) { |
| 357 | mLocalDataList = localDataList; |
| 358 | mLastPhotoId = lastPhotoId; |
| 359 | } |
| 360 | } |
| 361 | |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 362 | private class QueryTask extends AsyncTask<Context, Void, QueryTaskResult> { |
| 363 | // The maximum number of data to load metadata for in a single task. |
| 364 | private static final int MAX_METADATA = 5; |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 365 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Loads all the photo and video data in the camera folder in background |
| 368 | * and combine them into one single list. |
| 369 | * |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 370 | * @param contexts {@link Context} to load all the data. |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 371 | * @return An {@link com.android.camera.data.CameraDataAdapter.QueryTaskResult} containing |
| 372 | * all loaded data and the highest photo id in the dataset. |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 373 | */ |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 374 | @Override |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 375 | protected QueryTaskResult doInBackground(Context... contexts) { |
| 376 | final Context context = contexts[0]; |
| 377 | final ContentResolver cr = context.getContentResolver(); |
Sascha Haeberling | c291569 | 2013-09-16 11:04:25 -0700 | [diff] [blame] | 378 | LocalDataList l = new LocalDataList(); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 379 | // Photos |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 380 | List<LocalData> photoData = LocalMediaData.PhotoData.query(cr, |
| 381 | LocalMediaData.PhotoData.CONTENT_URI, LocalMediaData.QUERY_ALL_MEDIA_ID); |
| 382 | List<LocalData> videoData = LocalMediaData.VideoData.query(cr, |
Angus Kong | 571a8c3 | 2014-03-13 12:53:03 -0700 | [diff] [blame] | 383 | LocalMediaData.VideoData.CONTENT_URI, LocalMediaData.QUERY_ALL_MEDIA_ID); |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 384 | |
| 385 | long lastPhotoId = LocalMediaData.QUERY_ALL_MEDIA_ID; |
| 386 | if (!photoData.isEmpty()) { |
| 387 | lastPhotoId = photoData.get(0).getContentId(); |
Angus Kong | 750e8ec | 2013-05-06 10:42:28 -0700 | [diff] [blame] | 388 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 389 | |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 390 | l.addAll(photoData); |
| 391 | l.addAll(videoData); |
| 392 | l.sort(new LocalData.NewestFirstComparator()); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 393 | |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 394 | // Load enough metadata so it's already loaded when we open the filmstrip. |
| 395 | for (int i = 0; i < MAX_METADATA && i < l.size(); i++) { |
| 396 | LocalData data = l.get(i); |
| 397 | MetadataLoader.loadMetadata(context, data); |
| 398 | } |
| 399 | |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 400 | return new QueryTaskResult(l, lastPhotoId); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
Sam Judd | aeed91f | 2014-03-13 11:40:10 -0700 | [diff] [blame] | 404 | protected void onPostExecute(QueryTaskResult result) { |
| 405 | // Since we're wiping away all of our data, we should always replace any existing last |
| 406 | // photo id with the new one we just obtained so it matches the data we're showing. |
| 407 | mLastPhotoId = result.mLastPhotoId; |
| 408 | replaceData(result.mLocalDataList); |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 412 | private class DeletionTask extends AsyncTask<LocalData, Void, Void> { |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 413 | @Override |
| 414 | protected Void doInBackground(LocalData... data) { |
| 415 | for (int i = 0; i < data.length; i++) { |
Angus Kong | 740cbee | 2013-12-11 15:46:16 -0800 | [diff] [blame] | 416 | if (!data[i].isDataActionSupported(LocalData.DATA_ACTION_DELETE)) { |
Angus Kong | 0822103 | 2013-06-14 14:50:31 -0700 | [diff] [blame] | 417 | Log.v(TAG, "Deletion is not supported:" + data[i]); |
| 418 | continue; |
| 419 | } |
| 420 | data[i].delete(mContext); |
| 421 | } |
| 422 | return null; |
| 423 | } |
| 424 | } |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 425 | |
| 426 | private class MetadataUpdateTask extends AsyncTask<Integer, Void, List<Integer> > { |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 427 | @Override |
| 428 | protected List<Integer> doInBackground(Integer... dataId) { |
| 429 | List<Integer> updatedList = new ArrayList<Integer>(); |
| 430 | for (Integer id : dataId) { |
| 431 | if (id < 0 || id >= mImages.size()) { |
| 432 | continue; |
| 433 | } |
| 434 | final LocalData data = mImages.get(id); |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 435 | if (MetadataLoader.loadMetadata(mContext, data)) { |
| 436 | updatedList.add(id); |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 437 | } |
Angus Kong | e2f4c03 | 2013-12-19 10:24:33 -0800 | [diff] [blame] | 438 | } |
| 439 | return updatedList; |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | protected void onPostExecute(List<Integer> updatedData) { |
| 444 | if (mLocalDataListener == null) { |
| 445 | return; |
| 446 | } |
| 447 | mLocalDataListener.onMetadataUpdated(updatedData); |
| 448 | } |
| 449 | } |
Angus Kong | 49b9ba2 | 2013-05-13 13:42:21 -0700 | [diff] [blame] | 450 | } |