@Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = _dbAdapter.getWritableDatabase(); if (!(uriMatcher.match(uri) == PODCASTS)) throw new IllegalArgumentException("Illegal URI for insert"); if (values.get(COLUMN_MEDIA_URL) == null) throw new IllegalArgumentException("mediaUrl is required field for podcast"); Cursor mediaUrlCursor = db.rawQuery( "SELECT _id FROM podcasts WHERE mediaUrl = ?", new String[] {values.getAsString(COLUMN_MEDIA_URL)}); Long podcastId = null; if (mediaUrlCursor.moveToNext()) podcastId = mediaUrlCursor.getLong(0); mediaUrlCursor.close(); if (podcastId != null) { if (values.containsKey(COLUMN_MEDIA_URL) && values.containsKey(COLUMN_FILE_SIZE)) { String file = PodcastCursor.getStoragePath(getContext()) + String.valueOf(podcastId) + "." + PodcastCursor.getExtension(values.getAsString(COLUMN_MEDIA_URL)); // possible bug: file size shrinks for some reason -- don't use new one if (new File(file).length() > values.getAsInteger(COLUMN_FILE_SIZE)) values.remove(COLUMN_FILE_SIZE); } db.update("podcasts", values, COLUMN_ID + " = ?", new String[] {String.valueOf(podcastId)}); } else { podcastId = db.insert("podcasts", null, values); // find out if we should download new podcasts Cursor queueNewCursor = db.query( "subscriptions", new String[] {SubscriptionProvider.COLUMN_QUEUE_NEW}, "_id = ?", new String[] {String.valueOf(values.getAsLong(COLUMN_SUBSCRIPTION_ID))}, null, null, null); queueNewCursor.moveToFirst(); boolean queueNew = queueNewCursor.getInt(0) != 0; queueNewCursor.close(); // if the new podcast is less than 5 days old , and add it to the queue if (queueNew && values.containsKey(COLUMN_PUB_DATE)) { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, -5); if (new Date(values.getAsLong(COLUMN_PUB_DATE) * 1000L).after(c.getTime())) { updateQueuePosition(podcastId, Integer.MAX_VALUE); } } } getContext().getContentResolver().notifyChange(uri, null); return PodcastProvider.getContentUri(podcastId); }