2017-04-09 17:34:00 +00:00
|
|
|
/*
|
2016-08-02 13:06:02 +00:00
|
|
|
* Created by Christian Schabesberger on 02.08.16.
|
2017-04-09 17:34:00 +00:00
|
|
|
* <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.
|
2017-04-09 17:34:00 +00:00
|
|
|
* <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.
|
2017-04-09 17:34:00 +00:00
|
|
|
* <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.
|
2017-04-09 17:34:00 +00:00
|
|
|
* <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
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2021-10-12 14:44:17 +00:00
|
|
|
import static org.schabi.newpipe.CheckForNewAppVersion.startNewVersionCheckService;
|
2021-10-09 14:37:34 +00:00
|
|
|
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
|
|
|
|
2020-09-29 03:22:53 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2020-07-14 17:21:32 +00:00
|
|
|
import android.content.Intent;
|
2020-09-29 03:22:53 +00:00
|
|
|
import android.content.IntentFilter;
|
2020-07-14 17:21:32 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-04-06 21:19:45 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2018-06-13 23:55:55 +00:00
|
|
|
import android.os.Build;
|
2017-04-09 17:34:00 +00:00
|
|
|
import android.os.Bundle;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2017-04-26 19:32:04 +00:00
|
|
|
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;
|
2021-01-15 16:11:04 +00:00
|
|
|
|
2019-10-08 04:36:29 +00:00
|
|
|
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;
|
2019-10-08 04:36:29 +00:00
|
|
|
import androidx.core.view.GravityCompat;
|
|
|
|
import androidx.drawerlayout.widget.DrawerLayout;
|
|
|
|
import androidx.fragment.app.Fragment;
|
2019-11-24 15:38:06 +00:00
|
|
|
import androidx.fragment.app.FragmentManager;
|
2020-10-18 18:19:50 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2021-01-15 16:11:04 +00:00
|
|
|
|
2019-12-29 21:15:01 +00:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetBehavior;
|
2021-01-15 16:11:04 +00:00
|
|
|
|
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;
|
2020-12-11 14:07:18 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorActivity;
|
2018-02-19 20:04:13 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2017-04-09 17:34:00 +00:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2018-06-08 19:07:25 +00:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2019-11-24 15:38:06 +00:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.fragments.BackPressable;
|
2018-01-30 07:01:57 +00:00
|
|
|
import org.schabi.newpipe.fragments.MainFragment;
|
2017-04-09 17:34:00 +00:00
|
|
|
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.fragments.list.search.SearchFragment;
|
2021-07-20 10:20:51 +00:00
|
|
|
import org.schabi.newpipe.local.feed.notifications.NotificationWorker;
|
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;
|
2020-11-01 20:45:39 +00:00
|
|
|
import org.schabi.newpipe.player.helper.PlayerHolder;
|
2019-12-29 21:15:01 +00:00
|
|
|
import org.schabi.newpipe.player.playqueue.PlayQueue;
|
2020-07-14 17:21:32 +00:00
|
|
|
import org.schabi.newpipe.util.Constants;
|
2020-10-18 18:19:50 +00:00
|
|
|
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
|
|
|
|
2021-01-15 16:11:04 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2018-01-27 05:34:17 +00:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2017-08-12 04:50:25 +00:00
|
|
|
private static final String TAG = "MainActivity";
|
2021-06-04 23:17:10 +00:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
2018-01-30 07:01:57 +00:00
|
|
|
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;
|
|
|
|
|
2020-03-22 19:26:53 +00:00
|
|
|
private ActionBarDrawerToggle toggle;
|
2016-08-02 13:06:02 +00:00
|
|
|
|
2018-06-14 01:29:49 +00:00
|
|
|
private boolean servicesShown = false;
|
|
|
|
|
2020-09-29 03:22:53 +00:00
|
|
|
private BroadcastReceiver broadcastReceiver;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
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;
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Activity's LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-08-01 23:26:12 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onCreate() called with: "
|
|
|
|
+ "savedInstanceState = [" + savedInstanceState + "]");
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
|
2020-12-23 18:58:02 +00:00
|
|
|
// 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) {
|
2019-12-08 16:09:16 +00:00
|
|
|
TLSSocketFactoryCompat.setAsDefault();
|
2019-12-04 11:49:47 +00:00
|
|
|
}
|
2021-03-26 14:12:20 +00:00
|
|
|
|
|
|
|
ThemeHelper.setDayNightMode(this);
|
2018-01-30 07:01:57 +00:00
|
|
|
ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this));
|
|
|
|
|
2020-01-28 19:48:42 +00:00
|
|
|
assureCorrectAppLanguage(this);
|
2017-04-09 17:34:00 +00:00
|
|
|
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());
|
2017-04-17 04:21:48 +00:00
|
|
|
|
2020-11-19 23:54:27 +00:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
|
2017-04-17 04:21:48 +00:00
|
|
|
initFragments();
|
|
|
|
}
|
2017-04-26 19:32:04 +00:00
|
|
|
|
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) {
|
2021-01-09 15:33:59 +00:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Setting up drawer", e);
|
2018-06-08 19:07:25 +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);
|
|
|
|
}
|
2020-11-01 20:45:39 +00:00
|
|
|
openMiniPlayerUponPlayerStarted();
|
2021-10-12 14:44:17 +00:00
|
|
|
|
2021-11-02 22:22:59 +00:00
|
|
|
// schedule worker for checking for new streams and creating corresponding notifications
|
2019-05-08 17:17:54 +00:00
|
|
|
NotificationWorker.schedule(this);
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 21:55:14 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onPostCreate(savedInstanceState);
|
2021-10-23 19:09:53 +00:00
|
|
|
|
|
|
|
final App app = App.getApp();
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
|
|
|
|
|
|
|
|
if (prefs.getBoolean(app.getString(R.string.update_app_key), true)) {
|
|
|
|
// Start the service which is checking all conditions
|
|
|
|
// and eventually searching for a new version.
|
|
|
|
// The service searching for a new NewPipe version must not be started in background.
|
|
|
|
startNewVersionCheckService();
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 14:37:34 +00:00
|
|
|
private void setupDrawer() throws ExtractionException {
|
|
|
|
addDrawerMenuForCurrentService();
|
|
|
|
|
|
|
|
toggle = new ActionBarDrawerToggle(this, mainBinding.getRoot(),
|
|
|
|
toolbarLayoutBinding.toolbar, R.string.drawer_open, R.string.drawer_close);
|
|
|
|
toggle.syncState();
|
|
|
|
mainBinding.getRoot().addDrawerListener(toggle);
|
|
|
|
mainBinding.getRoot().addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
|
|
|
|
private int lastService;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDrawerOpened(final View drawerView) {
|
|
|
|
lastService = ServiceHelper.getSelectedServiceId(MainActivity.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDrawerClosed(final View drawerView) {
|
|
|
|
if (servicesShown) {
|
|
|
|
toggleServices();
|
|
|
|
}
|
|
|
|
if (lastService != ServiceHelper.getSelectedServiceId(MainActivity.this)) {
|
|
|
|
ActivityCompat.recreate(MainActivity.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
drawerLayoutBinding.navigation.setNavigationItemSelectedListener(this::drawerItemSelected);
|
|
|
|
setupDrawerHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the drawer menu for the current service.
|
|
|
|
*
|
|
|
|
* @throws ExtractionException
|
|
|
|
*/
|
|
|
|
private void addDrawerMenuForCurrentService() throws ExtractionException {
|
2018-06-08 19:07:25 +00:00
|
|
|
//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()
|
2020-03-31 17:20:15 +00:00
|
|
|
.add(R.id.menu_tabs_group, kioskId, 0, KioskTranslator
|
|
|
|
.getTranslatedKioskName(ks, this))
|
2020-03-31 07:36:35 +00:00
|
|
|
.setIcon(KioskTranslator.getKioskIcon(ks, this));
|
2020-05-30 06:14:54 +00:00
|
|
|
kioskId++;
|
2018-06-14 01:29:49 +00:00
|
|
|
}
|
2018-06-08 19:07:25 +00:00
|
|
|
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerLayoutBinding.navigation.getMenu()
|
2020-03-31 17:20:15 +00:00
|
|
|
.add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER,
|
|
|
|
R.string.tab_subscriptions)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.ic_tv);
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerLayoutBinding.navigation.getMenu()
|
2019-10-11 04:09:28 +00:00
|
|
|
.add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_feed_title)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.ic_rss_feed);
|
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)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.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)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.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)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.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)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.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)
|
2021-03-27 14:45:49 +00:00
|
|
|
.setIcon(R.drawable.ic_info_outline);
|
2017-08-12 04:50:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
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) {
|
2021-01-09 15:33:59 +00:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Selecting main page tab", e);
|
2018-06-08 19:07:25 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private void changeService(final MenuItem item) {
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerLayoutBinding.navigation.getMenu()
|
|
|
|
.getItem(ServiceHelper.getSelectedServiceId(this))
|
2020-03-31 17:20:15 +00:00
|
|
|
.setChecked(false);
|
2018-06-15 05:21:30 +00:00
|
|
|
ServiceHelper.setSelectedServiceId(this, item.getItemId());
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerLayoutBinding.navigation.getMenu()
|
|
|
|
.getItem(ServiceHelper.getSelectedServiceId(this))
|
2020-03-31 17:20:15 +00:00
|
|
|
.setChecked(true);
|
2018-02-16 22:56:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +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:
|
2019-04-28 20:43:54 +00:00
|
|
|
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()) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (kioskId == item.getItemId()) {
|
2018-06-08 19:07:25 +00:00
|
|
|
serviceName = ks;
|
|
|
|
}
|
2020-03-31 17:20:15 +00:00
|
|
|
kioskId++;
|
2018-06-08 19:07:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
NavigationHelper.openKioskFragment(getSupportFragmentManager(), currentServiceId,
|
|
|
|
serviceName);
|
2018-06-08 19:07:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
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());
|
2020-04-10 13:31:28 +00:00
|
|
|
|
|
|
|
// 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();
|
2020-04-10 13:31:28 +00:00
|
|
|
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()
|
2020-04-10 13:31:28 +00:00
|
|
|
.getDimensionPixelSize(R.dimen.drawer_header_newpipe_title_default_width));
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerHeaderBinding.drawerHeaderNewpipeTitle.setMaxWidth(getResources()
|
2020-04-10 13:31:28 +00:00
|
|
|
.getDimensionPixelSize(R.dimen.drawer_header_newpipe_title_max_width));
|
|
|
|
}
|
2018-02-16 22:56:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 01:29:49 +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);
|
2018-06-14 01:29:49 +00:00
|
|
|
|
2021-10-09 14:37:34 +00:00
|
|
|
// Show up or down arrow
|
|
|
|
drawerHeaderBinding.drawerArrow.setImageResource(
|
|
|
|
servicesShown ? R.drawable.ic_arrow_drop_up : R.drawable.ic_arrow_drop_down);
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
if (servicesShown) {
|
2018-06-14 01:29:49 +00:00
|
|
|
showServices();
|
|
|
|
} else {
|
|
|
|
try {
|
2021-10-09 14:37:34 +00:00
|
|
|
addDrawerMenuForCurrentService();
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final Exception e) {
|
2021-01-09 15:33:59 +00:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Showing main page tabs", e);
|
2018-06-14 01:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showServices() {
|
2020-08-16 08:24:58 +00:00
|
|
|
for (final StreamingService s : NewPipe.getServices()) {
|
2020-03-31 17:20:15 +00:00
|
|
|
final String title = s.getServiceInfo().getName()
|
|
|
|
+ (ServiceHelper.isBeta(s) ? " (beta)" : "");
|
2018-06-14 01:29:49 +00:00
|
|
|
|
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)
|
2018-06-14 01:29:49 +00:00
|
|
|
.setIcon(ServiceHelper.getIcon(s.getServiceId()));
|
2019-11-24 15:38:06 +00:00
|
|
|
|
|
|
|
// peertube specifics
|
2020-03-31 17:20:15 +00:00
|
|
|
if (s.getServiceId() == 3) {
|
2019-11-24 15:38:06 +00:00
|
|
|
enhancePeertubeMenu(s, menuItem);
|
|
|
|
}
|
2018-06-14 01:29:49 +00:00
|
|
|
}
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerLayoutBinding.navigation.getMenu()
|
|
|
|
.getItem(ServiceHelper.getSelectedServiceId(this))
|
2020-03-31 17:20:15 +00:00
|
|
|
.setChecked(true);
|
2018-06-14 01:29:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
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<>();
|
2019-11-24 15:38:06 +00:00
|
|
|
int defaultSelect = 0;
|
2020-08-16 08:24:58 +00:00
|
|
|
for (final PeertubeInstance instance : instances) {
|
2019-11-24 15:38:06 +00:00
|
|
|
items.add(instance.getName());
|
2020-12-06 15:28:46 +00:00
|
|
|
if (instance.getUrl().equals(currentInstance.getUrl())) {
|
2020-03-31 17:20:15 +00:00
|
|
|
defaultSelect = items.size() - 1;
|
2019-11-24 15:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 08:24:58 +00:00
|
|
|
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
|
2020-03-31 17:20:15 +00:00
|
|
|
R.layout.instance_spinner_item, items);
|
2019-11-24 15:38:06 +00:00
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
spinner.setAdapter(adapter);
|
|
|
|
spinner.setSelection(defaultSelect, false);
|
|
|
|
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
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);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (newInstance.getUrl().equals(PeertubeHelper.getCurrentInstance().getUrl())) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-24 15:38:06 +00:00
|
|
|
PeertubeHelper.selectInstance(newInstance, getApplicationContext());
|
|
|
|
changeService(menuItem);
|
2020-10-31 05:53:28 +00:00
|
|
|
mainBinding.getRoot().closeDrawers();
|
2019-11-24 15:38:06 +00:00
|
|
|
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
2020-03-31 17:20:15 +00:00
|
|
|
getSupportFragmentManager().popBackStack(null,
|
|
|
|
FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
2021-07-21 23:31:41 +00:00
|
|
|
ActivityCompat.recreate(MainActivity.this);
|
2019-11-24 15:38:06 +00:00
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onNothingSelected(final AdapterView<?> parent) {
|
2019-11-24 15:38:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menuItem.setActionView(spinner);
|
|
|
|
}
|
|
|
|
|
2017-08-12 04:50:25 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-09-03 06:04:18 +00:00
|
|
|
if (!isChangingConfigurations()) {
|
|
|
|
StateSaver.clearStateFiles();
|
|
|
|
}
|
2020-09-29 21:49:34 +00:00
|
|
|
if (broadcastReceiver != null) {
|
|
|
|
unregisterReceiver(broadcastReceiver);
|
|
|
|
}
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:33:01 +00:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
2020-01-28 19:48:42 +00:00
|
|
|
assureCorrectAppLanguage(this);
|
2020-03-31 17:20:15 +00:00
|
|
|
// Change the date format to match the selected language on resume
|
2021-01-06 13:48:34 +00:00
|
|
|
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
|
2017-06-05 19:33:01 +00:00
|
|
|
super.onResume();
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
// 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);
|
2018-02-19 20:04:13 +00:00
|
|
|
try {
|
2020-03-22 19:26:53 +00:00
|
|
|
final int selectedServiceId = ServiceHelper.getSelectedServiceId(this);
|
2020-03-31 17:20:15 +00:00
|
|
|
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-03-22 19:26:53 +00:00
|
|
|
|
2020-10-31 05:53:28 +00:00
|
|
|
drawerHeaderBinding.drawerHeaderServiceView.post(() -> drawerHeaderBinding
|
|
|
|
.drawerHeaderServiceView.setSelected(true));
|
|
|
|
drawerHeaderBinding.drawerHeaderActionButton.setContentDescription(
|
2019-09-22 09:59:50 +00:00
|
|
|
getString(R.string.drawer_header_description) + selectedServiceName);
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final Exception e) {
|
2021-01-09 15:33:59 +00:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Setting up service toggle", e);
|
2018-02-19 20:04:13 +00:00
|
|
|
}
|
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)) {
|
2020-03-31 17:20:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-30 07:01:57 +00:00
|
|
|
if (sharedPreferences.getBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false)) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "main page has changed, recreating main fragment...");
|
|
|
|
}
|
2017-10-13 11:49:31 +00:00
|
|
|
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
|
|
|
|
NavigationHelper.openMainActivity(this);
|
|
|
|
}
|
2020-01-14 18:30:36 +00:00
|
|
|
|
2020-02-22 10:34:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected void onNewIntent(final Intent intent) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onNewIntent() called with: intent = [" + intent + "]");
|
|
|
|
}
|
2017-04-17 04:21:48 +00:00
|
|
|
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();
|
2020-03-31 17:20:15 +00:00
|
|
|
if ((action != null && action.equals(Intent.ACTION_MAIN))
|
|
|
|
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 04:21:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
super.onNewIntent(intent);
|
2017-04-17 04:21:48 +00:00
|
|
|
setIntent(intent);
|
2017-04-09 17:34:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
2020-03-31 17:20:15 +00:00
|
|
|
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();
|
2019-09-20 09:07:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-04-09 17:34:00 +00:00
|
|
|
|
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
|
2019-12-29 21:15:01 +00:00
|
|
|
if (fragment instanceof BackPressable) {
|
2020-07-14 17:21:32 +00:00
|
|
|
if (((BackPressable) fragment).onBackPressed()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-29 21:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} 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
|
2019-12-29 21:15:01 +00:00
|
|
|
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
|
|
|
}
|
2019-12-29 21:15:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 19:33:01 +00:00
|
|
|
|
2017-07-10 03:09:57 +00:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
|
|
|
finish();
|
2020-03-31 17:20:15 +00:00
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
2016-08-02 13:06:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 21:19:45 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onRequestPermissionsResult(final int requestCode,
|
|
|
|
@NonNull final String[] permissions,
|
|
|
|
@NonNull final int[] grantResults) {
|
2021-06-16 02:08:41 +00:00
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
2020-08-16 08:24:58 +00:00
|
|
|
for (final int i : grantResults) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (i == PackageManager.PERMISSION_DENIED) {
|
2018-04-07 17:32:02 +00:00
|
|
|
return;
|
2018-04-06 21:19:45 +00:00
|
|
|
}
|
2018-04-07 17:32:02 +00:00
|
|
|
}
|
|
|
|
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);
|
2018-04-07 17:32:02 +00:00
|
|
|
if (fragment instanceof VideoDetailFragment) {
|
|
|
|
((VideoDetailFragment) fragment).openDownloadDialog();
|
|
|
|
}
|
|
|
|
break;
|
2018-04-06 21:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:03:55 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2018-01-30 07:01:57 +00:00
|
|
|
private void onHomeButtonPressed() {
|
2018-02-03 11:03:55 +00:00
|
|
|
// If search fragment wasn't found in the backstack...
|
|
|
|
if (!NavigationHelper.tryGotoSearchFragment(getSupportFragmentManager())) {
|
|
|
|
// ...go to the main fragment
|
|
|
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Menu
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-12-07 21:11:39 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
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);
|
2017-04-26 19:32:04 +00:00
|
|
|
|
2020-08-16 08:24:58 +00:00
|
|
|
final Fragment fragment
|
|
|
|
= getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
2017-04-26 19:32:04 +00:00
|
|
|
if (!(fragment instanceof SearchFragment)) {
|
2020-10-31 05:53:28 +00:00
|
|
|
toolbarLayoutBinding.toolbarSearchContainer.getRoot().setVisibility(View.GONE);
|
2017-04-26 19:32:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 08:24:58 +00:00
|
|
|
final ActionBar actionBar = getSupportActionBar();
|
2017-04-26 19:32:04 +00:00
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
|
|
|
|
updateDrawerNavigation();
|
|
|
|
|
2016-08-02 13:06:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-12-11 14:07:18 +00:00
|
|
|
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onOptionsItemSelected() called with: item = [" + item + "]");
|
|
|
|
}
|
2016-08-02 13:06:02 +00:00
|
|
|
|
2020-12-11 14:07:18 +00:00
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
onHomeButtonPressed();
|
|
|
|
return true;
|
2016-08-02 13:06:02 +00:00
|
|
|
}
|
2020-12-11 14:07:18 +00:00
|
|
|
return super.onOptionsItemSelected(item);
|
2016-08-01 23:26:12 +00:00
|
|
|
}
|
2017-04-09 17:34:00 +00:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void initFragments() {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "initFragments() called");
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
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) {
|
2019-12-29 21:15:01 +00:00
|
|
|
NavigationHelper.openMainFragment(getSupportFragmentManager());
|
2020-07-14 17:21:32 +00:00
|
|
|
}
|
2019-12-29 21:15:01 +00:00
|
|
|
|
2017-04-09 17:34:00 +00:00
|
|
|
handleIntent(getIntent());
|
2020-03-31 17:20:15 +00:00
|
|
|
} else {
|
|
|
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
|
|
|
}
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2018-01-30 07:01:57 +00:00
|
|
|
private void updateDrawerNavigation() {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (getSupportActionBar() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-30 07:01:57 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
final Fragment fragment = getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.fragment_holder);
|
2018-01-30 07:01:57 +00:00
|
|
|
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);
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-31 05:53:28 +00:00
|
|
|
mainBinding.getRoot().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
|
2018-01-30 07:01:57 +00:00
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
2020-10-31 05:53:28 +00:00
|
|
|
toolbarLayoutBinding.toolbar.setNavigationOnClickListener(v -> onHomeButtonPressed());
|
2018-01-30 07:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
private void handleIntent(final Intent intent) {
|
2018-07-08 15:46:21 +00:00
|
|
|
try {
|
2020-03-31 17:20:15 +00:00
|
|
|
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);
|
2020-10-18 18:19:50 +00:00
|
|
|
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:
|
2020-10-18 18:19:50 +00:00
|
|
|
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;
|
2020-10-18 18:19:50 +00:00
|
|
|
|
|
|
|
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(),
|
2020-10-18 18:19:50 +00:00
|
|
|
serviceId, url, title);
|
2018-07-08 15:46:21 +00:00
|
|
|
break;
|
|
|
|
case PLAYLIST:
|
|
|
|
NavigationHelper.openPlaylistFragment(getSupportFragmentManager(),
|
2020-10-18 18:19:50 +00:00
|
|
|
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);
|
2020-03-31 17:20:15 +00:00
|
|
|
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,
|
2018-07-10 14:26:42 +00:00
|
|
|
searchString);
|
2018-07-08 15:46:21 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final Exception e) {
|
2021-01-09 15:33:59 +00:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Handling intent", e);
|
2017-04-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 03:22:53 +00:00
|
|
|
|
2020-11-01 20:45:39 +00:00
|
|
|
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() {
|
2020-11-02 17:06:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-24 08:00:56 +00:00
|
|
|
if (PlayerHolder.getInstance().isPlayerOpen()) {
|
2020-11-02 17:06:14 +00:00
|
|
|
// if the player is already open, no need for a broadcast receiver
|
2020-11-01 20:45:39 +00:00
|
|
|
openMiniPlayerIfMissing();
|
|
|
|
} else {
|
2020-11-02 17:06:14 +00:00
|
|
|
// listen for player start intent being sent around
|
2020-11-01 20:45:39 +00:00
|
|
|
broadcastReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
2020-11-02 17:06:14 +00:00
|
|
|
if (Objects.equals(intent.getAction(),
|
|
|
|
VideoDetailFragment.ACTION_PLAYER_STARTED)) {
|
2020-11-01 20:45:39 +00:00
|
|
|
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;
|
2020-09-29 03:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-01 20:45:39 +00:00
|
|
|
};
|
|
|
|
final IntentFilter intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction(VideoDetailFragment.ACTION_PLAYER_STARTED);
|
|
|
|
registerReceiver(broadcastReceiver, intentFilter);
|
|
|
|
}
|
2020-09-29 03:22:53 +00:00
|
|
|
}
|
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
|
|
|
}
|