2017-04-09 19:34:00 +02:00
|
|
|
/*
|
2016-08-02 15:06:02 +02:00
|
|
|
* Created by Christian Schabesberger on 02.08.16.
|
2017-04-09 19:34:00 +02:00
|
|
|
* <p>
|
2016-09-12 00:33:11 +02:00
|
|
|
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
|
2016-09-27 21:33:26 +02:00
|
|
|
* DownloadActivity.java is part of NewPipe.
|
2017-04-09 19:34:00 +02:00
|
|
|
* <p>
|
2016-09-12 00:33:11 +02:00
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2017-04-09 19:34:00 +02:00
|
|
|
* <p>
|
2016-09-12 00:33:11 +02:00
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-04-09 19:34:00 +02:00
|
|
|
* <p>
|
2016-09-12 00:33:11 +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/>.
|
2016-08-02 15:06:02 +02:00
|
|
|
*/
|
2016-08-02 01:26:12 +02:00
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2017-06-05 21:33:01 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-04-09 19:34:00 +02:00
|
|
|
import android.os.Bundle;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2017-06-05 21:33:01 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-08-12 06:50:25 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2017-04-09 19:34:00 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2017-04-26 21:32:04 +02:00
|
|
|
import android.support.v7.app.ActionBar;
|
2017-04-09 19:34:00 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-04-26 21:32:04 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.util.Log;
|
2017-04-09 19:34:00 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2017-04-26 21:32:04 +02:00
|
|
|
import android.view.View;
|
2017-04-09 19:34:00 +02:00
|
|
|
|
2017-08-12 06:50:25 +02:00
|
|
|
import org.schabi.newpipe.database.AppDatabase;
|
|
|
|
import org.schabi.newpipe.database.history.dao.HistoryDAO;
|
|
|
|
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO;
|
|
|
|
import org.schabi.newpipe.database.history.dao.WatchHistoryDAO;
|
|
|
|
import org.schabi.newpipe.database.history.model.HistoryEntry;
|
|
|
|
import org.schabi.newpipe.database.history.model.SearchHistoryEntry;
|
|
|
|
import org.schabi.newpipe.database.history.model.WatchHistoryEntry;
|
2017-04-09 19:34:00 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
|
|
|
import org.schabi.newpipe.fragments.BackPressable;
|
2017-04-09 19:34:00 +02:00
|
|
|
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.fragments.list.search.SearchFragment;
|
|
|
|
import org.schabi.newpipe.history.HistoryListener;
|
2017-04-09 19:34:00 +02:00
|
|
|
import org.schabi.newpipe.util.Constants;
|
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.util.StateSaver;
|
2017-04-09 19:34:00 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
|
|
|
|
2017-08-12 06:50:25 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
import io.reactivex.disposables.Disposable;
|
2017-08-12 06:50:25 +02:00
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
import io.reactivex.subjects.PublishSubject;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
public class MainActivity extends AppCompatActivity implements HistoryListener {
|
2017-08-12 06:50:25 +02:00
|
|
|
private static final String TAG = "MainActivity";
|
2017-09-03 08:04:18 +02:00
|
|
|
public static final boolean DEBUG = false;
|
2017-08-12 06:50:25 +02:00
|
|
|
private SharedPreferences sharedPreferences;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Activity's LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-08-02 01:26:12 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
|
2017-04-26 21:32:04 +02:00
|
|
|
ThemeHelper.setTheme(this);
|
2017-04-09 19:34:00 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2016-08-02 01:26:12 +02:00
|
|
|
setContentView(R.layout.activity_main);
|
2017-04-17 06:21:48 +02:00
|
|
|
|
|
|
|
if (getSupportFragmentManager() != null && getSupportFragmentManager().getBackStackEntryCount() == 0) {
|
|
|
|
initFragments();
|
|
|
|
}
|
2017-04-26 21:32:04 +02:00
|
|
|
|
2017-08-12 06:50:25 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
2017-04-26 21:32:04 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2017-08-12 06:50:25 +02:00
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
initHistory();
|
2017-08-12 06:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-09-03 08:04:18 +02:00
|
|
|
if (!isChangingConfigurations()) {
|
|
|
|
StateSaver.clearStateFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
disposeHistory();
|
2017-04-09 19:34:00 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 21:33:01 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2017-06-05 21:33:01 +02:00
|
|
|
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
|
|
|
|
if (DEBUG) Log.d(TAG, "Theme has changed, recreating activity...");
|
|
|
|
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
|
2017-09-03 08:04:18 +02:00
|
|
|
// https://stackoverflow.com/questions/10844112/runtimeexception-performing-pause-of-activity-that-is-not-resumed
|
|
|
|
// Briefly, let the activity resume properly posting the recreate call to end of the message queue
|
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
MainActivity.this.recreate();
|
|
|
|
}
|
|
|
|
});
|
2017-06-05 21:33:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onNewIntent() called with: intent = [" + intent + "]");
|
2017-04-17 06:21:48 +02:00
|
|
|
if (intent != null) {
|
|
|
|
// Return if launched from a launcher (e.g. Nova Launcher, Pixel Launcher ...)
|
|
|
|
// to not destroy the already created backstack
|
|
|
|
String action = intent.getAction();
|
2017-09-03 08:04:18 +02:00
|
|
|
if ((action != null && action.equals(Intent.ACTION_MAIN)) && intent.hasCategory(Intent.CATEGORY_LAUNCHER)) return;
|
2017-04-17 06:21:48 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
super.onNewIntent(intent);
|
2017-04-17 06:21:48 +02:00
|
|
|
setIntent(intent);
|
2017-04-09 19:34:00 +02:00
|
|
|
handleIntent(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onBackPressed() called");
|
2017-06-05 21:33:01 +02:00
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
2017-09-03 08:04:18 +02:00
|
|
|
// If current fragment implements BackPressable (i.e. can/wanna handle back press) delegate the back press to it
|
|
|
|
if (fragment instanceof BackPressable) {
|
|
|
|
if (((BackPressable) fragment).onBackPressed()) return;
|
|
|
|
}
|
2017-04-09 19:34:00 +02:00
|
|
|
|
2017-06-05 21:33:01 +02:00
|
|
|
|
2017-07-10 05:09:57 +02:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
|
|
|
finish();
|
|
|
|
} else super.onBackPressed();
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Menu
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-12-07 22:11:39 +01:00
|
|
|
@Override
|
2016-08-02 15:06:02 +02:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onCreateOptionsMenu() called with: menu = [" + menu + "]");
|
2016-08-02 15:06:02 +02:00
|
|
|
super.onCreateOptionsMenu(menu);
|
2017-04-26 21:32:04 +02:00
|
|
|
|
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
|
|
|
if (!(fragment instanceof VideoDetailFragment)) {
|
|
|
|
findViewById(R.id.toolbar).findViewById(R.id.toolbar_spinner).setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fragment instanceof SearchFragment)) {
|
|
|
|
findViewById(R.id.toolbar).findViewById(R.id.toolbar_search_container).setVisibility(View.GONE);
|
|
|
|
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.main_menu, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayShowTitleEnabled(false);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
|
|
|
}
|
2016-08-02 15:06:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2017-04-26 21:32:04 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "onOptionsItemSelected() called with: item = [" + item + "]");
|
2016-08-02 15:06:02 +02:00
|
|
|
int id = item.getItemId();
|
|
|
|
|
2017-02-15 15:21:36 +01:00
|
|
|
switch (id) {
|
2017-09-03 08:04:18 +02:00
|
|
|
case android.R.id.home:
|
2017-07-10 05:09:57 +02:00
|
|
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
2016-08-02 15:06:02 +02:00
|
|
|
return true;
|
2017-09-03 08:04:18 +02:00
|
|
|
case R.id.action_settings:
|
2017-07-04 09:31:53 +02:00
|
|
|
NavigationHelper.openSettings(this);
|
2016-08-02 15:06:02 +02:00
|
|
|
return true;
|
2017-09-03 08:04:18 +02:00
|
|
|
case R.id.action_show_downloads:
|
2017-07-04 09:31:53 +02:00
|
|
|
return NavigationHelper.openDownloads(this);
|
|
|
|
case R.id.action_about:
|
|
|
|
NavigationHelper.openAbout(this);
|
|
|
|
return true;
|
2017-08-12 06:50:25 +02:00
|
|
|
case R.id.action_history:
|
2017-09-03 08:04:18 +02:00
|
|
|
NavigationHelper.openHistory(this);
|
2017-08-12 06:50:25 +02:00
|
|
|
return true;
|
2016-08-02 15:06:02 +02:00
|
|
|
default:
|
2017-01-19 19:38:33 +01:00
|
|
|
return super.onOptionsItemSelected(item);
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
2016-08-02 01:26:12 +02:00
|
|
|
}
|
2017-04-09 19:34:00 +02:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void initFragments() {
|
2017-09-03 08:04:18 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "initFragments() called");
|
|
|
|
StateSaver.clearStateFiles();
|
2017-07-10 05:09:57 +02:00
|
|
|
if (getIntent() != null && getIntent().hasExtra(Constants.KEY_LINK_TYPE)) {
|
2017-04-09 19:34:00 +02:00
|
|
|
handleIntent(getIntent());
|
2017-07-10 05:09:57 +02:00
|
|
|
} else NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
2017-04-09 19:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent) {
|
2017-06-05 21:33:01 +02:00
|
|
|
if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]");
|
2017-04-26 21:32:04 +02:00
|
|
|
|
2017-04-09 19:34:00 +02:00
|
|
|
if (intent.hasExtra(Constants.KEY_LINK_TYPE)) {
|
|
|
|
String url = intent.getStringExtra(Constants.KEY_URL);
|
|
|
|
int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
|
2017-06-05 21:33:01 +02:00
|
|
|
String title = intent.getStringExtra(Constants.KEY_TITLE);
|
|
|
|
switch (((StreamingService.LinkType) intent.getSerializableExtra(Constants.KEY_LINK_TYPE))) {
|
|
|
|
case STREAM:
|
|
|
|
boolean autoPlay = intent.getBooleanExtra(VideoDetailFragment.AUTO_PLAY, false);
|
|
|
|
NavigationHelper.openVideoDetailFragment(getSupportFragmentManager(), serviceId, url, title, autoPlay);
|
|
|
|
break;
|
|
|
|
case CHANNEL:
|
|
|
|
NavigationHelper.openChannelFragment(getSupportFragmentManager(), serviceId, url, title);
|
|
|
|
break;
|
2017-09-03 08:04:18 +02:00
|
|
|
case PLAYLIST:
|
|
|
|
NavigationHelper.openPlaylistFragment(getSupportFragmentManager(), serviceId, url, title);
|
|
|
|
break;
|
2017-04-09 19:34:00 +02:00
|
|
|
}
|
2017-04-26 21:32:04 +02:00
|
|
|
} else if (intent.hasExtra(Constants.KEY_OPEN_SEARCH)) {
|
|
|
|
String searchQuery = intent.getStringExtra(Constants.KEY_QUERY);
|
|
|
|
if (searchQuery == null) searchQuery = "";
|
|
|
|
int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
|
2017-06-05 21:33:01 +02:00
|
|
|
NavigationHelper.openSearchFragment(getSupportFragmentManager(), serviceId, searchQuery);
|
2017-04-09 19:34:00 +02:00
|
|
|
} else {
|
2017-07-10 05:09:57 +02:00
|
|
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
2017-04-09 19:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-12 06:50:25 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// History
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private WatchHistoryDAO watchHistoryDAO;
|
|
|
|
private SearchHistoryDAO searchHistoryDAO;
|
|
|
|
private PublishSubject<HistoryEntry> historyEntrySubject;
|
|
|
|
private Disposable disposable;
|
|
|
|
|
|
|
|
private void initHistory() {
|
|
|
|
final AppDatabase database = NewPipeDatabase.getInstance();
|
|
|
|
watchHistoryDAO = database.watchHistoryDAO();
|
|
|
|
searchHistoryDAO = database.searchHistoryDAO();
|
|
|
|
historyEntrySubject = PublishSubject.create();
|
|
|
|
disposable = historyEntrySubject
|
|
|
|
.observeOn(Schedulers.io())
|
|
|
|
.subscribe(getHistoryEntryConsumer());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disposeHistory() {
|
|
|
|
if (disposable != null) disposable.dispose();
|
|
|
|
watchHistoryDAO = null;
|
|
|
|
searchHistoryDAO = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
private Consumer<HistoryEntry> getHistoryEntryConsumer() {
|
|
|
|
return new Consumer<HistoryEntry>() {
|
|
|
|
@Override
|
|
|
|
public void accept(HistoryEntry historyEntry) throws Exception {
|
|
|
|
//noinspection unchecked
|
|
|
|
HistoryDAO<HistoryEntry> historyDAO = (HistoryDAO<HistoryEntry>)
|
|
|
|
(historyEntry instanceof SearchHistoryEntry ? searchHistoryDAO : watchHistoryDAO);
|
|
|
|
|
|
|
|
HistoryEntry latestEntry = historyDAO.getLatestEntry();
|
|
|
|
if (historyEntry.hasEqualValues(latestEntry)) {
|
|
|
|
latestEntry.setCreationDate(historyEntry.getCreationDate());
|
|
|
|
historyDAO.update(latestEntry);
|
|
|
|
} else {
|
|
|
|
historyDAO.insert(historyEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-08-12 06:50:25 +02:00
|
|
|
|
|
|
|
private void addWatchHistoryEntry(StreamInfo streamInfo) {
|
|
|
|
if (sharedPreferences.getBoolean(getString(R.string.enable_watch_history_key), true)) {
|
|
|
|
WatchHistoryEntry entry = new WatchHistoryEntry(streamInfo);
|
|
|
|
historyEntrySubject.onNext(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-03 08:04:18 +02:00
|
|
|
public void onVideoPlayed(StreamInfo streamInfo, VideoStream videoStream) {
|
2017-08-12 06:50:25 +02:00
|
|
|
addWatchHistoryEntry(streamInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-03 08:04:18 +02:00
|
|
|
public void onAudioPlayed(StreamInfo streamInfo, AudioStream audioStream) {
|
2017-08-12 06:50:25 +02:00
|
|
|
addWatchHistoryEntry(streamInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSearch(int serviceId, String query) {
|
|
|
|
// Add search history entry
|
|
|
|
if (sharedPreferences.getBoolean(getString(R.string.enable_search_history_key), true)) {
|
|
|
|
SearchHistoryEntry searchHistoryEntry = new SearchHistoryEntry(new Date(), serviceId, query);
|
|
|
|
historyEntrySubject.onNext(searchHistoryEntry);
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 01:26:12 +02:00
|
|
|
}
|