Skip to content

Commit

Permalink
The Great Refactoring: режим фонового плеера с множеством попутных пе…
Browse files Browse the repository at this point in the history
…реработок и исправлений
  • Loading branch information
sadr0b0t committed Feb 25, 2023
1 parent e195209 commit 3d024dd
Show file tree
Hide file tree
Showing 33 changed files with 3,083 additions and 1,669 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<activity
android:name=".WatchVideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:launchMode="singleTask"
android:label="@string/yashlang"
android:theme="@style/WatchVideoTheme" />
<activity
Expand Down Expand Up @@ -121,6 +122,8 @@

<service
android:name=".service.StreamCacheDownloadService" />
<service
android:name=".service.PlayerService" />

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import su.sadrobot.yashlang.controller.ThumbManager;
import su.sadrobot.yashlang.model.PlaylistInfo;
import su.sadrobot.yashlang.model.VideoItem;
import su.sadrobot.yashlang.player.RecommendationsProviderFactory;
import su.sadrobot.yashlang.util.PlaylistUrlUtil;
import su.sadrobot.yashlang.view.DataSourceListener;
import su.sadrobot.yashlang.view.OnListItemClickListener;
Expand Down Expand Up @@ -519,8 +520,9 @@ private void setupVideoListAdapter(final String plUrl) {
public void onItemClick(final View view, final int position, final VideoItem videoItem) {
final Intent intent = new Intent(AddPlaylistActivity.this, WatchVideoActivity.class);
intent.putExtra(WatchVideoActivity.PARAM_VIDEO_ITEM_URL, videoItem.getItemUrl());
intent.putExtra(WatchVideoActivity.PARAM_RECOMMENDATIONS_MODE, WatchVideoActivity.RecommendationsMode.PLAYLIST_URL);
intent.putExtra(WatchVideoActivity.PARAM_PLAYLIST_URL, plUrl);
intent.putExtra(WatchVideoActivity.PARAM_SCROLL_TO_IN_RECOMMENDATIONS, position);
intent.putExtra(WatchVideoActivity.PARAM_RECOMMENDATIONS_MODE, RecommendationsProviderFactory.RecommendationsMode.PLAYLIST_URL);
intent.putExtra(RecommendationsProviderFactory.PARAM_PLAYLIST_URL, plUrl);
startActivity(intent);
}

Expand Down Expand Up @@ -561,7 +563,7 @@ public boolean onMenuItemClick(final MenuItem item) {
adapter.registerAdapterDataObserver(emptyListObserver);

// Initial page size to fetch can also be configured here too
final PagedList.Config config = new PagedList.Config.Builder().setPageSize(20).build();
final PagedList.Config config = new PagedList.Config.Builder().setPageSize(ConfigOptions.PAGED_LIST_PAGE_SIZE).build();
// Pass in dependency
final DataSource.Factory factory =
new VideoItemOnlineDataSourceFactory(plUrl, new DataSourceListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public boolean showItemCheckbox(final VideoItem item) {
adapter.registerAdapterDataObserver(emptyListObserver);

// Initial page size to fetch can also be configured here too
final PagedList.Config config = new PagedList.Config.Builder().setPageSize(20).build();
final PagedList.Config config = new PagedList.Config.Builder().setPageSize(ConfigOptions.PAGED_LIST_PAGE_SIZE).build();

final DataSource.Factory factory = VideoDatabase.getDbInstance(BlacklistActivity.this).videoItemDao().getBlacklistDs();

Expand Down
101 changes: 74 additions & 27 deletions app/src/main/java/su/sadrobot/yashlang/ConfigOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public enum VideoStreamSelectPreferRes {
* NONE - не кэшировать
* ALL - кэшировать все
* WITH_OFFLINE_STREAMS - кэшировать только для роликов, у которых
* есть кэшированные оффлайн потоки
* есть кэшированные оффлайн потоки
*/
public enum VideoThumbCacheStrategy {
NONE, ALL, WITH_OFFLINE_STREAMS
Expand All @@ -84,11 +84,20 @@ public enum VideoThumbCacheStrategy {

public static final int UPDATE_PLAYLISTS_DELAY_MS = 500;

/** The default connection timeout, in milliseconds. */
public static final int PAGED_LIST_PAGE_SIZE = 20;

public static final int NOTIFICATION_ID_PLAYER = 1;
public static final int NOTIFICATION_ID_DOWNLOAD_STREAM = 2;

/**
* The default connection timeout, in milliseconds.
*/
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS =
com.google.android.exoplayer2.upstream.DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS;

/** The default read timeout, in milliseconds. */
/**
* The default read timeout, in milliseconds.
*/
public static final int DEFAULT_READ_TIMEOUT_MILLIS =
com.google.android.exoplayer2.upstream.DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS;

Expand Down Expand Up @@ -159,145 +168,183 @@ public enum VideoThumbCacheStrategy {
*/
private static final String PREF_OFFLINE_MODE_ON = "PREF_OFFLINE_MODE_ON";

/**
* false - классический плеер, играет только когда открыт экран плеера; если экран плеера
* скрыт, проигрывание прерывается, панель управления плеером в качестве уведомения не появляется
* true - плеер с автоматическим переходом в фоновый режим: если экран пллера скрыт,
* аудио-дорожка продолжает играть в фоне, панель управления плеером появляются в области
* уведомлений и на экране блокировки устройства
*/
private static final String PREF_BACKGROUND_PLAYBACK_ON = "PREF_BACKGROUND_PLAYBACK_ON";

/**
* В режиме фонового плеера PREF_BACKGROUND_PLAYBACK_ON=true
* true - ставить на паузу при скрытии экрана плеера
* false - при скрытии экрана плеера продолждать играть без остановки
*/
private static final String PREF_PAUSE_ON_HIDE = "PREF_PAUSE_ON_HIDE";


public static SortBy getPlaylistsSortBy(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: TIME_ADDED (чтобы сохранить старое поведение)
return SortBy.valueOf(sp.getString(PREF_PLAYLISTS_SORT_BY, SortBy.TIME_ADDED.name()));
}

public static void setPlaylistsSortBy(final Context context, final SortBy sortBy) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_PLAYLISTS_SORT_BY, sortBy.name());
editor.commit();
}

public static boolean getPlaylistsSortDir(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: TIME_ADDED + asc (чтобы сохранить старое поведение)
return sp.getBoolean(PREF_PLAYLISTS_SORT_DIR, true);
}

public static void setPlaylistsSortDir(final Context context, final boolean asc) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_PLAYLISTS_SORT_DIR, asc);
editor.commit();
}

public static SortBy getPlaylistSortBy(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: TIME_ADDED+desc (чтобы сохранить старое поведение)
return SortBy.valueOf(sp.getString(PREF_PLAYLIST_SORT_BY, SortBy.TIME_ADDED.name()));
}

public static void setPlaylistSortBy(final Context context, final SortBy sortBy) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_PLAYLIST_SORT_BY, sortBy.name());
editor.commit();
}

public static boolean getPlaylistSortDir(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: TIME_ADDED + desc (чтобы сохранить старое поведение)
return sp.getBoolean(PREF_PLAYLIST_SORT_DIR, false);
}

public static void setPlaylistSortDir(final Context context, final boolean asc) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_PLAYLIST_SORT_DIR, asc);
editor.commit();
}

