1 /* 2 * Copyright (C) 2009 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.cooliris.media; 18 19 import java.util.ArrayList; 20 21 import android.util.Log; 22 23 public final class ConcatenatedDataSource implements DataSource { 24 private static final String TAG = "ConcatenatedDataSource"; 25 private final DataSource mFirst; 26 private final DataSource mSecond; 27 28 public ConcatenatedDataSource(DataSource first, DataSource second) { 29 mFirst = first; 30 mSecond = second; 31 } 32 33 public void loadMediaSets(final MediaFeed feed) { 34 mFirst.loadMediaSets(feed); 35 mSecond.loadMediaSets(feed); 36 } 37 38 public void loadItemsForSet(final MediaFeed feed, final MediaSet parentSet, int rangeStart, int rangeEnd) { 39 if (parentSet != null) { 40 DataSource dataSource = parentSet.mDataSource; 41 if (dataSource != null) { 42 dataSource.loadItemsForSet(feed, parentSet, rangeStart, rangeEnd); 43 } else { 44 Log.e(TAG, "MediaSet was not added to the feed"); 45 } 46 } 47 } 48 49 public boolean performOperation(int operation, final ArrayList<MediaBucket> mediaBuckets, Object data) { 50 ArrayList<MediaBucket> singleBucket = new ArrayList<MediaBucket>(1); 51 singleBucket.add(null); 52 int numBuckets = mediaBuckets.size(); 53 boolean retVal = true; 54 for (int i = 0; i < numBuckets; ++i) { // CR: iterator for 55 MediaBucket bucket = mediaBuckets.get(i); 56 MediaSet set = bucket.mediaSet; 57 if (set != null) { 58 DataSource dataSource = set.mDataSource; 59 if (dataSource != null) { 60 singleBucket.set(0, bucket); 61 retVal &= dataSource.performOperation(operation, singleBucket, data); 62 } else { 63 Log.e(TAG, "MediaSet was not added to the feed"); 64 } 65 } 66 } 67 return retVal; 68 } 69 70 public DiskCache getThumbnailCache() { 71 throw new UnsupportedOperationException("ConcatenatedDataSource should not create MediaItems"); 72 } 73 74 public void shutdown() { 75 mFirst.shutdown(); 76 mSecond.shutdown(); 77 } 78 79 public void refresh(final MediaFeed feed, final String[] databaseUris) { 80 mFirst.refresh(feed, databaseUris); 81 mSecond.refresh(feed, databaseUris); 82 } 83 84 public String[] getDatabaseUris() { 85 String[] first = mFirst.getDatabaseUris(); 86 String[] second = mSecond.getDatabaseUris(); 87 // We concatenate 88 return (String[])ArrayUtils.addAll(first, second); 89 } 90 } 91