From 36ae526dc7affdd9b454e17cf8f93a22819f4dba Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sun, 29 Jul 2018 23:06:27 +0530 Subject: [PATCH] improved lyrics view --- .../retromusic/dialogs/HomeOptionDialog.java | 183 +-- .../ui/activities/LyricsActivity.java | 25 +- .../ui/activities/MainActivity.java | 526 +++---- .../mainactivity/home/HomeFragment.java | 688 +++++---- .../retromusic/util/PreferenceUtil.java | 1365 ++++++++--------- .../monkey/retromusic/views/LyricView.java | 7 +- 6 files changed, 1405 insertions(+), 1389 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/HomeOptionDialog.java b/app/src/main/java/code/name/monkey/retromusic/dialogs/HomeOptionDialog.java index 11873df9..1bb4730d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/HomeOptionDialog.java +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/HomeOptionDialog.java @@ -1,5 +1,7 @@ package code.name.monkey.retromusic.dialogs; +import static code.name.monkey.retromusic.Constants.USER_PROFILE; + import android.graphics.Bitmap; import android.os.Bundle; import android.support.annotation.NonNull; @@ -9,15 +11,13 @@ import android.support.v7.widget.AppCompatTextView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - -import java.io.File; -import java.util.Calendar; - import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.Unbinder; import code.name.monkey.retromusic.R; +import code.name.monkey.retromusic.ui.activities.MainActivity; +import code.name.monkey.retromusic.ui.fragments.mainactivity.folders.FoldersFragment; import code.name.monkey.retromusic.util.Compressor; import code.name.monkey.retromusic.util.NavigationUtil; import code.name.monkey.retromusic.util.PreferenceUtil; @@ -26,100 +26,109 @@ import code.name.monkey.retromusic.views.RoundedBottomSheetDialogFragment; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; - -import static code.name.monkey.retromusic.Constants.USER_PROFILE; +import java.io.File; +import java.util.Calendar; /** * @author Hemanth S (h4h13). */ public class HomeOptionDialog extends RoundedBottomSheetDialogFragment { - private static final String TAG = "HomeOptionDialog"; - Unbinder mUnbinder; - @BindView(R.id.user_image_bottom) - CircularImageView userImageBottom; - @BindView(R.id.title_welcome) - AppCompatTextView titleWelcome; - private CompositeDisposable disposable = new CompositeDisposable(); - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View layout = inflater.inflate(R.layout.user_action_details, container, false); - mUnbinder = ButterKnife.bind(this, layout); - return layout; + private static final String TAG = "HomeOptionDialog"; + Unbinder mUnbinder; + @BindView(R.id.user_image_bottom) + CircularImageView userImageBottom; + @BindView(R.id.title_welcome) + AppCompatTextView titleWelcome; + private CompositeDisposable disposable = new CompositeDisposable(); + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View layout = inflater.inflate(R.layout.user_action_details, container, false); + mUnbinder = ButterKnife.bind(this, layout); + return layout; + } + + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + loadImageFromStorage(); + titleWelcome.setText(String.format("%s, %s!", getTimeOfTheDay(), + PreferenceUtil.getInstance(getContext()).getUserName())); + } + + private String getTimeOfTheDay() { + String message = getString(R.string.title_good_day); + Calendar c = Calendar.getInstance(); + int timeOfDay = c.get(Calendar.HOUR_OF_DAY); + + if (timeOfDay >= 0 && timeOfDay < 6) { + message = getString(R.string.title_good_night); + } else if (timeOfDay >= 6 && timeOfDay < 12) { + message = getString(R.string.title_good_morning); + } else if (timeOfDay >= 12 && timeOfDay < 16) { + message = getString(R.string.title_good_afternoon); + } else if (timeOfDay >= 16 && timeOfDay < 20) { + message = getString(R.string.title_good_evening); + } else if (timeOfDay >= 20 && timeOfDay < 24) { + message = getString(R.string.title_good_night); } + return message; + } + @Override + public void onDestroyView() { + super.onDestroyView(); + disposable.clear(); + mUnbinder.unbind(); + } - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - loadImageFromStorage(); - titleWelcome.setText(String.format("%s, %s!", getTimeOfTheDay(), PreferenceUtil.getInstance(getContext()).getUserName())); - } - - private String getTimeOfTheDay() { - String message = getString(R.string.title_good_day); - Calendar c = Calendar.getInstance(); - int timeOfDay = c.get(Calendar.HOUR_OF_DAY); - - if (timeOfDay >= 0 && timeOfDay < 6) { - message = getString(R.string.title_good_night); - } else if (timeOfDay >= 6 && timeOfDay < 12) { - message = getString(R.string.title_good_morning); - } else if (timeOfDay >= 12 && timeOfDay < 16) { - message = getString(R.string.title_good_afternoon); - } else if (timeOfDay >= 16 && timeOfDay < 20) { - message = getString(R.string.title_good_evening); - } else if (timeOfDay >= 20 && timeOfDay < 24) { - message = getString(R.string.title_good_night); + @SuppressWarnings("ConstantConditions") + @OnClick({R.id.action_about, R.id.user_info_container, R.id.action_folder, R.id.action_settings, + R.id.action_sleep_timer}) + public void onViewClicked(View view) { + switch (view.getId()) { + case R.id.user_info_container: + NavigationUtil.goToUserInfo(getActivity()); + break; + case R.id.action_folder: + MainActivity mainActivity = (MainActivity) getActivity(); + if (mainActivity == null) { + return; } - return message; - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - disposable.clear(); - mUnbinder.unbind(); - } - - @OnClick({R.id.action_about, R.id.user_info_container, R.id.action_folder, R.id.action_settings, R.id.action_sleep_timer}) - public void onViewClicked(View view) { - switch (view.getId()) { - case R.id.user_info_container: - NavigationUtil.goToUserInfo(getActivity()); - break; - case R.id.action_folder: - //getMainActivity().setCurrentFragment(FoldersFragment.newInstance(getContext()), true); - break; - case R.id.action_settings: - NavigationUtil.goToSettings(getActivity()); - break; - case R.id.action_about: - NavigationUtil.goToAbout(getActivity()); - break; - case R.id.action_sleep_timer: - if (getFragmentManager() != null) { - new SleepTimerDialog().show(getFragmentManager(), TAG); - } - break; + mainActivity.setCurrentFragment(FoldersFragment.newInstance(getContext()), true); + break; + case R.id.action_settings: + NavigationUtil.goToSettings(getActivity()); + break; + case R.id.action_about: + NavigationUtil.goToAbout(getActivity()); + break; + case R.id.action_sleep_timer: + if (getFragmentManager() != null) { + new SleepTimerDialog().show(getFragmentManager(), TAG); } - dismiss(); + break; } + dismiss(); + } - private void loadImageFromStorage() { - //noinspection ConstantConditions - disposable.add(new Compressor(getContext()) - .setMaxHeight(300) - .setMaxWidth(300) - .setQuality(75) - .setCompressFormat(Bitmap.CompressFormat.WEBP) - .compressToBitmapAsFlowable( - new File(PreferenceUtil.getInstance(getContext()).getProfileImage(), USER_PROFILE)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(userImageBottom::setImageBitmap, - throwable -> userImageBottom.setImageDrawable(ContextCompat - .getDrawable(getContext(), R.drawable.ic_person_flat)))); - } + private void loadImageFromStorage() { + //noinspection ConstantConditions + disposable.add(new Compressor(getContext()) + .setMaxHeight(300) + .setMaxWidth(300) + .setQuality(75) + .setCompressFormat(Bitmap.CompressFormat.WEBP) + .compressToBitmapAsFlowable( + new File(PreferenceUtil.getInstance(getContext()).getProfileImage(), USER_PROFILE)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(userImageBottom::setImageBitmap, + throwable -> userImageBottom.setImageDrawable(ContextCompat + .getDrawable(getContext(), R.drawable.ic_person_flat)))); + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/ui/activities/LyricsActivity.java b/app/src/main/java/code/name/monkey/retromusic/ui/activities/LyricsActivity.java index 1e057144..a68ae024 100644 --- a/app/src/main/java/code/name/monkey/retromusic/ui/activities/LyricsActivity.java +++ b/app/src/main/java/code/name/monkey/retromusic/ui/activities/LyricsActivity.java @@ -2,9 +2,11 @@ package code.name.monkey.retromusic.ui.activities; import android.annotation.SuppressLint; import android.content.res.ColorStateList; +import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; import android.support.v7.widget.Toolbar; import android.text.InputType; import android.text.TextUtils; @@ -31,6 +33,7 @@ import code.name.monkey.retromusic.util.LyricUtil; import code.name.monkey.retromusic.util.MusicUtil; import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.RetroUtil; +import code.name.monkey.retromusic.views.LyricView; import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; import com.bumptech.glide.Glide; @@ -48,7 +51,8 @@ public class LyricsActivity extends AbsMusicServiceActivity implements TextView songTitle; @BindView(R.id.text) TextView songText; - + @BindView(R.id.lyrics_view) + LyricView lyricView; @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.offline_lyrics) @@ -91,11 +95,11 @@ public class LyricsActivity extends AbsMusicServiceActivity implements radioButton.setTextColor(ThemeStore.textColorPrimary(this)); offlineLyrics.setVisibility(View.GONE); - + lyricView.setVisibility(View.GONE); switch (group) { case R.id.synced_lyrics: loadLRCLyrics(); - + lyricView.setVisibility(View.VISIBLE); break; default: case R.id.normal_lyrics: @@ -117,6 +121,13 @@ public class LyricsActivity extends AbsMusicServiceActivity implements private void setupLyricsView() { disposable = new CompositeDisposable(); + + lyricView + .setOnPlayerClickListener((progress, content) -> MusicPlayerRemote.seekTo((int) progress)); + //lyricView.setHighLightTextColor(ThemeStore.accentColor(this)); + lyricView.setDefaultColor(ContextCompat.getColor(this, R.color.md_grey_400)); + //lyricView.setTouchable(false); + lyricView.setHintColor(Color.WHITE); } private void setupToolbar() { @@ -170,7 +181,7 @@ public class LyricsActivity extends AbsMusicServiceActivity implements @Override public void onUpdateProgressViews(int progress, int total) { - + lyricView.setCurrentTimeMillis(progress); } private void loadLrcFile() { @@ -193,7 +204,11 @@ public class LyricsActivity extends AbsMusicServiceActivity implements } private void showLyricsLocal(File file) { - + if (file == null) { + lyricView.reset(); + } else { + lyricView.setLyricFile(file, "UTF-8"); + } } @OnClick({R.id.edit_lyrics}) diff --git a/app/src/main/java/code/name/monkey/retromusic/ui/activities/MainActivity.java b/app/src/main/java/code/name/monkey/retromusic/ui/activities/MainActivity.java index 71b12467..9bea7bd3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/ui/activities/MainActivity.java +++ b/app/src/main/java/code/name/monkey/retromusic/ui/activities/MainActivity.java @@ -22,12 +22,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; - -import com.afollestad.materialdialogs.MaterialDialog; - -import java.util.ArrayList; -import java.util.concurrent.TimeUnit; - import butterknife.BindView; import butterknife.ButterKnife; import code.name.monkey.retromusic.R; @@ -43,294 +37,300 @@ import code.name.monkey.retromusic.ui.activities.base.AbsSlidingMusicPanelActivi import code.name.monkey.retromusic.ui.fragments.mainactivity.LibraryFragment; import code.name.monkey.retromusic.ui.fragments.mainactivity.home.HomeFragment; import code.name.monkey.retromusic.util.PreferenceUtil; +import com.afollestad.materialdialogs.MaterialDialog; import io.reactivex.Observable; import io.reactivex.disposables.CompositeDisposable; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; public class MainActivity extends AbsSlidingMusicPanelActivity implements - SharedPreferences.OnSharedPreferenceChangeListener, - BottomNavigationView.OnNavigationItemSelectedListener { + SharedPreferences.OnSharedPreferenceChangeListener, + BottomNavigationView.OnNavigationItemSelectedListener { - public static final int APP_INTRO_REQUEST = 2323; - private static final String TAG = "MainActivity"; - private static final int APP_USER_INFO_REQUEST = 9003; - private static final int REQUEST_CODE_THEME = 9002; + public static final int APP_INTRO_REQUEST = 2323; + private static final String TAG = "MainActivity"; + private static final int APP_USER_INFO_REQUEST = 9003; + private static final int REQUEST_CODE_THEME = 9002; - @Nullable - MainActivityFragmentCallbacks currentFragment; + @Nullable + MainActivityFragmentCallbacks currentFragment; - @BindView(R.id.parent_container) - FrameLayout drawerLayout; - - private boolean blockRequestPermissions; - private CompositeDisposable disposable = new CompositeDisposable(); - - private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (action != null && action.equals(Intent.ACTION_SCREEN_OFF)) { - if (PreferenceUtil.getInstance(context).getLockScreen() && MusicPlayerRemote.isPlaying()) { - Intent activity = new Intent(context, LockScreenActivity.class); - activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - activity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - ActivityCompat.startActivity(context, activity, null); - } - } - } - }; + @BindView(R.id.parent_container) + FrameLayout drawerLayout; + private boolean blockRequestPermissions; + private CompositeDisposable disposable = new CompositeDisposable(); + private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override - protected View createContentView() { - @SuppressLint("InflateParams") - View contentView = getLayoutInflater().inflate(R.layout.activity_main_drawer_layout, null); - ViewGroup drawerContent = contentView.findViewById(R.id.drawer_content_container); - drawerContent.addView(wrapSlidingMusicPanel(R.layout.activity_main_content)); - return contentView; + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action != null && action.equals(Intent.ACTION_SCREEN_OFF)) { + if (PreferenceUtil.getInstance(context).getLockScreen() && MusicPlayerRemote.isPlaying()) { + Intent activity = new Intent(context, LockScreenActivity.class); + activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + activity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + ActivityCompat.startActivity(context, activity, null); + } + } } + }; - @Override - protected void onCreate(Bundle savedInstanceState) { - setDrawUnderStatusbar(true); - super.onCreate(savedInstanceState); - ButterKnife.bind(this); + @Override + protected View createContentView() { + @SuppressLint("InflateParams") + View contentView = getLayoutInflater().inflate(R.layout.activity_main_drawer_layout, null); + ViewGroup drawerContent = contentView.findViewById(R.id.drawer_content_container); + drawerContent.addView(wrapSlidingMusicPanel(R.layout.activity_main_content)); + return contentView; + } - setBottomBarVisibility(View.VISIBLE); + @Override + protected void onCreate(Bundle savedInstanceState) { + setDrawUnderStatusbar(true); + super.onCreate(savedInstanceState); - drawerLayout.setOnApplyWindowInsetsListener((view, windowInsets) -> - windowInsets.replaceSystemWindowInsets(0, 0, 0, 0)); + ButterKnife.bind(this); - if (savedInstanceState == null) { - setCurrentFragment(PreferenceUtil.getInstance(this).getLastPage()); - } else { - restoreCurrentFragment(); - } + setBottomBarVisibility(View.VISIBLE); + + drawerLayout.setOnApplyWindowInsetsListener((view, windowInsets) -> + windowInsets.replaceSystemWindowInsets(0, 0, 0, 0)); + + if (savedInstanceState == null) { + setCurrentFragment(PreferenceUtil.getInstance(this).getLastPage()); + } else { + restoreCurrentFragment(); + } /*if (!RetroApplication.isProVersion() && !PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("shown", false)) { showPromotionalOffer(); }*/ - getBottomNavigationView().setOnNavigationItemSelectedListener(this); + getBottomNavigationView().setOnNavigationItemSelectedListener(this); + } + + @Override + protected void onResume() { + super.onResume(); + IntentFilter screenOnOff = new IntentFilter(); + screenOnOff.addAction(Intent.ACTION_SCREEN_OFF); + registerReceiver(broadcastReceiver, screenOnOff); + + PreferenceUtil.getInstance(this).registerOnSharedPreferenceChangedListener(this); + + if (getIntent().hasExtra("expand")) { + if (getIntent().getBooleanExtra("expand", false)) { + expandPanel(); + getIntent().putExtra("expand", false); + } } - @Override - protected void onResume() { - super.onResume(); - IntentFilter screenOnOff = new IntentFilter(); - screenOnOff.addAction(Intent.ACTION_SCREEN_OFF); - registerReceiver(broadcastReceiver, screenOnOff); + } - PreferenceUtil.getInstance(this).registerOnSharedPreferenceChangedListener(this); + @Override + public void onDestroy() { + super.onDestroy(); + disposable.clear(); + if (broadcastReceiver == null) { + return; + } + unregisterReceiver(broadcastReceiver); + PreferenceUtil.getInstance(this).unregisterOnSharedPreferenceChangedListener(this); + } - if (getIntent().hasExtra("expand")) { - if (getIntent().getBooleanExtra("expand", false)) { - expandPanel(); - getIntent().putExtra("expand", false); - } + public void setCurrentFragment(@Nullable Fragment fragment, boolean isStackAdd) { + FragmentManager fragmentManager = getSupportFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.replace(R.id.fragment_container, fragment, TAG); + if (isStackAdd) { + fragmentTransaction.addToBackStack(TAG); + } + fragmentTransaction.commit(); + currentFragment = (MainActivityFragmentCallbacks) fragment; + } + + private void restoreCurrentFragment() { + currentFragment = (MainActivityFragmentCallbacks) getSupportFragmentManager() + .findFragmentById(R.id.fragment_container); + } + + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + PreferenceUtil.getInstance(this).setLastPage(item.getItemId()); + disposable.add(Observable.just(item.getItemId()) + .throttleFirst(3, TimeUnit.SECONDS) + .subscribe(this::setCurrentFragment)); + return true; + } + + private void setCurrentFragment(int menuItem) { + switch (menuItem) { + case R.id.action_song: + case R.id.action_album: + case R.id.action_artist: + case R.id.action_playlist: + setCurrentFragment(LibraryFragment.newInstance(menuItem), false); + break; + default: + case R.id.action_home: + setCurrentFragment(HomeFragment.newInstance(), false); + break; + } + } + + private void handlePlaybackIntent(@Nullable Intent intent) { + if (intent == null) { + return; + } + + Uri uri = intent.getData(); + String mimeType = intent.getType(); + boolean handled = false; + + if (intent.getAction() != null && intent.getAction() + .equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) { + final ArrayList songs = SearchQueryHelper.getSongs(this, intent.getExtras()); + + if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) { + MusicPlayerRemote.openAndShuffleQueue(songs, true); + } else { + MusicPlayerRemote.openQueue(songs, 0, true); + } + handled = true; + } + + if (uri != null && uri.toString().length() > 0) { + MusicPlayerRemote.playFromUri(uri); + handled = true; + } else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) { + final int id = (int) parseIdFromIntent(intent, "playlistId", "playlist"); + if (id >= 0) { + int position = intent.getIntExtra("position", 0); + ArrayList songs = new ArrayList<>( + PlaylistSongsLoader.getPlaylistSongList(this, id).blockingFirst()); + MusicPlayerRemote.openQueue(songs, position, true); + handled = true; + } + } else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) { + final int id = (int) parseIdFromIntent(intent, "albumId", "album"); + if (id >= 0) { + int position = intent.getIntExtra("position", 0); + MusicPlayerRemote + .openQueue(AlbumLoader.getAlbum(this, id).blockingFirst().songs, position, true); + handled = true; + } + } else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) { + final int id = (int) parseIdFromIntent(intent, "artistId", "artist"); + if (id >= 0) { + int position = intent.getIntExtra("position", 0); + MusicPlayerRemote + .openQueue(ArtistLoader.getArtist(this, id).blockingFirst().getSongs(), position, true); + handled = true; + } + } + if (handled) { + setIntent(new Intent()); + } + } + + private long parseIdFromIntent(@NonNull Intent intent, String longKey, String stringKey) { + long id = intent.getLongExtra(longKey, -1); + if (id < 0) { + String idString = intent.getStringExtra(stringKey); + if (idString != null) { + try { + id = Long.parseLong(idString); + } catch (NumberFormatException e) { + Log.e(TAG, e.getMessage()); + } + } + } + return id; + } + + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + switch (requestCode) { + case APP_INTRO_REQUEST: + blockRequestPermissions = false; + if (!hasPermissions()) { + requestPermissions(); } + break; + case REQUEST_CODE_THEME: + case APP_USER_INFO_REQUEST: + postRecreate(); + break; } - @Override - public void onDestroy() { - super.onDestroy(); - disposable.clear(); - if (broadcastReceiver == null) { - return; - } - unregisterReceiver(broadcastReceiver); - PreferenceUtil.getInstance(this).unregisterOnSharedPreferenceChangedListener(this); + } + + @Override + public boolean handleBackPress() { + return super.handleBackPress() || (currentFragment != null && + currentFragment.handleBackPress()); + } + + @Override + public void onServiceConnected() { + super.onServiceConnected(); + handlePlaybackIntent(getIntent()); + } + + @Override + protected void requestPermissions() { + if (!blockRequestPermissions) { + super.requestPermissions(); } + } - public void setCurrentFragment(@Nullable Fragment fragment, boolean isStackAdd) { - FragmentManager fragmentManager = getSupportFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - fragmentTransaction.replace(R.id.fragment_container, fragment, TAG); - if (isStackAdd) { - fragmentTransaction.addToBackStack(TAG); - } - fragmentTransaction.commit(); - currentFragment = (MainActivityFragmentCallbacks) fragment; + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + return super.onOptionsItemSelected(item); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(PreferenceUtil.GENERAL_THEME) || + key.equals(PreferenceUtil.ADAPTIVE_COLOR_APP) || + key.equals(PreferenceUtil.DOMINANT_COLOR) || + key.equals(PreferenceUtil.USER_NAME) || + key.equals(PreferenceUtil.TOGGLE_FULL_SCREEN) || + key.equals(PreferenceUtil.TOGGLE_VOLUME) || + key.equals(PreferenceUtil.TOGGLE_TAB_TITLES) || + key.equals(PreferenceUtil.ROUND_CORNERS) || + key.equals(PreferenceUtil.CAROUSEL_EFFECT) || + key.equals(PreferenceUtil.NOW_PLAYING_SCREEN_ID) || + key.equals(PreferenceUtil.TOGGLE_GENRE) || + key.equals(PreferenceUtil.BANNER_IMAGE_PATH) || + key.equals(PreferenceUtil.PROFILE_IMAGE_PATH) || + key.equals(PreferenceUtil.CIRCULAR_ALBUM_ART) || + key.equals(PreferenceUtil.KEEP_SCREEN_ON) || + key.equals(PreferenceUtil.TOGGLE_SEPARATE_LINE) || + key.equals(PreferenceUtil.ALBUM_GRID_STYLE) || + key.equals(PreferenceUtil.ARTIST_GRID_STYLE)) { + postRecreate(); } + } - private void restoreCurrentFragment() { - currentFragment = (MainActivityFragmentCallbacks) getSupportFragmentManager() - .findFragmentById(R.id.fragment_container); - } - - @Override - public boolean onNavigationItemSelected(@NonNull MenuItem item) { - PreferenceUtil.getInstance(this).setLastPage(item.getItemId()); - disposable.add(Observable.just(item.getItemId()) - .throttleFirst(3, TimeUnit.SECONDS) - .subscribe(this::setCurrentFragment)); - return true; - } - - private void setCurrentFragment(int menuItem) { - switch (menuItem) { - case R.id.action_song: - case R.id.action_album: - case R.id.action_artist: - case R.id.action_playlist: - setCurrentFragment(LibraryFragment.newInstance(menuItem), false); - break; - default: - case R.id.action_home: - setCurrentFragment(HomeFragment.newInstance(), false); - break; - } - } - - private void handlePlaybackIntent(@Nullable Intent intent) { - if (intent == null) { - return; - } - - Uri uri = intent.getData(); - String mimeType = intent.getType(); - boolean handled = false; - - if (intent.getAction() != null && intent.getAction() - .equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) { - final ArrayList songs = SearchQueryHelper.getSongs(this, intent.getExtras()); - - if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) { - MusicPlayerRemote.openAndShuffleQueue(songs, true); - } else { - MusicPlayerRemote.openQueue(songs, 0, true); - } - handled = true; - } - - if (uri != null && uri.toString().length() > 0) { - MusicPlayerRemote.playFromUri(uri); - handled = true; - } else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) { - final int id = (int) parseIdFromIntent(intent, "playlistId", "playlist"); - if (id >= 0) { - int position = intent.getIntExtra("position", 0); - ArrayList songs = new ArrayList<>( - PlaylistSongsLoader.getPlaylistSongList(this, id).blockingFirst()); - MusicPlayerRemote.openQueue(songs, position, true); - handled = true; - } - } else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) { - final int id = (int) parseIdFromIntent(intent, "albumId", "album"); - if (id >= 0) { - int position = intent.getIntExtra("position", 0); - MusicPlayerRemote - .openQueue(AlbumLoader.getAlbum(this, id).blockingFirst().songs, position, true); - handled = true; - } - } else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) { - final int id = (int) parseIdFromIntent(intent, "artistId", "artist"); - if (id >= 0) { - int position = intent.getIntExtra("position", 0); - MusicPlayerRemote.openQueue(ArtistLoader.getArtist(this, id).blockingFirst().getSongs(), position, true); - handled = true; - } - } - if (handled) { - setIntent(new Intent()); - } - } - - private long parseIdFromIntent(@NonNull Intent intent, String longKey, String stringKey) { - long id = intent.getLongExtra(longKey, -1); - if (id < 0) { - String idString = intent.getStringExtra(stringKey); - if (idString != null) { - try { - id = Long.parseLong(idString); - } catch (NumberFormatException e) { - Log.e(TAG, e.getMessage()); - } - } - } - return id; - } - - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { - case APP_INTRO_REQUEST: - blockRequestPermissions = false; - if (!hasPermissions()) { - requestPermissions(); - } - - break; - case REQUEST_CODE_THEME: - case APP_USER_INFO_REQUEST: - postRecreate(); - break; - } - - } - - @Override - public boolean handleBackPress() { - return super.handleBackPress() || (currentFragment != null && - currentFragment.handleBackPress()); - } - - @Override - public void onServiceConnected() { - super.onServiceConnected(); - handlePlaybackIntent(getIntent()); - } - - @Override - protected void requestPermissions() { - if (!blockRequestPermissions) { - super.requestPermissions(); - } - } - - @Override - public boolean onOptionsItemSelected(@NonNull MenuItem item) { - return super.onOptionsItemSelected(item); - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equalsIgnoreCase(PreferenceUtil.GENERAL_THEME) || - key.equalsIgnoreCase(PreferenceUtil.ADAPTIVE_COLOR_APP) || - key.equalsIgnoreCase(PreferenceUtil.DOMINANT_COLOR) || - key.equalsIgnoreCase(PreferenceUtil.USER_NAME) || - key.equalsIgnoreCase(PreferenceUtil.TOGGLE_FULL_SCREEN) || - key.equalsIgnoreCase(PreferenceUtil.TOGGLE_VOLUME) || - key.equalsIgnoreCase(PreferenceUtil.TOGGLE_TAB_TITLES) || - key.equalsIgnoreCase(PreferenceUtil.ROUND_CORNERS) || - key.equals(PreferenceUtil.CAROUSEL_EFFECT) || - key.equals(PreferenceUtil.NOW_PLAYING_SCREEN_ID) || - key.equals(PreferenceUtil.TOGGLE_GENRE) || - key.equals(PreferenceUtil.BANNER_IMAGE_PATH) || - key.equals(PreferenceUtil.PROFILE_IMAGE_PATH) || - key.equals(PreferenceUtil.CIRCULAR_ALBUM_ART) || - key.equals(PreferenceUtil.KEEP_SCREEN_ON) || - key.equals(PreferenceUtil.TOGGLE_SEPARATE_LINE)) { - postRecreate(); - } - } - - private void showPromotionalOffer() { - new MaterialDialog.Builder(this) - .positiveText("Buy") - .onPositive((dialog, which) -> - startActivity(new Intent(MainActivity.this, ProVersionActivity.class))) - .negativeText(android.R.string.cancel) - .customView(R.layout.dialog_promotional_offer, false) - .dismissListener(dialog -> { - PreferenceManager.getDefaultSharedPreferences(MainActivity.this) - .edit() - .putBoolean("shown", true) - .apply(); - }) - .show(); - } + private void showPromotionalOffer() { + new MaterialDialog.Builder(this) + .positiveText("Buy") + .onPositive((dialog, which) -> + startActivity(new Intent(MainActivity.this, ProVersionActivity.class))) + .negativeText(android.R.string.cancel) + .customView(R.layout.dialog_promotional_offer, false) + .dismissListener(dialog -> { + PreferenceManager.getDefaultSharedPreferences(MainActivity.this) + .edit() + .putBoolean("shown", true) + .apply(); + }) + .show(); + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/ui/fragments/mainactivity/home/HomeFragment.java b/app/src/main/java/code/name/monkey/retromusic/ui/fragments/mainactivity/home/HomeFragment.java index f9085a3e..803d45ed 100644 --- a/app/src/main/java/code/name/monkey/retromusic/ui/fragments/mainactivity/home/HomeFragment.java +++ b/app/src/main/java/code/name/monkey/retromusic/ui/fragments/mainactivity/home/HomeFragment.java @@ -1,5 +1,8 @@ package code.name.monkey.retromusic.ui.fragments.mainactivity.home; +import static code.name.monkey.retromusic.Constants.USER_BANNER; +import static code.name.monkey.retromusic.Constants.USER_PROFILE; + import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; @@ -20,15 +23,6 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.load.engine.DiskCacheStrategy; - -import java.io.File; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Random; - import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; @@ -61,353 +55,357 @@ import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.RetroUtil; import code.name.monkey.retromusic.views.CircularImageView; import code.name.monkey.retromusic.views.MetalRecyclerViewPager; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; - -import static code.name.monkey.retromusic.Constants.USER_BANNER; -import static code.name.monkey.retromusic.Constants.USER_PROFILE; +import java.io.File; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Random; public class HomeFragment extends AbsMainActivityFragment implements MainActivityFragmentCallbacks, - HomeContract.HomeView { - private static final String TAG = "HomeFragment"; - Unbinder unbinder; - @BindView(R.id.home_toolbar) - Toolbar toolbar; - @BindView(R.id.appbar) - AppBarLayout appbar; - @BindView(R.id.image) - ImageView imageView; - @BindView(R.id.user_image) - CircularImageView userImage; - @BindView(R.id.collapsing_toolbar) - CollapsingToolbarLayout toolbarLayout; - @BindView(R.id.recycler_view) - RecyclerView recentArtistRV; - @BindView(R.id.recent_album) - RecyclerView recentAlbumRV; - @BindView(R.id.top_artist) - RecyclerView topArtistRV; - @BindView(R.id.top_album) - MetalRecyclerViewPager topAlbumRV; - @BindView(R.id.recent_artist_container) - View recentArtistContainer; - @BindView(R.id.recent_albums_container) - View recentAlbumsContainer; - @BindView(R.id.top_artist_container) - View topArtistContainer; - @BindView(R.id.top_albums_container) - View topAlbumContainer; - @BindView(R.id.genres) - RecyclerView genresRecyclerView; - @BindView(R.id.genre_container) - LinearLayout genreContainer; - @BindView(R.id.container) - View container; - @BindView(R.id.title) - TextView title; - @BindView(R.id.search) - ImageView search; + HomeContract.HomeView { + + private static final String TAG = "HomeFragment"; + Unbinder unbinder; + @BindView(R.id.home_toolbar) + Toolbar toolbar; + @BindView(R.id.appbar) + AppBarLayout appbar; + @BindView(R.id.image) + ImageView imageView; + @BindView(R.id.user_image) + CircularImageView userImage; + @BindView(R.id.collapsing_toolbar) + CollapsingToolbarLayout toolbarLayout; + @BindView(R.id.recycler_view) + RecyclerView recentArtistRV; + @BindView(R.id.recent_album) + RecyclerView recentAlbumRV; + @BindView(R.id.top_artist) + RecyclerView topArtistRV; + @BindView(R.id.top_album) + MetalRecyclerViewPager topAlbumRV; + @BindView(R.id.recent_artist_container) + View recentArtistContainer; + @BindView(R.id.recent_albums_container) + View recentAlbumsContainer; + @BindView(R.id.top_artist_container) + View topArtistContainer; + @BindView(R.id.top_albums_container) + View topAlbumContainer; + @BindView(R.id.genres) + RecyclerView genresRecyclerView; + @BindView(R.id.genre_container) + LinearLayout genreContainer; + @BindView(R.id.container) + View container; + @BindView(R.id.title) + TextView title; + @BindView(R.id.search) + ImageView search; - private HomePresenter homePresenter; - private CompositeDisposable disposable; + private HomePresenter homePresenter; + private CompositeDisposable disposable; - public static HomeFragment newInstance() { - Bundle args = new Bundle(); - HomeFragment fragment = new HomeFragment(); - fragment.setArguments(args); - return fragment; + public static HomeFragment newInstance() { + Bundle args = new Bundle(); + HomeFragment fragment = new HomeFragment(); + fragment.setArguments(args); + return fragment; + } + + private void getTimeOfTheDay() { + Calendar c = Calendar.getInstance(); + int timeOfDay = c.get(Calendar.HOUR_OF_DAY); + + String[] images = new String[]{}; + if (timeOfDay >= 0 && timeOfDay < 6) { + images = getResources().getStringArray(R.array.night); + } else if (timeOfDay >= 6 && timeOfDay < 12) { + images = getResources().getStringArray(R.array.morning); + } else if (timeOfDay >= 12 && timeOfDay < 16) { + images = getResources().getStringArray(R.array.after_noon); + } else if (timeOfDay >= 16 && timeOfDay < 20) { + images = getResources().getStringArray(R.array.evening); + } else if (timeOfDay >= 20 && timeOfDay < 24) { + images = getResources().getStringArray(R.array.night); + } + String day = images[new Random().nextInt(images.length)]; + loadTimeImage(day); + } + + private void loadTimeImage(String day) { + //noinspection ConstantConditions + if (PreferenceUtil.getInstance(getActivity()).getBannerImage().isEmpty()) { + if (imageView != null) { + Glide.with(getActivity()).load(day) + .asBitmap() + .placeholder(R.drawable.material_design_default) + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .into(imageView); + } + } else { + loadBannerFromStorage(); + } + } + + private void loadBannerFromStorage() { + //noinspection ConstantConditions + disposable.add(new Compressor(getContext()) + .setQuality(100) + .setCompressFormat(Bitmap.CompressFormat.WEBP) + .compressToBitmapAsFlowable( + new File(PreferenceUtil.getInstance(getContext()).getBannerImage(), USER_BANNER)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(imageView::setImageBitmap)); + } + + private void loadImageFromStorage(ImageView imageView) { + //noinspection ConstantConditions + disposable.add(new Compressor(getContext()) + .setMaxHeight(300) + .setMaxWidth(300) + .setQuality(75) + .setCompressFormat(Bitmap.CompressFormat.WEBP) + .compressToBitmapAsFlowable( + new File(PreferenceUtil.getInstance(getContext()).getProfileImage(), USER_PROFILE)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(imageView::setImageBitmap, + throwable -> imageView.setImageDrawable(ContextCompat + .getDrawable(getContext(), R.drawable.ic_person_flat)))); + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + disposable = new CompositeDisposable(); + //noinspection ConstantConditions + homePresenter = new HomePresenter(this); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_home, container, false); + unbinder = ButterKnife.bind(this, view); + return view; + } + + @Override + public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + getMainActivity().getSlidingUpPanelLayout().setShadowHeight(8); + getMainActivity().setBottomBarVisibility(View.VISIBLE); + + setupToolbar(); + loadImageFromStorage(userImage); + + homePresenter.subscribe(); + checkPadding(); + getTimeOfTheDay(); + } + + @SuppressWarnings("ConstantConditions") + private void setupToolbar() { + if (!PreferenceUtil.getInstance(getContext()).getFullScreenMode()) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar + .getLayoutParams(); + params.topMargin = RetroUtil.getStatusBarHeight(getContext()); + toolbar.setLayoutParams(params); } - private void getTimeOfTheDay() { - Calendar c = Calendar.getInstance(); - int timeOfDay = c.get(Calendar.HOUR_OF_DAY); - - String[] images = new String[]{}; - if (timeOfDay >= 0 && timeOfDay < 6) { - images = getResources().getStringArray(R.array.night); - } else if (timeOfDay >= 6 && timeOfDay < 12) { - images = getResources().getStringArray(R.array.morning); - } else if (timeOfDay >= 12 && timeOfDay < 16) { - images = getResources().getStringArray(R.array.after_noon); - } else if (timeOfDay >= 16 && timeOfDay < 20) { - images = getResources().getStringArray(R.array.evening); - } else if (timeOfDay >= 20 && timeOfDay < 24) { - images = getResources().getStringArray(R.array.night); + appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + public void onStateChanged(AppBarLayout appBarLayout, State state) { + int color; + switch (state) { + case COLLAPSED: + getMainActivity().setLightStatusbar(!ATHUtil.isWindowBackgroundDark(getContext())); + color = ThemeStore.textColorPrimary(getContext()); + break; + default: + case EXPANDED: + case IDLE: + getMainActivity().setLightStatusbar(false); + color = ContextCompat.getColor(getContext(), R.color.md_white_1000); + break; } - String day = images[new Random().nextInt(images.length)]; - loadTimeImage(day); + TintHelper.setTintAuto(search, color, false); + title.setTextColor(color); + } + }); + + int primaryColor = ThemeStore.primaryColor(getContext()); + + TintHelper.setTintAuto(container, primaryColor, true); + toolbarLayout.setStatusBarScrimColor(primaryColor); + toolbarLayout.setContentScrimColor(primaryColor); + + toolbar.setTitle(R.string.home); + getMainActivity().setSupportActionBar(toolbar); + + } + + @Override + public boolean handleBackPress() { + return false; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + unbinder.unbind(); + disposable.clear(); + homePresenter.unsubscribe(); + } + + @Override + public void loading() { + + } + + @Override + public void showEmptyView() { + + } + + @Override + public void completed() { + + } + + @Override + public void showData(ArrayList homes) { + //homeAdapter.swapDataSet(homes); + } + + @Override + public void onMediaStoreChanged() { + super.onMediaStoreChanged(); + homePresenter.subscribe(); + } + + @Override + public void onServiceConnected() { + super.onServiceConnected(); + checkPadding(); + } + + @Override + public void onQueueChanged() { + super.onQueueChanged(); + checkPadding(); + } + + private void checkPadding() { + int height = getResources().getDimensionPixelSize(R.dimen.mini_player_height); + container.setPadding(0, 0, 0, MusicPlayerRemote.getPlayingQueue().isEmpty() ? height * 2 : 0); + } + + @Override + public void recentArtist(ArrayList artists) { + recentArtistContainer.setVisibility(View.VISIBLE); + recentArtistRV.setLayoutManager(new GridLayoutManager(getMainActivity(), + 1, GridLayoutManager.HORIZONTAL, false)); + ArtistAdapter artistAdapter = new ArtistAdapter(getMainActivity(), artists, + R.layout.item_artist, false, null); + recentArtistRV.setAdapter(artistAdapter); + } + + @Override + public void recentAlbum(ArrayList albums) { + recentAlbumsContainer.setVisibility(View.VISIBLE); + AlbumFullWithAdapter artistAdapter = new AlbumFullWithAdapter(getMainActivity(), + getDisplayMetrics()); + artistAdapter.swapData(albums); + recentAlbumRV.setAdapter(artistAdapter); + } + + @Override + public void topArtists(ArrayList artists) { + topArtistContainer.setVisibility(View.VISIBLE); + topArtistRV.setLayoutManager(new GridLayoutManager(getMainActivity(), + 1, GridLayoutManager.HORIZONTAL, false)); + ArtistAdapter artistAdapter = new ArtistAdapter(getMainActivity(), artists, + R.layout.item_artist, false, null); + topArtistRV.setAdapter(artistAdapter); + + } + + @Override + public void topAlbums(ArrayList albums) { + topAlbumContainer.setVisibility(View.VISIBLE); + AlbumFullWithAdapter artistAdapter = new AlbumFullWithAdapter(getMainActivity(), + getDisplayMetrics()); + artistAdapter.swapData(albums); + topAlbumRV.setAdapter(artistAdapter); + } + + private DisplayMetrics getDisplayMetrics() { + Display display = getMainActivity().getWindowManager().getDefaultDisplay(); + DisplayMetrics metrics = new DisplayMetrics(); + display.getMetrics(metrics); + + return metrics; + } + + @Override + public void suggestions(ArrayList playlists) { + + } + + + @Override + public void geners(ArrayList genres) { + genreContainer.setVisibility(View.VISIBLE); + genresRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + //noinspection ConstantConditions + GenreAdapter genreAdapter = new GenreAdapter(getActivity(), genres, R.layout.item_list); + genresRecyclerView.setAdapter(genreAdapter); + } + + + @OnClick({R.id.last_added, R.id.top_played, R.id.action_shuffle, R.id.history, + R.id.user_image, R.id.search}) + void startUserInfo(View view) { + Activity activity = getActivity(); + if (activity != null) { + switch (view.getId()) { + case R.id.action_shuffle: + MusicPlayerRemote + .openAndShuffleQueue(SongLoader.getAllSongs(activity).blockingFirst(), true); + break; + case R.id.last_added: + NavigationUtil.goToPlaylistNew(activity, new LastAddedPlaylist(activity)); + break; + case R.id.top_played: + NavigationUtil.goToPlaylistNew(activity, new MyTopTracksPlaylist(activity)); + break; + case R.id.history: + NavigationUtil.goToPlaylistNew(activity, new HistoryPlaylist(activity)); + break; + case R.id.search: + NavigationUtil.goToSearch(activity); + break; + case R.id.user_image: + new HomeOptionDialog().show(getFragmentManager(), TAG); + break; + } } + } - private void loadTimeImage(String day) { - //noinspection ConstantConditions - if (PreferenceUtil.getInstance(getActivity()).getBannerImage().isEmpty()) { - if (imageView != null) { - Glide.with(getActivity()).load(day) - .asBitmap() - .placeholder(R.drawable.material_design_default) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - .into(imageView); - } - } else { - loadBannerFromStorage(); - } - } - - private void loadBannerFromStorage() { - //noinspection ConstantConditions - disposable.add(new Compressor(getContext()) - .setQuality(100) - .setCompressFormat(Bitmap.CompressFormat.WEBP) - .compressToBitmapAsFlowable( - new File(PreferenceUtil.getInstance(getContext()).getBannerImage(), USER_BANNER)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(imageView::setImageBitmap)); - } - - private void loadImageFromStorage(ImageView imageView) { - //noinspection ConstantConditions - disposable.add(new Compressor(getContext()) - .setMaxHeight(300) - .setMaxWidth(300) - .setQuality(75) - .setCompressFormat(Bitmap.CompressFormat.WEBP) - .compressToBitmapAsFlowable( - new File(PreferenceUtil.getInstance(getContext()).getProfileImage(), USER_PROFILE)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(imageView::setImageBitmap, - throwable -> imageView.setImageDrawable(ContextCompat - .getDrawable(getContext(), R.drawable.ic_person_flat)))); - } - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - disposable = new CompositeDisposable(); - //noinspection ConstantConditions - homePresenter = new HomePresenter(this); - } - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_home, container, false); - unbinder = ButterKnife.bind(this, view); - return view; - } - - @Override - public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - getMainActivity().getSlidingUpPanelLayout().setShadowHeight(8); - getMainActivity().setBottomBarVisibility(View.VISIBLE); - - setupToolbar(); - loadImageFromStorage(userImage); - - homePresenter.subscribe(); - checkPadding(); - getTimeOfTheDay(); - } - - @SuppressWarnings("ConstantConditions") - private void setupToolbar() { - if (!PreferenceUtil.getInstance(getContext()).getFullScreenMode()) { - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar - .getLayoutParams(); - params.topMargin = RetroUtil.getStatusBarHeight(getContext()); - toolbar.setLayoutParams(params); - } - - appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() { - @Override - public void onStateChanged(AppBarLayout appBarLayout, State state) { - int color; - switch (state) { - case COLLAPSED: - getMainActivity().setLightStatusbar(!ATHUtil.isWindowBackgroundDark(getContext())); - color = ThemeStore.textColorPrimary(getContext()); - break; - default: - case EXPANDED: - case IDLE: - getMainActivity().setLightStatusbar(false); - color = ContextCompat.getColor(getContext(), R.color.md_white_1000); - break; - } - TintHelper.setTintAuto(search, color, false); - title.setTextColor(color); - } - }); - - int primaryColor = ThemeStore.primaryColor(getContext()); - - TintHelper.setTintAuto(container, primaryColor, true); - toolbarLayout.setStatusBarScrimColor(primaryColor); - toolbarLayout.setContentScrimColor(primaryColor); - - toolbar.setTitle(R.string.home); - getMainActivity().setSupportActionBar(toolbar); - - } - - @Override - public boolean handleBackPress() { - return false; - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - unbinder.unbind(); - disposable.clear(); - homePresenter.unsubscribe(); - } - - @Override - public void loading() { - - } - - @Override - public void showEmptyView() { - - } - - @Override - public void completed() { - - } - - @Override - public void showData(ArrayList homes) { - //homeAdapter.swapDataSet(homes); - } - - @Override - public void onMediaStoreChanged() { - super.onMediaStoreChanged(); - homePresenter.subscribe(); - } - - @Override - public void onServiceConnected() { - super.onServiceConnected(); - checkPadding(); - } - - @Override - public void onQueueChanged() { - super.onQueueChanged(); - checkPadding(); - } - - private void checkPadding() { - int height = getResources().getDimensionPixelSize(R.dimen.mini_player_height); - container.setPadding(0, 0, 0, MusicPlayerRemote.getPlayingQueue().isEmpty() ? height * 2 : 0); - } - - @Override - public void recentArtist(ArrayList artists) { - recentArtistContainer.setVisibility(View.VISIBLE); - recentArtistRV.setLayoutManager(new GridLayoutManager(getMainActivity(), - 1, GridLayoutManager.HORIZONTAL, false)); - ArtistAdapter artistAdapter = new ArtistAdapter(getMainActivity(), artists, - R.layout.item_artist, false, null); - recentArtistRV.setAdapter(artistAdapter); - } - - @Override - public void recentAlbum(ArrayList albums) { - recentAlbumsContainer.setVisibility(View.VISIBLE); - AlbumFullWithAdapter artistAdapter = new AlbumFullWithAdapter(getMainActivity(), - getDisplayMetrics()); - artistAdapter.swapData(albums); - recentAlbumRV.setAdapter(artistAdapter); - } - - @Override - public void topArtists(ArrayList artists) { - topArtistContainer.setVisibility(View.VISIBLE); - topArtistRV.setLayoutManager(new GridLayoutManager(getMainActivity(), - 1, GridLayoutManager.HORIZONTAL, false)); - ArtistAdapter artistAdapter = new ArtistAdapter(getMainActivity(), artists, - R.layout.item_artist, false, null); - topArtistRV.setAdapter(artistAdapter); - - } - - @Override - public void topAlbums(ArrayList albums) { - topAlbumContainer.setVisibility(View.VISIBLE); - AlbumFullWithAdapter artistAdapter = new AlbumFullWithAdapter(getMainActivity(), - getDisplayMetrics()); - artistAdapter.swapData(albums); - topAlbumRV.setAdapter(artistAdapter); - } - - private DisplayMetrics getDisplayMetrics() { - Display display = getMainActivity().getWindowManager().getDefaultDisplay(); - DisplayMetrics metrics = new DisplayMetrics(); - display.getMetrics(metrics); - - return metrics; - } - - @Override - public void suggestions(ArrayList playlists) { - - } - - - @Override - public void geners(ArrayList genres) { - genreContainer.setVisibility(View.VISIBLE); - genresRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - //noinspection ConstantConditions - GenreAdapter genreAdapter = new GenreAdapter(getActivity(), genres, R.layout.item_list); - genresRecyclerView.setAdapter(genreAdapter); - } - - - @OnClick({R.id.last_added, R.id.top_played, R.id.action_shuffle, R.id.history, - R.id.user_image, R.id.search}) - void startUserInfo(View view) { - Activity activity = getActivity(); - if (activity != null) { - switch (view.getId()) { - case R.id.action_shuffle: - MusicPlayerRemote - .openAndShuffleQueue(SongLoader.getAllSongs(activity).blockingFirst(), true); - break; - case R.id.last_added: - NavigationUtil.goToPlaylistNew(activity, new LastAddedPlaylist(activity)); - break; - case R.id.top_played: - NavigationUtil.goToPlaylistNew(activity, new MyTopTracksPlaylist(activity)); - break; - case R.id.history: - NavigationUtil.goToPlaylistNew(activity, new HistoryPlaylist(activity)); - break; - case R.id.search: - NavigationUtil.goToSearch(activity); - break; - case R.id.user_image: - new HomeOptionDialog().show(getFragmentManager(), TAG); - break; - } - } - } - - @Override - public void onPlayingMetaChanged() { - super.onPlayingMetaChanged(); - homePresenter.loadRecentArtists(); - homePresenter.loadRecentAlbums(); - } + @Override + public void onPlayingMetaChanged() { + super.onPlayingMetaChanged(); + homePresenter.loadRecentArtists(); + homePresenter.loadRecentAlbums(); + } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java index 3192aa36..85561de5 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.java @@ -9,697 +9,694 @@ import android.preference.PreferenceManager; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.annotation.StyleRes; - -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; -import com.google.gson.reflect.TypeToken; - -import java.io.File; -import java.lang.reflect.Type; -import java.util.ArrayList; - import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.RetroApplication; import code.name.monkey.retromusic.helper.SortOrder; import code.name.monkey.retromusic.model.CategoryInfo; import code.name.monkey.retromusic.ui.fragments.NowPlayingScreen; import code.name.monkey.retromusic.ui.fragments.mainactivity.folders.FoldersFragment; +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; +import java.io.File; +import java.lang.reflect.Type; +import java.util.ArrayList; public final class PreferenceUtil { - public static final String KEEP_SCREEN_ON = "keep_screen_on"; - public static final String NOW_PLAYING_SCREEN_ID = "now_playing_screen_id"; - public static final String CAROUSEL_EFFECT = "carousel_effect"; - public static final String COLORED_NOTIFICATION = "colored_notification"; - public static final String CLASSIC_NOTIFICATION = "classic_notification"; - public static final String GAPLESS_PLAYBACK = "gapless_playback"; - public static final String ALBUM_ART_ON_LOCKSCREEN = "album_art_on_lockscreen"; - public static final String BLURRED_ALBUM_ART = "blurred_album_art"; - public static final String TOGGLE_HEADSET = "toggle_headset"; - public static final String DOMINANT_COLOR = "dominant_color"; - public static final String GENERAL_THEME = "general_theme"; - public static final String CIRCULAR_ALBUM_ART = "circular_album_art"; - public static final String USER_NAME = "user_name"; - public static final String TOGGLE_FULL_SCREEN = "toggle_full_screen"; - public static final String TOGGLE_VOLUME = "toggle_volume"; - public static final String TOGGLE_TAB_TITLES = "toggle_tab_titles"; - public static final String ROUND_CORNERS = "corner_window"; - public static final String TOGGLE_GENRE = "toggle_genre"; - public static final String PROFILE_IMAGE_PATH = "profile_image_path"; - public static final String BANNER_IMAGE_PATH = "banner_image_path"; - public static final String ADAPTIVE_COLOR_APP = "adaptive_color_app"; - public static final String TOGGLE_SEPARATE_LINE = "toggle_separate_line"; - private static final String GENRE_SORT_ORDER = "genre_sort_order"; - private static final String ALBUM_GRID_STYLE = "album_grid_style"; - private static final String ARTIST_GRID_STYLE = "artist_grid_style"; - private static final String LIBRARY_CATEGORIES = "library_categories"; - private static final String LAST_PAGE = "last_start_page"; - private static final String LAST_MUSIC_CHOOSER = "last_music_chooser"; - private static final String DEFAULT_START_PAGE = "default_start_page"; - private static final String INITIALIZED_BLACKLIST = "initialized_blacklist"; - private static final String ARTIST_SORT_ORDER = "artist_sort_order"; - private static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order"; - private static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order"; - private static final String ALBUM_SORT_ORDER = "album_sort_order"; - private static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order"; - private static final String SONG_SORT_ORDER = "song_sort_order"; - private static final String ALBUM_GRID_SIZE = "album_grid_size"; - private static final String ALBUM_GRID_SIZE_LAND = "album_grid_size_land"; - private static final String SONG_GRID_SIZE = "song_grid_size"; - private static final String SONG_GRID_SIZE_LAND = "song_grid_size_land"; - private static final String ARTIST_GRID_SIZE = "artist_grid_size"; - private static final String ARTIST_GRID_SIZE_LAND = "artist_grid_size_land"; - private static final String ALBUM_COLORED_FOOTERS = "album_colored_footers"; - private static final String SONG_COLORED_FOOTERS = "song_colored_footers"; - private static final String ARTIST_COLORED_FOOTERS = "artist_colored_footers"; - private static final String ALBUM_ARTIST_COLORED_FOOTERS = "album_artist_colored_footers"; - private static final String COLORED_APP_SHORTCUTS = "colored_app_shortcuts"; - private static final String AUDIO_DUCKING = "audio_ducking"; - private static final String LAST_ADDED_CUTOFF = "last_added_interval"; - private static final String LAST_SLEEP_TIMER_VALUE = "last_sleep_timer_value"; - private static final String NEXT_SLEEP_TIMER_ELAPSED_REALTIME = "next_sleep_timer_elapsed_real_time"; - private static final String IGNORE_MEDIA_STORE_ARTWORK = "ignore_media_store_artwork"; - private static final String LAST_CHANGELOG_VERSION = "last_changelog_version"; - private static final String INTRO_SHOWN = "intro_shown"; - private static final String AUTO_DOWNLOAD_IMAGES_POLICY = "auto_download_images_policy"; - private static final String START_DIRECTORY = "start_directory"; - private static final String SYNCHRONIZED_LYRICS_SHOW = "synchronized_lyrics_show"; - private static final String LOCK_SCREEN = "lock_screen"; - private static final String ALBUM_DETAIL_SONG_SORT_ORDER = "album_detail_song_sort_order"; - private static final String ARTIST_DETAIL_SONG_SORT_ORDER = "artist_detail_song_sort_order"; - private static final String LYRICS_OPTIONS = "lyrics_options"; - private static final String SAF_SDCARD_URI = "saf_sdcard_uri"; - private static final String DOCUMENT_TREE_URI = "document_tree_uri"; - private static final String CHOOSE_EQUALIZER = "choose_equalizer"; - private static final String TOGGLE_SHUFFLE = "toggle_shuffle"; - private static final String SONG_GRID_STYLE = "song_grid_style"; - private static final String TOGGLE_ANIMATIONS = "toggle_animations"; - private static final String TAG = "PreferenceUtil"; - private static PreferenceUtil sInstance; - private final SharedPreferences mPreferences; - - private PreferenceUtil(@NonNull final Context context) { - mPreferences = PreferenceManager.getDefaultSharedPreferences(context); - } - - public static PreferenceUtil getInstance(@NonNull final Context context) { - if (sInstance == null) { - sInstance = new PreferenceUtil(context.getApplicationContext()); - } - return sInstance; - } - - @StyleRes - public static int getThemeResFromPrefValue(String themePrefValue) { - switch (themePrefValue) { - case "dark": - return R.style.Theme_RetroMusic; - case "color": - return R.style.Theme_RetroMusic_Color; - case "acolor": - return R.style.Theme_RetroMusic_Black; - case "black": - return R.style.Theme_RetroMusic_Black; - case "light": - default: - return R.style.Theme_RetroMusic_Light; - } - } - - public int getAlbumDetailsPageStyle() { - - TypedArray typedArray = RetroApplication.getInstance(). - getResources().obtainTypedArray(R.array.pref_album_detail_style_values); - int layout = typedArray.getResourceId(mPreferences.getInt("album_detail_style", 0), -1); - typedArray.recycle(); - return layout; - } - - public final String getArtistSortOrder() { - return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z); - } - - public void setArtistSortOrder(final String sortOrder) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(ARTIST_SORT_ORDER, sortOrder); - editor.apply(); - } - - public final String getArtistSongSortOrder() { - return mPreferences.getString(ARTIST_SONG_SORT_ORDER, SortOrder.ArtistSongSortOrder.SONG_A_Z); - } - - public final String getArtistAlbumSortOrder() { - return mPreferences - .getString(ARTIST_ALBUM_SORT_ORDER, SortOrder.ArtistAlbumSortOrder.ALBUM_YEAR); - } - - public final String getAlbumSortOrder() { - return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z); - } - - public void setAlbumSortOrder(final String sortOrder) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(ALBUM_SORT_ORDER, sortOrder); - editor.apply(); - } - - public final String getAlbumSongSortOrder() { - return mPreferences - .getString(ALBUM_SONG_SORT_ORDER, SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST); - } - - public final String getSongSortOrder() { - return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z); - } - - public void setSongSortOrder(final String sortOrder) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(SONG_SORT_ORDER, sortOrder); - editor.commit(); - } - - public final String getGenreSortOrder() { - return mPreferences.getString(GENRE_SORT_ORDER, SortOrder.GenreSortOrder.GENRE_A_Z); - } - - public boolean isScreenOnEnabled() { - return mPreferences.getBoolean(KEEP_SCREEN_ON, false); - } - - public void setInitializedBlacklist() { - final Editor editor = mPreferences.edit(); - editor.putBoolean(INITIALIZED_BLACKLIST, true); - editor.apply(); - } - - public final boolean initializedBlacklist() { - return mPreferences.getBoolean(INITIALIZED_BLACKLIST, false); - } - - - public boolean circularAlbumArt() { - return mPreferences.getBoolean(CIRCULAR_ALBUM_ART, false); - } - - public boolean carouselEffect() { - return mPreferences.getBoolean(CAROUSEL_EFFECT, false); - } - - public ArrayList getLibraryCategoryInfos() { - String data = mPreferences.getString(LIBRARY_CATEGORIES, null); - if (data != null) { - Gson gson = new Gson(); - Type collectionType = new TypeToken>() { - }.getType(); - - try { - return gson.fromJson(data, collectionType); - } catch (JsonSyntaxException e) { - e.printStackTrace(); - } - } - - return getDefaultLibraryCategoryInfos(); - } - - public void setLibraryCategoryInfos(ArrayList categories) { - Gson gson = new Gson(); - Type collectionType = new TypeToken>() { - }.getType(); - - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType)); - editor.apply(); - } - - public ArrayList getDefaultLibraryCategoryInfos() { - ArrayList defaultCategoryInfos = new ArrayList<>(5); - defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.SONGS, true)); - defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true)); - defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ARTISTS, true)); - defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.GENRES, true)); - defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.PLAYLISTS, true)); - return defaultCategoryInfos; - } - - public boolean isRoundCorners() { - return mPreferences.getBoolean(ROUND_CORNERS, false); - } - - public void registerOnSharedPreferenceChangedListener( - SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { - mPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); - } - - public void unregisterOnSharedPreferenceChangedListener( - SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { - mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); - } - - public final int getDefaultStartPage() { - return Integer.parseInt(mPreferences.getString(DEFAULT_START_PAGE, "-1")); - } - - - public final int getLastPage() { - return mPreferences.getInt(LAST_PAGE, 0); - } - - public void setLastPage(final int value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(LAST_PAGE, value); - editor.apply(); - } - - public final int getLastMusicChooser() { - return mPreferences.getInt(LAST_MUSIC_CHOOSER, 0); - } - - public void setLastMusicChooser(final int value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(LAST_MUSIC_CHOOSER, value); - editor.apply(); - } - - public final boolean coloredNotification() { - return mPreferences.getBoolean(COLORED_NOTIFICATION, true); - } - - public final boolean classicNotification() { - return mPreferences.getBoolean(CLASSIC_NOTIFICATION, false); - } - - public void setClassicNotification(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(CLASSIC_NOTIFICATION, value); - editor.apply(); - } - - public final NowPlayingScreen getNowPlayingScreen() { - int id = mPreferences.getInt(NOW_PLAYING_SCREEN_ID, 0); - for (NowPlayingScreen nowPlayingScreen : NowPlayingScreen.values()) { - if (nowPlayingScreen.id == id) { - return nowPlayingScreen; - } - } - return NowPlayingScreen.NORMAL; - } - - @SuppressLint("CommitPrefEdits") - public void setNowPlayingScreen(NowPlayingScreen nowPlayingScreen) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(NOW_PLAYING_SCREEN_ID, nowPlayingScreen.id); - editor.apply(); - } - - public void setColoredAppShortcuts(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(COLORED_APP_SHORTCUTS, value); - editor.apply(); - } - - public final boolean coloredAppShortcuts() { - return mPreferences.getBoolean(COLORED_APP_SHORTCUTS, true); - } - - public final boolean gaplessPlayback() { - return mPreferences.getBoolean(GAPLESS_PLAYBACK, false); - } - - public final boolean audioDucking() { - return mPreferences.getBoolean(AUDIO_DUCKING, true); - } - - public final boolean albumArtOnLockscreen() { - return mPreferences.getBoolean(ALBUM_ART_ON_LOCKSCREEN, true); - } - - public final boolean blurredAlbumArt() { - return mPreferences.getBoolean(BLURRED_ALBUM_ART, false); - } - - public final boolean ignoreMediaStoreArtwork() { - return mPreferences.getBoolean(IGNORE_MEDIA_STORE_ARTWORK, false); - } - - - public int getLastSleepTimerValue() { - return mPreferences.getInt(LAST_SLEEP_TIMER_VALUE, 30); - } - - public void setLastSleepTimerValue(final int value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(LAST_SLEEP_TIMER_VALUE, value); - editor.apply(); - } - - public long getNextSleepTimerElapsedRealTime() { - return mPreferences.getLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, -1); - } - - public void setNextSleepTimerElapsedRealtime(final long value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, value); - editor.apply(); - } - - public final int getAlbumGridSize(Context context) { - return mPreferences - .getInt(ALBUM_GRID_SIZE, context.getResources().getInteger(R.integer.default_grid_columns)); - } - - public void setSongGridSize(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(SONG_GRID_SIZE, gridSize); - editor.apply(); - } - - public final int getSongGridSize(Context context) { - return mPreferences - .getInt(SONG_GRID_SIZE, context.getResources().getInteger(R.integer.default_list_columns)); - } - - public void setArtistGridSize(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(ARTIST_GRID_SIZE, gridSize); - editor.apply(); - } - - public final int getArtistGridSize(Context context) { - return mPreferences.getInt(ARTIST_GRID_SIZE, - context.getResources().getInteger(R.integer.default_list_artist_columns)); - } - - public void setAlbumGridSizeLand(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(ALBUM_GRID_SIZE_LAND, gridSize); - editor.apply(); - } - - public final int getAlbumGridSizeLand(Context context) { - return mPreferences.getInt(ALBUM_GRID_SIZE_LAND, - context.getResources().getInteger(R.integer.default_grid_columns_land)); - } - - public void setSongGridSizeLand(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(SONG_GRID_SIZE_LAND, gridSize); - editor.apply(); - } - - public final int getSongGridSizeLand(Context context) { - return mPreferences.getInt(SONG_GRID_SIZE_LAND, - context.getResources().getInteger(R.integer.default_list_columns_land)); - } - - public void setArtistGridSizeLand(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(ARTIST_GRID_SIZE_LAND, gridSize); - editor.apply(); - } - - public final int getArtistGridSizeLand(Context context) { - return mPreferences.getInt(ARTIST_GRID_SIZE_LAND, - context.getResources().getInteger(R.integer.default_list_artist_columns_land)); - } - - public void setAlbumGridSize(final int gridSize) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putInt(ALBUM_GRID_SIZE, gridSize); - editor.apply(); - } - - public void setAlbumColoredFooters(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(ALBUM_COLORED_FOOTERS, value); - editor.apply(); - } - - public final boolean albumColoredFooters() { - return mPreferences.getBoolean(ALBUM_COLORED_FOOTERS, false); - } - - public void setAlbumArtistColoredFooters(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(ALBUM_ARTIST_COLORED_FOOTERS, value); - editor.apply(); - } - - public final boolean albumArtistColoredFooters() { - return mPreferences.getBoolean(ALBUM_ARTIST_COLORED_FOOTERS, true); - } - - public void setSongColoredFooters(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(SONG_COLORED_FOOTERS, value); - editor.apply(); - } - - public final boolean songColoredFooters() { - return mPreferences.getBoolean(SONG_COLORED_FOOTERS, false); - } - - public void setArtistColoredFooters(final boolean value) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putBoolean(ARTIST_COLORED_FOOTERS, value); - editor.apply(); - } - - public final boolean artistColoredFooters() { - return mPreferences.getBoolean(ARTIST_COLORED_FOOTERS, true); - } - - public void setLastChangeLogVersion(int version) { - mPreferences.edit().putInt(LAST_CHANGELOG_VERSION, version).apply(); - } - - public final int getLastChangelogVersion() { - return mPreferences.getInt(LAST_CHANGELOG_VERSION, -1); - } - - @SuppressLint("CommitPrefEdits") - public void setIntroShown() { - // don't use apply here - mPreferences.edit().putBoolean(INTRO_SHOWN, true).commit(); - } - - public final File getStartDirectory() { - return new File(mPreferences - .getString(START_DIRECTORY, FoldersFragment.getDefaultStartDirectory().getPath())); - } - - public void setStartDirectory(File file) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(START_DIRECTORY, FileUtil.safeGetCanonicalPath(file)); - editor.apply(); - } - - public final boolean introShown() { - return mPreferences.getBoolean(INTRO_SHOWN, false); - } - - public final String autoDownloadImagesPolicy() { - return mPreferences.getString(AUTO_DOWNLOAD_IMAGES_POLICY, "only_wifi"); - } - - public final boolean synchronizedLyricsShow() { - return mPreferences.getBoolean(SYNCHRONIZED_LYRICS_SHOW, true); - } - - public int getGeneralTheme() { - return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "light")); - } - - public void setGeneralTheme(String theme) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putString(GENERAL_THEME, theme); - editor.apply(); - } - - public long getLastAddedCutoff() { - final CalendarUtil calendarUtil = new CalendarUtil(); - long interval; - - switch (mPreferences.getString(LAST_ADDED_CUTOFF, "")) { - case "today": - interval = calendarUtil.getElapsedToday(); - break; - case "this_week": - interval = calendarUtil.getElapsedWeek(); - break; - case "past_three_months": - interval = calendarUtil.getElapsedMonths(3); - break; - case "this_year": - interval = calendarUtil.getElapsedYear(); - break; - case "this_month": - default: - interval = calendarUtil.getElapsedMonth(); - break; - } - - return (System.currentTimeMillis() - interval) / 1000; - } - - public boolean getAdaptiveColor() { - return mPreferences.getBoolean(ADAPTIVE_COLOR_APP, false); - } - - public boolean getLockScreen() { - return mPreferences.getBoolean(LOCK_SCREEN, false); - } - - public String getUserName() { - return mPreferences.getString(USER_NAME, "User"); - } - - public void setUserName(String name) { - mPreferences.edit().putString(USER_NAME, name).apply(); - } - - public boolean getFullScreenMode() { - return mPreferences.getBoolean(TOGGLE_FULL_SCREEN, false); - } - - public void setFullScreenMode(int newValue) { - mPreferences.edit().putInt(TOGGLE_FULL_SCREEN, newValue).apply(); - } - - public String lyricsOptions() { - return mPreferences.getString(LYRICS_OPTIONS, "offline"); - } - - public void saveProfileImage(String profileImagePath) { - mPreferences.edit().putString(PROFILE_IMAGE_PATH, profileImagePath) - .apply(); - - } - - public String getProfileImage() { - return mPreferences.getString(PROFILE_IMAGE_PATH, ""); - } - - public String getBannerImage() { - return mPreferences.getString(BANNER_IMAGE_PATH, ""); - } - - public void setBannerImagePath(String bannerImagePath) { - mPreferences.edit().putString(BANNER_IMAGE_PATH, bannerImagePath) - .apply(); - } - - public String getAlbumDetailSongSortOrder() { - return mPreferences - .getString(ALBUM_DETAIL_SONG_SORT_ORDER, SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST); - } - - public void setAlbumDetailSongSortOrder(String sortOrder) { - Editor edit = this.mPreferences.edit(); - edit.putString(ALBUM_DETAIL_SONG_SORT_ORDER, sortOrder); - edit.apply(); - } - - public String getArtistDetailSongSortOrder() { - return mPreferences - .getString(ARTIST_DETAIL_SONG_SORT_ORDER, SortOrder.ArtistSongSortOrder.SONG_A_Z); - } - - public void setArtistDetailSongSortOrder(String sortOrder) { - Editor edit = this.mPreferences.edit(); - edit.putString(ARTIST_DETAIL_SONG_SORT_ORDER, sortOrder); - edit.apply(); - } - - public boolean getVolumeToggle() { - return mPreferences.getBoolean(TOGGLE_VOLUME, false); - } - - public int getLyricsOptions() { - return mPreferences.getInt(LYRICS_OPTIONS, 1); - } - - public void setLyricsOptions(int i) { - mPreferences.edit().putInt(LYRICS_OPTIONS, i).apply(); - } - - public boolean getHeadsetPlugged() { - return mPreferences.getBoolean(TOGGLE_HEADSET, false); - } - - public boolean tabTitles() { - return mPreferences.getBoolean(TOGGLE_TAB_TITLES, true); - } - - public boolean isDominantColor() { - return mPreferences.getBoolean(DOMINANT_COLOR, false); - } - - public boolean isGenreShown() { - return mPreferences.getBoolean(TOGGLE_GENRE, false); - } - - public String getSelectedEqualizer() { - return mPreferences.getString(CHOOSE_EQUALIZER, "system"); - } - - public boolean isShuffleModeOn() { - return mPreferences.getBoolean(TOGGLE_SHUFFLE, false); - } - - public void resetCarouselEffect() { - mPreferences.edit().putBoolean(CAROUSEL_EFFECT, false).apply(); - } - - public void resetCircularAlbumArt() { - mPreferences.edit().putBoolean(CIRCULAR_ALBUM_ART, false).apply(); - } - - @LayoutRes - public int getAlbumGridStyle(Context context) { - int pos = Integer.parseInt(mPreferences.getString(ALBUM_GRID_STYLE, "0")); - TypedArray typedArray = context.getResources().obtainTypedArray(R.array.pref_grid_style_layout); - int layoutRes = typedArray.getResourceId(pos, -1); - typedArray.recycle(); - if (layoutRes == -1) { - return R.layout.item_card; - } - return layoutRes; - } - - public void setAlbumGridStyle(int type) { - mPreferences.edit().putInt(ALBUM_GRID_STYLE, type).apply(); - } - - public int getArtistGridStyle(Context context) { - int pos = Integer.parseInt(mPreferences.getString(ARTIST_GRID_STYLE, "0")); - TypedArray typedArray = context.getResources().obtainTypedArray(R.array.pref_grid_style_layout); - int layoutRes = typedArray.getResourceId(pos, -1); - typedArray.recycle(); - if (layoutRes == -1) { - return R.layout.item_card; - } - return layoutRes; - } - - public void setArtistGridStyle(int viewAs) { - mPreferences.edit().putInt(ARTIST_GRID_STYLE, viewAs).apply(); - } - - public boolean toggleSeparateLine() { - return mPreferences.getBoolean(TOGGLE_SEPARATE_LINE, false); - } - - public int getSongGridStyle() { - return mPreferences.getInt(SONG_GRID_STYLE, R.layout.item_list); - } - - public void setSongGridStyle(int viewAs) { - mPreferences.edit().putInt(SONG_GRID_STYLE, viewAs).apply(); - } - - public boolean enableAnimations() { - return mPreferences.getBoolean(TOGGLE_ANIMATIONS, false); - } + public static final String KEEP_SCREEN_ON = "keep_screen_on"; + public static final String NOW_PLAYING_SCREEN_ID = "now_playing_screen_id"; + public static final String CAROUSEL_EFFECT = "carousel_effect"; + public static final String COLORED_NOTIFICATION = "colored_notification"; + public static final String CLASSIC_NOTIFICATION = "classic_notification"; + public static final String GAPLESS_PLAYBACK = "gapless_playback"; + public static final String ALBUM_ART_ON_LOCKSCREEN = "album_art_on_lockscreen"; + public static final String BLURRED_ALBUM_ART = "blurred_album_art"; + public static final String TOGGLE_HEADSET = "toggle_headset"; + public static final String DOMINANT_COLOR = "dominant_color"; + public static final String GENERAL_THEME = "general_theme"; + public static final String CIRCULAR_ALBUM_ART = "circular_album_art"; + public static final String USER_NAME = "user_name"; + public static final String TOGGLE_FULL_SCREEN = "toggle_full_screen"; + public static final String TOGGLE_VOLUME = "toggle_volume"; + public static final String TOGGLE_TAB_TITLES = "toggle_tab_titles"; + public static final String ROUND_CORNERS = "corner_window"; + public static final String TOGGLE_GENRE = "toggle_genre"; + public static final String PROFILE_IMAGE_PATH = "profile_image_path"; + public static final String BANNER_IMAGE_PATH = "banner_image_path"; + public static final String ADAPTIVE_COLOR_APP = "adaptive_color_app"; + public static final String TOGGLE_SEPARATE_LINE = "toggle_separate_line"; + public static final String ALBUM_GRID_STYLE = "album_grid_style"; + public static final String ARTIST_GRID_STYLE = "artist_grid_style"; + private static final String GENRE_SORT_ORDER = "genre_sort_order"; + private static final String LIBRARY_CATEGORIES = "library_categories"; + private static final String LAST_PAGE = "last_start_page"; + private static final String LAST_MUSIC_CHOOSER = "last_music_chooser"; + private static final String DEFAULT_START_PAGE = "default_start_page"; + private static final String INITIALIZED_BLACKLIST = "initialized_blacklist"; + private static final String ARTIST_SORT_ORDER = "artist_sort_order"; + private static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order"; + private static final String ARTIST_ALBUM_SORT_ORDER = "artist_album_sort_order"; + private static final String ALBUM_SORT_ORDER = "album_sort_order"; + private static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order"; + private static final String SONG_SORT_ORDER = "song_sort_order"; + private static final String ALBUM_GRID_SIZE = "album_grid_size"; + private static final String ALBUM_GRID_SIZE_LAND = "album_grid_size_land"; + private static final String SONG_GRID_SIZE = "song_grid_size"; + private static final String SONG_GRID_SIZE_LAND = "song_grid_size_land"; + private static final String ARTIST_GRID_SIZE = "artist_grid_size"; + private static final String ARTIST_GRID_SIZE_LAND = "artist_grid_size_land"; + private static final String ALBUM_COLORED_FOOTERS = "album_colored_footers"; + private static final String SONG_COLORED_FOOTERS = "song_colored_footers"; + private static final String ARTIST_COLORED_FOOTERS = "artist_colored_footers"; + private static final String ALBUM_ARTIST_COLORED_FOOTERS = "album_artist_colored_footers"; + private static final String COLORED_APP_SHORTCUTS = "colored_app_shortcuts"; + private static final String AUDIO_DUCKING = "audio_ducking"; + private static final String LAST_ADDED_CUTOFF = "last_added_interval"; + private static final String LAST_SLEEP_TIMER_VALUE = "last_sleep_timer_value"; + private static final String NEXT_SLEEP_TIMER_ELAPSED_REALTIME = "next_sleep_timer_elapsed_real_time"; + private static final String IGNORE_MEDIA_STORE_ARTWORK = "ignore_media_store_artwork"; + private static final String LAST_CHANGELOG_VERSION = "last_changelog_version"; + private static final String INTRO_SHOWN = "intro_shown"; + private static final String AUTO_DOWNLOAD_IMAGES_POLICY = "auto_download_images_policy"; + private static final String START_DIRECTORY = "start_directory"; + private static final String SYNCHRONIZED_LYRICS_SHOW = "synchronized_lyrics_show"; + private static final String LOCK_SCREEN = "lock_screen"; + private static final String ALBUM_DETAIL_SONG_SORT_ORDER = "album_detail_song_sort_order"; + private static final String ARTIST_DETAIL_SONG_SORT_ORDER = "artist_detail_song_sort_order"; + private static final String LYRICS_OPTIONS = "lyrics_options"; + private static final String SAF_SDCARD_URI = "saf_sdcard_uri"; + private static final String DOCUMENT_TREE_URI = "document_tree_uri"; + private static final String CHOOSE_EQUALIZER = "choose_equalizer"; + private static final String TOGGLE_SHUFFLE = "toggle_shuffle"; + private static final String SONG_GRID_STYLE = "song_grid_style"; + private static final String TOGGLE_ANIMATIONS = "toggle_animations"; + private static final String TAG = "PreferenceUtil"; + private static PreferenceUtil sInstance; + private final SharedPreferences mPreferences; + + private PreferenceUtil(@NonNull final Context context) { + mPreferences = PreferenceManager.getDefaultSharedPreferences(context); + } + + public static PreferenceUtil getInstance(@NonNull final Context context) { + if (sInstance == null) { + sInstance = new PreferenceUtil(context.getApplicationContext()); + } + return sInstance; + } + + @StyleRes + public static int getThemeResFromPrefValue(String themePrefValue) { + switch (themePrefValue) { + case "dark": + return R.style.Theme_RetroMusic; + case "color": + return R.style.Theme_RetroMusic_Color; + case "acolor": + return R.style.Theme_RetroMusic_Black; + case "black": + return R.style.Theme_RetroMusic_Black; + case "light": + default: + return R.style.Theme_RetroMusic_Light; + } + } + + public int getAlbumDetailsPageStyle() { + + TypedArray typedArray = RetroApplication.getInstance(). + getResources().obtainTypedArray(R.array.pref_album_detail_style_values); + int layout = typedArray.getResourceId(mPreferences.getInt("album_detail_style", 0), -1); + typedArray.recycle(); + return layout; + } + + public final String getArtistSortOrder() { + return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z); + } + + public void setArtistSortOrder(final String sortOrder) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(ARTIST_SORT_ORDER, sortOrder); + editor.apply(); + } + + public final String getArtistSongSortOrder() { + return mPreferences.getString(ARTIST_SONG_SORT_ORDER, SortOrder.ArtistSongSortOrder.SONG_A_Z); + } + + public final String getArtistAlbumSortOrder() { + return mPreferences + .getString(ARTIST_ALBUM_SORT_ORDER, SortOrder.ArtistAlbumSortOrder.ALBUM_YEAR); + } + + public final String getAlbumSortOrder() { + return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z); + } + + public void setAlbumSortOrder(final String sortOrder) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(ALBUM_SORT_ORDER, sortOrder); + editor.apply(); + } + + public final String getAlbumSongSortOrder() { + return mPreferences + .getString(ALBUM_SONG_SORT_ORDER, SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST); + } + + public final String getSongSortOrder() { + return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z); + } + + public void setSongSortOrder(final String sortOrder) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(SONG_SORT_ORDER, sortOrder); + editor.commit(); + } + + public final String getGenreSortOrder() { + return mPreferences.getString(GENRE_SORT_ORDER, SortOrder.GenreSortOrder.GENRE_A_Z); + } + + public boolean isScreenOnEnabled() { + return mPreferences.getBoolean(KEEP_SCREEN_ON, false); + } + + public void setInitializedBlacklist() { + final Editor editor = mPreferences.edit(); + editor.putBoolean(INITIALIZED_BLACKLIST, true); + editor.apply(); + } + + public final boolean initializedBlacklist() { + return mPreferences.getBoolean(INITIALIZED_BLACKLIST, false); + } + + + public boolean circularAlbumArt() { + return mPreferences.getBoolean(CIRCULAR_ALBUM_ART, false); + } + + public boolean carouselEffect() { + return mPreferences.getBoolean(CAROUSEL_EFFECT, false); + } + + public ArrayList getLibraryCategoryInfos() { + String data = mPreferences.getString(LIBRARY_CATEGORIES, null); + if (data != null) { + Gson gson = new Gson(); + Type collectionType = new TypeToken>() { + }.getType(); + + try { + return gson.fromJson(data, collectionType); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } + } + + return getDefaultLibraryCategoryInfos(); + } + + public void setLibraryCategoryInfos(ArrayList categories) { + Gson gson = new Gson(); + Type collectionType = new TypeToken>() { + }.getType(); + + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType)); + editor.apply(); + } + + public ArrayList getDefaultLibraryCategoryInfos() { + ArrayList defaultCategoryInfos = new ArrayList<>(5); + defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.SONGS, true)); + defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true)); + defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ARTISTS, true)); + defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.GENRES, true)); + defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.PLAYLISTS, true)); + return defaultCategoryInfos; + } + + public boolean isRoundCorners() { + return mPreferences.getBoolean(ROUND_CORNERS, false); + } + + public void registerOnSharedPreferenceChangedListener( + SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { + mPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); + } + + public void unregisterOnSharedPreferenceChangedListener( + SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { + mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); + } + + public final int getDefaultStartPage() { + return Integer.parseInt(mPreferences.getString(DEFAULT_START_PAGE, "-1")); + } + + + public final int getLastPage() { + return mPreferences.getInt(LAST_PAGE, 0); + } + + public void setLastPage(final int value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(LAST_PAGE, value); + editor.apply(); + } + + public final int getLastMusicChooser() { + return mPreferences.getInt(LAST_MUSIC_CHOOSER, 0); + } + + public void setLastMusicChooser(final int value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(LAST_MUSIC_CHOOSER, value); + editor.apply(); + } + + public final boolean coloredNotification() { + return mPreferences.getBoolean(COLORED_NOTIFICATION, true); + } + + public final boolean classicNotification() { + return mPreferences.getBoolean(CLASSIC_NOTIFICATION, false); + } + + public void setClassicNotification(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(CLASSIC_NOTIFICATION, value); + editor.apply(); + } + + public final NowPlayingScreen getNowPlayingScreen() { + int id = mPreferences.getInt(NOW_PLAYING_SCREEN_ID, 0); + for (NowPlayingScreen nowPlayingScreen : NowPlayingScreen.values()) { + if (nowPlayingScreen.id == id) { + return nowPlayingScreen; + } + } + return NowPlayingScreen.NORMAL; + } + + @SuppressLint("CommitPrefEdits") + public void setNowPlayingScreen(NowPlayingScreen nowPlayingScreen) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(NOW_PLAYING_SCREEN_ID, nowPlayingScreen.id); + editor.apply(); + } + + public void setColoredAppShortcuts(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(COLORED_APP_SHORTCUTS, value); + editor.apply(); + } + + public final boolean coloredAppShortcuts() { + return mPreferences.getBoolean(COLORED_APP_SHORTCUTS, true); + } + + public final boolean gaplessPlayback() { + return mPreferences.getBoolean(GAPLESS_PLAYBACK, false); + } + + public final boolean audioDucking() { + return mPreferences.getBoolean(AUDIO_DUCKING, true); + } + + public final boolean albumArtOnLockscreen() { + return mPreferences.getBoolean(ALBUM_ART_ON_LOCKSCREEN, true); + } + + public final boolean blurredAlbumArt() { + return mPreferences.getBoolean(BLURRED_ALBUM_ART, false); + } + + public final boolean ignoreMediaStoreArtwork() { + return mPreferences.getBoolean(IGNORE_MEDIA_STORE_ARTWORK, false); + } + + + public int getLastSleepTimerValue() { + return mPreferences.getInt(LAST_SLEEP_TIMER_VALUE, 30); + } + + public void setLastSleepTimerValue(final int value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(LAST_SLEEP_TIMER_VALUE, value); + editor.apply(); + } + + public long getNextSleepTimerElapsedRealTime() { + return mPreferences.getLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, -1); + } + + public void setNextSleepTimerElapsedRealtime(final long value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putLong(NEXT_SLEEP_TIMER_ELAPSED_REALTIME, value); + editor.apply(); + } + + public final int getAlbumGridSize(Context context) { + return mPreferences + .getInt(ALBUM_GRID_SIZE, context.getResources().getInteger(R.integer.default_grid_columns)); + } + + public void setSongGridSize(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(SONG_GRID_SIZE, gridSize); + editor.apply(); + } + + public final int getSongGridSize(Context context) { + return mPreferences + .getInt(SONG_GRID_SIZE, context.getResources().getInteger(R.integer.default_list_columns)); + } + + public void setArtistGridSize(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(ARTIST_GRID_SIZE, gridSize); + editor.apply(); + } + + public final int getArtistGridSize(Context context) { + return mPreferences.getInt(ARTIST_GRID_SIZE, + context.getResources().getInteger(R.integer.default_list_artist_columns)); + } + + public void setAlbumGridSizeLand(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(ALBUM_GRID_SIZE_LAND, gridSize); + editor.apply(); + } + + public final int getAlbumGridSizeLand(Context context) { + return mPreferences.getInt(ALBUM_GRID_SIZE_LAND, + context.getResources().getInteger(R.integer.default_grid_columns_land)); + } + + public void setSongGridSizeLand(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(SONG_GRID_SIZE_LAND, gridSize); + editor.apply(); + } + + public final int getSongGridSizeLand(Context context) { + return mPreferences.getInt(SONG_GRID_SIZE_LAND, + context.getResources().getInteger(R.integer.default_list_columns_land)); + } + + public void setArtistGridSizeLand(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(ARTIST_GRID_SIZE_LAND, gridSize); + editor.apply(); + } + + public final int getArtistGridSizeLand(Context context) { + return mPreferences.getInt(ARTIST_GRID_SIZE_LAND, + context.getResources().getInteger(R.integer.default_list_artist_columns_land)); + } + + public void setAlbumGridSize(final int gridSize) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(ALBUM_GRID_SIZE, gridSize); + editor.apply(); + } + + public void setAlbumColoredFooters(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(ALBUM_COLORED_FOOTERS, value); + editor.apply(); + } + + public final boolean albumColoredFooters() { + return mPreferences.getBoolean(ALBUM_COLORED_FOOTERS, false); + } + + public void setAlbumArtistColoredFooters(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(ALBUM_ARTIST_COLORED_FOOTERS, value); + editor.apply(); + } + + public final boolean albumArtistColoredFooters() { + return mPreferences.getBoolean(ALBUM_ARTIST_COLORED_FOOTERS, true); + } + + public void setSongColoredFooters(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(SONG_COLORED_FOOTERS, value); + editor.apply(); + } + + public final boolean songColoredFooters() { + return mPreferences.getBoolean(SONG_COLORED_FOOTERS, false); + } + + public void setArtistColoredFooters(final boolean value) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putBoolean(ARTIST_COLORED_FOOTERS, value); + editor.apply(); + } + + public final boolean artistColoredFooters() { + return mPreferences.getBoolean(ARTIST_COLORED_FOOTERS, true); + } + + public void setLastChangeLogVersion(int version) { + mPreferences.edit().putInt(LAST_CHANGELOG_VERSION, version).apply(); + } + + public final int getLastChangelogVersion() { + return mPreferences.getInt(LAST_CHANGELOG_VERSION, -1); + } + + @SuppressLint("CommitPrefEdits") + public void setIntroShown() { + // don't use apply here + mPreferences.edit().putBoolean(INTRO_SHOWN, true).commit(); + } + + public final File getStartDirectory() { + return new File(mPreferences + .getString(START_DIRECTORY, FoldersFragment.getDefaultStartDirectory().getPath())); + } + + public void setStartDirectory(File file) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(START_DIRECTORY, FileUtil.safeGetCanonicalPath(file)); + editor.apply(); + } + + public final boolean introShown() { + return mPreferences.getBoolean(INTRO_SHOWN, false); + } + + public final String autoDownloadImagesPolicy() { + return mPreferences.getString(AUTO_DOWNLOAD_IMAGES_POLICY, "only_wifi"); + } + + public final boolean synchronizedLyricsShow() { + return mPreferences.getBoolean(SYNCHRONIZED_LYRICS_SHOW, true); + } + + public int getGeneralTheme() { + return getThemeResFromPrefValue(mPreferences.getString(GENERAL_THEME, "light")); + } + + public void setGeneralTheme(String theme) { + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putString(GENERAL_THEME, theme); + editor.apply(); + } + + public long getLastAddedCutoff() { + final CalendarUtil calendarUtil = new CalendarUtil(); + long interval; + + switch (mPreferences.getString(LAST_ADDED_CUTOFF, "")) { + case "today": + interval = calendarUtil.getElapsedToday(); + break; + case "this_week": + interval = calendarUtil.getElapsedWeek(); + break; + case "past_three_months": + interval = calendarUtil.getElapsedMonths(3); + break; + case "this_year": + interval = calendarUtil.getElapsedYear(); + break; + case "this_month": + default: + interval = calendarUtil.getElapsedMonth(); + break; + } + + return (System.currentTimeMillis() - interval) / 1000; + } + + public boolean getAdaptiveColor() { + return mPreferences.getBoolean(ADAPTIVE_COLOR_APP, false); + } + + public boolean getLockScreen() { + return mPreferences.getBoolean(LOCK_SCREEN, false); + } + + public String getUserName() { + return mPreferences.getString(USER_NAME, "User"); + } + + public void setUserName(String name) { + mPreferences.edit().putString(USER_NAME, name).apply(); + } + + public boolean getFullScreenMode() { + return mPreferences.getBoolean(TOGGLE_FULL_SCREEN, false); + } + + public void setFullScreenMode(int newValue) { + mPreferences.edit().putInt(TOGGLE_FULL_SCREEN, newValue).apply(); + } + + public String lyricsOptions() { + return mPreferences.getString(LYRICS_OPTIONS, "offline"); + } + + public void saveProfileImage(String profileImagePath) { + mPreferences.edit().putString(PROFILE_IMAGE_PATH, profileImagePath) + .apply(); + + } + + public String getProfileImage() { + return mPreferences.getString(PROFILE_IMAGE_PATH, ""); + } + + public String getBannerImage() { + return mPreferences.getString(BANNER_IMAGE_PATH, ""); + } + + public void setBannerImagePath(String bannerImagePath) { + mPreferences.edit().putString(BANNER_IMAGE_PATH, bannerImagePath) + .apply(); + } + + public String getAlbumDetailSongSortOrder() { + return mPreferences + .getString(ALBUM_DETAIL_SONG_SORT_ORDER, SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST); + } + + public void setAlbumDetailSongSortOrder(String sortOrder) { + Editor edit = this.mPreferences.edit(); + edit.putString(ALBUM_DETAIL_SONG_SORT_ORDER, sortOrder); + edit.apply(); + } + + public String getArtistDetailSongSortOrder() { + return mPreferences + .getString(ARTIST_DETAIL_SONG_SORT_ORDER, SortOrder.ArtistSongSortOrder.SONG_A_Z); + } + + public void setArtistDetailSongSortOrder(String sortOrder) { + Editor edit = this.mPreferences.edit(); + edit.putString(ARTIST_DETAIL_SONG_SORT_ORDER, sortOrder); + edit.apply(); + } + + public boolean getVolumeToggle() { + return mPreferences.getBoolean(TOGGLE_VOLUME, false); + } + + public int getLyricsOptions() { + return mPreferences.getInt(LYRICS_OPTIONS, 1); + } + + public void setLyricsOptions(int i) { + mPreferences.edit().putInt(LYRICS_OPTIONS, i).apply(); + } + + public boolean getHeadsetPlugged() { + return mPreferences.getBoolean(TOGGLE_HEADSET, false); + } + + public boolean tabTitles() { + return mPreferences.getBoolean(TOGGLE_TAB_TITLES, true); + } + + public boolean isDominantColor() { + return mPreferences.getBoolean(DOMINANT_COLOR, false); + } + + public boolean isGenreShown() { + return mPreferences.getBoolean(TOGGLE_GENRE, false); + } + + public String getSelectedEqualizer() { + return mPreferences.getString(CHOOSE_EQUALIZER, "system"); + } + + public boolean isShuffleModeOn() { + return mPreferences.getBoolean(TOGGLE_SHUFFLE, false); + } + + public void resetCarouselEffect() { + mPreferences.edit().putBoolean(CAROUSEL_EFFECT, false).apply(); + } + + public void resetCircularAlbumArt() { + mPreferences.edit().putBoolean(CIRCULAR_ALBUM_ART, false).apply(); + } + + @LayoutRes + public int getAlbumGridStyle(Context context) { + int pos = Integer.parseInt(mPreferences.getString(ALBUM_GRID_STYLE, "0")); + TypedArray typedArray = context.getResources().obtainTypedArray(R.array.pref_grid_style_layout); + int layoutRes = typedArray.getResourceId(pos, -1); + typedArray.recycle(); + if (layoutRes == -1) { + return R.layout.item_card; + } + return layoutRes; + } + + public void setAlbumGridStyle(int type) { + mPreferences.edit().putInt(ALBUM_GRID_STYLE, type).apply(); + } + + public int getArtistGridStyle(Context context) { + int pos = Integer.parseInt(mPreferences.getString(ARTIST_GRID_STYLE, "0")); + TypedArray typedArray = context.getResources().obtainTypedArray(R.array.pref_grid_style_layout); + int layoutRes = typedArray.getResourceId(pos, -1); + typedArray.recycle(); + if (layoutRes == -1) { + return R.layout.item_card; + } + return layoutRes; + } + + public void setArtistGridStyle(int viewAs) { + mPreferences.edit().putInt(ARTIST_GRID_STYLE, viewAs).apply(); + } + + public boolean toggleSeparateLine() { + return mPreferences.getBoolean(TOGGLE_SEPARATE_LINE, false); + } + + public int getSongGridStyle() { + return mPreferences.getInt(SONG_GRID_STYLE, R.layout.item_list); + } + + public void setSongGridStyle(int viewAs) { + mPreferences.edit().putInt(SONG_GRID_STYLE, viewAs).apply(); + } + + public boolean enableAnimations() { + return mPreferences.getBoolean(TOGGLE_ANIMATIONS, false); + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/views/LyricView.java b/app/src/main/java/code/name/monkey/retromusic/views/LyricView.java index 0f9660d3..f0e98bef 100644 --- a/app/src/main/java/code/name/monkey/retromusic/views/LyricView.java +++ b/app/src/main/java/code/name/monkey/retromusic/views/LyricView.java @@ -25,9 +25,7 @@ import android.view.View; import android.view.ViewConfiguration; import android.view.animation.DecelerateInterpolator; import android.view.animation.LinearInterpolator; - -import org.mozilla.universalchardet.UniversalDetector; - +import code.name.monkey.retromusic.R; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -40,8 +38,7 @@ import java.lang.annotation.RetentionPolicy; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; - -import code.name.monkey.retromusic.R; +import org.mozilla.universalchardet.UniversalDetector; /** * Created by zhengken.me on 2016/11/27.