use of android.content.ContentProviderOperation in project muzei by romannurik.
the class GalleryArtSource method updateMeta.
private void updateMeta() {
Cursor chosenUris = getContentResolver().query(GalleryContract.ChosenPhotos.CONTENT_URI, new String[] { BaseColumns._ID, GalleryContract.ChosenPhotos.COLUMN_NAME_IS_TREE_URI, GalleryContract.ChosenPhotos.COLUMN_NAME_URI }, null, null, null);
int numImages = 0;
ArrayList<ContentProviderOperation> rowsToDelete = new ArrayList<>();
while (chosenUris != null && chosenUris.moveToNext()) {
boolean isTreeUri = chosenUris.getInt(chosenUris.getColumnIndex(GalleryContract.ChosenPhotos.COLUMN_NAME_IS_TREE_URI)) != 0;
if (isTreeUri && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Uri treeUri = Uri.parse(chosenUris.getString(chosenUris.getColumnIndex(GalleryContract.ChosenPhotos.COLUMN_NAME_URI)));
try {
numImages += addAllImagesFromTree(null, treeUri, DocumentsContract.getTreeDocumentId(treeUri));
} catch (SecurityException e) {
Log.w(TAG, "Unable to load images from " + treeUri + ", deleting row", e);
rowsToDelete.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(GalleryContract.ChosenPhotos.CONTENT_URI, chosenUris.getLong(chosenUris.getColumnIndex(BaseColumns._ID)))).build());
}
} else {
numImages++;
}
}
if (chosenUris != null) {
chosenUris.close();
}
if (!rowsToDelete.isEmpty()) {
try {
getContentResolver().applyBatch(GalleryContract.AUTHORITY, rowsToDelete);
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "Error deleting invalid rows", e);
}
}
setDescription(numImages > 0 ? getResources().getQuantityString(R.plurals.gallery_description_choice_template, numImages, numImages) : getString(R.string.gallery_description));
if (numImages != 1) {
setUserCommands(BUILTIN_COMMAND_ID_NEXT_ARTWORK);
} else {
removeAllUserCommands();
}
}
use of android.content.ContentProviderOperation in project muzei by romannurik.
the class SourceManager method selectSource.
public static void selectSource(Context context, ComponentName source) {
if (source == null) {
Log.e(TAG, "selectSource: Empty source");
return;
}
ComponentName selectedSource = getSelectedSource(context);
if (source.equals(selectedSource)) {
return;
}
if (BuildConfig.DEBUG) {
Log.d(TAG, "Source " + source + " selected.");
}
final ArrayList<ContentProviderOperation> operations = new ArrayList<>();
if (selectedSource != null) {
unsubscribeToSelectedSource(context);
// Unselect the old source
operations.add(ContentProviderOperation.newUpdate(MuzeiContract.Sources.CONTENT_URI).withValue(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, selectedSource.flattenToShortString()).withValue(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED, false).withSelection(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + "=?", new String[] { selectedSource.flattenToShortString() }).build());
}
// Select the new source
operations.add(ContentProviderOperation.newUpdate(MuzeiContract.Sources.CONTENT_URI).withValue(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, source.flattenToShortString()).withValue(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED, true).withSelection(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + "=?", new String[] { source.flattenToShortString() }).build());
try {
context.getContentResolver().applyBatch(MuzeiContract.AUTHORITY, operations);
sendSelectedSourceAnalytics(context, source);
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "Error writing sources to ContentProvider", e);
}
subscribeToSelectedSource(context);
// Ensure the artwork from the newly selected source is downloaded
context.startService(TaskQueueService.getDownloadCurrentArtworkIntent(context));
}
use of android.content.ContentProviderOperation in project android_frameworks_base by crdroidandroid.
the class TvInputManagerService method registerBroadcastReceivers.
private void registerBroadcastReceivers() {
PackageMonitor monitor = new PackageMonitor() {
private void buildTvInputList(String[] packages) {
synchronized (mLock) {
if (mCurrentUserId == getChangingUserId()) {
buildTvInputListLocked(mCurrentUserId, packages);
buildTvContentRatingSystemListLocked(mCurrentUserId);
}
}
}
@Override
public void onPackageUpdateFinished(String packageName, int uid) {
if (DEBUG)
Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
// This callback is invoked when the TV input is reinstalled.
// In this case, isReplacing() always returns true.
buildTvInputList(new String[] { packageName });
}
@Override
public void onPackagesAvailable(String[] packages) {
if (DEBUG) {
Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
}
// available.
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onPackagesUnavailable(String[] packages) {
// unavailable.
if (DEBUG) {
Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
}
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onSomePackagesChanged() {
// the TV inputs.
if (DEBUG)
Slog.d(TAG, "onSomePackagesChanged()");
if (isReplacing()) {
if (DEBUG)
Slog.d(TAG, "Skipped building TV input list due to replacing");
// methods instead.
return;
}
buildTvInputList(null);
}
@Override
public boolean onPackageChanged(String packageName, int uid, String[] components) {
// the update can be handled in {@link #onSomePackagesChanged}.
return true;
}
@Override
public void onPackageRemoved(String packageName, int uid) {
synchronized (mLock) {
UserState userState = getOrCreateUserStateLocked(getChangingUserId());
if (!userState.packageSet.contains(packageName)) {
// Not a TV input package.
return;
}
}
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
String[] selectionArgs = { packageName };
operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
ContentProviderResult[] results = null;
try {
ContentResolver cr = getContentResolverForUser(getChangingUserId());
results = cr.applyBatch(TvContract.AUTHORITY, operations);
} catch (RemoteException | OperationApplicationException e) {
Slog.e(TAG, "error in applyBatch", e);
}
if (DEBUG) {
Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
Slog.d(TAG, "results=" + results);
}
}
};
monitor.register(mContext, null, UserHandle.ALL, true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiverAsUser(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_USER_SWITCHED.equals(action)) {
switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
} else if (Intent.ACTION_USER_REMOVED.equals(action)) {
removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
}
}
}, UserHandle.ALL, intentFilter, null, null);
}
use of android.content.ContentProviderOperation in project android_frameworks_base by DirtyUnicorns.
the class TvInputManagerService method registerBroadcastReceivers.
private void registerBroadcastReceivers() {
PackageMonitor monitor = new PackageMonitor() {
private void buildTvInputList(String[] packages) {
synchronized (mLock) {
if (mCurrentUserId == getChangingUserId()) {
buildTvInputListLocked(mCurrentUserId, packages);
buildTvContentRatingSystemListLocked(mCurrentUserId);
}
}
}
@Override
public void onPackageUpdateFinished(String packageName, int uid) {
if (DEBUG)
Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
// This callback is invoked when the TV input is reinstalled.
// In this case, isReplacing() always returns true.
buildTvInputList(new String[] { packageName });
}
@Override
public void onPackagesAvailable(String[] packages) {
if (DEBUG) {
Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
}
// available.
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onPackagesUnavailable(String[] packages) {
// unavailable.
if (DEBUG) {
Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
}
if (isReplacing()) {
buildTvInputList(packages);
}
}
@Override
public void onSomePackagesChanged() {
// the TV inputs.
if (DEBUG)
Slog.d(TAG, "onSomePackagesChanged()");
if (isReplacing()) {
if (DEBUG)
Slog.d(TAG, "Skipped building TV input list due to replacing");
// methods instead.
return;
}
buildTvInputList(null);
}
@Override
public boolean onPackageChanged(String packageName, int uid, String[] components) {
// the update can be handled in {@link #onSomePackagesChanged}.
return true;
}
@Override
public void onPackageRemoved(String packageName, int uid) {
synchronized (mLock) {
UserState userState = getOrCreateUserStateLocked(getChangingUserId());
if (!userState.packageSet.contains(packageName)) {
// Not a TV input package.
return;
}
}
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
String[] selectionArgs = { packageName };
operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
ContentProviderResult[] results = null;
try {
ContentResolver cr = getContentResolverForUser(getChangingUserId());
results = cr.applyBatch(TvContract.AUTHORITY, operations);
} catch (RemoteException | OperationApplicationException e) {
Slog.e(TAG, "error in applyBatch", e);
}
if (DEBUG) {
Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
Slog.d(TAG, "results=" + results);
}
}
};
monitor.register(mContext, null, UserHandle.ALL, true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiverAsUser(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_USER_SWITCHED.equals(action)) {
switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
} else if (Intent.ACTION_USER_REMOVED.equals(action)) {
removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
}
}
}, UserHandle.ALL, intentFilter, null, null);
}
use of android.content.ContentProviderOperation in project qksms by moezbhatti.
the class ContactOperations method insertContact.
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void insertContact(VCard vcard) throws RemoteException, OperationApplicationException {
// TODO handle Raw properties - Raw properties include various extension which start with "X-" like X-ASSISTANT, X-AIM, X-SPOUSE
List<NonEmptyContentValues> contentValues = new ArrayList<>();
contentValues.add(account);
convertName(contentValues, vcard);
convertNickname(contentValues, vcard);
convertPhones(contentValues, vcard);
convertEmails(contentValues, vcard);
convertAddresses(contentValues, vcard);
convertIms(contentValues, vcard);
// handle Android Custom fields..This is only valid for Android generated Vcards. As the Android would
// generate NickName, ContactEvents other than Birthday and RelationShip with this "X-ANDROID-CUSTOM" name
convertCustomFields(contentValues, vcard);
// handle Iphone kinda of group properties. which are grouped together.
convertGroupedProperties(contentValues, vcard);
convertBirthdays(contentValues, vcard);
convertWebsites(contentValues, vcard);
convertNotes(contentValues, vcard);
convertPhotos(contentValues, vcard);
convertOrganization(contentValues, vcard);
ArrayList<ContentProviderOperation> operations = new ArrayList<>(contentValues.size());
for (NonEmptyContentValues values : contentValues) {
ContentValues cv = values.getContentValues();
if (cv.size() == 0) {
continue;
}
// @formatter:off
ContentProviderOperation operation = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID).withValues(cv).build();
// @formatter:on
operations.add(operation);
}
// Executing all the insert operations as a single database transaction
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
}
Aggregations