Home | History | Annotate | Download | only in media
      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 
     17 package android.media;
     18 
     19 import android.content.ContentValues;
     20 import android.content.IContentProvider;
     21 import android.net.Uri;
     22 import android.os.RemoteException;
     23 
     24 import java.util.ArrayList;
     25 import java.util.HashMap;
     26 import 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  */
     34 public 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 final IContentProvider mProvider;
     41     private final String mPackageName;
     42     private final int mBufferSizePerUri;
     43 
     44     public MediaInserter(IContentProvider provider, String packageName, int bufferSizePerUri) {
     45         mProvider = provider;
     46         mPackageName = packageName;
     47         mBufferSizePerUri = bufferSizePerUri;
     48     }
     49 
     50     public void insert(Uri tableUri, ContentValues values) throws RemoteException {
     51         insert(tableUri, values, false);
     52     }
     53 
     54     public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException {
     55         insert(tableUri, values, true);
     56     }
     57 
     58     private void insert(Uri tableUri, ContentValues values, boolean priority) throws RemoteException {
     59         HashMap<Uri, List<ContentValues>> rowmap = priority ? mPriorityRowMap : mRowMap;
     60         List<ContentValues> list = rowmap.get(tableUri);
     61         if (list == null) {
     62             list = new ArrayList<ContentValues>();
     63             rowmap.put(tableUri, list);
     64         }
     65         list.add(new ContentValues(values));
     66         if (list.size() >= mBufferSizePerUri) {
     67             flushAllPriority();
     68             flush(tableUri, list);
     69         }
     70     }
     71 
     72     public void flushAll() throws RemoteException {
     73         flushAllPriority();
     74         for (Uri tableUri : mRowMap.keySet()){
     75             List<ContentValues> list = mRowMap.get(tableUri);
     76             flush(tableUri, list);
     77         }
     78         mRowMap.clear();
     79     }
     80 
     81     private void flushAllPriority() throws RemoteException {
     82         for (Uri tableUri : mPriorityRowMap.keySet()){
     83             List<ContentValues> list = mPriorityRowMap.get(tableUri);
     84             flush(tableUri, list);
     85         }
     86         mPriorityRowMap.clear();
     87     }
     88 
     89     private void flush(Uri tableUri, List<ContentValues> list) throws RemoteException {
     90         if (!list.isEmpty()) {
     91             ContentValues[] valuesArray = new ContentValues[list.size()];
     92             valuesArray = list.toArray(valuesArray);
     93             mProvider.bulkInsert(mPackageName, tableUri, valuesArray);
     94             list.clear();
     95         }
     96     }
     97 }
     98