@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); }
private long getFirstDownloadedId() { String[] projection = { PodcastProvider.COLUMN_ID, PodcastProvider.COLUMN_FILE_SIZE, PodcastProvider.COLUMN_MEDIA_URL, }; Cursor c = query(QUEUE_URI, projection, null, null, null); long podcastId = -1; try { while (c.moveToNext()) { PodcastCursor podcast = new PodcastCursor(c); if (podcast.isDownloaded(getContext())) { podcastId = podcast.getId(); break; } } } finally { c.close(); } return podcastId; }
public static void deleteDownload(Context context, final long podcastId) { File storage = new File(PodcastCursor.getStoragePath(context)); File[] files = storage.listFiles( new FileFilter() { public boolean accept(File pathname) { return pathname.getName().startsWith(String.valueOf(podcastId)) && pathname.getPath().endsWith(".mp3"); } }); for (File f : files) f.delete(); }
private String getNeedsDownloadIds() { SQLiteDatabase db = _dbAdapter.getReadableDatabase(); SQLiteQueryBuilder queueBuilder = new SQLiteQueryBuilder(); queueBuilder.setTables("podcasts"); queueBuilder.appendWhere("queuePosition IS NOT NULL"); Cursor queue = queueBuilder.query( db, new String[] {"_id, mediaUrl, fileSize"}, null, null, null, null, "queuePosition"); String queueIds = ""; if (queue != null) { while (queue.moveToNext()) { PodcastCursor podcast = new PodcastCursor(queue); if (!podcast.isDownloaded(getContext())) queueIds = queueIds + queue.getLong(0) + ","; } queue.close(); } if (queueIds.length() > 0) queueIds = queueIds.substring(0, queueIds.length() - 1); return queueIds; }
private void showNotification() { String[] projection = new String[] { PodcastProvider.COLUMN_ID, PodcastProvider.COLUMN_TITLE, PodcastProvider.COLUMN_SUBSCRIPTION_ID, PodcastProvider.COLUMN_SUBSCRIPTION_TITLE, PodcastProvider.COLUMN_SUBSCRIPTION_THUMBNAIL, }; Cursor c = getContentResolver() .query(PodcastProvider.ACTIVE_PODCAST_URI, projection, null, null, null); if (c == null) return; if (c.isAfterLast()) { c.close(); return; } PodcastCursor podcast = new PodcastCursor(c); // both paths use the pendingintent Intent showIntent = new Intent(this, MainActivity.class); PendingIntent showPendingIntent = PendingIntent.getActivity(this, 0, showIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon) .setWhen(0) .setContentTitle(podcast.getTitle()) .setContentText(podcast.getSubscriptionTitle()) .setContentIntent(showPendingIntent) .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_DEFAULT); // set up pause intent Intent pauseIntent = new Intent(this, PlayerService.class); // use data to make intent unique pauseIntent.setData(Uri.parse("podax://playercommand/playpause")); pauseIntent.putExtra(Constants.EXTRA_PLAYER_COMMAND, Constants.PLAYER_COMMAND_PLAYPAUSE); pauseIntent.putExtra(Constants.EXTRA_PLAYER_COMMAND_ARG, Constants.PAUSE_MEDIABUTTON); PendingIntent pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, 0); // set up forward intent Intent forwardIntent = new Intent(this, ActivePodcastReceiver.class); forwardIntent.setData(Constants.ACTIVE_PODCAST_DATA_FORWARD); PendingIntent forwardPendingIntent = PendingIntent.getService(this, 0, forwardIntent, 0); Bitmap subscriptionBitmap = Helper.getCachedImage(this, podcast.getSubscriptionThumbnailUrl(), 128, 128); if (subscriptionBitmap != null) builder.setLargeIcon(subscriptionBitmap); if (PlayerStatus.getPlayerState(this) == PlayerStates.PLAYING) builder.addAction( R.drawable.ic_media_pause_normal, getString(R.string.pause), pausePendingIntent); else builder.addAction( R.drawable.ic_media_play_normal, getString(R.string.play), pausePendingIntent); builder.addAction( R.drawable.ic_media_ff_normal, getString(R.string.fast_forward), forwardPendingIntent); Notification notification = builder.build(); startForeground(Constants.NOTIFICATION_PLAYING, notification); c.close(); }