1/* 2 * Copyright (C) 2011 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 17package android.media; 18 19import android.content.ContentValues; 20import android.content.IContentProvider; 21import android.net.Uri; 22import android.os.RemoteException; 23 24import java.util.ArrayList; 25import java.util.HashMap; 26import java.util.List; 27 28/** 29 * A MediaScanner helper class which enables us to do lazy insertion on the 30 * given provider. This class manages buffers internally and flushes when they 31 * are full. Note that you should call flushAll() after using this class. 32 * {@hide} 33 */ 34public class MediaInserter { 35 private final HashMap<Uri, List<ContentValues>> mRowMap = 36 new HashMap<Uri, List<ContentValues>>(); 37 private final HashMap<Uri, List<ContentValues>> mPriorityRowMap = 38 new HashMap<Uri, List<ContentValues>>(); 39 40 private IContentProvider mProvider; 41 private int mBufferSizePerUri; 42 43 public MediaInserter(IContentProvider provider, int bufferSizePerUri) { 44 mProvider = provider; 45 mBufferSizePerUri = bufferSizePerUri; 46 } 47 48 public void insert(Uri tableUri, ContentValues values) throws RemoteException { 49 insert(tableUri, values, false); 50 } 51 52 public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException { 53 insert(tableUri, values, true); 54 } 55 56 private void insert(Uri tableUri, ContentValues values, boolean priority) throws RemoteException { 57 HashMap<Uri, List<ContentValues>> rowmap = priority ? mPriorityRowMap : mRowMap; 58 List<ContentValues> list = rowmap.get(tableUri); 59 if (list == null) { 60 list = new ArrayList<ContentValues>(); 61 rowmap.put(tableUri, list); 62 } 63 list.add(new ContentValues(values)); 64 if (list.size() >= mBufferSizePerUri) { 65 flushAllPriority(); 66 flush(tableUri, list); 67 } 68 } 69 70 public void flushAll() throws RemoteException { 71 flushAllPriority(); 72 for (Uri tableUri : mRowMap.keySet()){ 73 List<ContentValues> list = mRowMap.get(tableUri); 74 flush(tableUri, list); 75 } 76 mRowMap.clear(); 77 } 78 79 private void flushAllPriority() throws RemoteException { 80 for (Uri tableUri : mPriorityRowMap.keySet()){ 81 List<ContentValues> list = mPriorityRowMap.get(tableUri); 82 flush(tableUri, list); 83 } 84 mPriorityRowMap.clear(); 85 } 86 87 private void flush(Uri tableUri, List<ContentValues> list) throws RemoteException { 88 if (!list.isEmpty()) { 89 ContentValues[] valuesArray = new ContentValues[list.size()]; 90 valuesArray = list.toArray(valuesArray); 91 mProvider.bulkInsert(tableUri, valuesArray); 92 list.clear(); 93 } 94 } 95} 96