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.LocalData.ActionCallback; 25 import com.android.camera.debug.Log; 26 import com.android.camera.filmstrip.DataAdapter; 27 import com.android.camera.filmstrip.ImageData; 28 29 /** 30 * A {@link LocalDataAdapter} which puts a {@link LocalData} fixed at the first 31 * position. It's done by combining a {@link LocalData} and another 32 * {@link LocalDataAdapter}. 33 */ 34 public class FixedFirstDataAdapter extends AbstractLocalDataAdapterWrapper 35 implements DataAdapter.Listener { 36 37 @SuppressWarnings("unused") 38 private static final Log.Tag TAG = new Log.Tag("FixedFirstDataAdpt"); 39 40 private LocalData mFirstData; 41 private Listener mListener; 42 43 /** 44 * Constructor. 45 * 46 * @param context Valid Android context. 47 * @param wrappedAdapter The {@link LocalDataAdapter} to be wrapped. 48 * @param firstData The {@link LocalData} to be placed at the first 49 * position. 50 */ FixedFirstDataAdapter( Context context, LocalDataAdapter wrappedAdapter, LocalData firstData)51 public FixedFirstDataAdapter( 52 Context context, 53 LocalDataAdapter wrappedAdapter, 54 LocalData firstData) { 55 super(context, wrappedAdapter); 56 if (firstData == null) { 57 throw new AssertionError("data is null"); 58 } 59 mFirstData = firstData; 60 } 61 62 @Override getLocalData(int dataID)63 public LocalData getLocalData(int dataID) { 64 if (dataID == 0) { 65 return mFirstData; 66 } 67 return mAdapter.getLocalData(dataID - 1); 68 } 69 70 @Override removeData(int dataID)71 public void removeData(int dataID) { 72 if (dataID > 0) { 73 mAdapter.removeData(dataID - 1); 74 } 75 } 76 77 @Override findDataByContentUri(Uri uri)78 public int findDataByContentUri(Uri uri) { 79 int pos = mAdapter.findDataByContentUri(uri); 80 if (pos != -1) { 81 return pos + 1; 82 } 83 return -1; 84 } 85 86 @Override updateData(int pos, LocalData data)87 public void updateData(int pos, LocalData data) { 88 if (pos == 0) { 89 mFirstData = data; 90 if (mListener != null) { 91 mListener.onDataUpdated(new UpdateReporter() { 92 @Override 93 public boolean isDataRemoved(int dataID) { 94 return false; 95 } 96 97 @Override 98 public boolean isDataUpdated(int dataID) { 99 return (dataID == 0); 100 } 101 }); 102 } 103 } else { 104 mAdapter.updateData(pos - 1, data); 105 } 106 } 107 108 @Override getTotalNumber()109 public int getTotalNumber() { 110 return (mAdapter.getTotalNumber() + 1); 111 } 112 113 @Override getView(Context context, View recycled, int dataID, ActionCallback actionCallback)114 public View getView(Context context, View recycled, int dataID, ActionCallback actionCallback) { 115 if (dataID == 0) { 116 return mFirstData.getView(context, recycled, mSuggestedWidth, mSuggestedHeight, 0, 117 null, false, actionCallback); 118 } 119 return mAdapter.getView(context, recycled, dataID - 1, actionCallback); 120 } 121 122 @Override getItemViewType(int dataId)123 public int getItemViewType(int dataId) { 124 if (dataId == 0) { 125 return mFirstData.getItemViewType().ordinal(); 126 } 127 return mAdapter.getItemViewType(dataId); 128 } 129 130 @Override resizeView(Context context, int dataID, View view, int w, int h)131 public void resizeView(Context context, int dataID, View view, int w, int h) { 132 // Do nothing. 133 } 134 135 @Override getImageData(int dataID)136 public ImageData getImageData(int dataID) { 137 if (dataID == 0) { 138 return mFirstData; 139 } 140 return mAdapter.getImageData(dataID - 1); 141 } 142 143 @Override setListener(Listener listener)144 public void setListener(Listener listener) { 145 mListener = listener; 146 mAdapter.setListener((listener == null) ? null : this); 147 // The first data is always there. Thus, When the listener is set, 148 // we should call listener.onDataLoaded(). 149 if (mListener != null) { 150 mListener.onDataLoaded(); 151 } 152 } 153 154 @Override canSwipeInFullScreen(int dataID)155 public boolean canSwipeInFullScreen(int dataID) { 156 if (dataID == 0) { 157 return mFirstData.canSwipeInFullScreen(); 158 } 159 return mAdapter.canSwipeInFullScreen(dataID - 1); 160 } 161 162 @Override onDataLoaded()163 public void onDataLoaded() { 164 if (mListener == null) { 165 return; 166 } 167 mListener.onDataUpdated(new UpdateReporter() { 168 @Override 169 public boolean isDataRemoved(int dataID) { 170 return false; 171 } 172 173 @Override 174 public boolean isDataUpdated(int dataID) { 175 return (dataID != 0); 176 } 177 }); 178 } 179 180 @Override onDataUpdated(final UpdateReporter reporter)181 public void onDataUpdated(final UpdateReporter reporter) { 182 mListener.onDataUpdated(new UpdateReporter() { 183 @Override 184 public boolean isDataRemoved(int dataID) { 185 return (dataID != 0) && reporter.isDataRemoved(dataID - 1); 186 } 187 188 @Override 189 public boolean isDataUpdated(int dataID) { 190 return (dataID != 0) && reporter.isDataUpdated(dataID - 1); 191 } 192 }); 193 } 194 195 @Override onDataInserted(int dataID, ImageData data)196 public void onDataInserted(int dataID, ImageData data) { 197 mListener.onDataInserted(dataID + 1, data); 198 } 199 200 @Override onDataRemoved(int dataID, ImageData data)201 public void onDataRemoved(int dataID, ImageData data) { 202 mListener.onDataRemoved(dataID + 1, data); 203 } 204 205 @Override updateMetadata(int dataId)206 public AsyncTask updateMetadata(int dataId) { 207 if (dataId > 0) { 208 return mAdapter.updateMetadata(dataId - 1); 209 } else { 210 MetadataLoader.loadMetadata(mContext, mFirstData); 211 } 212 return null; 213 } 214 215 @Override isMetadataUpdated(int dataId)216 public boolean isMetadataUpdated(int dataId) { 217 if (dataId > 0) { 218 return mAdapter.isMetadataUpdated(dataId - 1); 219 } else { 220 return mFirstData.isMetadataUpdated(); 221 } 222 } 223 } 224