Dianne Hackborn 35654b61e8 More work on App Ops service.
Implemented reading and writing state to retain information
across boots, API to retrieve state from it, improved location
manager interaction to monitor both coarse and fine access
and only note operations when location data is being delivered
back to app (not when it is just registering to get the data at
some time in the future).

Also implement tracking of read/write ops on contacts and the
call log.  This involved tweaking the content provider protocol
to pass over the name of the calling package, and some
infrastructure in the ContentProvider transport to note incoming
calls with the app ops service.  The contacts provider and call
log provider turn this on for themselves.

This also implements some of the mechanics of being able to ignore
incoming provider calls...  all that is left are some new APIs for
the real content provider implementation to be involved with
providing the correct behavior for query() (return an empty
cursor with the right columns) and insert() (need to figure out
what URI to return).

Change-Id: I36ebbcd63dee58264a480f3d3786891ca7cbdb4c
2013-01-16 12:11:01 -08:00

98 lines
3.4 KiB
Java

/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.media;
import android.content.ContentValues;
import android.content.IContentProvider;
import android.net.Uri;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A MediaScanner helper class which enables us to do lazy insertion on the
* given provider. This class manages buffers internally and flushes when they
* are full. Note that you should call flushAll() after using this class.
* {@hide}
*/
public class MediaInserter {
private final HashMap<Uri, List<ContentValues>> mRowMap =
new HashMap<Uri, List<ContentValues>>();
private final HashMap<Uri, List<ContentValues>> mPriorityRowMap =
new HashMap<Uri, List<ContentValues>>();
private final IContentProvider mProvider;
private final String mPackageName;
private final int mBufferSizePerUri;
public MediaInserter(IContentProvider provider, String packageName, int bufferSizePerUri) {
mProvider = provider;
mPackageName = packageName;
mBufferSizePerUri = bufferSizePerUri;
}
public void insert(Uri tableUri, ContentValues values) throws RemoteException {
insert(tableUri, values, false);
}
public void insertwithPriority(Uri tableUri, ContentValues values) throws RemoteException {
insert(tableUri, values, true);
}
private void insert(Uri tableUri, ContentValues values, boolean priority) throws RemoteException {
HashMap<Uri, List<ContentValues>> rowmap = priority ? mPriorityRowMap : mRowMap;
List<ContentValues> list = rowmap.get(tableUri);
if (list == null) {
list = new ArrayList<ContentValues>();
rowmap.put(tableUri, list);
}
list.add(new ContentValues(values));
if (list.size() >= mBufferSizePerUri) {
flushAllPriority();
flush(tableUri, list);
}
}
public void flushAll() throws RemoteException {
flushAllPriority();
for (Uri tableUri : mRowMap.keySet()){
List<ContentValues> list = mRowMap.get(tableUri);
flush(tableUri, list);
}
mRowMap.clear();
}
private void flushAllPriority() throws RemoteException {
for (Uri tableUri : mPriorityRowMap.keySet()){
List<ContentValues> list = mPriorityRowMap.get(tableUri);
flush(tableUri, list);
}
mPriorityRowMap.clear();
}
private void flush(Uri tableUri, List<ContentValues> list) throws RemoteException {
if (!list.isEmpty()) {
ContentValues[] valuesArray = new ContentValues[list.size()];
valuesArray = list.toArray(valuesArray);
mProvider.bulkInsert(mPackageName, tableUri, valuesArray);
list.clear();
}
}
}