diff --git a/README.md b/README.md index 3255eef..eae1888 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ | Lite | Online | | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [Github release v0.12](https://github.com/AP-Atul/music_player_lite/releases/download/v0.12/mplite.apk) | [Github release v0.3](https://github.com/AP-Atul/music_player_lite/releases/download/v0.3/mplite_online.apk) | +| [Github release v0.13](https://github.com/AP-Atul/music_player_lite/releases/download/v0.13/mplite.apk) | [Github release v0.3](https://github.com/AP-Atul/music_player_lite/releases/download/v0.3/mplite_online.apk) | | | | | | | diff --git a/src/app/build.gradle b/src/app/build.gradle index 97bad8d..00f62b8 100644 --- a/src/app/build.gradle +++ b/src/app/build.gradle @@ -18,8 +18,8 @@ android { applicationId "com.atul.musicplayer" minSdkVersion 23 targetSdkVersion 33 - versionCode 12 - versionName "0.12" + versionCode 13 + versionName "0.13" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -35,14 +35,15 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + namespace 'com.atul.musicplayer' } dependencies { implementation 'androidx.legacy:legacy-support-v4:1.0.0' - implementation 'androidx.appcompat:appcompat:1.3.0' - implementation 'com.google.android.material:material:1.3.0' - implementation 'androidx.constraintlayout:constraintlayout:2.0.4' - implementation 'androidx.lifecycle:lifecycle-livedata:2.3.1' - implementation 'androidx.lifecycle:lifecycle-viewmodel:2.3.1' - implementation 'com.github.bumptech.glide:glide:4.11.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.lifecycle:lifecycle-livedata:2.5.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1' + implementation 'com.github.bumptech.glide:glide:4.14.2' } \ No newline at end of file diff --git a/src/app/src/main/AndroidManifest.xml b/src/app/src/main/AndroidManifest.xml index e4b4308..838ebdd 100644 --- a/src/app/src/main/AndroidManifest.xml +++ b/src/app/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + diff --git a/src/app/src/main/java/com/atul/musicplayer/MainActivity.java b/src/app/src/main/java/com/atul/musicplayer/MainActivity.java index 3cb8350..8cfe6e7 100644 --- a/src/app/src/main/java/com/atul/musicplayer/MainActivity.java +++ b/src/app/src/main/java/com/atul/musicplayer/MainActivity.java @@ -68,8 +68,8 @@ public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setTheme(ThemeHelper.getTheme(MPPreferences.getTheme(getApplicationContext()))); - AppCompatDelegate.setDefaultNightMode(MPPreferences.getThemeMode(getApplicationContext())); + setTheme(ThemeHelper.getTheme(MPPreferences.getTheme(MainActivity.this))); + AppCompatDelegate.setDefaultNightMode(MPPreferences.getThemeMode(MainActivity.this)); setContentView(R.layout.activity_main); MPConstants.musicSelectListener = this; @@ -82,7 +82,6 @@ protected void onCreate(Bundle savedInstanceState) { manageStoragePermission(MainActivity.this); albumState = MPPreferences.getAlbumRequest(this); - MPConstants.musicSelectListener = this; MaterialCardView playerLayout = findViewById(R.id.player_layout); albumArt = findViewById(R.id.albumArt); @@ -274,7 +273,6 @@ else if (id == R.id.player_layout) private void setUpPlayerDialog() { playerDialog = new PlayerDialog(this, playerManager, this); - playerDialog.show(); } @@ -324,7 +322,11 @@ public void sleepTimerOptionSelect() { private void setUpQueueDialog() { queueDialog = new QueueDialog(MainActivity.this, playerManager.getPlayerQueue()); - queueDialog.setOnDismissListener(v -> playerDialog.show()); + queueDialog.setOnDismissListener(v -> { + if(!this.isDestroyed()) { + playerDialog.show(); + } + }); playerDialog.dismiss(); queueDialog.show(); @@ -341,7 +343,9 @@ private void setUpSleepTimerDialog() { return; } SleepTimerDialog sleepTimerDialog = new SleepTimerDialog(MainActivity.this, this); - sleepTimerDialog.setOnDismissListener(v -> playerDialog.show()); + sleepTimerDialog.setOnDismissListener(v -> { + if(!this.isDestroyed()) playerDialog.show(); + }); playerDialog.dismiss(); sleepTimerDialog.show(); @@ -349,7 +353,9 @@ private void setUpSleepTimerDialog() { private void setUpSleepTimerDisplayDialog() { SleepTimerDisplayDialog sleepTimerDisplayDialog = new SleepTimerDisplayDialog(MainActivity.this, this); - sleepTimerDisplayDialog.setOnDismissListener(v -> playerDialog.show()); + sleepTimerDisplayDialog.setOnDismissListener(v -> { + if(!this.isDestroyed()) playerDialog.show(); + }); playerDialog.dismiss(); sleepTimerDisplayDialog.show(); diff --git a/src/app/src/main/java/com/atul/musicplayer/activities/PlayerDialog.java b/src/app/src/main/java/com/atul/musicplayer/activities/PlayerDialog.java index e72e1d8..d4cdee2 100644 --- a/src/app/src/main/java/com/atul/musicplayer/activities/PlayerDialog.java +++ b/src/app/src/main/java/com/atul/musicplayer/activities/PlayerDialog.java @@ -194,7 +194,6 @@ public void onClick(View v) { else if (id == R.id.music_queue) this.playerDialogListener.queueOptionSelect(); else if (id == R.id.sleep_timer) this.playerDialogListener.sleepTimerOptionSelect(); - setUpUi(); } private void setRepeat() { diff --git a/src/app/src/main/java/com/atul/musicplayer/adapter/QueueAdapter.java b/src/app/src/main/java/com/atul/musicplayer/adapter/QueueAdapter.java index 1b397e9..56035fe 100644 --- a/src/app/src/main/java/com/atul/musicplayer/adapter/QueueAdapter.java +++ b/src/app/src/main/java/com/atul/musicplayer/adapter/QueueAdapter.java @@ -24,6 +24,7 @@ public class QueueAdapter extends RecyclerView.Adapter musicList; private final PlayerQueue playerQueue; + private final Music currentMusic; private final @ColorInt int colorInt; int defaultTint; @@ -31,6 +32,7 @@ public class QueueAdapter extends RecyclerView.Adapter musics, PlayerQueue playerQueue) { this.musicList = musics; this.playerQueue = playerQueue; + this.currentMusic = playerQueue.getCurrentMusic(); colorInt = ThemeHelper.resolveColorAttr( context, @@ -51,7 +53,7 @@ public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.songName.setText(musicList.get(position).title); - if (playerQueue.getCurrentMusic().title.equals(musicList.get(position).title)) { + if (currentMusic.title.equals(musicList.get(position).title)) { holder.albumName.setText(R.string.now_playing); holder.albumName.setTextColor(colorInt); holder.drag.setImageResource(R.drawable.ic_current_playing); @@ -106,9 +108,17 @@ public MyViewHolder(@NonNull View itemView) { drag = itemView.findViewById(R.id.control_drag); itemView.findViewById(R.id.control_close).setOnClickListener(v -> { - musicList.remove(getAdapterPosition()); - playerQueue.removeMusicFromQueue(getAdapterPosition()); - notifyDataSetChanged(); + int position = getAdapterPosition(); + + if(position >= 0 && position < musicList.size()) { + boolean isPlaying = currentMusic.title.equals(musicList.get(position).title); + + if (!isPlaying) { + musicList.remove(position); + playerQueue.removeMusicFromQueue(position); + notifyItemRemoved(position); + } + } }); } } diff --git a/src/app/src/main/java/com/atul/musicplayer/fragments/SongsFragment.java b/src/app/src/main/java/com/atul/musicplayer/fragments/SongsFragment.java index a8b69f3..810fe80 100644 --- a/src/app/src/main/java/com/atul/musicplayer/fragments/SongsFragment.java +++ b/src/app/src/main/java/com/atul/musicplayer/fragments/SongsFragment.java @@ -114,13 +114,21 @@ private void setUpSearchView() { @Override public boolean onQueryTextSubmit(String query) { - updateAdapter(ListHelper.searchMusicByName(unChangedList, query.toLowerCase())); + if(query.length() > 0) { + updateAdapter(ListHelper.searchMusicByName(unChangedList, query.toLowerCase())); + } else { + updateAdapter(unChangedList); + } return true; } @Override - public boolean onQueryTextChange(String newText) { - updateAdapter(ListHelper.searchMusicByName(unChangedList, newText.toLowerCase())); + public boolean onQueryTextChange(String query) { + if(query.length() > 0) { + updateAdapter(ListHelper.searchMusicByName(unChangedList, query.toLowerCase())); + }else { + updateAdapter(unChangedList); + } return true; } diff --git a/src/app/src/main/java/com/atul/musicplayer/helper/ThemeHelper.java b/src/app/src/main/java/com/atul/musicplayer/helper/ThemeHelper.java index 0578aa8..51dea86 100644 --- a/src/app/src/main/java/com/atul/musicplayer/helper/ThemeHelper.java +++ b/src/app/src/main/java/com/atul/musicplayer/helper/ThemeHelper.java @@ -61,7 +61,11 @@ public static HashMap getThemeMap() { } public static Integer getTheme(Integer accentColor) { - return getThemeMap().get(accentColor); + Integer theme = getThemeMap().get(accentColor); + if(theme == null) { + return getThemeMap().get(R.color.blue); + } + return theme; } public static void applySettings(Activity activity) { diff --git a/src/app/src/main/java/com/atul/musicplayer/player/PlayerManager.java b/src/app/src/main/java/com/atul/musicplayer/player/PlayerManager.java index b2993e4..73c6858 100644 --- a/src/app/src/main/java/com/atul/musicplayer/player/PlayerManager.java +++ b/src/app/src/main/java/com/atul/musicplayer/player/PlayerManager.java @@ -84,7 +84,7 @@ public void onAudioFocusChange(final int focusChange) { public PlayerManager(@NonNull PlayerService playerService) { this.playerService = playerService; this.context = playerService.getApplicationContext(); - this.playerQueue = new PlayerQueue(); + this.playerQueue = PlayerQueue.getInstance(); this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); Observer progressObserver = percent -> { @@ -174,7 +174,7 @@ public void setPlayerListener(PlayerListener listener) { } public void setMusicList(List musicList) { - playerQueue.setCurrentQueue(musicList); + playerQueue.setCurrentQueue(new ArrayList<>(musicList)); initMediaPlayer(); // play now } @@ -186,7 +186,7 @@ public void setMusic(Music music) { } public void addMusicQueue(List musicList) { - playerQueue.addMusicListToQueue(musicList); + playerQueue.addMusicListToQueue(new ArrayList<>(musicList)); if (!mediaPlayer.isPlaying()) initMediaPlayer(); // play when ready diff --git a/src/app/src/main/java/com/atul/musicplayer/player/PlayerQueue.java b/src/app/src/main/java/com/atul/musicplayer/player/PlayerQueue.java index 9b265df..dd64da0 100644 --- a/src/app/src/main/java/com/atul/musicplayer/player/PlayerQueue.java +++ b/src/app/src/main/java/com/atul/musicplayer/player/PlayerQueue.java @@ -8,6 +8,7 @@ import java.util.Random; public class PlayerQueue { + private static PlayerQueue instance = null; private final Random random = new Random(); private List currentQueue; private List played; @@ -15,8 +16,15 @@ public class PlayerQueue { private boolean repeat = false; private int currentPosition = 0; + public static PlayerQueue getInstance() { + if (instance == null) { + instance = new PlayerQueue(); + } + return instance; + } + private boolean isCurrentPositionOutOfBound(int pos) { - return pos >= currentQueue.size() || pos <= 0; + return pos >= currentQueue.size() || pos < 0; } public boolean isShuffle() { @@ -85,11 +93,19 @@ public void prev() { public void removeMusicFromQueue(int position) { if (!isCurrentPositionOutOfBound(position)) { currentQueue.remove(position); + if(currentPosition > position) + currentPosition -= 1; } } public void swap(int one, int two) { if (!isCurrentPositionOutOfBound(one) && !isCurrentPositionOutOfBound(two)) { + if(one == currentPosition) { + currentPosition = two; + } + else if(two == currentPosition) { + currentPosition = one; + } Collections.swap(currentQueue, one, two); } } diff --git a/src/app/src/main/java/com/atul/musicplayer/player/PlayerService.java b/src/app/src/main/java/com/atul/musicplayer/player/PlayerService.java index df2356f..6a5b73f 100644 --- a/src/app/src/main/java/com/atul/musicplayer/player/PlayerService.java +++ b/src/app/src/main/java/com/atul/musicplayer/player/PlayerService.java @@ -104,6 +104,9 @@ private void configureMediaSession() { private boolean handleMediaButtonEvent(Intent mediaButtonEvent) { boolean isSuccess = false; + if (mediaButtonEvent == null) { + return false; + } KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); diff --git a/src/build.gradle b/src/build.gradle index 9d8b761..bcfbab2 100644 --- a/src/build.gradle +++ b/src/build.gradle @@ -5,7 +5,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.2.2' + classpath 'com.android.tools.build:gradle:7.3.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/src/gradle/wrapper/gradle-wrapper.properties b/src/gradle/wrapper/gradle-wrapper.properties index ccfdedb..cfb07bb 100644 --- a/src/gradle/wrapper/gradle-wrapper.properties +++ b/src/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip