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.filmstrip.ImageData; 26 27 /** 28 * A {@link LocalDataAdapter} which puts a {@link LocalData} fixed at the last 29 * position. It's done by combining a {@link LocalData} and another 30 * {@link LocalDataAdapter}. 31 */ 32 public class FixedLastDataAdapter extends AbstractLocalDataAdapterWrapper { 33 34 private LocalData mLastData; 35 private Listener mListener; 36 37 /** 38 * Constructor. 39 * 40 * @param context A valid Android context. 41 * @param wrappedAdapter The {@link LocalDataAdapter} to be wrapped. 42 * @param lastData The {@link LocalData} to be placed at the last position. 43 */ 44 public FixedLastDataAdapter( 45 Context context, 46 LocalDataAdapter wrappedAdapter, 47 LocalData lastData) { 48 super(context, wrappedAdapter); 49 if (lastData == null) { 50 throw new AssertionError("data is null"); 51 } 52 mLastData = lastData; 53 } 54 55 @Override 56 public void setListener(Listener listener) { 57 super.setListener(listener); 58 mListener = listener; 59 } 60 61 @Override 62 public LocalData getLocalData(int dataID) { 63 int totalNumber = mAdapter.getTotalNumber(); 64 65 if (dataID < totalNumber) { 66 return mAdapter.getLocalData(dataID); 67 } else if (dataID == totalNumber) { 68 return mLastData; 69 } 70 return null; 71 } 72 73 @Override 74 public void removeData(int dataID) { 75 if (dataID < mAdapter.getTotalNumber()) { 76 mAdapter.removeData(dataID); 77 } 78 } 79 80 @Override 81 public int findDataByContentUri(Uri uri) { 82 return mAdapter.findDataByContentUri(uri); 83 } 84 85 @Override 86 public void updateData(final int pos, LocalData data) { 87 int totalNumber = mAdapter.getTotalNumber(); 88 89 if (pos < totalNumber) { 90 mAdapter.updateData(pos, data); 91 } else if (pos == totalNumber) { 92 mLastData = data; 93 if (mListener != null) { 94 mListener.onDataUpdated(new UpdateReporter() { 95 @Override 96 public boolean isDataRemoved(int dataID) { 97 return false; 98 } 99 100 @Override 101 public boolean isDataUpdated(int dataID) { 102 return (dataID == pos); 103 } 104 }); 105 } 106 } 107 } 108 109 @Override 110 public int getTotalNumber() { 111 return mAdapter.getTotalNumber() + 1; 112 } 113 114 @Override 115 public View getView(Context context, View recycled, int dataID, ActionCallback actionCallback) { 116 int totalNumber = mAdapter.getTotalNumber(); 117 118 if (dataID < totalNumber) { 119 return mAdapter.getView(context, recycled, dataID, actionCallback); 120 } else if (dataID == totalNumber) { 121 return mLastData.getView(context, recycled, mSuggestedWidth, mSuggestedHeight, 0, null, 122 false, actionCallback); 123 } 124 return null; 125 } 126 127 @Override 128 public int getItemViewType(int dataId) { 129 int totalNumber = mAdapter.getTotalNumber(); 130 131 if (dataId < totalNumber) { 132 return mAdapter.getItemViewType(dataId); 133 } else if (dataId == totalNumber) { 134 return mLastData.getItemViewType().ordinal(); 135 } 136 return -1; 137 } 138 139 @Override 140 public void resizeView(Context context, int dataID, View view, int w, int h) { 141 // Do nothing. 142 } 143 144 @Override 145 public ImageData getImageData(int dataID) { 146 int totalNumber = mAdapter.getTotalNumber(); 147 148 if (dataID < totalNumber) { 149 return mAdapter.getImageData(dataID); 150 } else if (dataID == totalNumber) { 151 return mLastData; 152 } 153 return null; 154 } 155 156 @Override 157 public boolean canSwipeInFullScreen(int dataID) { 158 int totalNumber = mAdapter.getTotalNumber(); 159 160 if (dataID < totalNumber) { 161 return mAdapter.canSwipeInFullScreen(dataID); 162 } else if (dataID == totalNumber) { 163 return mLastData.canSwipeInFullScreen(); 164 } 165 return false; 166 } 167 168 @Override 169 public AsyncTask updateMetadata(int dataId) { 170 if (dataId < mAdapter.getTotalNumber()) { 171 return mAdapter.updateMetadata(dataId); 172 } else { 173 MetadataLoader.loadMetadata(mContext, mLastData); 174 } 175 return null; 176 } 177 178 @Override 179 public boolean isMetadataUpdated(int dataId) { 180 if (dataId < mAdapter.getTotalNumber()) { 181 return mAdapter.isMetadataUpdated(dataId); 182 } else { 183 return MetadataLoader.isMetadataCached(mLastData); 184 } 185 } 186 } 187