2017-04-09 14:34:00 -03:00
|
|
|
/*
|
2016-08-02 15:06:02 +02:00
|
|
|
* Created by Christian Schabesberger on 02.08.16.
|
2017-04-09 14:34:00 -03: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 14:34:00 -03: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 14:34:00 -03: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 14:34:00 -03: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 14:34:00 -03:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
2016-08-02 01:26:12 +02:00
|
|
|
|
2017-04-09 14:34:00 -03:00
|
|
|
import org.schabi.newpipe.download.DownloadActivity;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.fragments.OnItemSelectedListener;
|
|
|
|
import org.schabi.newpipe.fragments.channel.ChannelFragment;
|
|
|
|
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
|
|
|
import org.schabi.newpipe.fragments.search.SearchFragment;
|
|
|
|
import org.schabi.newpipe.settings.SettingsActivity;
|
|
|
|
import org.schabi.newpipe.util.Constants;
|
|
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
|
|
|
import org.schabi.newpipe.util.PermissionHelper;
|
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
|
2017-02-15 15:21:36 +01:00
|
|
|
private static final String TAG = MainActivity.class.toString();
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2017-04-09 14:34:00 -03:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Activity's LifeCycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-08-02 01:26:12 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2017-02-27 17:25:15 +05:30
|
|
|
ThemeHelper.setTheme(this, true);
|
2017-04-09 14:34:00 -03:00
|
|
|
super.onCreate(savedInstanceState);
|
2016-08-02 01:26:12 +02:00
|
|
|
setContentView(R.layout.activity_main);
|
2016-08-02 15:06:02 +02:00
|
|
|
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
2017-04-09 14:34:00 -03:00
|
|
|
if (savedInstanceState == null) initFragments();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
handleIntent(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
|
|
|
if (fragment instanceof VideoDetailFragment) if (((VideoDetailFragment) fragment).onActivityBackPressed()) return;
|
|
|
|
|
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() >= 2) {
|
|
|
|
getSupportFragmentManager().popBackStackImmediate();
|
|
|
|
} else {
|
|
|
|
if (fragment instanceof SearchFragment) {
|
|
|
|
SearchFragment searchFragment = (SearchFragment) fragment;
|
|
|
|
if (!searchFragment.isMainBgVisible()) {
|
|
|
|
getSupportFragmentManager().beginTransaction().remove(fragment).commitNow();
|
|
|
|
NavigationHelper.openMainActivity(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-09 14:34:00 -03:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Menu
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2016-12-07 22:11:39 +01:00
|
|
|
@Override
|
2016-08-02 15:06:02 +02:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
super.onCreateOptionsMenu(menu);
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.main_menu, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
2017-02-15 15:21:36 +01:00
|
|
|
switch (id) {
|
2016-08-02 15:06:02 +02:00
|
|
|
case android.R.id.home: {
|
2017-04-09 14:34:00 -03:00
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
|
|
|
if (fragment instanceof VideoDetailFragment) ((VideoDetailFragment) fragment).clearHistory();
|
|
|
|
|
|
|
|
NavigationHelper.openMainActivity(this);
|
2016-08-02 15:06:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_settings: {
|
|
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_show_downloads: {
|
2017-02-15 15:21:36 +01:00
|
|
|
if (!PermissionHelper.checkStoragePermissions(this)) {
|
2017-01-10 11:41:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-15 15:21:36 +01:00
|
|
|
Intent intent = new Intent(this, DownloadActivity.class);
|
2016-08-02 15:06:02 +02:00
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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 14:34:00 -03:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void initFragments() {
|
|
|
|
if (getIntent() != null && getIntent().hasExtra(Constants.KEY_URL)) {
|
|
|
|
handleIntent(getIntent());
|
|
|
|
} else openSearchFragment();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// OnItemSelectedListener
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(StreamingService.LinkType linkType, int serviceId, String url, String name) {
|
|
|
|
switch (linkType) {
|
|
|
|
case STREAM:
|
|
|
|
openVideoDetailFragment(serviceId, url, name, false);
|
|
|
|
break;
|
|
|
|
case CHANNEL:
|
|
|
|
openChannelFragment(serviceId, url, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent) {
|
|
|
|
if (intent.hasExtra(Constants.KEY_LINK_TYPE)) {
|
|
|
|
String url = intent.getStringExtra(Constants.KEY_URL);
|
|
|
|
int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
|
|
|
|
try {
|
|
|
|
switch (((StreamingService.LinkType) intent.getSerializableExtra(Constants.KEY_LINK_TYPE))) {
|
|
|
|
case STREAM:
|
|
|
|
handleVideoDetailIntent(serviceId, url, intent);
|
|
|
|
break;
|
|
|
|
case CHANNEL:
|
|
|
|
handleChannelIntent(serviceId, url, intent);
|
|
|
|
break;
|
|
|
|
case NONE:
|
|
|
|
throw new Exception("Url not known to service. service=" + Integer.toString(serviceId) + " url=" + url);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
|
|
|
openSearchFragment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void openSearchFragment() {
|
|
|
|
ImageLoader.getInstance().clearMemoryCache();
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out)
|
|
|
|
.replace(R.id.fragment_holder, new SearchFragment())
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void openVideoDetailFragment(int serviceId, String url, String title, boolean autoPlay) {
|
|
|
|
ImageLoader.getInstance().clearMemoryCache();
|
|
|
|
|
|
|
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
|
|
|
if (title == null) title = "";
|
|
|
|
|
|
|
|
if (fragment instanceof VideoDetailFragment && fragment.isVisible()) {
|
|
|
|
VideoDetailFragment detailFragment = (VideoDetailFragment) fragment;
|
|
|
|
detailFragment.setAutoplay(autoPlay);
|
|
|
|
detailFragment.selectAndLoadVideo(serviceId, url, title);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoDetailFragment instance = VideoDetailFragment.getInstance(serviceId, url, title);
|
|
|
|
instance.setAutoplay(autoPlay);
|
|
|
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out)
|
|
|
|
.replace(R.id.fragment_holder, instance)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void openChannelFragment(int serviceId, String url, String name) {
|
|
|
|
ImageLoader.getInstance().clearMemoryCache();
|
|
|
|
if (name == null) name = "";
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out)
|
|
|
|
.replace(R.id.fragment_holder, ChannelFragment.newInstance(serviceId, url, name))
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleVideoDetailIntent(int serviceId, String url, Intent intent) {
|
|
|
|
boolean autoPlay = intent.getBooleanExtra(VideoDetailFragment.AUTO_PLAY, false);
|
|
|
|
String title = intent.getStringExtra(Constants.KEY_TITLE);
|
|
|
|
openVideoDetailFragment(serviceId, url, title, autoPlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleChannelIntent(int serviceId, String url, Intent intent) {
|
|
|
|
String name = intent.getStringExtra(Constants.KEY_TITLE);
|
|
|
|
openChannelFragment(serviceId, url, name);
|
|
|
|
}
|
|
|
|
|
2016-08-02 01:26:12 +02:00
|
|
|
}
|