NewPipe/app/src/main/java/org/schabi/newpipe/fragments/detail/ActionBarHandler.java

164 lines
5.7 KiB
Java
Raw Normal View History

package org.schabi.newpipe.fragments.detail;
2015-09-04 02:15:03 +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;
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;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.util.ListHelper;
2016-02-05 14:09:04 +01:00
import java.util.List;
/*
2015-09-04 02:15:03 +02:00
* Created by Christian Schabesberger on 18.08.15.
* <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.
* <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.
* <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.
* <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
@SuppressWarnings("WeakerAccess")
2015-11-29 13:06:27 +01:00
class ActionBarHandler {
private static final String TAG = "ActionBarHandler";
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
private SharedPreferences defaultPreferences;
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.
// those are edited directly. Typically VideoDetailFragment will implement those callbacks.
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
}
public ActionBarHandler(AppCompatActivity activity) {
this.activity = activity;
}
public void setupStreamList(final List<VideoStream> videoStreams, Spinner toolbarSpinner) {
if (activity == null) return;
2015-09-04 02:15:03 +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);
toolbarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedVideoStream = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
2016-02-08 18:46:42 +01:00
}
});
2016-02-08 18:46:42 +01:00
}
2015-11-29 13:06:27 +01:00
public void setupMenu(Menu menu, MenuInflater inflater) {
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);
defaultPreferences = PreferenceManager.getDefaultSharedPreferences(activity);
inflater.inflate(R.menu.video_detail_menu, menu);
2015-09-04 02:15:03 +02:00
updateItemsVisibility();
}
public void updateItemsVisibility(){
showPlayWithKodiAction(defaultPreferences.getBoolean(activity.getString(R.string.show_play_with_kodi_key), false));
2015-09-04 02:15:03 +02: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: {
if (onShareListener != null) {
onShareListener.onActionSelected(selectedVideoStream);
}
return true;
2015-09-04 02:15:03 +02:00
}
2016-02-08 18:46:42 +01:00
case R.id.menu_item_openInBrowser: {
if (onOpenInBrowserListener != null) {
onOpenInBrowserListener.onActionSelected(selectedVideoStream);
}
return true;
2016-02-08 18:46:42 +01:00
}
case R.id.menu_item_download:
if (onDownloadListener != null) {
onDownloadListener.onActionSelected(selectedVideoStream);
}
2016-02-08 18:46:42 +01:00
return true;
case R.id.action_play_with_kodi:
if (onPlayWithKodiListener != null) {
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
}
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;
}
2016-02-08 18:46:42 +01:00
public void setOnDownloadListener(OnActionListener listener) {
onDownloadListener = listener;
2015-09-04 02:15:03 +02:00
}
2016-02-08 18:46:42 +01:00
public void setOnPlayWithKodiListener(OnActionListener listener) {
onPlayWithKodiListener = listener;
}
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
}