Search in sources :

Example 11 with OperationApplicationException

use of android.content.OperationApplicationException in project Etar-Calendar by Etar-Group.

the class AsyncQueryServiceHelper method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    OperationInfo args;
    if (AsyncQueryService.localLOGV) {
        Log.d(TAG, "onHandleIntent: queue size=" + sWorkQueue.size());
    }
    synchronized (sWorkQueue) {
        while (true) {
            /*
                 * This method can be called with no work because of
                 * cancellations
                 */
            if (sWorkQueue.size() == 0) {
                return;
            } else if (sWorkQueue.size() == 1) {
                OperationInfo first = sWorkQueue.peek();
                long waitTime = first.mScheduledTimeMillis - SystemClock.elapsedRealtime();
                if (waitTime > 0) {
                    try {
                        sWorkQueue.wait(waitTime);
                    } catch (InterruptedException e) {
                    }
                }
            }
            args = sWorkQueue.poll();
            if (args != null) {
                // Got work to do. Break out of waiting loop
                break;
            }
        }
    }
    if (AsyncQueryService.localLOGV) {
        Log.d(TAG, "onHandleIntent: " + args);
    }
    ContentResolver resolver = args.resolver;
    if (resolver != null) {
        switch(args.op) {
            case Operation.EVENT_ARG_QUERY:
                Cursor cursor;
                try {
                    cursor = resolver.query(args.uri, args.projection, args.selection, args.selectionArgs, args.orderBy);
                    /*
                         * Calling getCount() causes the cursor window to be
                         * filled, which will make the first access on the main
                         * thread a lot faster
                         */
                    if (cursor != null) {
                        cursor.getCount();
                    }
                } catch (Exception e) {
                    Log.w(TAG, e.toString());
                    cursor = null;
                }
                args.result = cursor;
                break;
            case Operation.EVENT_ARG_INSERT:
                args.result = resolver.insert(args.uri, args.values);
                break;
            case Operation.EVENT_ARG_UPDATE:
                args.result = resolver.update(args.uri, args.values, args.selection, args.selectionArgs);
                break;
            case Operation.EVENT_ARG_DELETE:
                try {
                    args.result = resolver.delete(args.uri, args.selection, args.selectionArgs);
                } catch (IllegalArgumentException e) {
                    Log.w(TAG, "Delete failed.");
                    Log.w(TAG, e.toString());
                    args.result = 0;
                }
                break;
            case Operation.EVENT_ARG_BATCH:
                try {
                    args.result = resolver.applyBatch(args.authority, args.cpo);
                } catch (RemoteException e) {
                    Log.e(TAG, e.toString());
                    args.result = null;
                } catch (OperationApplicationException e) {
                    Log.e(TAG, e.toString());
                    args.result = null;
                }
                break;
        }
        /*
             * passing the original token value back to the caller on top of the
             * event values in arg1.
             */
        Message reply = args.handler.obtainMessage(args.token);
        reply.obj = args;
        reply.arg1 = args.op;
        if (AsyncQueryService.localLOGV) {
            Log.d(TAG, "onHandleIntent: op=" + Operation.opToChar(args.op) + ", token=" + reply.what);
        }
        reply.sendToTarget();
    }
}
Also used : Message(android.os.Message) Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException) OperationApplicationException(android.content.OperationApplicationException) ContentResolver(android.content.ContentResolver)

Example 12 with OperationApplicationException

use of android.content.OperationApplicationException 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();
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) OperationApplicationException(android.content.OperationApplicationException)

Example 13 with OperationApplicationException

use of android.content.OperationApplicationException 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));
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) ComponentName(android.content.ComponentName) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 14 with OperationApplicationException

use of android.content.OperationApplicationException in project platform_frameworks_base by android.

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);
}
Also used : Context(android.content.Context) ContentProviderResult(android.content.ContentProviderResult) IntentFilter(android.content.IntentFilter) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 15 with OperationApplicationException

use of android.content.OperationApplicationException in project ListenerMusicPlayer by hefuyicoder.

the class PlaylistSongLoader method cleanupPlaylist.

/**
     * 清空某playlist,用cursor填充
     * @param context
     * @param playlistId
     * @param cursor
     */
private static void cleanupPlaylist(final Context context, final long playlistId, final Cursor cursor) {
    final int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
    final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newDelete(uri).build());
    final int YIELD_FREQUENCY = 100;
    if (cursor.moveToFirst() && cursor.getCount() > 0) {
        do {
            final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(uri).withValue(MediaStore.Audio.Playlists.Members.PLAY_ORDER, cursor.getPosition()).withValue(MediaStore.Audio.Playlists.Members.AUDIO_ID, cursor.getLong(idCol));
            if ((cursor.getPosition() + 1) % YIELD_FREQUENCY == 0) {
                builder.withYieldAllowed(true);
            }
            ops.add(builder.build());
        } while (cursor.moveToNext());
    }
    try {
        context.getContentResolver().applyBatch(MediaStore.AUTHORITY, ops);
    } catch (RemoteException e) {
    } catch (OperationApplicationException e) {
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) Uri(android.net.Uri) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

OperationApplicationException (android.content.OperationApplicationException)63 ContentProviderOperation (android.content.ContentProviderOperation)57 ArrayList (java.util.ArrayList)52 RemoteException (android.os.RemoteException)42 ContentProviderResult (android.content.ContentProviderResult)21 ContentValues (android.content.ContentValues)15 ContentResolver (android.content.ContentResolver)12 Cursor (android.database.Cursor)11 Uri (android.net.Uri)11 IOException (java.io.IOException)11 Intent (android.content.Intent)9 LinkedList (java.util.LinkedList)6 List (java.util.List)6 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4 PackageMonitor (com.android.internal.content.PackageMonitor)4