Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: enricocid <enrico2588@gmail.com>
  • Loading branch information
enricocid committed Jun 18, 2018
1 parent 74b4f71 commit 3e39922
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void run() {
}

private void inflateLayout(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.player_buttons, this, true);
final View v = LayoutInflater.from(context).inflate(R.layout.player_buttons, this, true);
container = v.findViewById(R.id.layout_button_container);
}
}
43 changes: 20 additions & 23 deletions project/app/src/main/java/com/iven/musicplayergo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void onPause() {
}

@Override
public void onAccentChanged(int color) {
public void onAccentChanged(final int color) {
mMusicNotificationManager.setAccentColor(color);
if (mPlayerAdapter.isMediaPlayer()) {
mMusicNotificationManager.getNotificationManager().notify(MusicNotificationManager.NOTIFICATION_ID, mMusicNotificationManager.createNotification());
Expand Down Expand Up @@ -160,7 +160,7 @@ private void checkReadStoragePermissions() {

@TargetApi(23)
private void showPermissionRationale() {
AlertDialog builder = new AlertDialog.Builder(this).create();
final AlertDialog builder = new AlertDialog.Builder(this).create();
builder.setIcon(R.drawable.ic_folder);
builder.setTitle(getString(R.string.app_name));
builder.setMessage(getString(R.string.perm_rationale));
Expand Down Expand Up @@ -290,7 +290,7 @@ public void onGlobalLayout() {

private void initializeSettings() {
if (!EqualizerUtils.hasEqualizer(this)) {
ImageView eqButton = findViewById(R.id.eq);
final ImageView eqButton = findViewById(R.id.eq);
eqButton.getDrawable().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
}
initializeColorsSettings();
Expand All @@ -316,10 +316,10 @@ public void playAllSelectedArtistSongs(View v) {
onSongSelected(mAllSelectedArtistSongs.get(0), mAllSelectedArtistSongs);
}

private void setArtistsRecyclerView(List<Artist> data) {
private void setArtistsRecyclerView(@NonNull final List<Artist> data) {
mArtistsLayoutManager = new LinearLayoutManager(this);
mArtistsRecyclerView.setLayoutManager(mArtistsLayoutManager);
ArtistsAdapter artistsAdapter = new ArtistsAdapter(this, data);
final ArtistsAdapter artistsAdapter = new ArtistsAdapter(this, data);
mArtistsRecyclerView.setAdapter(artistsAdapter);
if (mSavedRecyclerLayoutState != null) {
mArtistsLayoutManager.onRestoreInstanceState(mSavedRecyclerLayoutState);
Expand Down Expand Up @@ -423,8 +423,8 @@ public void switchTheme(View v) {

private void initializeColorsSettings() {

RecyclerView colorsRecyclerView = mSettingsView.findViewById(R.id.colors_rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
final RecyclerView colorsRecyclerView = mSettingsView.findViewById(R.id.colors_rv);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
colorsRecyclerView.setLayoutManager(linearLayoutManager);
colorsRecyclerView.setAdapter(new ColorsAdapter(this, mAccent));
}
Expand All @@ -434,12 +434,12 @@ private void onPermissionGranted() {
}

private void updateResetStatus(boolean onPlaybackCompletion) {
int color = onPlaybackCompletion ? Color.BLACK : mPlayerAdapter.isReset() ? Color.WHITE : Color.BLACK;
final int color = onPlaybackCompletion ? Color.BLACK : mPlayerAdapter.isReset() ? Color.WHITE : Color.BLACK;
mResetButton.getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}

private void updatePlayingStatus() {
int drawable = mPlayerAdapter.getState() != PlaybackInfoListener.State.PAUSED ? R.drawable.ic_pause : R.drawable.ic_play;
final int drawable = mPlayerAdapter.getState() != PlaybackInfoListener.State.PAUSED ? R.drawable.ic_pause : R.drawable.ic_play;
mPlayPauseButton.setImageResource(drawable);
}

Expand All @@ -462,7 +462,7 @@ public void run() {
mSeekBarAudio.setMax(duration);
mDuration.setText(Song.formatDuration(duration));

Spanned spanned = Utils.buildSpanned(getString(R.string.playing_song, mSelectedArtist, selectedSong.title));
final Spanned spanned = Utils.buildSpanned(getString(R.string.playing_song, mSelectedArtist, selectedSong.title));

mPlayingSong.setText(spanned);
mPlayingAlbum.setText(selectedSong.albumName);
Expand Down Expand Up @@ -508,7 +508,7 @@ private void doBindService() {
MusicService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;

Intent startNotStickyIntent = new Intent(this, MusicService.class);
final Intent startNotStickyIntent = new Intent(this, MusicService.class);
startService(startNotStickyIntent);
}

Expand Down Expand Up @@ -549,15 +549,14 @@ private void setArtistDetails(List<Album> albums) {
//only notify recycler view of item changed if an adapter already exists
mAlbumsAdapter.swapArtist(albums);
} else {
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
final LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mAlbumsRecyclerView.setLayoutManager(horizontalLayoutManager);
mAlbumsAdapter = new AlbumsAdapter(this, albums, mPlayerAdapter, ContextCompat.getColor(this, mAccent));
mAlbumsRecyclerView.setAdapter(mAlbumsAdapter);
}

mAllSelectedArtistSongs = SongProvider.getAllArtistSongs(albums);
int albumCount = albums.size();
mArtistAlbumCount.setText(getString(R.string.albums, mSelectedArtist, albumCount));
mArtistAlbumCount.setText(getString(R.string.albums, mSelectedArtist, albums.size()));

if (sExpandPanel) {
revealView(mArtistDetails, mArtistsRecyclerView, false, true);
Expand All @@ -579,7 +578,7 @@ protected void onDestroy() {
}

@Override
public void onSongSelected(Song song, List<Song> songs) {
public void onSongSelected(@NonNull final Song song, @NonNull final List<Song> songs) {

if (mSettingsView.getVisibility() == View.VISIBLE) {
revealView(mSettingsView, mControlsContainer, true, false);
Expand All @@ -592,18 +591,16 @@ public void onSongSelected(Song song, List<Song> songs) {
}

@Override
public void onArtistSelected(String artist) {
public void onArtistSelected(@NonNull final String artist) {

if (!mSelectedArtist.equals(artist)) {

//make the panel expandable
sExpandPanel = true;

mPlayerAdapter.setSelectedAlbum(null);

//load artist albums only if not already loaded
mSelectedArtist = artist;

setArtistDetails(ArtistProvider.getArtist(mArtists, mSelectedArtist).albums);

} else {
Expand All @@ -614,13 +611,13 @@ public void onArtistSelected(String artist) {
}

@Override
public void onAlbumSelected(Album album) {
public void onAlbumSelected(@NonNull final Album album) {
mSelectedAlbum.setText(album.getTitle());
mPlayerAdapter.setSelectedAlbum(album);
if (mSongsAdapter != null) {
mSongsAdapter.swapSongs(album);
} else {
LinearLayoutManager songsLayoutManager = new LinearLayoutManager(this);
final LinearLayoutManager songsLayoutManager = new LinearLayoutManager(this);
mSongsRecyclerView.setLayoutManager(songsLayoutManager);
mSongsAdapter = new SongsAdapter(this, album);
mSongsRecyclerView.setAdapter(mSongsAdapter);
Expand All @@ -639,7 +636,7 @@ private void rotateExpandImage(final boolean expand) {
final float from = expand ? 0.0f : 180.0f;
final float to = expand ? 180.0f : 0.0f;

AnimationSet animSet = new AnimationSet(true);
final AnimationSet animSet = new AnimationSet(true);
animSet.setInterpolator(new DecelerateInterpolator());
animSet.setFillAfter(true);
animSet.setFillEnabled(true);
Expand Down Expand Up @@ -680,7 +677,7 @@ private void revealView(final View viewToReveal, final View viewToHide, final bo
final int fromY = isSettings ? viewToRevealHeight / 2 : viewToHide.getTop() / 2;

if (show) {
Animator anim = ViewAnimationUtils.createCircularReveal(viewToReveal, viewToRevealHalfWidth, fromY, 0, radius);
final Animator anim = ViewAnimationUtils.createCircularReveal(viewToReveal, viewToRevealHalfWidth, fromY, 0, radius);
anim.setDuration(ANIMATION_DURATION);
anim.addListener(new Animator.AnimatorListener() {
@Override
Expand Down Expand Up @@ -712,7 +709,7 @@ public void onAnimationRepeat(Animator animator) {

} else {

Animator anim = ViewAnimationUtils.createCircularReveal(viewToReveal, viewToRevealHalfWidth, fromY, radius, 0);
final Animator anim = ViewAnimationUtils.createCircularReveal(viewToReveal, viewToRevealHalfWidth, fromY, radius, 0);
anim.setDuration(ANIMATION_DURATION);
anim.addListener(new Animator.AnimatorListener() {
@Override
Expand Down
16 changes: 8 additions & 8 deletions project/app/src/main/java/com/iven/musicplayergo/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static Spanned buildSpanned(String res) {
}

static void invertTheme(@NonNull final Activity activity) {
boolean isDark = isThemeInverted(activity);
boolean value = !isDark;
SharedPreferences preferences = activity.getSharedPreferences(THEME_PREF, Context.MODE_PRIVATE);
final boolean isDark = isThemeInverted(activity);
final boolean value = !isDark;
final SharedPreferences preferences = activity.getSharedPreferences(THEME_PREF, Context.MODE_PRIVATE);
preferences.edit().putBoolean(THEME_VALUE, value).apply();
activity.recreate();
}
Expand All @@ -49,7 +49,7 @@ static boolean isThemeInverted(@NonNull final Context context) {
}

static void setTheme(@NonNull final Activity activity, boolean isThemeInverted, int accent) {
int theme = resolveTheme(isThemeInverted, accent);
final int theme = resolveTheme(isThemeInverted, accent);
activity.setTheme(theme);
if (isMarshmallow()) {
enableLightStatusBar(activity, ContextCompat.getColor(activity, accent));
Expand All @@ -61,11 +61,11 @@ static void setTheme(@NonNull final Activity activity, boolean isThemeInverted,
@TargetApi(23)
private static void enableLightStatusBar(Activity activity, int accent) {

View decorView = activity.getWindow().getDecorView();
int oldSystemUiFlags = decorView.getSystemUiVisibility();
final View decorView = activity.getWindow().getDecorView();
final int oldSystemUiFlags = decorView.getSystemUiVisibility();
int newSystemUiFlags = oldSystemUiFlags;

boolean isColorDark = ColorUtils.calculateLuminance(accent) < 0.35;
final boolean isColorDark = ColorUtils.calculateLuminance(accent) < 0.35;
if (isColorDark) {
newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
Expand Down Expand Up @@ -154,7 +154,7 @@ private static int resolveTheme(boolean isThemeDark, int accent) {
}

static void setThemeAccent(@NonNull final Activity activity, int accent) {
SharedPreferences preferences = activity.getSharedPreferences(ACCENT_PREF, Context.MODE_PRIVATE);
final SharedPreferences preferences = activity.getSharedPreferences(ACCENT_PREF, Context.MODE_PRIVATE);
preferences.edit().putInt(ACCENT_VALUE, accent).apply();
activity.recreate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.SimpleView
private List<Album> mAlbums;
private Album mSelectedAlbum;

public AlbumsAdapter(@NonNull Activity activity, List<Album> albums, PlayerAdapter playerAdapter, int accent) {
public AlbumsAdapter(@NonNull final Activity activity, @NonNull final List<Album> albums, @NonNull final PlayerAdapter playerAdapter, final int accent) {
mActivity = activity;
mAlbums = albums;
mPlayerAdapter = playerAdapter;
Expand All @@ -49,21 +49,21 @@ private void updateAlbumsForArtist() {
@NonNull
public SimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(mActivity)
final View itemView = LayoutInflater.from(mActivity)
.inflate(R.layout.album_item, parent, false);

return new SimpleViewHolder(itemView);
}

private String getYear(int year) {
private String getYear(final int year) {
return year != 0 && year != -1 ? String.valueOf(year) : mActivity.getString(R.string.unknown_year);
}

@Override
public void onBindViewHolder(@NonNull SimpleViewHolder holder, int position) {

Album album = mAlbums.get(holder.getAdapterPosition());
String albumTitle = album.getTitle();
final Album album = mAlbums.get(holder.getAdapterPosition());
final String albumTitle = album.getTitle();
holder.title.setText(albumTitle);
holder.year.setText(getYear(album.getYear()));
}
Expand All @@ -74,19 +74,19 @@ public int getItemCount() {
}

public interface AlbumSelectedListener {
void onAlbumSelected(Album album);
void onAlbumSelected(@NonNull final Album album);
}

class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

final TextView title, year;

SimpleViewHolder(View itemView) {
SimpleViewHolder(@NonNull final View itemView) {
super(itemView);

title = itemView.findViewById(R.id.album);
year = itemView.findViewById(R.id.year);
CardView cardContainer = (CardView) itemView;
final CardView cardContainer = (CardView) itemView;
cardContainer.setCardBackgroundColor(ColorUtils.setAlphaComponent(mAccent, 15));
itemView.setOnClickListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ColorsAdapter extends RecyclerView.Adapter<ColorsAdapter.SimpleView
R.color.blue_gray_400
};

public ColorsAdapter(@NonNull Activity activity, int accent) {
public ColorsAdapter(@NonNull Activity activity, final int accent) {
mActivity = activity;
mAccent = accent;
mOnAccentChangedListener = (AccentChangedListener) mActivity;
Expand All @@ -47,19 +47,17 @@ public ColorsAdapter(@NonNull Activity activity, int accent) {
@NonNull
public SimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(parent.getContext())
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.color_option, parent, false);
return new SimpleViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull SimpleViewHolder holder, int position) {

int color = colors[holder.getAdapterPosition()];

int drawable = color != mAccent ? R.drawable.ic_checkbox_blank : R.drawable.ic_checkbox_marked;

int parsedColor = ContextCompat.getColor(mActivity, color);
final int color = colors[holder.getAdapterPosition()];
final int drawable = color != mAccent ? R.drawable.ic_checkbox_blank : R.drawable.ic_checkbox_marked;
final int parsedColor = ContextCompat.getColor(mActivity, color);
holder.color.setImageResource(drawable);
holder.color.setColorFilter(parsedColor);
}
Expand All @@ -71,14 +69,14 @@ public int getItemCount() {
}

public interface AccentChangedListener {
void onAccentChanged(int color);
void onAccentChanged(final int color);
}

class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private final ImageView color;

SimpleViewHolder(View itemView) {
SimpleViewHolder(@NonNull final View itemView) {
super(itemView);

color = (ImageView) itemView;
Expand All @@ -88,7 +86,7 @@ class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickLi
@Override
public void onClick(View v) {
//recreate the activity only if necessary
int color = colors[getAdapterPosition()];
final int color = colors[getAdapterPosition()];
if (color != mAccent) {
mOnAccentChangedListener.onAccentChanged(color);
}
Expand Down
Loading

0 comments on commit 3e39922

Please sign in to comment.