public static VideoStreamSelectStrategy getVideoStreamSelectStrategy(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: MAX_RES
return VideoStreamSelectStrategy.valueOf(sp.getString(PREF_VIDEO_STREAM_SELECT_STRATEGY, VideoStreamSelectStrategy.MAX_RES.name()));
}

public static void setVideoStreamSelectStrategy(final Context context, final VideoStreamSelectStrategy strategy) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_STREAM_SELECT_STRATEGY, strategy.name());
editor.commit();
}

public static String getVideoStreamCustomRes(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: среднее качество, доступно почти всегда
return sp.getString(PREF_VIDEO_STREAM_CUSTOM_RES, DEFAULT_VIDEO_RESOLUTION);
}

public static void setVideoStreamCustomRes(final Context context, final String resolution) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_STREAM_CUSTOM_RES, resolution);
editor.commit();
}

public static String getVideoStreamLastSelectedRes(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: среднее качество, доступно почти всегда
return sp.getString(PREF_VIDEO_STREAM_LAST_SELECTED_RES, DEFAULT_VIDEO_RESOLUTION);
}

public static void setVideoStreamLastSelectedRes(final Context context, final String resolution) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_STREAM_LAST_SELECTED_RES, resolution);
editor.commit();
}

public static VideoStreamSelectPreferRes getVideoStreamSelectCustomPreferRes(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return VideoStreamSelectPreferRes.valueOf(sp.getString(PREF_VIDEO_STREAM_SELECT_CUSTOM_PREFER_RES, VideoStreamSelectPreferRes.HIGHER_RES.name()));
}

public static void setVideoStreamSelectCustomPreferRes(final Context context, final VideoStreamSelectPreferRes preferRes) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_STREAM_SELECT_CUSTOM_PREFER_RES, preferRes.name());
editor.commit();
}

