2017-04-09 19:34:00 +02:00
|
|
|
package org.schabi.newpipe.fragments.detail;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
2015-09-15 20:11:42 +02:00
|
|
|
import android.content.SharedPreferences;
|
2015-09-04 02:15:03 +02:00
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2017-04-26 21:32:04 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.Spinner;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
2016-08-02 21:17:54 +02:00
|
|
|
import org.schabi.newpipe.R;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
|
|
|
import org.schabi.newpipe.util.ListHelper;
|
2015-12-28 00:32:38 +01:00
|
|
|
|
2016-02-05 14:09:04 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*
|
2015-09-04 02:15:03 +02:00
|
|
|
* Created by Christian Schabesberger on 18.08.15.
|
2017-04-26 21:32:04 +02:00
|
|
|
* <p>
|
2016-02-05 17:09:29 +01:00
|
|
|
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
|
2015-09-04 02:15:03 +02:00
|
|
|
* DetailsMenuHandler.java is part of NewPipe.
|
2017-04-26 21:32:04 +02:00
|
|
|
* <p>
|
2015-09-04 02:15:03 +02:00
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2017-04-26 21:32:04 +02:00
|
|
|
* <p>
|
2015-09-04 02:15:03 +02:00
|
|
|
* NewPipe 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
|
|
|
|
* GNU General Public License for more details.
|
2017-04-26 21:32:04 +02:00
|
|
|
* <p>
|
2015-09-04 02:15:03 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-11-29 13:06:27 +01:00
|
|
|
|
2017-05-09 03:23:27 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2015-11-29 13:06:27 +01:00
|
|
|
class ActionBarHandler {
|
2017-04-26 21:32:04 +02:00
|
|
|
private static final String TAG = "ActionBarHandler";
|
2015-09-15 20:11:42 +02:00
|
|
|
|
2015-09-04 02:15:03 +02:00
|
|
|
private AppCompatActivity activity;
|
2016-02-08 18:46:42 +01:00
|
|
|
private int selectedVideoStream = -1;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
2016-06-24 02:37:44 +02:00
|
|
|
private SharedPreferences defaultPreferences;
|
2015-09-15 20:11:42 +02:00
|
|
|
|
2016-02-17 21:39:41 +01:00
|
|
|
private Menu menu;
|
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
// Only callbacks are listed here, there are more actions which don't need a callback.
|
2017-04-09 19:34:00 +02:00
|
|
|
// those are edited directly. Typically VideoDetailFragment will implement those callbacks.
|
2016-06-24 02:37:44 +02:00
|
|
|
private OnActionListener onShareListener;
|
|
|
|
private OnActionListener onOpenInBrowserListener;
|
|
|
|
private OnActionListener onDownloadListener;
|
|
|
|
private OnActionListener onPlayWithKodiListener;
|
2016-02-08 18:46:42 +01:00
|
|
|
|
|
|
|
// Triggered when a stream related action is triggered.
|
|
|
|
public interface OnActionListener {
|
|
|
|
void onActionSelected(int selectedStreamId);
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:56:35 +01:00
|
|
|
public ActionBarHandler(AppCompatActivity activity) {
|
|
|
|
this.activity = activity;
|
|
|
|
}
|
|
|
|
|
2017-04-26 21:32:04 +02:00
|
|
|
public void setupStreamList(final List<VideoStream> videoStreams, Spinner toolbarSpinner) {
|
|
|
|
if (activity == null) return;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
selectedVideoStream = ListHelper.getDefaultResolutionIndex(activity, videoStreams);
|
2017-06-28 03:39:33 +02:00
|
|
|
|
2017-05-15 05:57:57 +02:00
|
|
|
boolean isExternalPlayerEnabled = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(activity.getString(R.string.use_external_video_player_key), false);
|
|
|
|
toolbarSpinner.setAdapter(new SpinnerToolbarAdapter(activity, videoStreams, isExternalPlayerEnabled));
|
2017-06-28 03:39:33 +02:00
|
|
|
toolbarSpinner.setSelection(selectedVideoStream);
|
2017-04-26 21:32:04 +02:00
|
|
|
toolbarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
selectedVideoStream = position;
|
|
|
|
}
|
2015-12-25 00:09:35 +01:00
|
|
|
|
2017-04-26 21:32:04 +02:00
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
2016-02-08 18:46:42 +01:00
|
|
|
}
|
2017-04-26 21:32:04 +02:00
|
|
|
});
|
2016-02-08 18:46:42 +01:00
|
|
|
|
2015-12-25 00:09:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 13:06:27 +01:00
|
|
|
public void setupMenu(Menu menu, MenuInflater inflater) {
|
2016-02-17 21:39:41 +01:00
|
|
|
this.menu = menu;
|
|
|
|
|
2015-09-04 02:15:03 +02:00
|
|
|
// CAUTION set item properties programmatically otherwise it would not be accepted by
|
|
|
|
// appcompat itemsinflater.inflate(R.menu.videoitem_detail, menu);
|
|
|
|
|
2015-10-29 17:56:35 +01:00
|
|
|
defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
|
2017-04-26 21:32:04 +02:00
|
|
|
inflater.inflate(R.menu.video_detail_menu, menu);
|
2015-09-04 02:15:03 +02:00
|
|
|
|
2017-05-09 03:23:27 +02:00
|
|
|
updateItemsVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateItemsVisibility(){
|
2017-04-26 21:32:04 +02:00
|
|
|
showPlayWithKodiAction(defaultPreferences.getBoolean(activity.getString(R.string.show_play_with_kodi_key), false));
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:56:35 +01:00
|
|
|
public boolean onItemSelected(MenuItem item) {
|
2016-02-08 18:46:42 +01:00
|
|
|
int id = item.getItemId();
|
|
|
|
switch (id) {
|
|
|
|
case R.id.menu_item_share: {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (onShareListener != null) {
|
2016-02-17 21:39:41 +01:00
|
|
|
onShareListener.onActionSelected(selectedVideoStream);
|
|
|
|
}
|
2015-10-29 17:56:35 +01:00
|
|
|
return true;
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
2016-02-08 18:46:42 +01:00
|
|
|
case R.id.menu_item_openInBrowser: {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (onOpenInBrowserListener != null) {
|
2016-02-17 21:39:41 +01:00
|
|
|
onOpenInBrowserListener.onActionSelected(selectedVideoStream);
|
|
|
|
}
|
2017-04-26 21:32:04 +02:00
|
|
|
return true;
|
2016-02-08 18:46:42 +01:00
|
|
|
}
|
|
|
|
case R.id.menu_item_download:
|
2017-04-26 21:32:04 +02:00
|
|
|
if (onDownloadListener != null) {
|
2016-02-17 21:39:41 +01:00
|
|
|
onDownloadListener.onActionSelected(selectedVideoStream);
|
|
|
|
}
|
2016-02-08 18:46:42 +01:00
|
|
|
return true;
|
|
|
|
case R.id.action_play_with_kodi:
|
2017-04-26 21:32:04 +02:00
|
|
|
if (onPlayWithKodiListener != null) {
|
2016-02-17 21:39:41 +01:00
|
|
|
onPlayWithKodiListener.onActionSelected(selectedVideoStream);
|
|
|
|
}
|
2016-02-08 18:46:42 +01:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
Log.e(TAG, "Menu Item not known");
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
2015-10-29 17:56:35 +01:00
|
|
|
return false;
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
public int getSelectedVideoStream() {
|
|
|
|
return selectedVideoStream;
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
public void setOnShareListener(OnActionListener listener) {
|
|
|
|
onShareListener = listener;
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
public void setOnOpenInBrowserListener(OnActionListener listener) {
|
|
|
|
onOpenInBrowserListener = listener;
|
2015-11-14 12:47:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
public void setOnDownloadListener(OnActionListener listener) {
|
|
|
|
onDownloadListener = listener;
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|
2015-09-15 20:11:42 +02:00
|
|
|
|
2016-02-08 18:46:42 +01:00
|
|
|
public void setOnPlayWithKodiListener(OnActionListener listener) {
|
|
|
|
onPlayWithKodiListener = listener;
|
2015-09-15 20:11:42 +02:00
|
|
|
}
|
2015-09-21 21:12:48 +02:00
|
|
|
|
2016-02-17 21:39:41 +01:00
|
|
|
public void showDownloadAction(boolean visible) {
|
|
|
|
menu.findItem(R.id.menu_item_download).setVisible(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showPlayWithKodiAction(boolean visible) {
|
|
|
|
menu.findItem(R.id.action_play_with_kodi).setVisible(visible);
|
|
|
|
}
|
2015-09-04 02:15:03 +02:00
|
|
|
}
|