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

858 lines
36 KiB
Java
Raw Normal View History

/*
2016-08-02 13:06:02 +00:00
* Created by Christian Schabesberger on 02.08.16.
* <p>
2016-09-11 22:33:11 +00:00
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
2016-09-27 19:33:26 +00:00
* DownloadActivity.java is part of NewPipe.
* <p>
2016-09-11 22:33:11 +00: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>
2016-09-11 22:33:11 +00: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>
2016-09-11 22:33:11 +00:00
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2016-08-02 13:06:02 +00:00
*/
2016-08-01 23:26:12 +00:00
package org.schabi.newpipe;
import android.content.BroadcastReceiver;
import android.content.Context;
2020-07-14 17:21:32 +00:00
import android.content.Intent;
import android.content.IntentFilter;
2020-07-14 17:21:32 +00:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
2020-07-14 17:21:32 +00:00
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Spinner;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
2020-12-15 06:21:26 +00:00
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
2020-10-31 05:53:28 +00:00
import org.schabi.newpipe.databinding.ActivityMainBinding;
import org.schabi.newpipe.databinding.DrawerHeaderBinding;
import org.schabi.newpipe.databinding.DrawerLayoutBinding;
import org.schabi.newpipe.databinding.InstanceSpinnerLayoutBinding;
import org.schabi.newpipe.databinding.ToolbarLayoutBinding;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
2018-06-08 19:07:25 +00:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.fragments.BackPressable;
import org.schabi.newpipe.fragments.MainFragment;
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.fragments.list.search.SearchFragment;
2021-01-08 17:35:33 +00:00
import org.schabi.newpipe.player.Player;
2020-07-13 01:17:21 +00:00
import org.schabi.newpipe.player.event.OnKeyDownListener;
import org.schabi.newpipe.player.helper.PlayerHolder;
import org.schabi.newpipe.player.playqueue.PlayQueue;
import org.schabi.newpipe.report.ErrorActivity;
2020-07-14 17:21:32 +00:00
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.DeviceUtils;
2020-07-14 17:21:32 +00:00
import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PeertubeHelper;
import org.schabi.newpipe.util.PermissionHelper;
import org.schabi.newpipe.util.SerializedCache;
import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.StateSaver;
import org.schabi.newpipe.util.TLSSocketFactoryCompat;
import org.schabi.newpipe.util.ThemeHelper;
2019-09-23 07:17:03 +00:00
import org.schabi.newpipe.views.FocusOverlayView;
2018-07-08 15:46:21 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release");
2020-10-31 05:53:28 +00:00
private ActivityMainBinding mainBinding;
private DrawerHeaderBinding drawerHeaderBinding;
private DrawerLayoutBinding drawerLayoutBinding;
private ToolbarLayoutBinding toolbarLayoutBinding;
private ActionBarDrawerToggle toggle;
2016-08-02 13:06:02 +00:00
private boolean servicesShown = false;
private BroadcastReceiver broadcastReceiver;
private static final int ITEM_ID_SUBSCRIPTIONS = -1;
private static final int ITEM_ID_FEED = -2;
private static final int ITEM_ID_BOOKMARKS = -3;
private static final int ITEM_ID_DOWNLOADS = -4;
private static final int ITEM_ID_HISTORY = -5;
2018-06-16 11:11:45 +00:00
private static final int ITEM_ID_SETTINGS = 0;
private static final int ITEM_ID_ABOUT = 1;
private static final int ORDER = 0;
/*//////////////////////////////////////////////////////////////////////////
// Activity's LifeCycle
//////////////////////////////////////////////////////////////////////////*/
2016-08-01 23:26:12 +00:00
@Override
protected void onCreate(final Bundle savedInstanceState) {
if (DEBUG) {
Log.d(TAG, "onCreate() called with: "
+ "savedInstanceState = [" + savedInstanceState + "]");
}
// enable TLS1.1/1.2 for kitkat devices, to fix download and play for media.ccc.de sources
2019-12-04 11:49:47 +00:00
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
TLSSocketFactoryCompat.setAsDefault();
2019-12-04 11:49:47 +00:00
}
ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this));
assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState);
2020-10-31 05:53:28 +00:00
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
drawerLayoutBinding = mainBinding.drawerLayout;
drawerHeaderBinding = DrawerHeaderBinding.bind(drawerLayoutBinding.navigation
.getHeaderView(0));
toolbarLayoutBinding = mainBinding.toolbarLayout;
setContentView(mainBinding.getRoot());
2020-11-19 23:54:27 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
initFragments();
}
2020-10-31 05:53:28 +00:00
setSupportActionBar(toolbarLayoutBinding.toolbar);
2018-06-08 19:07:25 +00:00
try {
setupDrawer();
2020-08-16 08:24:58 +00:00
} catch (final Exception e) {
2018-06-08 19:07:25 +00:00
ErrorActivity.reportUiError(this, e);
}
2019-09-23 07:17:03 +00:00
2020-07-20 22:43:49 +00:00
if (DeviceUtils.isTv(this)) {
2019-09-23 07:17:03 +00:00
FocusOverlayView.setupFocusObserver(this);
}
openMiniPlayerUponPlayerStarted();
}
2018-06-08 19:07:25 +00:00
private void setupDrawer() throws Exception {
//Tabs
2020-08-16 08:24:58 +00:00
final int currentServiceId = ServiceHelper.getSelectedServiceId(this);
final StreamingService service = NewPipe.getService(currentServiceId);
2018-06-08 19:07:25 +00:00
int kioskId = 0;
for (final String ks : service.getKioskList().getAvailableKiosks()) {
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.add(R.id.menu_tabs_group, kioskId, 0, KioskTranslator
.getTranslatedKioskName(ks, this))
.setIcon(KioskTranslator.getKioskIcon(ks, this));
2020-05-30 06:14:54 +00:00
kioskId++;
}
2018-06-08 19:07:25 +00:00
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER,
R.string.tab_subscriptions)
2018-06-08 19:07:25 +00:00
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_feed_title)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_rss));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, R.string.tab_bookmarks)
2018-06-08 19:07:25 +00:00
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_file_download));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_history));
2018-06-08 19:07:25 +00:00
//Settings and About
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_settings));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_info_outline));
2018-06-08 19:07:25 +00:00
2020-10-31 05:53:28 +00:00
toggle = new ActionBarDrawerToggle(this, mainBinding.getRoot(),
toolbarLayoutBinding.toolbar, R.string.drawer_open, R.string.drawer_close);
2018-03-30 09:09:07 +00:00
toggle.syncState();
2020-10-31 05:53:28 +00:00
mainBinding.getRoot().addDrawerListener(toggle);
mainBinding.getRoot().addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
2018-03-30 09:09:07 +00:00
private int lastService;
@Override
public void onDrawerOpened(final View drawerView) {
2018-03-30 09:09:07 +00:00
lastService = ServiceHelper.getSelectedServiceId(MainActivity.this);
}
2018-03-30 09:09:07 +00:00
@Override
public void onDrawerClosed(final View drawerView) {
if (servicesShown) {
toggleServices();
}
2018-03-30 09:09:07 +00:00
if (lastService != ServiceHelper.getSelectedServiceId(MainActivity.this)) {
2020-12-15 06:21:26 +00:00
ActivityCompat.recreate(MainActivity.this);
}
2018-03-30 09:09:07 +00:00
}
});
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.setNavigationItemSelectedListener(this::drawerItemSelected);
2018-03-30 09:09:07 +00:00
setupDrawerHeader();
}
private boolean drawerItemSelected(final MenuItem item) {
2018-06-08 19:07:25 +00:00
switch (item.getGroupId()) {
case R.id.menu_services_group:
changeService(item);
break;
case R.id.menu_tabs_group:
try {
tabSelected(item);
2020-08-16 08:24:58 +00:00
} catch (final Exception e) {
2018-06-08 19:07:25 +00:00
ErrorActivity.reportUiError(this, e);
}
break;
case R.id.menu_options_about_group:
optionsAboutSelected(item);
break;
default:
return false;
}
2018-03-30 09:09:07 +00:00
2020-10-31 05:53:28 +00:00
mainBinding.getRoot().closeDrawers();
2018-06-08 19:07:25 +00:00
return true;
}
private void changeService(final MenuItem item) {
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.getItem(ServiceHelper.getSelectedServiceId(this))
.setChecked(false);
ServiceHelper.setSelectedServiceId(this, item.getItemId());
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.getItem(ServiceHelper.getSelectedServiceId(this))
.setChecked(true);
2018-02-16 22:56:04 +00:00
}
private void tabSelected(final MenuItem item) throws ExtractionException {
switch (item.getItemId()) {
2018-06-16 11:11:45 +00:00
case ITEM_ID_SUBSCRIPTIONS:
2018-06-08 19:07:25 +00:00
NavigationHelper.openSubscriptionFragment(getSupportFragmentManager());
break;
2018-06-16 11:11:45 +00:00
case ITEM_ID_FEED:
NavigationHelper.openFeedFragment(getSupportFragmentManager());
2018-06-08 19:07:25 +00:00
break;
2018-06-16 11:11:45 +00:00
case ITEM_ID_BOOKMARKS:
2018-06-08 19:07:25 +00:00
NavigationHelper.openBookmarksFragment(getSupportFragmentManager());
break;
2018-06-16 11:11:45 +00:00
case ITEM_ID_DOWNLOADS:
2018-06-08 19:07:25 +00:00
NavigationHelper.openDownloads(this);
break;
2018-06-16 11:11:45 +00:00
case ITEM_ID_HISTORY:
2018-06-08 19:07:25 +00:00
NavigationHelper.openStatisticFragment(getSupportFragmentManager());
break;
default:
2020-08-16 08:24:58 +00:00
final int currentServiceId = ServiceHelper.getSelectedServiceId(this);
final StreamingService service = NewPipe.getService(currentServiceId);
2018-06-08 19:07:25 +00:00
String serviceName = "";
2018-02-16 13:45:52 +00:00
2018-06-08 19:07:25 +00:00
int kioskId = 0;
for (final String ks : service.getKioskList().getAvailableKiosks()) {
if (kioskId == item.getItemId()) {
2018-06-08 19:07:25 +00:00
serviceName = ks;
}
kioskId++;
2018-06-08 19:07:25 +00:00
}
NavigationHelper.openKioskFragment(getSupportFragmentManager(), currentServiceId,
serviceName);
2018-06-08 19:07:25 +00:00
break;
}
}
private void optionsAboutSelected(final MenuItem item) {
switch (item.getItemId()) {
2018-06-16 11:11:45 +00:00
case ITEM_ID_SETTINGS:
2018-06-08 19:07:25 +00:00
NavigationHelper.openSettings(this);
break;
2018-06-16 11:11:45 +00:00
case ITEM_ID_ABOUT:
2018-06-08 19:07:25 +00:00
NavigationHelper.openAbout(this);
break;
}
2018-02-16 13:45:52 +00:00
}
2018-02-16 22:56:04 +00:00
private void setupDrawerHeader() {
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerHeaderActionButton.setOnClickListener(view -> toggleServices());
// If the current app name is bigger than the default "NewPipe" (7 chars),
// let the text view grow a little more as well.
if (getString(R.string.app_name).length() > "NewPipe".length()) {
2020-10-31 05:53:28 +00:00
final ViewGroup.LayoutParams layoutParams =
drawerHeaderBinding.drawerHeaderNewpipeTitle.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerHeaderNewpipeTitle.setLayoutParams(layoutParams);
drawerHeaderBinding.drawerHeaderNewpipeTitle.setMaxLines(2);
drawerHeaderBinding.drawerHeaderNewpipeTitle.setMinWidth(getResources()
.getDimensionPixelSize(R.dimen.drawer_header_newpipe_title_default_width));
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerHeaderNewpipeTitle.setMaxWidth(getResources()
.getDimensionPixelSize(R.dimen.drawer_header_newpipe_title_max_width));
}
2018-02-16 22:56:04 +00:00
}
private void toggleServices() {
servicesShown = !servicesShown;
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_services_group);
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_tabs_group);
drawerLayoutBinding.navigation.getMenu().removeGroup(R.id.menu_options_about_group);
if (servicesShown) {
showServices();
} else {
try {
showTabs();
2020-08-16 08:24:58 +00:00
} catch (final Exception e) {
ErrorActivity.reportUiError(this, e);
}
}
}
private void showServices() {
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerArrow.setImageResource(R.drawable.ic_arrow_drop_up_white_24dp);
2020-08-16 08:24:58 +00:00
for (final StreamingService s : NewPipe.getServices()) {
final String title = s.getServiceInfo().getName()
+ (ServiceHelper.isBeta(s) ? " (beta)" : "");
2020-10-31 05:53:28 +00:00
final MenuItem menuItem = drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_services_group, s.getServiceId(), ORDER, title)
.setIcon(ServiceHelper.getIcon(s.getServiceId()));
// peertube specifics
if (s.getServiceId() == 3) {
enhancePeertubeMenu(s, menuItem);
}
}
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.getItem(ServiceHelper.getSelectedServiceId(this))
.setChecked(true);
}
private void enhancePeertubeMenu(final StreamingService s, final MenuItem menuItem) {
2020-12-06 15:28:46 +00:00
final PeertubeInstance currentInstance = PeertubeHelper.getCurrentInstance();
menuItem.setTitle(currentInstance.getName() + (ServiceHelper.isBeta(s) ? " (beta)" : ""));
2020-10-31 05:53:28 +00:00
final Spinner spinner = InstanceSpinnerLayoutBinding.inflate(LayoutInflater.from(this))
.getRoot();
2020-08-16 08:24:58 +00:00
final List<PeertubeInstance> instances = PeertubeHelper.getInstanceList(this);
final List<String> items = new ArrayList<>();
int defaultSelect = 0;
2020-08-16 08:24:58 +00:00
for (final PeertubeInstance instance : instances) {
items.add(instance.getName());
2020-12-06 15:28:46 +00:00
if (instance.getUrl().equals(currentInstance.getUrl())) {
defaultSelect = items.size() - 1;
}
}
2020-08-16 08:24:58 +00:00
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.instance_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(defaultSelect, false);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(final AdapterView<?> parent, final View view,
final int position, final long id) {
2020-08-16 08:24:58 +00:00
final PeertubeInstance newInstance = instances.get(position);
if (newInstance.getUrl().equals(PeertubeHelper.getCurrentInstance().getUrl())) {
return;
}
PeertubeHelper.selectInstance(newInstance, getApplicationContext());
changeService(menuItem);
2020-10-31 05:53:28 +00:00
mainBinding.getRoot().closeDrawers();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
getSupportFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
recreate();
}, 300);
}
@Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
menuItem.setActionView(spinner);
}
private void showTabs() throws ExtractionException {
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerArrow.setImageResource(R.drawable.ic_arrow_drop_down_white_24dp);
//Tabs
2020-08-16 08:24:58 +00:00
final int currentServiceId = ServiceHelper.getSelectedServiceId(this);
final StreamingService service = NewPipe.getService(currentServiceId);
int kioskId = 0;
for (final String ks : service.getKioskList().getAvailableKiosks()) {
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.add(R.id.menu_tabs_group, kioskId, ORDER,
KioskTranslator.getTranslatedKioskName(ks, this))
.setIcon(KioskTranslator.getKioskIcon(ks, this));
kioskId++;
}
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, R.string.tab_subscriptions)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
.add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_feed_title)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_rss));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, R.string.tab_bookmarks)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_file_download));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_history));
//Settings and About
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_settings));
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu()
2018-06-16 11:11:45 +00:00
.add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_info_outline));
}
@Override
protected void onDestroy() {
super.onDestroy();
if (!isChangingConfigurations()) {
StateSaver.clearStateFiles();
}
2020-09-29 21:49:34 +00:00
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
}
}
2017-06-05 19:33:01 +00:00
@Override
protected void onResume() {
assureCorrectAppLanguage(this);
// Change the date format to match the selected language on resume
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
2017-06-05 19:33:01 +00:00
super.onResume();
// Close drawer on return, and don't show animation,
// so it looks like the drawer isn't open when the user returns to MainActivity
2020-10-31 05:53:28 +00:00
mainBinding.getRoot().closeDrawer(GravityCompat.START, false);
try {
final int selectedServiceId = ServiceHelper.getSelectedServiceId(this);
final String selectedServiceName = NewPipe.getService(selectedServiceId)
.getServiceInfo().getName();
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerHeaderServiceView.setText(selectedServiceName);
drawerHeaderBinding.drawerHeaderServiceIcon.setImageResource(ServiceHelper
.getIcon(selectedServiceId));
2020-10-31 05:53:28 +00:00
drawerHeaderBinding.drawerHeaderServiceView.post(() -> drawerHeaderBinding
.drawerHeaderServiceView.setSelected(true));
drawerHeaderBinding.drawerHeaderActionButton.setContentDescription(
getString(R.string.drawer_header_description) + selectedServiceName);
2020-08-16 08:24:58 +00:00
} catch (final Exception e) {
ErrorActivity.reportUiError(this, e);
}
2018-02-16 22:56:04 +00:00
2020-08-16 08:24:58 +00:00
final SharedPreferences sharedPreferences
= PreferenceManager.getDefaultSharedPreferences(this);
2017-06-05 19:33:01 +00:00
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
if (DEBUG) {
Log.d(TAG, "Theme has changed, recreating activity...");
}
2017-06-05 19:33:01 +00:00
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
2020-12-15 06:21:26 +00:00
ActivityCompat.recreate(this);
2017-06-05 19:33:01 +00:00
}
if (sharedPreferences.getBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false)) {
if (DEBUG) {
Log.d(TAG, "main page has changed, recreating main fragment...");
}
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
NavigationHelper.openMainActivity(this);
}
final boolean isHistoryEnabled = sharedPreferences.getBoolean(
getString(R.string.enable_watch_history_key), true);
2020-10-31 05:53:28 +00:00
drawerLayoutBinding.navigation.getMenu().findItem(ITEM_ID_HISTORY)
.setVisible(isHistoryEnabled);
2017-06-05 19:33:01 +00:00
}
@Override
protected void onNewIntent(final Intent intent) {
if (DEBUG) {
Log.d(TAG, "onNewIntent() called with: intent = [" + intent + "]");
}
if (intent != null) {
// Return if launched from a launcher (e.g. Nova Launcher, Pixel Launcher ...)
// to not destroy the already created backstack
2020-08-16 08:24:58 +00:00
final String action = intent.getAction();
if ((action != null && action.equals(Intent.ACTION_MAIN))
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)) {
return;
}
}
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
2020-07-13 01:17:21 +00:00
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
2020-07-14 17:21:32 +00:00
final Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.fragment_player_holder);
if (fragment instanceof OnKeyDownListener
&& !bottomSheetHiddenOrCollapsed()) {
// Provide keyDown event to fragment which then sends this event
// to the main player service
return ((OnKeyDownListener) fragment).onKeyDown(keyCode)
|| super.onKeyDown(keyCode, event);
2020-07-13 01:17:21 +00:00
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
if (DEBUG) {
Log.d(TAG, "onBackPressed() called");
}
2017-06-05 19:33:01 +00:00
2020-07-20 22:43:49 +00:00
if (DeviceUtils.isTv(this)) {
2020-10-31 05:53:28 +00:00
if (mainBinding.getRoot().isDrawerOpen(drawerLayoutBinding.navigation)) {
mainBinding.getRoot().closeDrawers();
return;
}
}
2020-07-13 01:17:21 +00:00
// In case bottomSheet is not visible on the screen or collapsed we can assume that the user
2020-07-14 17:21:32 +00:00
// interacts with a fragment inside fragment_holder so all back presses should be
// handled by it
2020-07-13 01:17:21 +00:00
if (bottomSheetHiddenOrCollapsed()) {
2020-07-14 17:21:32 +00:00
final Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.fragment_holder);
// If current fragment implements BackPressable (i.e. can/wanna handle back press)
// delegate the back press to it
if (fragment instanceof BackPressable) {
2020-07-14 17:21:32 +00:00
if (((BackPressable) fragment).onBackPressed()) {
return;
}
}
} else {
2020-07-14 17:21:32 +00:00
final Fragment fragmentPlayer = getSupportFragmentManager()
.findFragmentById(R.id.fragment_player_holder);
// If current fragment implements BackPressable (i.e. can/wanna handle back press)
// delegate the back press to it
if (fragmentPlayer instanceof BackPressable) {
2020-07-13 01:17:21 +00:00
if (!((BackPressable) fragmentPlayer).onBackPressed()) {
2020-10-31 05:53:28 +00:00
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
2020-07-14 17:21:32 +00:00
.setState(BottomSheetBehavior.STATE_COLLAPSED);
2020-07-13 01:17:21 +00:00
}
return;
}
}
2017-06-05 19:33:01 +00:00
2017-07-10 03:09:57 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
2016-08-02 13:06:02 +00:00
}
@Override
public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions,
@NonNull final int[] grantResults) {
2020-08-16 08:24:58 +00:00
for (final int i : grantResults) {
if (i == PackageManager.PERMISSION_DENIED) {
return;
}
}
switch (requestCode) {
case PermissionHelper.DOWNLOADS_REQUEST_CODE:
NavigationHelper.openDownloads(this);
break;
case PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE:
2020-08-16 08:24:58 +00:00
final Fragment fragment = getSupportFragmentManager()
2020-07-14 17:21:32 +00:00
.findFragmentById(R.id.fragment_player_holder);
if (fragment instanceof VideoDetailFragment) {
((VideoDetailFragment) fragment).openDownloadDialog();
}
break;
}
}
/**
* Implement the following diagram behavior for the up button:
* <pre>
* +---------------+
* | Main Screen +----+
* +-------+-------+ |
* | |
* Up | Search Button
* | |
* +----+-----+ |
* +------------+ Search |-----+
* | +----+-----+
* | Open |
* | something Up
* | |
* | +------------+-------------+
* | | |
* | | Video <-> Channel |
* +---| Channel <-> Playlist |
* | Video <-> .... |
* | |
* +--------------------------+
* </pre>
*/
private void onHomeButtonPressed() {
// If search fragment wasn't found in the backstack...
if (!NavigationHelper.tryGotoSearchFragment(getSupportFragmentManager())) {
// ...go to the main fragment
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
}
}
/*//////////////////////////////////////////////////////////////////////////
// Menu
//////////////////////////////////////////////////////////////////////////*/
2016-12-07 21:11:39 +00:00
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
if (DEBUG) {
Log.d(TAG, "onCreateOptionsMenu() called with: menu = [" + menu + "]");
}
2016-08-02 13:06:02 +00:00
super.onCreateOptionsMenu(menu);
2020-08-16 08:24:58 +00:00
final Fragment fragment
= getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
if (!(fragment instanceof SearchFragment)) {
2020-10-31 05:53:28 +00:00
toolbarLayoutBinding.toolbarSearchContainer.getRoot().setVisibility(View.GONE);
}
2020-08-16 08:24:58 +00:00
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
}
updateDrawerNavigation();
2016-08-02 13:06:02 +00:00
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (DEBUG) {
Log.d(TAG, "onOptionsItemSelected() called with: item = [" + item + "]");
}
2020-08-16 08:24:58 +00:00
final int id = item.getItemId();
2016-08-02 13:06:02 +00:00
2017-02-15 14:21:36 +00:00
switch (id) {
case android.R.id.home:
onHomeButtonPressed();
2016-08-02 13:06:02 +00:00
return true;
default:
return super.onOptionsItemSelected(item);
2016-08-02 13:06:02 +00:00
}
2016-08-01 23:26:12 +00:00
}
/*//////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////*/
private void initFragments() {
if (DEBUG) {
Log.d(TAG, "initFragments() called");
}
StateSaver.clearStateFiles();
2017-07-10 03:09:57 +00:00
if (getIntent() != null && getIntent().hasExtra(Constants.KEY_LINK_TYPE)) {
2020-07-14 17:21:32 +00:00
// When user watch a video inside popup and then tries to open the video in main player
// while the app is closed he will see a blank fragment on place of kiosk.
// Let's open it first
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
NavigationHelper.openMainFragment(getSupportFragmentManager());
2020-07-14 17:21:32 +00:00
}
handleIntent(getIntent());
} else {
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
}
}
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
private void updateDrawerNavigation() {
if (getSupportActionBar() == null) {
return;
}
final Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.fragment_holder);
if (fragment instanceof MainFragment) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
if (toggle != null) {
toggle.syncState();
2020-10-31 05:53:28 +00:00
toolbarLayoutBinding.toolbar.setNavigationOnClickListener(v -> mainBinding.getRoot()
.openDrawer(GravityCompat.START));
mainBinding.getRoot().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
}
} else {
2020-10-31 05:53:28 +00:00
mainBinding.getRoot().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2020-10-31 05:53:28 +00:00
toolbarLayoutBinding.toolbar.setNavigationOnClickListener(v -> onHomeButtonPressed());
}
}
private void handleIntent(final Intent intent) {
2018-07-08 15:46:21 +00:00
try {
if (DEBUG) {
Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]");
}
2018-07-08 15:46:21 +00:00
if (intent.hasExtra(Constants.KEY_LINK_TYPE)) {
2020-08-16 08:24:58 +00:00
final String url = intent.getStringExtra(Constants.KEY_URL);
final int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
String title = intent.getStringExtra(Constants.KEY_TITLE);
if (title == null) {
title = "";
}
final StreamingService.LinkType linkType = ((StreamingService.LinkType) intent
.getSerializableExtra(Constants.KEY_LINK_TYPE));
assert linkType != null;
switch (linkType) {
2018-07-08 15:46:21 +00:00
case STREAM:
final String intentCacheKey = intent.getStringExtra(
2021-01-08 17:35:33 +00:00
Player.PLAY_QUEUE_KEY);
2020-07-14 17:21:32 +00:00
final PlayQueue playQueue = intentCacheKey != null
? SerializedCache.getInstance()
.take(intentCacheKey, PlayQueue.class)
: null;
final boolean switchingPlayers = intent.getBooleanExtra(
VideoDetailFragment.KEY_SWITCHING_PLAYERS, false);
NavigationHelper.openVideoDetailFragment(
getApplicationContext(), getSupportFragmentManager(),
serviceId, url, title, playQueue, switchingPlayers);
2018-07-08 15:46:21 +00:00
break;
case CHANNEL:
NavigationHelper.openChannelFragment(getSupportFragmentManager(),
serviceId, url, title);
2018-07-08 15:46:21 +00:00
break;
case PLAYLIST:
NavigationHelper.openPlaylistFragment(getSupportFragmentManager(),
serviceId, url, title);
2018-07-08 15:46:21 +00:00
break;
}
} else if (intent.hasExtra(Constants.KEY_OPEN_SEARCH)) {
String searchString = intent.getStringExtra(Constants.KEY_SEARCH_STRING);
if (searchString == null) {
searchString = "";
}
2020-08-16 08:24:58 +00:00
final int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
2018-07-08 15:46:21 +00:00
NavigationHelper.openSearchFragment(
getSupportFragmentManager(),
serviceId,
searchString);
2018-07-08 15:46:21 +00:00
} else {
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
}
2020-08-16 08:24:58 +00:00
} catch (final Exception e) {
2018-07-08 15:46:21 +00:00
ErrorActivity.reportUiError(this, e);
}
}
private void openMiniPlayerIfMissing() {
final Fragment fragmentPlayer = getSupportFragmentManager()
.findFragmentById(R.id.fragment_player_holder);
if (fragmentPlayer == null) {
// We still don't have a fragment attached to the activity. It can happen when a user
// started popup or background players without opening a stream inside the fragment.
// Adding it in a collapsed state (only mini player will be visible).
NavigationHelper.showMiniPlayer(getSupportFragmentManager());
}
}
private void openMiniPlayerUponPlayerStarted() {
if (getIntent().getSerializableExtra(Constants.KEY_LINK_TYPE)
== StreamingService.LinkType.STREAM) {
// handleIntent() already takes care of opening video detail fragment
// due to an intent containing a STREAM link
return;
}
if (PlayerHolder.isPlayerOpen()) {
// if the player is already open, no need for a broadcast receiver
openMiniPlayerIfMissing();
} else {
// listen for player start intent being sent around
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
if (Objects.equals(intent.getAction(),
VideoDetailFragment.ACTION_PLAYER_STARTED)) {
openMiniPlayerIfMissing();
// At this point the player is added 100%, we can unregister. Other actions
// are useless since the fragment will not be removed after that.
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
}
};
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(VideoDetailFragment.ACTION_PLAYER_STARTED);
registerReceiver(broadcastReceiver, intentFilter);
}
}
2020-07-13 01:17:21 +00:00
private boolean bottomSheetHiddenOrCollapsed() {
2020-07-14 17:21:32 +00:00
final BottomSheetBehavior<FrameLayout> bottomSheetBehavior =
2020-10-31 05:53:28 +00:00
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder);
2020-07-13 01:17:21 +00:00
final int sheetState = bottomSheetBehavior.getState();
2020-07-14 17:21:32 +00:00
return sheetState == BottomSheetBehavior.STATE_HIDDEN
|| sheetState == BottomSheetBehavior.STATE_COLLAPSED;
2020-07-13 01:17:21 +00:00
}
2016-08-01 23:26:12 +00:00
}