public static VideoStreamSelectPreferRes getVideoStreamSelectLastPreferRes(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return VideoStreamSelectPreferRes.valueOf(sp.getString(PREF_VIDEO_STREAM_SELECT_LAST_PREFER_RES, VideoStreamSelectPreferRes.HIGHER_RES.name()));
}

public static void setVideoStreamSelectLastPreferRes(final Context context, final VideoStreamSelectPreferRes preferRes) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_STREAM_SELECT_LAST_PREFER_RES, preferRes.name());
editor.commit();
}

public static boolean getVideoStreamSelectOffline(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: true
return sp.getBoolean(PREF_VIDEO_STREAM_SELECT_OFFLINE, true);
}

public static void setVideoStreamSelectOffline(final Context context, final boolean selectOffline) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_VIDEO_STREAM_SELECT_OFFLINE, selectOffline);
editor.commit();
}

public static VideoThumbCacheStrategy getVideoThumbCacheStrategy(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return VideoThumbCacheStrategy.valueOf(sp.getString(PREF_VIDEO_THUMB_CACHE_STRATEGY, VideoThumbCacheStrategy.ALL.name()));
}

public static void setVideoThumbCacheStrategy(final Context context, final VideoThumbCacheStrategy strategy) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putString(PREF_VIDEO_THUMB_CACHE_STRATEGY, strategy.name());
editor.commit();
}

public static void setOfflineModeOn(final Context context, final boolean offlineModeOn) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0).edit();
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_OFFLINE_MODE_ON, offlineModeOn);
editor.commit();
}

public static boolean getOfflineModeOn(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME,0);
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
// по умолчанию: false
return sp.getBoolean(PREF_OFFLINE_MODE_ON, false);
}

public static boolean getBackgroundPlaybackOn(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return sp.getBoolean(PREF_BACKGROUND_PLAYBACK_ON, true);
}

public static void setBackgroundPlaybackOn(final Context context, final boolean backgroundPlaybackOn) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_BACKGROUND_PLAYBACK_ON, backgroundPlaybackOn);
editor.commit();
}

public static boolean getPauseOnHide(final Context context) {
final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return sp.getBoolean(PREF_PAUSE_ON_HIDE, true);
}

public static void setPauseOnHide(final Context context, final boolean pauseOnHide) {
final SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0).edit();
editor.putBoolean(PREF_PAUSE_ON_HIDE, pauseOnHide);
editor.commit();
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/su/sadrobot/yashlang/ConfigureMoarFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import su.sadrobot.yashlang.service.PlayerService;


public class ConfigureMoarFragment extends Fragment {

private Switch offlineModeSwitch;

private Switch backgroundPlaybackOnSwitch;
private Switch pauseOnHideSwitch;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
Expand All @@ -44,6 +49,8 @@ public View onCreateView(@NonNull LayoutInflater inflater,
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
offlineModeSwitch = view.findViewById(R.id.offline_mode_switch);
backgroundPlaybackOnSwitch = view.findViewById(R.id.background_playback_on_switch);
pauseOnHideSwitch = view.findViewById(R.id.pause_on_hide_switch);

offlineModeSwitch.setChecked(ConfigOptions.getOfflineModeOn(
ConfigureMoarFragment.this.getContext()));
Expand All @@ -53,5 +60,38 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ConfigOptions.setOfflineModeOn(ConfigureMoarFragment.this.getContext(), isChecked);
}
});

backgroundPlaybackOnSwitch.setChecked(ConfigOptions.getBackgroundPlaybackOn(
ConfigureMoarFragment.this.getContext()));
backgroundPlaybackOnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ConfigOptions.setBackgroundPlaybackOn(ConfigureMoarFragment.this.getContext(), isChecked);
updateControlsStates();

if (!isChecked) {
PlayerService.cmdStop(ConfigureMoarFragment.this.getContext());
}
}
});

pauseOnHideSwitch.setChecked(ConfigOptions.getPauseOnHide(
ConfigureMoarFragment.this.getContext()));
pauseOnHideSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ConfigOptions.setPauseOnHide(ConfigureMoarFragment.this.getContext(), isChecked);
}
});

updateControlsStates();
}

private void updateControlsStates() {
if (backgroundPlaybackOnSwitch.isChecked()) {
pauseOnHideSwitch.setEnabled(true);
} else {
pauseOnHideSwitch.setEnabled(false);
}
}
}
Loading

0 comments on commit 3d024dd

Please sign in to comment.