Code example for ContentValues

Methods: containsKeygetAsByteArrayputNull

0
 
    @Override 
    public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId,
            ContentValues values) {
 
        if (values.containsKey(SKIP_PROCESSING_KEY)) {
            values.remove(SKIP_PROCESSING_KEY);
        } else { 
            // Pre-process the photo if one exists. 
            if (!preProcessPhoto(values)) {
                return 0; 
            } 
        } 
 
        long dataId = super.insert(db, txContext, rawContactId, values);
        if (!txContext.isNewRawContact(rawContactId)) {
            mContactAggregator.updatePhotoId(db, rawContactId);
        } 
        return dataId;
    } 
 
    @Override 
    public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
            Cursor c, boolean callerIsSyncAdapter) {
        long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
 
        if (values.containsKey(SKIP_PROCESSING_KEY)) {
            values.remove(SKIP_PROCESSING_KEY);
        } else { 
            // Pre-process the photo if one exists. 
            if (!preProcessPhoto(values)) {
                return false; 
            } 
        } 
 
        // Do the actual update. 
        if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) {
            return false; 
        } 
 
        mContactAggregator.updatePhotoId(db, rawContactId);
        return true; 
    } 
 
    /** 
     * Pre-processes the given content values for update or insert.  If the photo column contains 
     * null or an empty byte array, both that column and the photo file ID will be nulled out. 
     * If a photo was specified but could not be processed, this will return false. 
     * @param values The content values passed in. 
     * @return Whether processing was successful - on failure, the operation should abort. 
     */ 
    private boolean preProcessPhoto(ContentValues values) {
        if (values.containsKey(Photo.PHOTO)) {
            boolean photoExists = hasNonNullPhoto(values);
            if (photoExists) {
                if (!processPhoto(values)) {
                    // A photo was passed in, but we couldn't process it.  Update failed. 
                    return false; 
                } 
            } else { 
                // The photo key was passed in, but it was either null or an empty byte[]. 
                // We should set the photo and photo file ID fields to null for the update. 
                values.putNull(Photo.PHOTO);
                values.putNull(Photo.PHOTO_FILE_ID);
            } 
        } 
        return true; 
    } 
 
    private boolean hasNonNullPhoto(ContentValues values) {
        byte[] photoBytes = values.getAsByteArray(Photo.PHOTO);
        return photoBytes != null && photoBytes.length > 0;
    } 
 
    @Override 
    public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) {
Experience pair programming with AI  Get Codota for Java