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.compat.annotation.UnsupportedAppUsage;
20 import android.content.ContentProviderClient;
21 import android.content.ContentValues;
22 import android.net.Uri;
23 import android.os.RemoteException;
24 
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 
29 /**
30  * A MediaScanner helper class which enables us to do lazy insertion on the
31  * given provider. This class manages buffers internally and flushes when they
32  * are full. Note that you should call flushAll() after using this class.
33  * {@hide}
34  */
35 public class MediaInserter {
36     private final HashMap<Uri, List<ContentValues>> mRowMap =
37             new HashMap<Uri, List<ContentValues>>();
38     private final HashMap<Uri, List<ContentValues>> mPriorityRowMap =
39             new HashMap<Uri, List<ContentValues>>();
40 
41     private final ContentProviderClient mProvider;
42     private final int mBufferSizePerUri;
43 
MediaInserter(ContentProviderClient provider, int bufferSizePerUri)44     public MediaInserter(ContentProviderClient provider, int bufferSizePerUri) {
45         mProvider = provider;
46         mBufferSizePerUri = bufferSizePerUri;
47     }
48 
insert(Uri tableUri, ContentValues values)49     public void insert(Uri tableUri, ContentValues values) throws RemoteException {
50         insert(tableUri, values, false);
51     }
52 
insertwithPriority(Uri tableUri, ContentValues values)53     public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException {
54         insert(tableUri, values, true);
55     }
56 
insert(Uri tableUri, ContentValues values, boolean priority)57     private void insert(Uri tableUri, ContentValues values, boolean priority) throws RemoteException {
58         HashMap<Uri, List<ContentValues>> rowmap = priority ? mPriorityRowMap : mRowMap;
59         List<ContentValues> list = rowmap.get(tableUri);
60         if (list == null) {
61             list = new ArrayList<ContentValues>();
62             rowmap.put(tableUri, list);
63         }
64         list.add(new ContentValues(values));
65         if (list.size() >= mBufferSizePerUri) {
66             flushAllPriority();
67             flush(tableUri, list);
68         }
69     }
70 
71     @UnsupportedAppUsage
flushAll()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 
flushAllPriority()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 
flush(Uri tableUri, List<ContentValues> list)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(tableUri, valuesArray);
94             list.clear();
95         }
96     }
97 }
98