[Android] Audioplayer code cleaning
Geoffrey Métais
git at videolan.org
Mon Jan 2 16:00:45 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan 2 14:52:36 2017 +0100| [94e6817ebc227a82ce538597278ff59f13e1655a] | committer: Geoffrey Métais
Audioplayer code cleaning
> https://code.videolan.org/videolan/vlc-android/commit/94e6817ebc227a82ce538597278ff59f13e1655a
---
.../org/videolan/vlc/gui/audio/AudioPlayer.java | 51 +++++++++-------------
.../videolan/vlc/gui/audio/PlaylistAdapter.java | 12 +----
2 files changed, 22 insertions(+), 41 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
index 2b71b62..81604db 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
@@ -29,6 +29,7 @@ import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
+import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
@@ -73,8 +74,6 @@ import org.videolan.vlc.gui.view.HeaderMediaSwitcher;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.Strings;
-import java.util.List;
-
public class AudioPlayer extends PlaybackServiceFragment implements PlaybackService.Callback, View.OnClickListener, PlaylistAdapter.IPlayer, TextWatcher {
public static final String TAG = "VLC/AudioPlayer";
@@ -156,18 +155,24 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
mPlaylistSearchText.getEditText().addTextChangedListener(this);
mPlaylist = (RecyclerView) v.findViewById(R.id.songs_list);
- final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
- layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
- mPlaylist.setLayoutManager(layoutManager);
+
+ mSwitcher = (ViewSwitcher) v.findViewById(R.id.view_switcher);
+
+ return v;
+ }
+
+ @Override
+ public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+ mPlaylist.setLayoutManager(new LinearLayoutManager(view.getContext()));
mPlaylist.setAdapter(mPlaylistAdapter);
ItemTouchHelper.Callback callback = new SwipeDragItemTouchHelperCallback(mPlaylistAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(mPlaylist);
- mSwitcher = (ViewSwitcher) v.findViewById(R.id.view_switcher);
- mSwitcher.setInAnimation(getActivity(), android.R.anim.fade_in);
- mSwitcher.setOutAnimation(getActivity(), android.R.anim.fade_out);
+ mSwitcher.setInAnimation(view.getContext(), android.R.anim.fade_in);
+ mSwitcher.setOutAnimation(view.getContext(), android.R.anim.fade_out);
mAdvFuncVisible = false;
mPlaylistSwitchVisible = false;
@@ -219,10 +224,10 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
}
});
mNext.setOnTouchListener(new LongSeekListener(true,
- UiTools.getResourceFromAttribute(getActivity(), R.attr.ic_next),
+ UiTools.getResourceFromAttribute(view.getContext(), R.attr.ic_next),
R.drawable.ic_next_pressed));
mPrevious.setOnTouchListener(new LongSeekListener(false,
- UiTools.getResourceFromAttribute(getActivity(), R.attr.ic_previous),
+ UiTools.getResourceFromAttribute(view.getContext(), R.attr.ic_previous),
R.drawable.ic_previous_pressed));
mShuffle.setOnClickListener(new View.OnClickListener() {
@Override
@@ -247,18 +252,16 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
public void onClick(View v) {
mSwitcher.showNext();
if (mSwitcher.getDisplayedChild() == 0)
- mPlaylistSwitch.setImageResource(UiTools.getResourceFromAttribute(getActivity(),
+ mPlaylistSwitch.setImageResource(UiTools.getResourceFromAttribute(view.getContext(),
R.attr.ic_playlist_on));
else
- mPlaylistSwitch.setImageResource(UiTools.getResourceFromAttribute(getActivity(),
+ mPlaylistSwitch.setImageResource(UiTools.getResourceFromAttribute(view.getContext(),
R.attr.ic_playlist));
}
});
registerForContextMenu(mPlaylist);
getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
-
- return v;
}
public void onPopupMenu(View anchor, final int position) {
@@ -364,20 +367,7 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
mShuffle.setVisibility(mService.canShuffle() ? View.VISIBLE : View.INVISIBLE);
mTimeline.setOnSeekBarChangeListener(mTimelineListner);
- if (playlistDiffer())
- updateList();
- }
-
- private boolean playlistDiffer() {
- int serviceListSize = mService.getMediaListSize();
- if (serviceListSize != mPlaylistAdapter.getItemCount())
- return true;
- List<MediaWrapper> adapterList = mPlaylistAdapter.getMedias();
- List<MediaWrapper> serviceList = mService.getMedias();
- for (int i = 0 ; i < serviceListSize ; ++i)
- if (serviceList.get(i) != adapterList.get(i))
- return true;
- return false;
+ updateList();
}
@Override
@@ -416,9 +406,8 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
public void updateList() {
hideSearchField();
- if (mService == null)
- return;
- mPlaylistAdapter.dispatchUpdate(mService.getMedias());
+ if (mService != null)
+ mPlaylistAdapter.dispatchUpdate(mService.getMedias());
}
@Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
index d8a3a4f..ec92c05 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
@@ -115,10 +115,6 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
return item == null ? "" : item.getLocation();
}
- List<MediaWrapper> getMedias() {
- return mDataSet;
- }
-
public void addAll(List<MediaWrapper> playList) {
mDataSet.addAll(playList);
}
@@ -142,16 +138,12 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
});
}
- public void add(MediaWrapper mw) {
- mDataSet.add(mw);
- }
-
@MainThread
public void remove(int position) {
if (mService == null)
return;
- mDataSet.remove(position);
mService.remove(position);
+ mDataSet.remove(position);
notifyItemRemoved(position);
}
@@ -164,7 +156,7 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
}
public void setCurrentIndex(int position) {
- if (position == mCurrentIndex || position < 0 || position >= mDataSet.size())
+ if (position == mCurrentIndex || position < 0 || position >= getItemCount())
return;
int former = mCurrentIndex;
mCurrentIndex = position;
More information about the Android
mailing list