Move deleteMedia into PlaybackService
This commit is contained in:
parent
6cdaf3787d
commit
d17df425cd
@ -559,13 +559,12 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
|
||||
int type = intent.getIntExtra("type", 1);
|
||||
long id = intent.getLongExtra("id", -1);
|
||||
|
||||
ContentResolver resolver = getContentResolver();
|
||||
if (type == MediaUtils.TYPE_PLAYLIST) {
|
||||
Playlist.deletePlaylist(resolver, id);
|
||||
Playlist.deletePlaylist(getContentResolver(), id);
|
||||
String message = getResources().getString(R.string.playlist_deleted, intent.getStringExtra("title"));
|
||||
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
int count = MediaUtils.deleteMedia(this, type, id);
|
||||
int count = PlaybackService.get(this).deleteMedia(type, id);
|
||||
String message = getResources().getQuantityString(R.plurals.deleted, count, count);
|
||||
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
@ -23,11 +23,9 @@
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -189,43 +187,6 @@ public class MediaUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all the songs in the given media set. Should be run on a
|
||||
* background thread.
|
||||
*
|
||||
* @param context A context to use.
|
||||
* @param type One of the TYPE_* constants, excluding playlists.
|
||||
* @param id The MediaStore id of the media to delete.
|
||||
* @return The number of songs deleted.
|
||||
*/
|
||||
public static int deleteMedia(Context context, int type, long id)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
String[] projection = new String [] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA };
|
||||
Cursor cursor = buildQuery(type, id, projection, null).runQuery(resolver);
|
||||
|
||||
if (cursor != null) {
|
||||
PlaybackService service = PlaybackService.hasInstance() ? PlaybackService.get(context) : null;
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
if (new File(cursor.getString(1)).delete()) {
|
||||
long songId = cursor.getLong(0);
|
||||
String where = MediaStore.Audio.Media._ID + '=' + songId;
|
||||
resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where, null);
|
||||
if (service != null)
|
||||
service.removeSong(songId);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the MediaStore to determine the id of the genre the song belongs
|
||||
* to.
|
||||
|
@ -22,21 +22,17 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Handler;
|
||||
@ -51,6 +47,12 @@ import android.telephony.PhoneStateListener;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public final class PlaybackService extends Service implements Handler.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener, SongTimeline.Callback {
|
||||
/**
|
||||
@ -949,15 +951,36 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the song with the given id from the timeline and advance to the
|
||||
* next song if the given song is currently playing.
|
||||
* Delete all the songs in the given media set. Should be run on a
|
||||
* background thread.
|
||||
*
|
||||
* @param id The MediaStore id of the song to remove.
|
||||
* @see SongTimeline#removeSong(long)
|
||||
* @param type One of the TYPE_* constants, excluding playlists.
|
||||
* @param id The MediaStore id of the media to delete.
|
||||
* @return The number of songs deleted.
|
||||
*/
|
||||
public void removeSong(long id)
|
||||
public int deleteMedia(int type, long id)
|
||||
{
|
||||
mTimeline.removeSong(id);
|
||||
int count = 0;
|
||||
|
||||
ContentResolver resolver = getContentResolver();
|
||||
String[] projection = new String [] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA };
|
||||
Cursor cursor = MediaUtils.buildQuery(type, id, projection, null).runQuery(resolver);
|
||||
|
||||
if (cursor != null) {
|
||||
while (cursor.moveToNext()) {
|
||||
if (new File(cursor.getString(1)).delete()) {
|
||||
long songId = cursor.getLong(0);
|
||||
String where = MediaStore.Audio.Media._ID + '=' + songId;
|
||||
resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where, null);
|
||||
mTimeline.removeSong(songId);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user