From 6ecdfaf19e91b43cce2cb05dda6a5e966e1bf6c4 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Mon, 5 Jun 2017 16:33:01 -0300 Subject: [PATCH] Improve backstack and theme change --- app/src/main/AndroidManifest.xml | 177 +++++++++--------- .../java/org/schabi/newpipe/MainActivity.java | 139 ++++---------- .../org/schabi/newpipe/RouterActivity.java | 49 +++-- .../schabi/newpipe/RouterPopupActivity.java | 98 +--------- .../newpipe/fragments/BaseFragment.java | 2 - .../newpipe/fragments/MainFragment.java | 3 +- .../fragments/OnItemSelectedListener.java | 10 - .../fragments/channel/ChannelFragment.java | 2 +- .../fragments/detail/VideoDetailFragment.java | 4 +- .../fragments/search/SearchFragment.java | 6 +- .../newpipe/player/PopupVideoPlayer.java | 1 + .../newpipe/settings/SettingsFragment.java | 14 +- .../schabi/newpipe/util/NavigationHelper.java | 84 ++++++--- 13 files changed, 241 insertions(+), 348 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/fragments/OnItemSelectedListener.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 928706831..8b87da217 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,13 @@ - - - - - + + + + + android:label="@string/app_name"> - - - + + @@ -32,12 +31,16 @@ android:name=".player.PlayVideoActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:theme="@style/VideoPlayerTheme" - tools:ignore="UnusedAttribute" /> + tools:ignore="UnusedAttribute"/> + + + android:label="@string/settings"/> - + - + - + android:theme="@android:style/Theme.NoDisplay"/> + + android:theme="@style/AppTheme"/> - + + android:label="@string/reCaptchaActivity"/> + android:resource="@xml/provider_paths"/> - - - + + + - - + + - - - - - + + + + + - - - - + + + + - - - + + + - - + + - - - - + + + + - - - + + + - - + + - - + + - + - + - + @@ -158,57 +161,55 @@ android:theme="@android:style/Theme.NoDisplay" android:label="@string/popup_mode_share_menu_title"> - - - + + + - - + + - - - - - + + + + + - - - - + + + + - - - + + + - - + + - - - - + + + + - - - + + + - - + + - - + + - - - + + + - - diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 1bb0858ee..a9ff3176e 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -21,7 +21,9 @@ package org.schabi.newpipe; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.ActionBar; @@ -33,13 +35,9 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import com.nostra13.universalimageloader.core.ImageLoader; - import org.schabi.newpipe.download.DownloadActivity; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.fragments.MainFragment; -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; @@ -48,7 +46,7 @@ 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 { +public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; public static final boolean DEBUG = false; @@ -71,6 +69,19 @@ public class MainActivity extends AppCompatActivity implements OnItemSelectedLis setSupportActionBar(toolbar); } + @Override + protected void onResume() { + super.onResume(); + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + 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(); + this.recreate(); + } + + } + @Override protected void onNewIntent(Intent intent) { if (DEBUG) Log.d(TAG, "onNewIntent() called with: intent = [" + intent + "]"); @@ -89,10 +100,16 @@ public class MainActivity extends AppCompatActivity implements OnItemSelectedLis @Override public void onBackPressed() { if (DEBUG) Log.d(TAG, "onBackPressed() called"); + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder); if (fragment instanceof VideoDetailFragment) if (((VideoDetailFragment) fragment).onActivityBackPressed()) return; super.onBackPressed(); + + fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder); + if (getSupportFragmentManager().getBackStackEntryCount() == 0 && !(fragment instanceof MainFragment)) { + super.onBackPressed(); + } } /*////////////////////////////////////////////////////////////////////////// @@ -134,7 +151,8 @@ public class MainActivity extends AppCompatActivity implements OnItemSelectedLis Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder); if (fragment instanceof VideoDetailFragment) ((VideoDetailFragment) fragment).clearHistory(); - NavigationHelper.openMainActivity(this); + getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); + NavigationHelper.openMainFragment(getSupportFragmentManager()); return true; } case R.id.action_settings: { @@ -160,26 +178,9 @@ public class MainActivity extends AppCompatActivity implements OnItemSelectedLis //////////////////////////////////////////////////////////////////////////*/ private void initFragments() { - openMainFragment(); if (getIntent() != null && getIntent().hasExtra(Constants.KEY_URL)) { handleIntent(getIntent()); - } - } - - /*////////////////////////////////////////////////////////////////////////// - // 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; - } + } else NavigationHelper.openMainFragment(getSupportFragmentManager()); } /*////////////////////////////////////////////////////////////////////////// @@ -187,96 +188,32 @@ public class MainActivity extends AppCompatActivity implements OnItemSelectedLis //////////////////////////////////////////////////////////////////////////*/ private void handleIntent(Intent intent) { - if (intent.hasExtra(Constants.KEY_THEME_CHANGE) && intent.getBooleanExtra(Constants.KEY_THEME_CHANGE, false)) { - this.recreate(); - Intent setI = new Intent(this, SettingsActivity.class); - startActivity(setI); + if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]"); + if (intent.getBooleanExtra(Constants.KEY_THEME_CHANGE, false)) { return; } 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(); + 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; } } 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); - openSearchFragment(serviceId, searchQuery); + NavigationHelper.openSearchFragment(getSupportFragmentManager(), serviceId, searchQuery); } else { getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); - openMainFragment();//openSearchFragment(); + NavigationHelper.openMainFragment(getSupportFragmentManager()); } } - - private void openMainFragment() { - 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 MainFragment()) - .commit(); - } - - private void openSearchFragment(int serviceId, String query) { - 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, SearchFragment.getInstance(serviceId, query)) - .addToBackStack(null) - .commit(); - } - - private void openVideoDetailFragment(int serviceId, String url, String title, boolean autoPlay) { - 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) { - 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.getInstance(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); - } - } diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index 134a8b102..65bb526fd 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -33,26 +33,39 @@ import java.util.HashSet; * to the part of the service which can handle the url. */ public class RouterActivity extends Activity { - //private static final String TAG = "RouterActivity" + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + String videoUrl = getUrl(getIntent()); + handleUrl(videoUrl); + + finish(); + } + + protected void handleUrl(String url) { + try { + NavigationHelper.openByLink(this, url); + } catch (Exception e) { + Toast.makeText(this, R.string.url_not_supported_toast, Toast.LENGTH_LONG).show(); + } + } + + /*////////////////////////////////////////////////////////////////////////// + // Utils + //////////////////////////////////////////////////////////////////////////*/ /** * Removes invisible separators (\p{Z}) and punctuation characters including * brackets (\p{P}). See http://www.regular-expressions.info/unicode.html for * more details. */ - private final static String REGEX_REMOVE_FROM_URL = "[\\p{Z}\\p{P}]"; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - handleIntent(getIntent()); - finish(); - } - - private void handleIntent(Intent intent) { - String videoUrl = ""; + protected final static String REGEX_REMOVE_FROM_URL = "[\\p{Z}\\p{P}]"; + protected String getUrl(Intent intent) { // first gather data and find service + String videoUrl = null; if (intent.getData() != null) { // this means the video was called though another app videoUrl = intent.getData().toString(); @@ -62,14 +75,10 @@ public class RouterActivity extends Activity { videoUrl = getUris(extraText)[0]; } - try { - NavigationHelper.openByLink(this, videoUrl); - } catch (Exception e) { - Toast.makeText(this, R.string.url_not_supported_toast, Toast.LENGTH_LONG).show(); - } + return videoUrl; } - private static String removeHeadingGibberish(final String input) { + protected String removeHeadingGibberish(final String input) { int start = 0; for (int i = input.indexOf("://") - 1; i >= 0; i--) { if (!input.substring(i, i + 1).matches("\\p{L}")) { @@ -80,7 +89,7 @@ public class RouterActivity extends Activity { return input.substring(start, input.length()); } - private static String trim(final String input) { + protected String trim(final String input) { if (input == null || input.length() < 1) { return input; } else { @@ -103,7 +112,7 @@ public class RouterActivity extends Activity { * @param sharedText text to scan for URLs. * @return potential URLs */ - private String[] getUris(final String sharedText) { + protected String[] getUris(final String sharedText) { final Collection result = new HashSet<>(); if (sharedText != null) { final String[] array = sharedText.split("\\p{Space}"); diff --git a/app/src/main/java/org/schabi/newpipe/RouterPopupActivity.java b/app/src/main/java/org/schabi/newpipe/RouterPopupActivity.java index 1cfcd29f2..1dfc4648a 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterPopupActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterPopupActivity.java @@ -1,9 +1,7 @@ package org.schabi.newpipe; -import android.app.Activity; import android.content.Intent; import android.os.Build; -import android.os.Bundle; import android.widget.Toast; import org.schabi.newpipe.extractor.NewPipe; @@ -12,57 +10,26 @@ import org.schabi.newpipe.player.PopupVideoPlayer; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.PermissionHelper; -import java.util.Collection; -import java.util.HashSet; - /** - * This activity is thought to open video streams form an external app using the popup player. + * Get the url from the intent and open a popup player */ -public class RouterPopupActivity extends Activity { - //private static final String TAG = "RouterPopupActivity"; - - /** - * Removes invisible separators (\p{Z}) and punctuation characters including - * brackets (\p{P}). See http://www.regular-expressions.info/unicode.html for - * more details. - */ - private final static String REGEX_REMOVE_FROM_URL = "[\\p{Z}\\p{P}]"; +public class RouterPopupActivity extends RouterActivity { @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - handleIntent(getIntent()); - finish(); - } - - - private void handleIntent(Intent intent) { + protected void handleUrl(String url) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !PermissionHelper.checkSystemAlertWindowPermission(this)) { Toast.makeText(this, R.string.msg_popup_permission, Toast.LENGTH_LONG).show(); return; } - String videoUrl = ""; - StreamingService service; - - // first gather data and find service - if (intent.getData() != null) { - // this means the video was called though another app - videoUrl = intent.getData().toString(); - } else if (intent.getStringExtra(Intent.EXTRA_TEXT) != null) { - //this means that vidoe was called through share menu - String extraText = intent.getStringExtra(Intent.EXTRA_TEXT); - videoUrl = getUris(extraText)[0]; - } - - service = NewPipe.getServiceByUrl(videoUrl); + StreamingService service = NewPipe.getServiceByUrl(url); if (service == null) { Toast.makeText(this, R.string.url_not_supported_toast, Toast.LENGTH_LONG).show(); return; } Intent callIntent = new Intent(this, PopupVideoPlayer.class); - switch (service.getLinkTypeByUrl(videoUrl)) { + switch (service.getLinkTypeByUrl(url)) { case STREAM: break; default: @@ -70,61 +37,8 @@ public class RouterPopupActivity extends Activity { return; } - callIntent.putExtra(Constants.KEY_URL, videoUrl); + callIntent.putExtra(Constants.KEY_URL, url); callIntent.putExtra(Constants.KEY_SERVICE_ID, service.getServiceId()); startService(callIntent); } - - private static String removeHeadingGibberish(final String input) { - int start = 0; - for (int i = input.indexOf("://") - 1; i >= 0; i--) { - if (!input.substring(i, i + 1).matches("\\p{L}")) { - start = i + 1; - break; - } - } - return input.substring(start, input.length()); - } - - private static String trim(final String input) { - if (input == null || input.length() < 1) { - return input; - } else { - String output = input; - while (output.length() > 0 && output.substring(0, 1).matches(REGEX_REMOVE_FROM_URL)) { - output = output.substring(1); - } - while (output.length() > 0 - && output.substring(output.length() - 1, output.length()).matches(REGEX_REMOVE_FROM_URL)) { - output = output.substring(0, output.length() - 1); - } - return output; - } - } - - /** - * Retrieves all Strings which look remotely like URLs from a text. - * Used if NewPipe was called through share menu. - * - * @param sharedText text to scan for URLs. - * @return potential URLs - */ - private String[] getUris(final String sharedText) { - final Collection result = new HashSet<>(); - if (sharedText != null) { - final String[] array = sharedText.split("\\p{Space}"); - for (String s : array) { - s = trim(s); - if (s.length() != 0) { - if (s.matches(".+://.+")) { - result.add(removeHeadingGibberish(s)); - } else if (s.matches(".+\\..+")) { - result.add("http://" + s); - } - } - } - } - return result.toArray(new String[result.size()]); - } - } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/BaseFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/BaseFragment.java index 5337912b8..975c5081b 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/BaseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/BaseFragment.java @@ -33,7 +33,6 @@ public abstract class BaseFragment extends Fragment { protected static final boolean DEBUG = MainActivity.DEBUG; protected AppCompatActivity activity; - protected OnItemSelectedListener onItemSelectedListener; protected AtomicBoolean isLoading = new AtomicBoolean(false); protected AtomicBoolean wasLoading = new AtomicBoolean(false); @@ -64,7 +63,6 @@ public abstract class BaseFragment extends Fragment { if (DEBUG) Log.d(TAG, "onAttach() called with: context = [" + context + "]"); activity = (AppCompatActivity) context; - onItemSelectedListener = (OnItemSelectedListener) context; } @Override diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index 97563e15e..f8e460123 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -51,6 +51,7 @@ public class MainFragment extends Fragment { /*////////////////////////////////////////////////////////////////////////// // Menu //////////////////////////////////////////////////////////////////////////*/ + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); @@ -68,7 +69,7 @@ public class MainFragment extends Fragment { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: - NavigationHelper.openSearch(activity, 0, ""); + NavigationHelper.openSearchFragment(getFragmentManager(), 0, ""); return true; } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/OnItemSelectedListener.java b/app/src/main/java/org/schabi/newpipe/fragments/OnItemSelectedListener.java deleted file mode 100644 index d35c063da..000000000 --- a/app/src/main/java/org/schabi/newpipe/fragments/OnItemSelectedListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.schabi.newpipe.fragments; - -import org.schabi.newpipe.extractor.StreamingService; - -/** - * Interface for communication purposes between activity and fragment - */ -public interface OnItemSelectedListener { - void onItemSelected(StreamingService.LinkType linkType, int serviceId, String url, String name); -} diff --git a/app/src/main/java/org/schabi/newpipe/fragments/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/channel/ChannelFragment.java index 032212ed6..9a1cfe1a7 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/channel/ChannelFragment.java @@ -240,7 +240,7 @@ public class ChannelFragment extends BaseFragment implements ChannelExtractorWor @Override public void selected(int serviceId, String url, String title) { if (DEBUG) Log.d(TAG, "selected() called with: serviceId = [" + serviceId + "], url = [" + url + "], title = [" + title + "]"); - NavigationHelper.openVideoDetail(onItemSelectedListener, serviceId, url, title); + NavigationHelper.openVideoDetailFragment(getFragmentManager(), serviceId, url, title); } }); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 6eb070ae6..2721eefc9 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -314,7 +314,7 @@ public class VideoDetailFragment extends BaseFragment implements StreamExtractor switch (requestCode) { case ReCaptchaActivity.RECAPTCHA_REQUEST: if (resultCode == Activity.RESULT_OK) { - NavigationHelper.openVideoDetail(onItemSelectedListener, serviceId, videoUrl, videoTitle); + NavigationHelper.openVideoDetailFragment(getFragmentManager(), serviceId, videoUrl, videoTitle); } else Log.e(TAG, "ReCaptcha failed"); break; default: @@ -354,7 +354,7 @@ public class VideoDetailFragment extends BaseFragment implements StreamExtractor openInPopup(); break; case R.id.detail_uploader_button: - NavigationHelper.openChannel(onItemSelectedListener, currentStreamInfo.service_id, currentStreamInfo.channel_url, currentStreamInfo.uploader); + NavigationHelper.openChannelFragment(getFragmentManager(), currentStreamInfo.service_id, currentStreamInfo.channel_url, currentStreamInfo.uploader); break; case R.id.detail_thumbnail_background_button: playVideo(currentStreamInfo); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/search/SearchFragment.java index d31addbfb..3e04b94fd 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/search/SearchFragment.java @@ -224,13 +224,13 @@ public class SearchFragment extends BaseFragment implements SuggestionWorker.OnS infoListAdapter.setOnStreamInfoItemSelectedListener(new InfoItemBuilder.OnInfoItemSelectedListener() { @Override public void selected(int serviceId, String url, String title) { - NavigationHelper.openVideoDetail(onItemSelectedListener, serviceId, url, title); + NavigationHelper.openVideoDetailFragment(getFragmentManager(), serviceId, url, title); } }); infoListAdapter.setOnChannelInfoItemSelectedListener(new InfoItemBuilder.OnInfoItemSelectedListener() { @Override public void selected(int serviceId, String url, String title) { - NavigationHelper.openChannel(onItemSelectedListener, serviceId, url, title); + NavigationHelper.openChannelFragment(getFragmentManager(), serviceId, url, title); } }); } @@ -389,7 +389,7 @@ public class SearchFragment extends BaseFragment implements SuggestionWorker.OnS public void onClick(View v) { if (DEBUG) Log.d(TAG, "onClick() called with: v = [" + v + "]"); if (TextUtils.isEmpty(searchEditText.getText())) { - NavigationHelper.openMainActivity(activity); + NavigationHelper.openMainFragment(getFragmentManager()); return; } diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index ba0590f7c..cfb1a5c81 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -263,6 +263,7 @@ public class PopupVideoPlayer extends Service { i.putExtra(Constants.KEY_TITLE, videoTitle); i.putExtra(Constants.KEY_LINK_TYPE, StreamingService.LinkType.STREAM); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(i); context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); } diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsFragment.java index 8ac9b0b41..119bc7e8c 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsFragment.java @@ -69,7 +69,7 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - Log.d("TAG", "onPreferenceTreeClick() called with: preferenceScreen = [" + preferenceScreen + "], preference = [" + preference + "]"); + if (MainActivity.DEBUG) Log.d("TAG", "onPreferenceTreeClick() called with: preferenceScreen = [" + preferenceScreen + "], preference = [" + preference + "]"); if (preference.getKey().equals(DOWNLOAD_PATH_PREFERENCE) || preference.getKey().equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { Intent i = new Intent(activity, FilePickerActivity.class) .putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false) @@ -87,7 +87,7 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - Log.d("TAG", "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); + if (MainActivity.DEBUG) Log.d("TAG", "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); if ((requestCode == REQUEST_DOWNLOAD_PATH || requestCode == REQUEST_DOWNLOAD_AUDIO_PATH) && resultCode == Activity.RESULT_OK) { String key = getString(requestCode == REQUEST_DOWNLOAD_PATH ? R.string.download_path_key : R.string.download_path_audio_key); @@ -101,7 +101,7 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer } } - /** + /* * Update ONLY the summary of some preferences that don't fire in the onSharedPreferenceChanged or CAN'T be update via xml (%s) * * For example, the download_path use the startActivityForResult, firing the onStop of this fragment, @@ -114,7 +114,7 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - Log.d("TAG", "onSharedPreferenceChanged() called with: sharedPreferences = [" + sharedPreferences + "], key = [" + key + "]"); + if (MainActivity.DEBUG) Log.d("TAG", "onSharedPreferenceChanged() called with: sharedPreferences = [" + sharedPreferences + "], key = [" + key + "]"); String summary = null; if (key.equals(USE_TOR_KEY)) { @@ -131,10 +131,10 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer } else if (key.equals(THEME)) { summary = sharedPreferences.getString(THEME, getString(R.string.default_theme_value)); if (!summary.equals(currentTheme)) { // If it's not the current theme - Intent intentToMain = new Intent(activity, MainActivity.class); - intentToMain.putExtra(Constants.KEY_THEME_CHANGE, true); - startActivity(intentToMain); + getActivity().recreate(); } + + defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply(); } if (!TextUtils.isEmpty(summary)) findPreference(key).setSummary(summary); diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 129adb5c2..56a44cf55 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -3,6 +3,10 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.Intent; import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; + +import com.nostra13.universalimageloader.core.ImageLoader; import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; @@ -10,8 +14,10 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.stream_info.AudioStream; import org.schabi.newpipe.extractor.stream_info.StreamInfo; -import org.schabi.newpipe.fragments.OnItemSelectedListener; +import org.schabi.newpipe.fragments.MainFragment; +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.player.BackgroundPlayer; import org.schabi.newpipe.player.BasePlayer; import org.schabi.newpipe.player.VideoPlayer; @@ -36,7 +42,6 @@ public class NavigationHelper { return mIntent; } - public static Intent getOpenVideoPlayerIntent(Context context, Class targetClazz, VideoPlayer instance) { return new Intent(context, targetClazz) .putExtra(BasePlayer.VIDEO_TITLE, instance.getVideoTitle()) @@ -66,31 +71,69 @@ public class NavigationHelper { } /*////////////////////////////////////////////////////////////////////////// - // Through Interface (faster) + // Through FragmentManager //////////////////////////////////////////////////////////////////////////*/ - public static void openChannel(OnItemSelectedListener listener, int serviceId, String url) { - openChannel(listener, serviceId, url, null); + public static void openMainFragment(FragmentManager fragmentManager) { + ImageLoader.getInstance().clearMemoryCache(); + fragmentManager.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 MainFragment()) + .commit(); } - public static void openChannel(OnItemSelectedListener listener, int serviceId, String url, String name) { - listener.onItemSelected(StreamingService.LinkType.CHANNEL, serviceId, url, name); + public static void openSearchFragment(FragmentManager fragmentManager, int serviceId, String query) { + fragmentManager.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, SearchFragment.getInstance(serviceId, query)) + .addToBackStack(null) + .commit(); } - public static void openVideoDetail(OnItemSelectedListener listener, int serviceId, String url) { - openVideoDetail(listener, serviceId, url, null); + public static void openVideoDetailFragment(FragmentManager fragmentManager, int serviceId, String url, String title) { + openVideoDetailFragment(fragmentManager, serviceId, url, title, false); } - public static void openVideoDetail(OnItemSelectedListener listener, int serviceId, String url, String title) { - listener.onItemSelected(StreamingService.LinkType.STREAM, serviceId, url, title); + public static void openVideoDetailFragment(FragmentManager fragmentManager, int serviceId, String url, String title, boolean autoPlay) { + Fragment fragment = fragmentManager.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); + + fragmentManager.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(); + } + + public static void openChannelFragment(FragmentManager fragmentManager, int serviceId, String url, String name) { + if (name == null) name = ""; + fragmentManager.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.getInstance(serviceId, url, name)) + .addToBackStack(null) + .commit(); } /*////////////////////////////////////////////////////////////////////////// // Through Intents //////////////////////////////////////////////////////////////////////////*/ - public static void openByLink(Context context, String url) throws Exception { - context.startActivity(getIntentByLink(context, url)); + public static void openSearch(Context context, int serviceId, String query) { + Intent mIntent = new Intent(context, MainActivity.class); + mIntent.putExtra(Constants.KEY_SERVICE_ID, serviceId); + mIntent.putExtra(Constants.KEY_QUERY, query); + mIntent.putExtra(Constants.KEY_OPEN_SEARCH, true); + context.startActivity(mIntent); } public static void openChannel(Context context, int serviceId, String url) { @@ -118,12 +161,12 @@ public class NavigationHelper { context.startActivity(mIntent); } - public static void openSearch(Context context, int serviceId, String query) { - Intent mIntent = new Intent(context, MainActivity.class); - mIntent.putExtra(Constants.KEY_SERVICE_ID, serviceId); - mIntent.putExtra(Constants.KEY_QUERY, query); - mIntent.putExtra(Constants.KEY_OPEN_SEARCH, true); - context.startActivity(mIntent); + public static void openByLink(Context context, String url) throws Exception { + Intent intentByLink = getIntentByLink(context, url); + if (intentByLink == null) throw new NullPointerException("getIntentByLink(context = [" + context + "], url = [" + url + "]) returned null"); + intentByLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intentByLink.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); + context.startActivity(intentByLink); } private static Intent getOpenIntent(Context context, String url, int serviceId, StreamingService.LinkType type) { @@ -147,8 +190,7 @@ public class NavigationHelper { case CHANNEL: return getOpenIntent(context, url, serviceId, StreamingService.LinkType.CHANNEL); case NONE: - throw new Exception("Url not known to service. service=" - + Integer.toString(serviceId) + " url=" + url); + throw new Exception("Url not known to service. service=" + serviceId + " url=" + url); } return null; }