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

127 lines
4.5 KiB
Java
Raw Normal View History

2019-01-07 22:33:10 +01:00
/*
2020-11-28 22:36:47 +01:00
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
2019-01-07 22:33:10 +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.
2019-01-07 22:33:10 +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.
2019-01-07 22:33:10 +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/>.
2019-01-07 22:33:10 +01:00
*/
package net.schueller.peertube.fragment;
import android.content.Context;
2019-01-09 17:53:26 +01:00
import android.content.SharedPreferences;
2019-01-07 22:33:10 +01:00
import android.os.Bundle;
2019-01-09 17:53:26 +01:00
import android.preference.PreferenceManager;
2019-01-07 22:33:10 +01:00
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
2019-01-09 17:53:26 +01:00
import com.mikepenz.iconics.Iconics;
2019-01-07 22:33:10 +01:00
import net.schueller.peertube.R;
import net.schueller.peertube.model.File;
2019-01-09 17:53:26 +01:00
import net.schueller.peertube.model.Resolution;
2019-01-07 22:33:10 +01:00
import java.util.ArrayList;
import androidx.annotation.Nullable;
public class VideoMenuQualityFragment extends BottomSheetDialogFragment {
private static ArrayList<File> mFiles;
public static final String TAG = "VideoMenuQuality";
2019-01-09 18:05:56 +01:00
private static File autoQualityFile;
2019-01-07 22:33:10 +01:00
public static VideoMenuQualityFragment newInstance(Context context, ArrayList<File> files) {
2019-01-09 17:53:26 +01:00
2019-01-07 22:33:10 +01:00
mFiles = files;
2019-01-09 17:53:26 +01:00
// Auto quality
2019-01-09 18:05:56 +01:00
if (autoQualityFile == null) {
autoQualityFile = new File();
Resolution autoQualityResolution = new Resolution();
2021-01-24 00:31:51 +01:00
autoQualityResolution.setId(999999);
autoQualityResolution.setLabel(context.getString(R.string.menu_video_options_quality_automated));
2021-01-24 00:31:51 +01:00
autoQualityFile.setId(999999);
2019-01-09 18:05:56 +01:00
autoQualityFile.setResolution(autoQualityResolution);
}
2019-01-09 17:53:26 +01:00
if (!mFiles.contains(autoQualityFile)) {
mFiles.add(0, autoQualityFile);
}
2019-01-07 22:33:10 +01:00
return new VideoMenuQualityFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_video_options_quality_popup_menu, container,
false);
2019-01-09 17:53:26 +01:00
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
2021-01-24 00:31:51 +01:00
Integer videoQuality = sharedPref.getInt(getString(R.string.pref_quality_key), 999999);
2019-01-09 17:53:26 +01:00
2019-01-09 18:05:56 +01:00
for (File file : mFiles) {
2019-01-07 22:33:10 +01:00
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);
2019-01-09 18:13:42 +01:00
iconView.setId(file.getResolution().getId());
2019-01-07 22:33:10 +01:00
TextView textView = menuRow.findViewById(R.id.video_quality_text);
Log.v(TAG, file.getResolution().getLabel());
textView.setText(file.getResolution().getLabel());
2019-01-09 17:53:26 +01:00
textView.setOnClickListener(view1 -> {
// Log.v(TAG, file.getResolution().getLabel());
SharedPreferences.Editor editor = sharedPref.edit();
2020-07-05 16:12:35 +02:00
editor.putInt(getString(R.string.pref_quality_key), file.getResolution().getId());
2019-01-09 17:53:26 +01:00
editor.apply();
2019-01-09 18:05:56 +01:00
2019-01-09 18:13:42 +01:00
for (File fileV : mFiles) {
TextView iconViewV = view.findViewById(fileV.getResolution().getId());
2021-01-24 00:31:51 +01:00
if (iconViewV != null) {
iconViewV.setText("");
}
2019-01-09 18:13:42 +01:00
}
2019-01-09 18:05:56 +01:00
2019-01-09 17:53:26 +01:00
iconView.setText(R.string.video_quality_active_icon);
2021-01-24 00:31:51 +01:00
new Iconics.Builder().on(iconView).build();
2019-01-09 18:13:42 +01:00
//TODO: set new video quality on running video
2019-01-09 17:53:26 +01:00
});
2019-01-07 22:33:10 +01:00
// Add to menu
LinearLayout menuHolder = view.findViewById(R.id.video_quality_menu);
menuHolder.addView(menuRow);
2019-01-09 18:13:42 +01:00
// Set current
2019-01-09 17:53:26 +01:00
if (videoQuality.equals(file.getResolution().getId())) {
iconView.setText(R.string.video_quality_active_icon);
2021-01-24 00:31:51 +01:00
new Iconics.Builder().on(iconView).build();
2019-01-09 17:53:26 +01:00
}
2019-01-07 22:33:10 +01:00
}
return view;
}
}