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.ContentProviderClient;
20 import android.content.ContentValues;
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 ContentProviderClient mProvider;
41     private final int mBufferSizePerUri;
42 
MediaInserter(ContentProviderClient provider, int bufferSizePerUri)43     public MediaInserter(ContentProviderClient provider, int bufferSizePerUri) {
44         mProvider = provider;
45         mBufferSizePerUri = bufferSizePerUri;
46     }
47 
insert(Uri tableUri, ContentValues values)48     public void insert(Uri tableUri, ContentValues values) throws RemoteException {
49         insert(tableUri, values, false);
50     }
51 
insertwithPriority(Uri tableUri, ContentValues values)52     public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException {
53         insert(tableUri, values, true);
54     }
55 
insert(Uri tableUri, ContentValues values, boolean priority)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 
flushAll()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 
flushAllPriority()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 
flush(Uri tableUri, List<ContentValues> list)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