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

231 lines
8.4 KiB
Java
Raw Normal View History

2015-09-04 02:15:03 +02:00
package org.schabi.newpipe;
import android.content.Intent;
import android.content.SharedPreferences;
2015-09-04 02:15:03 +02:00
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
2016-02-18 11:50:22 +01:00
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.StreamInfo;
import org.schabi.newpipe.extractor.VideoStream;
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.
*
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.
*
* 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.
*
* 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.
*
* 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
class ActionBarHandler {
2015-09-04 02:15:03 +02:00
private static final String TAG = ActionBarHandler.class.toString();
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
2015-11-29 13:06:27 +01:00
private SharedPreferences defaultPreferences = null;
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 VideoItemDetailFragment will implement those callbacks.
private OnActionListener onShareListener = null;
private OnActionListener onOpenInBrowserListener = null;
private OnActionListener onDownloadListener = null;
private OnActionListener onPlayWithKodiListener = null;
private OnActionListener onPlayAudioListener = null;
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;
}
2015-11-29 13:06:27 +01:00
@SuppressWarnings({"deprecation", "ConstantConditions"})
2015-09-04 02:15:03 +02:00
public void setupNavMenu(AppCompatActivity activity) {
this.activity = activity;
2015-11-29 13:06:27 +01:00
try {
activity.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
2016-02-08 18:46:42 +01:00
} catch (NullPointerException e) {
2015-11-29 13:06:27 +01:00
e.printStackTrace();
}
2015-09-04 02:15:03 +02:00
}
public void setupStreamList(final List<VideoStream> videoStreams) {
2016-02-08 18:46:42 +01:00
if (activity != null) {
selectedVideoStream = 0;
2016-02-08 18:46:42 +01:00
// this array will be shown in the dropdown menu for selecting the stream/resolution.
String[] itemArray = new String[videoStreams.size()];
for (int i = 0; i < videoStreams.size(); i++) {
VideoStream item = videoStreams.get(i);
2016-02-08 18:46:42 +01:00
itemArray[i] = MediaFormat.getNameById(item.format) + " " + item.resolution;
}
int defaultResolution = getDefaultResolution(videoStreams);
ArrayAdapter<String> itemAdapter = new ArrayAdapter<>(activity.getBaseContext(),
android.R.layout.simple_spinner_dropdown_item, itemArray);
ActionBar ab = activity.getSupportActionBar();
//todo: make this throwsable
assert ab != null : "Could not get actionbar";
ab.setListNavigationCallbacks(itemAdapter
, new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
selectedVideoStream = (int) itemId;
return true;
}
});
ab.setSelectedNavigationItem(defaultResolution);
}
}
2016-02-08 18:46:42 +01:00
private int getDefaultResolution(final List<VideoStream> videoStreams) {
String defaultResolution = defaultPreferences
.getString(activity.getString(R.string.default_resolution_key),
activity.getString(R.string.default_resolution_value));
2016-02-08 18:46:42 +01:00
for (int i = 0; i < videoStreams.size(); i++) {
VideoStream item = videoStreams.get(i);
2016-02-08 18:46:42 +01:00
if (defaultResolution.equals(item.resolution)) {
return i;
}
}
2016-02-08 18:46:42 +01:00
// this is actually an error,
// but maybe there is really no stream fitting to the default value.
return 0;
2015-09-04 02:15:03 +02: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);
2015-09-04 02:15:03 +02:00
inflater.inflate(R.menu.videoitem_detail, menu);
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: {
/*
2015-09-04 02:15:03 +02:00
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, websiteUrl);
2015-09-04 02:15:03 +02:00
intent.setType("text/plain");
activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.share_dialog_title)));
2016-02-08 18:46:42 +01:00
*/
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);
}
2016-02-08 18:46:42 +01:00
}
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_settings: {
Intent intent = new Intent(activity, SettingsActivity.class);
activity.startActivity(intent);
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;
case R.id.menu_item_play_audio:
if(onPlayAudioListener != null) {
onPlayAudioListener.onActionSelected(selectedVideoStream);
}
2016-02-08 18:46:42 +01:00
return true;
case R.id.menu_item_downloads: {
Intent intent =
new Intent(activity, org.schabi.newpipe.download.MainActivity.class);
activity.startActivity(intent);
return true;
}
2016-02-08 18:46:42 +01:00
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;
}
2016-02-08 18:46:42 +01:00
public void setOnPlayAudioListener(OnActionListener listener) {
onPlayAudioListener = listener;
}
public void showAudioAction(boolean visible) {
menu.findItem(R.id.menu_item_play_audio).setVisible(visible);
}
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
}