Thorium-android-app/app/src/main/java/net/schueller/peertube/fragment/VideoOptionsFragment.java

130 lines
5.2 KiB
Java
Raw Normal View History

2018-12-29 14:19:17 +01:00
/*
2020-11-28 22:36:47 +01:00
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
2018-12-29 14:19:17 +01:00
*
* This program is free software: you can redistribute it and/or modify
2020-11-28 22:36:47 +01:00
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-12-29 14:19:17 +01:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020-11-28 22:36:47 +01:00
* GNU Affero General Public License for more details.
2018-12-29 14:19:17 +01:00
*
2020-11-28 22:36:47 +01:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-29 14:19:17 +01:00
*/
package net.schueller.peertube.fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-01-07 22:33:10 +01:00
import android.widget.LinearLayout;
2018-12-25 14:44:52 +01:00
import android.widget.TextView;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
2018-12-26 16:15:34 +01:00
import com.mikepenz.iconics.Iconics;
2020-07-05 15:07:50 +02:00
import net.schueller.peertube.R;
2019-01-07 22:33:10 +01:00
import net.schueller.peertube.model.File;
2018-12-25 14:44:52 +01:00
import net.schueller.peertube.service.VideoPlayerService;
2019-01-07 22:33:10 +01:00
import java.util.ArrayList;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
public class VideoOptionsFragment extends BottomSheetDialogFragment {
2018-12-25 14:44:52 +01:00
private static VideoPlayerService videoPlayerService;
2019-01-07 22:33:10 +01:00
private static ArrayList<File> files;
public static final String TAG = "VideoOptions";
2018-12-25 14:44:52 +01:00
2018-12-26 16:15:34 +01:00
2019-01-07 22:33:10 +01:00
public static VideoOptionsFragment newInstance(VideoPlayerService mService, ArrayList<File> mFiles) {
2018-12-25 14:44:52 +01:00
videoPlayerService = mService;
2019-01-07 22:33:10 +01:00
files = mFiles;
return new VideoOptionsFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
2019-01-06 02:14:03 +01:00
View view = inflater.inflate(R.layout.fragment_video_options_popup_menu, container,
false);
2019-01-07 22:33:10 +01:00
LinearLayout menuHolder = view.findViewById(R.id.video_options_popup);
// Video Speed
2020-07-05 15:11:48 +02:00
LinearLayout menuRow = (LinearLayout) inflater.inflate(R.layout.row_popup_menu, container);
2019-01-07 22:33:10 +01:00
TextView iconView = menuRow.findViewById(R.id.video_quality_icon);
TextView textView = menuRow.findViewById(R.id.video_quality_text);
2020-07-05 15:07:50 +02:00
textView.setText(
getString(
R.string.menu_video_options_playback_speed,
getCurrentVideoPlaybackSpeedString(videoPlayerService.getPlayBackSpeed()
)
)
);
2019-01-09 08:40:28 +01:00
iconView.setText(R.string.video_option_speed_icon);
2021-01-24 00:31:51 +01:00
new Iconics.Builder().on(iconView).build();
2019-01-07 22:33:10 +01:00
textView.setOnClickListener(view1 -> {
VideoMenuSpeedFragment videoMenuSpeedFragment =
VideoMenuSpeedFragment.newInstance(videoPlayerService);
2020-07-05 15:07:50 +02:00
videoMenuSpeedFragment.show(requireActivity().getSupportFragmentManager(),
2019-01-07 22:33:10 +01:00
VideoMenuSpeedFragment.TAG);
});
menuHolder.addView(menuRow);
// Video Quality
2020-07-05 15:11:48 +02:00
LinearLayout menuRow2 = (LinearLayout) inflater.inflate(R.layout.row_popup_menu, container);
2019-01-07 22:33:10 +01:00
TextView iconView2 = menuRow2.findViewById(R.id.video_quality_icon);
TextView textView2 = menuRow2.findViewById(R.id.video_quality_text);
textView2.setText(String.format(getString(R.string.menu_video_options_quality), getCurrentVideoQuality(files)));
2019-01-09 08:40:28 +01:00
iconView2.setText(R.string.video_option_quality_icon);
2021-01-24 00:31:51 +01:00
new Iconics.Builder().on(iconView2).build();
2019-01-07 22:33:10 +01:00
textView2.setOnClickListener(view1 -> {
VideoMenuQualityFragment videoMenuQualityFragment =
VideoMenuQualityFragment.newInstance(getContext(), files);
2020-07-05 15:07:50 +02:00
videoMenuQualityFragment.show(requireActivity().getSupportFragmentManager(),
VideoMenuQualityFragment.TAG);
2019-01-07 22:33:10 +01:00
});
menuHolder.addView(menuRow2);
2018-12-25 14:44:52 +01:00
return view;
}
2018-12-26 16:15:34 +01:00
private String getCurrentVideoQuality(ArrayList<File> files) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
2021-01-24 00:31:51 +01:00
Integer videoQuality = sharedPref.getInt(getString(R.string.pref_quality_key), 999999);
for (File file : files) {
if (videoQuality.equals(file.getResolution().getId())) {
return file.getResolution().getLabel();
}
}
// Returning Automated as a placeholder
return getString(R.string.menu_video_options_quality_automated);
}
private String getCurrentVideoPlaybackSpeedString(float playbackSpeed) {
String speed = String.valueOf(playbackSpeed);
// Remove all non-digit characters from the string
speed = speed.replaceAll("[^0-9]", "");
// Dynamically get the localized string corresponding to the speed
@StringRes int stringId = getResources().getIdentifier("video_speed_" + speed, "string", videoPlayerService.getPackageName());
return getString(stringId);
}
}