Code refactor

This commit is contained in:
Hemanth S 2020-05-09 14:48:29 +05:30
parent 6e7d945fb1
commit 2d3ad54ab5
54 changed files with 436 additions and 142 deletions

View file

@ -88,7 +88,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
slide.excludeTarget(R.id.status_bar, true)
slide.excludeTarget(android.R.id.statusBarBackground, true)
slide.excludeTarget(android.R.id.navigationBarBackground, true)
window.enterTransition = slide
}
@ -348,7 +347,8 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
R.id.action_sort_order_title -> sortOrder = AlbumSongSortOrder.SONG_A_Z
R.id.action_sort_order_title_desc -> sortOrder = AlbumSongSortOrder.SONG_Z_A
R.id.action_sort_order_track_list -> sortOrder = AlbumSongSortOrder.SONG_TRACK_LIST
R.id.action_sort_order_artist_song_duration -> sortOrder = AlbumSongSortOrder.SONG_DURATION
R.id.action_sort_order_artist_song_duration ->
sortOrder = AlbumSongSortOrder.SONG_DURATION
}
if (sortOrder != null) {
item.isChecked = true
@ -364,8 +364,7 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
AlbumSongSortOrder.SONG_Z_A -> sortOrder.findItem(R.id.action_sort_order_title_desc)
.isChecked = true
AlbumSongSortOrder.SONG_TRACK_LIST -> sortOrder.findItem(R.id.action_sort_order_track_list)
.isChecked =
true
.isChecked = true
AlbumSongSortOrder.SONG_DURATION -> sortOrder.findItem(R.id.action_sort_order_artist_song_duration)
.isChecked = true
}
@ -373,7 +372,29 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
private fun setSaveSortOrder(sortOrder: String?) {
PreferenceUtil.getInstance(this).albumDetailSongSortOrder = sortOrder
reload()
when (sortOrder) {
AlbumSongSortOrder.SONG_TRACK_LIST -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.trackNumber.compareTo(
o2.trackNumber
)
})
AlbumSongSortOrder.SONG_A_Z -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.title.compareTo(
o2.title
)
})
AlbumSongSortOrder.SONG_Z_A -> album.songs?.sortWith(Comparator { o1, o2 ->
o2.title.compareTo(
o1.title
)
})
AlbumSongSortOrder.SONG_DURATION -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.duration.compareTo(
o2.duration
)
})
}
album.songs?.let { simpleSongAdapter.swapDataSet(it) }
}
override fun onMediaStoreChanged() {

View file

@ -185,7 +185,7 @@ class PlaylistDetailActivity : AbsSlidingMusicPanelActivity(), CabHolder, Playli
}
private fun setToolbarTitle(title: String) {
supportActionBar!!.title = title
supportActionBar?.title = title
}
private fun checkForPadding() {

View file

@ -113,16 +113,16 @@ class HomeAdapter(
inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(list: ArrayList<Artist>, titleRes: Int) {
if (list.isNotEmpty()) {
val manager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
val artistAdapter = ArtistAdapter(
activity,
list,
PreferenceUtil.getInstance(activity).getHomeGridStyle(activity),
null
)
recyclerView.apply {
show()
layoutManager =
LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
val artistAdapter = ArtistAdapter(
activity,
list,
PreferenceUtil.getInstance(activity).getHomeGridStyle(activity),
null
)
layoutManager = manager
adapter = artistAdapter
}
title.text = activity.getString(titleRes)

View file

@ -77,6 +77,7 @@ class AlbumFullWidthAdapter(
if (holder.image == null) {
return
}
AlbumGlideRequest.Builder.from(Glide.with(activity), album.safeGetFirstSong())
.checkIgnoreMediaStore(activity)
.generatePalette(activity)

View file

@ -41,6 +41,8 @@ public class MediaEntryViewHolder extends AbstractDraggableSwipeableItemViewHold
@Nullable
public ImageView image;
@Nullable
public ImageView artistImage;
@Nullable
public ImageView playerImage;
@ -87,6 +89,7 @@ public class MediaEntryViewHolder extends AbstractDraggableSwipeableItemViewHold
text = itemView.findViewById(R.id.text);
image = itemView.findViewById(R.id.image);
artistImage = itemView.findViewById(R.id.artistImage);
playerImage = itemView.findViewById(R.id.player_image);
time = itemView.findViewById(R.id.time);

View file

@ -196,8 +196,8 @@ class AdaptiveFragment : AbsPlayerFragment(), MusicProgressViewUpdateHelper.Call
}
override fun onColorChanged(color: MediaNotificationProcessor) {
playbackControlsFragment.setDark(color.backgroundColor)
lastColor = color.backgroundColor
playbackControlsFragment.setDark(color.primaryTextColor)
lastColor = color.primaryTextColor
callbacks?.onPaletteColorChanged()
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,

View file

@ -45,10 +45,9 @@ class CardFragment : AbsPlayerFragment() {
}
override fun onColorChanged(color: MediaNotificationProcessor) {
playbackControlsFragment.setDark(color.backgroundColor)
lastColor = color.backgroundColor
callbacks!!.onPaletteColorChanged()
playbackControlsFragment.setDark(color.primaryTextColor)
lastColor = color.primaryTextColor
callbacks?.onPaletteColorChanged()
ToolbarContentTintHelper.colorizeToolbar(playerToolbar, Color.WHITE, activity)
}

View file

@ -129,7 +129,7 @@ class CirclePlayerFragment : AbsPlayerFragment(), Callback, OnAudioVolumeChanged
if (audioVolumeObserver == null) {
audioVolumeObserver = AudioVolumeObserver(requireActivity())
}
audioVolumeObserver!!.register(AudioManager.STREAM_MUSIC, this)
audioVolumeObserver?.register(AudioManager.STREAM_MUSIC, this)
val audioManager = audioManager
if (audioManager != null) {
@ -163,6 +163,7 @@ class CirclePlayerFragment : AbsPlayerFragment(), Callback, OnAudioVolumeChanged
get() = Color.BLACK
override fun onColorChanged(color: MediaNotificationProcessor) {
}
override fun onFavoriteToggled() {

View file

@ -7,7 +7,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
@ -66,7 +65,6 @@ class ClassicPlayerFragment : AbsPlayerFragment(), View.OnLayoutChangeListener,
playerQueueSheet.contentPaddingRight,
playerQueueSheet.contentPaddingBottom
)
val corner = (1 - slideOffset) * DensityUtil.dip2px(requireContext(), 16f)
shapeDrawable.interpolation = 1 - slideOffset
}
@ -120,6 +118,11 @@ class ClassicPlayerFragment : AbsPlayerFragment(), View.OnLayoutChangeListener,
0
).build()
)
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,
Color.WHITE,
requireActivity()
)
}
private fun hideVolumeIfAvailable() {
@ -270,6 +273,12 @@ class ClassicPlayerFragment : AbsPlayerFragment(), View.OnLayoutChangeListener,
updateRepeatState()
updateShuffleState()
updatePrevNextColor()
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,
Color.WHITE,
requireActivity()
)
}
override fun toggleFavorite(song: Song) {

View file

@ -44,8 +44,8 @@ class FitFragment : AbsPlayerFragment() {
}
override fun onColorChanged(color: MediaNotificationProcessor) {
playbackControlsFragment.setDark(color.backgroundColor)
lastColor = color.backgroundColor
playbackControlsFragment.setDark(color.primaryTextColor)
lastColor = color.primaryTextColor
callbacks?.onPaletteColorChanged()
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,

View file

@ -29,13 +29,13 @@ class FlatPlayerFragment : AbsPlayerFragment() {
}
private var valueAnimator: ValueAnimator? = null
private lateinit var flatPlaybackControlsFragment: FlatPlaybackControlsFragment
private lateinit var controlsFragment: FlatPlaybackControlsFragment
private var lastColor: Int = 0
override val paletteColor: Int
get() = lastColor
private fun setUpSubFragments() {
flatPlaybackControlsFragment =
controlsFragment =
childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as FlatPlaybackControlsFragment
val playerAlbumCoverFragment =
childFragmentManager.findFragmentById(R.id.playerAlbumCoverFragment) as PlayerAlbumCoverFragment
@ -55,11 +55,11 @@ class FlatPlayerFragment : AbsPlayerFragment() {
private fun colorize(i: Int) {
if (valueAnimator != null) {
valueAnimator!!.cancel()
valueAnimator?.cancel()
}
valueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), android.R.color.transparent, i)
valueAnimator!!.addUpdateListener { animation ->
valueAnimator?.addUpdateListener { animation ->
val drawable = DrawableGradient(
GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(animation.animatedValue as Int, android.R.color.transparent), 0
@ -67,7 +67,7 @@ class FlatPlayerFragment : AbsPlayerFragment() {
colorGradientBackground?.background = drawable
}
valueAnimator!!.setDuration(ViewUtil.RETRO_MUSIC_ANIM_TIME.toLong()).start()
valueAnimator?.setDuration(ViewUtil.RETRO_MUSIC_ANIM_TIME.toLong())?.start()
}
override fun onCreateView(
@ -84,11 +84,11 @@ class FlatPlayerFragment : AbsPlayerFragment() {
}
override fun onShow() {
flatPlaybackControlsFragment.show()
controlsFragment.show()
}
override fun onHide() {
flatPlaybackControlsFragment.hide()
controlsFragment.hide()
onBackPressed()
}
@ -106,7 +106,7 @@ class FlatPlayerFragment : AbsPlayerFragment() {
override fun onColorChanged(color: MediaNotificationProcessor) {
lastColor = color.backgroundColor
flatPlaybackControlsFragment.setDark(color.backgroundColor)
controlsFragment.setDark(color.primaryTextColor)
callbacks?.onPaletteColorChanged()
val isLight = ColorUtil.isColorLight(color.backgroundColor)
val iconColor = if (PreferenceUtil.getInstance(requireContext()).adaptiveColor)

View file

@ -125,7 +125,7 @@ class FullPlayerFragment : AbsPlayerFragment(), MusicProgressViewUpdateHelper.Ca
private var lastColor: Int = 0
override val paletteColor: Int
get() = lastColor
private lateinit var fullPlaybackControlsFragment: FullPlaybackControlsFragment
private lateinit var controlsFragment: FullPlaybackControlsFragment
private fun setUpPlayerToolbar() {
playerToolbar.apply {
@ -174,7 +174,7 @@ class FullPlayerFragment : AbsPlayerFragment(), MusicProgressViewUpdateHelper.Ca
}
private fun setUpSubFragments() {
fullPlaybackControlsFragment =
controlsFragment =
childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as FullPlaybackControlsFragment
val playerAlbumCoverFragment =
@ -199,14 +199,14 @@ class FullPlayerFragment : AbsPlayerFragment(), MusicProgressViewUpdateHelper.Ca
override fun onColorChanged(color: MediaNotificationProcessor) {
lastColor = color.backgroundColor
fullPlaybackControlsFragment.setDark(color.backgroundColor)
controlsFragment.setDark(color.primaryTextColor)
callbacks?.onPaletteColorChanged()
ToolbarContentTintHelper.colorizeToolbar(playerToolbar, Color.WHITE, activity)
}
override fun onFavoriteToggled() {
toggleFavorite(MusicPlayerRemote.currentSong)
fullPlaybackControlsFragment.onFavoriteToggled()
controlsFragment.onFavoriteToggled()
}
override fun toggleFavorite(song: Song) {

View file

@ -28,7 +28,7 @@ class PlayerFragment : AbsPlayerFragment() {
override val paletteColor: Int
get() = lastColor
private lateinit var playbackControlsFragment: PlayerPlaybackControlsFragment
private lateinit var controlsFragment: PlayerPlaybackControlsFragment
private var valueAnimator: ValueAnimator? = null
@ -58,11 +58,11 @@ class PlayerFragment : AbsPlayerFragment() {
}
override fun onShow() {
playbackControlsFragment.show()
controlsFragment.show()
}
override fun onHide() {
playbackControlsFragment.hide()
controlsFragment.hide()
onBackPressed()
}
@ -75,7 +75,7 @@ class PlayerFragment : AbsPlayerFragment() {
}
override fun onColorChanged(color: MediaNotificationProcessor) {
playbackControlsFragment.setDark(color.backgroundColor)
controlsFragment.setDark(color.primaryTextColor)
lastColor = color.backgroundColor
callbacks?.onPaletteColorChanged()
@ -118,7 +118,7 @@ class PlayerFragment : AbsPlayerFragment() {
private fun setUpSubFragments() {
playbackControlsFragment =
controlsFragment =
childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as PlayerPlaybackControlsFragment
val playerAlbumCoverFragment =
childFragmentManager.findFragmentById(R.id.playerAlbumCoverFragment) as PlayerAlbumCoverFragment

View file

@ -40,7 +40,7 @@ import kotlinx.android.synthetic.main.fragment_peak_player.*
class PeakPlayerFragment : AbsPlayerFragment() {
private lateinit var playbackControlsFragment: PeakPlayerControlFragment
private lateinit var controlsFragment: PeakPlayerControlFragment
private var lastColor: Int = 0
override fun onCreateView(
@ -62,7 +62,7 @@ class PeakPlayerFragment : AbsPlayerFragment() {
}
private fun setUpSubFragments() {
playbackControlsFragment =
controlsFragment =
childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as PeakPlayerControlFragment
}
@ -101,8 +101,8 @@ class PeakPlayerFragment : AbsPlayerFragment() {
get() = lastColor
override fun onColorChanged(color: MediaNotificationProcessor) {
playbackControlsFragment.setDark(color.backgroundColor)
lastColor = color.backgroundColor
controlsFragment.setDark(color.primaryTextColor)
lastColor = color.primaryTextColor
callbacks?.onPaletteColorChanged()
}
@ -127,7 +127,7 @@ class PeakPlayerFragment : AbsPlayerFragment() {
.build()
.into(object : RetroMusicColoredTarget(playerImage) {
override fun onColorReady(colors: MediaNotificationProcessor) {
playbackControlsFragment.setDark(colors.backgroundColor)
controlsFragment.setDark(colors.primaryTextColor)
}
})
}

View file

@ -95,8 +95,8 @@ class PlainPlayerFragment : AbsPlayerFragment() {
}
override fun onColorChanged(color: MediaNotificationProcessor) {
plainPlaybackControlsFragment.setDark(color.backgroundColor)
lastColor = color.backgroundColor
plainPlaybackControlsFragment.setDark(color.primaryTextColor)
lastColor = color.primaryTextColor
callbacks!!.onPaletteColorChanged()
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,

View file

@ -29,7 +29,7 @@ class SimplePlayerFragment : AbsPlayerFragment() {
override val paletteColor: Int
get() = lastColor
private lateinit var simplePlaybackControlsFragment: SimplePlaybackControlsFragment
private lateinit var controlsFragment: SimplePlaybackControlsFragment
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@ -49,16 +49,16 @@ class SimplePlayerFragment : AbsPlayerFragment() {
val playerAlbumCoverFragment =
childFragmentManager.findFragmentById(R.id.playerAlbumCoverFragment) as PlayerAlbumCoverFragment
playerAlbumCoverFragment.setCallbacks(this)
simplePlaybackControlsFragment =
controlsFragment =
childFragmentManager.findFragmentById(R.id.playbackControlsFragment) as SimplePlaybackControlsFragment
}
override fun onShow() {
simplePlaybackControlsFragment.show()
controlsFragment.show()
}
override fun onHide() {
simplePlaybackControlsFragment.hide()
controlsFragment.hide()
}
override fun onBackPressed(): Boolean {
@ -72,7 +72,7 @@ class SimplePlayerFragment : AbsPlayerFragment() {
override fun onColorChanged(color: MediaNotificationProcessor) {
lastColor = color.backgroundColor
callbacks?.onPaletteColorChanged()
simplePlaybackControlsFragment.setDark(color.backgroundColor)
controlsFragment.setDark(color.primaryTextColor)
ToolbarContentTintHelper.colorizeToolbar(
playerToolbar,
ATHUtil.resolveColor(requireContext(), R.attr.colorControlNormal),

View file

@ -75,7 +75,7 @@ class TinyPlayerFragment : AbsPlayerFragment(), MusicProgressViewUpdateHelper.Ca
override fun onColorChanged(color: MediaNotificationProcessor) {
val colorFinal = if (PreferenceUtil.getInstance(requireContext()).adaptiveColor) {
color.backgroundColor
color.primaryTextColor
} else {
ThemeStore.accentColor(requireContext())
}

View file

@ -16,13 +16,13 @@ package code.name.monkey.retromusic.loaders
import android.content.Context
import android.provider.MediaStore.Audio.AudioColumns
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.helper.SortOrder
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PreferenceUtil
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.isNotEmpty
import kotlin.collections.sortWith
/**
@ -108,12 +108,32 @@ object AlbumLoader {
}
private fun sortSongsByTrackNumber(album: Album) {
album.songs?.sortWith(Comparator { o1, o2 -> o1.trackNumber.compareTo(o2.trackNumber) })
when (PreferenceUtil.getInstance(App.getContext()).albumDetailSongSortOrder) {
SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.trackNumber.compareTo(
o2.trackNumber
)
})
SortOrder.AlbumSongSortOrder.SONG_A_Z -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.title.compareTo(
o2.title
)
})
SortOrder.AlbumSongSortOrder.SONG_Z_A -> album.songs?.sortWith(Comparator { o1, o2 ->
o2.title.compareTo(
o1.title
)
})
SortOrder.AlbumSongSortOrder.SONG_DURATION -> album.songs?.sortWith(Comparator { o1, o2 ->
o1.duration.compareTo(
o2.duration
)
})
}
}
private fun getSongLoaderSortOrder(context: Context): String {
return PreferenceUtil.getInstance(context).albumSortOrder + ", " + PreferenceUtil.getInstance(
context
).albumSongSortOrder
return PreferenceUtil.getInstance(context).albumSortOrder + ", " +
PreferenceUtil.getInstance(context).albumSongSortOrder
}
}

View file

@ -31,7 +31,6 @@ import java.util.*
object SongLoader {
fun getAllSongs(
context: Context
): ArrayList<Song> {
@ -128,9 +127,8 @@ object SongLoader {
return context.contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
baseProjection,
selectionFinal + " AND " + MediaStore.Audio.Media.DURATION + ">= " + (PreferenceUtil.getInstance(
context
).filterLength * 1000),
selectionFinal + " AND " + MediaStore.Audio.Media.DURATION + ">= " +
(PreferenceUtil.getInstance(context).filterLength * 1000),
selectionValuesFinal,
sortOrder
)

View file

@ -42,7 +42,241 @@ import static android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
public class PlaylistsUtil {
public static void addToPlaylist(@NonNull Context context,
public static int createPlaylist(@NonNull final Context context, @Nullable final String name) {
int id = -1;
if (name != null && name.length() > 0) {
try {
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Playlists._ID},
MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name},
null);
if (cursor == null || cursor.getCount() < 1) {
final ContentValues values = new ContentValues(1);
values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
final Uri uri = context.getContentResolver().insert(
EXTERNAL_CONTENT_URI,
values);
if (uri != null) {
// Necessary because somehow the MediaStoreObserver is not notified when adding a playlist
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
Toast.makeText(context, context.getResources().getString(
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
id = Integer.parseInt(uri.getLastPathSegment());
}
} else {
// Playlist exists
if (cursor.moveToFirst()) {
id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
}
}
if (cursor != null) {
cursor.close();
}
} catch (SecurityException e) {
e.printStackTrace();
Toast.makeText(context, "Don't have permission for adding to playlist", Toast.LENGTH_SHORT).show();
}
}
if (id == -1) {
Toast.makeText(context, context.getResources().getString(
R.string.could_not_create_playlist), Toast.LENGTH_SHORT).show();
}
return id;
}
public static void deletePlaylists(@NonNull final Context context, @NonNull final ArrayList<Playlist> playlists) {
final StringBuilder selection = new StringBuilder();
selection.append(MediaStore.Audio.Playlists._ID + " IN (");
for (int i = 0; i < playlists.size(); i++) {
selection.append(playlists.get(i).id);
if (i < playlists.size() - 1) {
selection.append(",");
}
}
selection.append(")");
try {
context.getContentResolver().delete(EXTERNAL_CONTENT_URI, selection.toString(), null);
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
} catch (SecurityException ignored) {
}
}
public static void addToPlaylist(@NonNull final Context context, final Song song, final int playlistId, final boolean showToastOnFinish) {
List<Song> helperList = new ArrayList<>();
helperList.add(song);
addToPlaylist(context, helperList, playlistId, showToastOnFinish);
}
public static void addToPlaylist(@NonNull final Context context, @NonNull final List<Song> songs, final int playlistId, final boolean showToastOnFinish) {
final int size = songs.size();
final ContentResolver resolver = context.getContentResolver();
final String[] projection = new String[]{
"max(" + MediaStore.Audio.Playlists.Members.PLAY_ORDER + ")",
};
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
Cursor cursor = null;
int base = 0;
try {
try {
cursor = resolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
base = cursor.getInt(0) + 1;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
int numInserted = 0;
for (int offSet = 0; offSet < size; offSet += 1000)
numInserted += resolver.bulkInsert(uri, makeInsertItems(songs, offSet, 1000, base));
if (showToastOnFinish) {
Toast.makeText(context, context.getResources().getString(
R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId)), Toast.LENGTH_SHORT).show();
}
} catch (SecurityException ignored) {
ignored.printStackTrace();
Toast.makeText(context, "Don't have permission for adding to playlist", Toast.LENGTH_SHORT).show();
}
}
@NonNull
public static ContentValues[] makeInsertItems(@NonNull final List<Song> songs, final int offset, int len, final int base) {
if (offset + len > songs.size()) {
len = songs.size() - offset;
}
ContentValues[] contentValues = new ContentValues[len];
for (int i = 0; i < len; i++) {
contentValues[i] = new ContentValues();
contentValues[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base + offset + i);
contentValues[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songs.get(offset + i).getId());
}
return contentValues;
}
public static String getNameForPlaylist(@NonNull final Context context, final long id) {
try {
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.PlaylistsColumns.NAME},
BaseColumns._ID + "=?",
new String[]{String.valueOf(id)},
null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
return cursor.getString(0);
}
} finally {
cursor.close();
}
}
} catch (SecurityException ignored) {
}
return "";
}
public static void removeFromPlaylist(@NonNull final Context context, @NonNull final Song song, int playlistId) {
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlistId);
String selection = MediaStore.Audio.Playlists.Members.AUDIO_ID + " =?";
String[] selectionArgs = new String[]{String.valueOf(song.getId())};
try {
context.getContentResolver().delete(uri, selection, selectionArgs);
} catch (SecurityException ignored) {
}
}
public static void removeFromPlaylist(@NonNull final Context context, @NonNull final List<PlaylistSong> songs) {
final int playlistId = songs.get(0).getPlaylistId();
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlistId);
String selectionArgs[] = new String[songs.size()];
for (int i = 0; i < selectionArgs.length; i++) {
selectionArgs[i] = String.valueOf(songs.get(i).getIdInPlayList());
}
String selection = MediaStore.Audio.Playlists.Members._ID + " in (";
//noinspection unused
for (String selectionArg : selectionArgs) selection += "?, ";
selection = selection.substring(0, selection.length() - 2) + ")";
try {
context.getContentResolver().delete(uri, selection, selectionArgs);
} catch (SecurityException ignored) {
}
}
public static boolean doPlaylistContains(@NonNull final Context context, final long playlistId, final int songId) {
if (playlistId != -1) {
try {
Cursor c = context.getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
new String[]{MediaStore.Audio.Playlists.Members.AUDIO_ID}, MediaStore.Audio.Playlists.Members.AUDIO_ID + "=?", new String[]{String.valueOf(songId)}, null);
int count = 0;
if (c != null) {
count = c.getCount();
c.close();
}
return count > 0;
} catch (SecurityException ignored) {
}
}
return false;
}
public static boolean moveItem(@NonNull final Context context, int playlistId, int from, int to) {
return MediaStore.Audio.Playlists.Members.moveItem(context.getContentResolver(),
playlistId, from, to);
}
public static void renamePlaylist(@NonNull final Context context, final long id, final String newName) {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Audio.PlaylistsColumns.NAME, newName);
try {
context.getContentResolver().update(EXTERNAL_CONTENT_URI,
contentValues,
MediaStore.Audio.Playlists._ID + "=?",
new String[]{String.valueOf(id)});
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
} catch (SecurityException ignored) {
}
}
public static File savePlaylist(Context context, Playlist playlist) throws IOException {
return M3UWriter.write(context, new File(Environment.getExternalStorageDirectory(), "Playlists"), playlist);
}
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
return playlistId != -1 && doesPlaylistExist(context,
MediaStore.Audio.Playlists._ID + "=?",
new String[]{String.valueOf(playlistId)});
}
public static boolean doesPlaylistExist(@NonNull final Context context, final String name) {
return doesPlaylistExist(context,
MediaStore.Audio.PlaylistsColumns.NAME + "=?",
new String[]{name});
}
private static boolean doesPlaylistExist(@NonNull Context context, @NonNull final String selection, @NonNull final String[] values) {
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
new String[]{}, selection, values, null);
boolean exists = false;
if (cursor != null) {
exists = cursor.getCount() != 0;
cursor.close();
}
return exists;
}
/*public static void addToPlaylist(@NonNull Context context,
@NonNull List<Song> songs,
int playlistId,
boolean showToastOnFinish) {
@ -273,5 +507,5 @@ public class PlaylistsUtil {
cursor.close();
}
return exists;
}
}*/
}

View file

@ -37,7 +37,7 @@ class RetroShapeableImageView @JvmOverloads constructor(
typedArray.recycle()
}
fun updateCornerSize(cornerSize: Float) {
private fun updateCornerSize(cornerSize: Float) {
shapeAppearanceModel = ShapeAppearanceModel.Builder()
.setAllCorners(CornerFamily.ROUNDED, cornerSize)
.build()