diff --git a/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsCollapsingHeaderTest.kt b/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsCollapsingHeaderTest.kt deleted file mode 100644 index df71d22a5c1..00000000000 --- a/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsCollapsingHeaderTest.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2026 WizeStream contributors - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -package org.schabi.newpipe.settings - -import androidx.test.core.app.ActivityScenario -import com.google.android.material.appbar.AppBarLayout -import com.google.android.material.appbar.CollapsingToolbarLayout -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertTrue -import org.junit.Test -import org.schabi.newpipe.R - -class SettingsCollapsingHeaderTest { - @Test - fun settingsScreensShareOneCollapsingAppBar() { - ActivityScenario.launch(SettingsActivity::class.java).use { scenario -> - scenario.onActivity { activity -> - val appBar = activity.findViewById(R.id.settings_app_bar) - val header = activity.findViewById( - R.id.settings_collapsing_toolbar - ) - assertNotNull(appBar) - assertNotNull(header) - // Keep the large M3 title while reclaiming the empty space below the toolbar. - val density = activity.resources.displayMetrics.density - assertEquals(120, kotlin.math.round(header.layoutParams.height / density).toInt()) - val toolbar = activity.findViewById(R.id.toolbar) - assertTrue(header.layoutParams.height > toolbar.layoutParams.height) - activity.setSettingsSectionTitle("Playback") - assertEquals("Playback", header.title) - appBar.setExpanded(false, false) - } - } - } -} diff --git a/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsToolbarLayoutTest.kt b/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsToolbarLayoutTest.kt new file mode 100644 index 00000000000..eeacd5b4d9e --- /dev/null +++ b/app/src/androidTest/java/org/schabi/newpipe/settings/SettingsToolbarLayoutTest.kt @@ -0,0 +1,150 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package org.schabi.newpipe.settings + +import android.graphics.Rect +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.appcompat.widget.Toolbar +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.children +import androidx.preference.Preference +import androidx.preference.PreferenceGroupAdapter +import androidx.recyclerview.widget.RecyclerView +import androidx.test.core.app.ActivityScenario +import androidx.test.platform.app.InstrumentationRegistry +import com.google.android.material.appbar.AppBarLayout +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.schabi.newpipe.R +import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment + +class SettingsToolbarLayoutTest { + @Test + fun compactToolbarRestoresSectionTitleAfterSearchAndRecreation() { + ActivityScenario.launch(SettingsActivity::class.java).use { scenario -> + scenario.onActivity(::openSponsorBlock) + idle() + scenario.onActivity { activity -> + assertCompactTitle(activity) + val search = activity.findViewById(R.id.toolbar).menu.findItem(R.id.action_search) + assertNotNull(search) + assertTrue(search.isVisible) + assertTrue(activity.onOptionsItemSelected(search)) + activity.supportFragmentManager.executePendingTransactions() + } + idle() + scenario.onActivity(::assertSearchVisible) + scenario.recreate() + idle() + scenario.onActivity { activity -> + assertSearchVisible(activity) + activity.onBackPressedDispatcher.onBackPressed() + activity.supportFragmentManager.executePendingTransactions() + } + idle() + scenario.onActivity { activity -> + assertTrue(current(activity) is SponsorBlockSettingsFragment) + assertEquals(View.GONE, activity.findViewById(R.id.toolbar_search_container).visibility) + assertCompactTitle(activity) + } + } + } + + @Test + fun finalPreferencesRemainAboveSystemNavigationWithoutCollapsingTheHeader() { + ActivityScenario.launch(SettingsActivity::class.java).use { scenario -> + scenario.onActivity { activity -> + val main = categories(activity) + // Release builds end at Learning Mode rather than the debug-only category. + main.findPreference(activity.getString(R.string.debug_pref_screen_key))?.let { + main.preferenceScreen.removePreference(it) + } + } + idle() + scenario.onActivity { activity -> + val list = categories(activity).listView + val adapter = list.adapter as PreferenceGroupAdapter + assertEquals(LearningSettingsFragment::class.java.name, adapter.getItem(adapter.itemCount - 1)?.fragment) + list.scrollToPosition(adapter.itemCount - 1) + } + idle() + scenario.onActivity { activity -> + assertLastRowInsideSafeWindow(activity, categories(activity).listView) + openSponsorBlock(activity) + } + idle() + scenario.onActivity { activity -> + val list = (current(activity) as SponsorBlockSettingsFragment).listView + val adapter = list.adapter as PreferenceGroupAdapter + assertEquals(activity.getString(R.string.dearrow_thumbnails_key), adapter.getItem(adapter.itemCount - 1)?.key) + list.scrollToPosition(adapter.itemCount - 1) + } + idle() + scenario.onActivity { activity -> + assertLastRowInsideSafeWindow(activity, (current(activity) as SponsorBlockSettingsFragment).listView) + } + } + } + + private fun assertCompactTitle(activity: SettingsActivity) { + val toolbar = activity.findViewById(R.id.toolbar) + val appBar = activity.findViewById(R.id.settings_app_bar) + val title = (current(activity) as BasePreferenceFragment).preferenceScreen.title.toString() + assertEquals(title, toolbar.title?.toString()) + assertNotNull(toolbar.navigationIcon) + val label = toolbar.children.filterIsInstance().firstOrNull { it.text.toString() == title } + assertNotNull("The section title should be drawn in the navigation toolbar", label) + assertTrue(label!!.width > 0 && label.height > 0) + assertTrue(label.top >= 0 && label.bottom <= toolbar.height) + assertEquals("Settings should not reserve a second title row", toolbar.height, appBar.height) + assertEquals("Scrolling settings must not change the header height", 0, appBar.totalScrollRange) + } + + private fun assertSearchVisible(activity: SettingsActivity) { + assertTrue(current(activity) is PreferenceSearchFragment) + assertEquals(View.VISIBLE, activity.findViewById(R.id.toolbar_search_container).visibility) + assertTrue("The section title must make room for search", activity.findViewById(R.id.toolbar).title.isNullOrEmpty()) + } + + private fun assertLastRowInsideSafeWindow(activity: SettingsActivity, list: RecyclerView) { + val appBar = activity.findViewById(R.id.settings_app_bar) + val root = appBar.parent as ViewGroup + val insets = ViewCompat.getRootWindowInsets(root)!! + val navigation = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) + assertTrue("Settings must reserve the system navigation area", root.paddingBottom >= navigation.bottom) + val row = list.findViewHolderForAdapterPosition(list.adapter!!.itemCount - 1)?.itemView + assertNotNull("The final preference must be laid out after scrolling", row) + assertFalse("The list must have reached its end", list.canScrollVertically(1)) + val bounds = Rect(0, 0, row!!.width, row.height) + root.offsetDescendantRectToMyCoords(row, bounds) + assertTrue("The complete preference must fit above system navigation: $bounds", bounds.bottom <= root.height - root.paddingBottom) + assertTrue("The preference must not overlap the toolbar: $bounds", bounds.top >= appBar.bottom) + val visible = Rect() + assertTrue(row.getGlobalVisibleRect(visible)) + assertEquals("The final row must not be clipped by an ancestor", row.height, visible.height()) + } + + private fun openSponsorBlock(activity: SettingsActivity) { + val destination = Preference(activity).apply { fragment = SponsorBlockSettingsFragment::class.java.name } + assertTrue(activity.onPreferenceStartFragment(categories(activity), destination)) + activity.supportFragmentManager.executePendingTransactions() + } + + private fun categories(activity: SettingsActivity) = ( + activity.supportFragmentManager.findFragmentById(R.id.settings_navigation_holder) + ?: current(activity) + ) as MainSettingsFragment + + private fun current(activity: SettingsActivity) = activity.supportFragmentManager.findFragmentById(R.id.settings_fragment_holder) + + private fun idle() = InstrumentationRegistry.getInstrumentation().waitForIdleSync() +} diff --git a/app/src/main/assets/changelog.html b/app/src/main/assets/changelog.html index d82f59b801d..d092f5af29f 100644 --- a/app/src/main/assets/changelog.html +++ b/app/src/main/assets/changelog.html @@ -130,6 +130,11 @@

Unreleased

+

Settings layout

+
    +
  • Use a compact toolbar with the settings title beside the back button, leaving more space for preferences.
  • +
  • Keep the last settings rows fully reachable above the system navigation buttons, including after restoring a scrolled page.
  • +

Single-channel feed refresh

  • Add Refresh channel feed to subscribed channels' menus to fetch their saved feed immediately, including muted channels, while keeping the mute setting enabled for regular updates.
  • diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index d08f2740ece..6faea9b7e29 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -11,8 +11,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; -import com.google.android.material.appbar.CollapsingToolbarLayout; -import com.google.android.material.appbar.AppBarLayout; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; @@ -74,8 +72,6 @@ public class SettingsActivity extends AppCompatActivity implements "org.schabi.newpipe.settings.OPEN_PLUGIN_SETTINGS"; private static final String SEARCH_TEXT = "settings_search_text"; private static final String TWO_PANE = "settings_two_pane"; - private AppBarLayout settingsAppBar; - private CollapsingToolbarLayout settingsCollapsingToolbar; private CharSequence sectionTitle = ""; private boolean twoPane; @@ -111,8 +107,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) { final SettingsLayoutBinding binding = SettingsLayoutBinding.inflate(getLayoutInflater()); settingsRoot = binding.getRoot(); - settingsAppBar = binding.settingsAppBar; - settingsCollapsingToolbar = binding.settingsCollapsingToolbar; setContentView(settingsRoot); twoPane = getResources().getBoolean(R.bool.use_two_pane_settings); binding.settingsNavigationHolder.setVisibility(twoPane ? View.VISIBLE : View.GONE); @@ -121,8 +115,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) { final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); - // The shared collapsing toolbar draws both the expanded and compact title. - actionBar.setDisplayShowTitleEnabled(false); + actionBar.setDisplayShowTitleEnabled(true); } if (savedInstanceState != null) { @@ -358,29 +351,25 @@ private boolean isSearchActive() { } /** - * Keeps the expanded and collapsed settings titles synchronized across fragments. + * Keeps the compact toolbar title synchronized across settings fragments. * * @param title current settings section title */ public void setSettingsSectionTitle(@NonNull final CharSequence title) { sectionTitle = title; - if (!isSearchActive() && settingsCollapsingToolbar != null) { - settingsCollapsingToolbar.setTitle(title); + final ActionBar actionBar = getSupportActionBar(); + if (!isSearchActive() && actionBar != null) { + actionBar.setTitle(title); } } private void updateSearchUi() { final boolean active = isSearchActive(); searchContainer.setVisibility(active ? View.VISIBLE : View.GONE); - if (settingsCollapsingToolbar != null) { - settingsCollapsingToolbar.setTitle(active ? "" : sectionTitle); - } - if (active && settingsAppBar != null) { - settingsAppBar.setExpanded(false, false); - } final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { - actionBar.setDisplayShowTitleEnabled(false); + actionBar.setTitle(sectionTitle); + actionBar.setDisplayShowTitleEnabled(!active); } invalidateOptionsMenu(); if (active) { diff --git a/app/src/main/res/layout/settings_layout.xml b/app/src/main/res/layout/settings_layout.xml index 0d549495387..fe05ec3796f 100644 --- a/app/src/main/res/layout/settings_layout.xml +++ b/app/src/main/res/layout/settings_layout.xml @@ -14,27 +14,11 @@ android:background="?attr/colorSurface" app:liftOnScroll="true"> - - - - + android:layout_height="wrap_content" /> 4dp 2dp - - 120dp - 16dp 8dp diff --git a/app/src/test/java/org/schabi/newpipe/settings/SettingsViewportTest.kt b/app/src/test/java/org/schabi/newpipe/settings/SettingsViewportTest.kt new file mode 100644 index 00000000000..e2ce41b8206 --- /dev/null +++ b/app/src/test/java/org/schabi/newpipe/settings/SettingsViewportTest.kt @@ -0,0 +1,177 @@ +/* + * SPDX-FileCopyrightText: 2026 WizeStream contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package org.schabi.newpipe.settings + +import android.graphics.Rect +import android.os.Looper +import android.view.View +import android.view.ViewGroup +import androidx.core.graphics.Insets +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.preference.Preference +import androidx.preference.PreferenceGroupAdapter +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.appbar.AppBarLayout +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.MockedStatic +import org.mockito.Mockito.mock +import org.mockito.Mockito.mockStatic +import org.mockito.Mockito.`when` +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.schabi.newpipe.App +import org.schabi.newpipe.R + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class SettingsViewportTest { + private lateinit var appInstance: MockedStatic + + @Before + fun provideApplicationForDeviceDetection() { + // JVM tests use a plain Application to avoid starting services and workers. + val app = mock(App::class.java) + `when`(app.packageManager).thenReturn(RuntimeEnvironment.getApplication().packageManager) + appInstance = mockStatic(App::class.java) + // Settings' Java callers use the static bridge rather than Kotlin's companion getter. + appInstance.`when` { App::class.java.getMethod("getInstance").invoke(null) }.thenReturn(app) + } + + @After + fun releaseApplicationStub() { + appInstance.close() + } + + @Test + @Config(qualifiers = "w360dp-h800dp") + fun mainSettingsEndRowRemainsAboveNavigationButtons() = withSettings(360, 800) { + assertLearningModeReachable() + } + + @Test + @Config(qualifiers = "w360dp-h800dp") + fun sponsorBlockEndRowRemainsAboveNavigationButtons() = withSettings(360, 800) { + assertReplaceThumbnailsReachable() + } + + @Test + @Config(qualifiers = "w640dp-h360dp-land") + fun shortLandscapeKeepsBothEndRowsInsideTheSafeViewport() = withSettings(640, 360) { + assertLearningModeReachable() + assertReplaceThumbnailsReachable() + } + + @Test + @Config(qualifiers = "w1000dp-h700dp-land") + fun twoPaneSettingsKeepBothListsInsideTheSafeViewport() = withSettings(1000, 700) { + assertEquals(View.VISIBLE, activity.findViewById(R.id.settings_navigation_holder).visibility) + assertLearningModeReachable() + assertReplaceThumbnailsReachable() + } + + private fun withSettings(width: Int, height: Int, check: SettingsWindow.() -> Unit) { + Robolectric.buildActivity(SettingsActivity::class.java).setup().visible().use { controller -> + val activity = controller.get() + activity.supportFragmentManager.executePendingTransactions() + shadowOf(Looper.getMainLooper()).idle() + SettingsWindow(activity, width, height).apply { + layout() + check() + } + } + } + + private class SettingsWindow(val activity: SettingsActivity, width: Int, height: Int) { + private val appBar = activity.findViewById(R.id.settings_app_bar) + private val root = appBar.parent as ViewGroup + private val widthPixels = dp(width) + private val heightPixels = dp(height) + private val safeInsets = WindowInsetsCompat.Builder() + .setInsets(WindowInsetsCompat.Type.statusBars(), Insets.of(0, dp(24), 0, 0)) + .setInsets(WindowInsetsCompat.Type.navigationBars(), Insets.of(0, 0, 0, dp(48))) + .build() + + fun assertLearningModeReachable() { + val categories = categories() + // Learning Mode is the final category in releases; debug builds append Debug. + categories.findPreference(activity.getString(R.string.debug_pref_screen_key))?.let { + categories.preferenceScreen.removePreference(it) + } + shadowOf(Looper.getMainLooper()).idle() + layout() + val list = categories.listView + val adapter = list.adapter as PreferenceGroupAdapter + assertEquals(LearningSettingsFragment::class.java.name, adapter.getItem(adapter.itemCount - 1)?.fragment) + assertLastRowReachable(list, "Learning Mode") + } + + fun assertReplaceThumbnailsReachable() { + val destination = Preference(activity).apply { + fragment = SponsorBlockSettingsFragment::class.java.name + } + assertTrue(activity.onPreferenceStartFragment(categories(), destination)) + activity.supportFragmentManager.executePendingTransactions() + shadowOf(Looper.getMainLooper()).idle() + layout() + val sponsorBlock = activity.supportFragmentManager.findFragmentById(R.id.settings_fragment_holder) as SponsorBlockSettingsFragment + val list = sponsorBlock.listView + val adapter = list.adapter as PreferenceGroupAdapter + assertEquals(activity.getString(R.string.dearrow_thumbnails_key), adapter.getItem(adapter.itemCount - 1)?.key) + assertLastRowReachable(list, "Replace thumbnails") + } + + fun layout() { + ViewCompat.dispatchApplyWindowInsets(root, safeInsets) + root.measure( + View.MeasureSpec.makeMeasureSpec(widthPixels, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(heightPixels, View.MeasureSpec.EXACTLY) + ) + root.layout(0, 0, widthPixels, heightPixels) + assertEquals(dp(24), root.paddingTop) + assertEquals(dp(48), root.paddingBottom) + } + + private fun categories() = ( + activity.supportFragmentManager.findFragmentById(R.id.settings_navigation_holder) + ?: activity.supportFragmentManager.findFragmentById(R.id.settings_fragment_holder) + ) as MainSettingsFragment + + private fun assertLastRowReachable(list: RecyclerView, title: String) { + val lastPosition = list.adapter!!.itemCount - 1 + // Search highlighting, focus, and restored scroll positions can reach the end + // without a drag. Do not manually collapse the app bar to make this pass. + list.scrollToPosition(lastPosition) + layout() + val row = list.findViewHolderForAdapterPosition(lastPosition)?.itemView + assertNotNull("The final $title row must be bound after scrolling", row) + assertFalse("The list must have reached its end", list.canScrollVertically(1)) + val bounds = Rect(0, 0, row!!.width, row.height) + root.offsetDescendantRectToMyCoords(row, bounds) + val safeBottom = root.height - root.paddingBottom + println("Settings viewport $title: row=$bounds, safe bottom=$safeBottom, toolbar height=${appBar.height}, scroll range=${appBar.totalScrollRange}") + assertTrue("$title has no usable height", bounds.height() > 0) + assertTrue("$title overlaps the toolbar: $bounds, toolbar bottom ${appBar.bottom}", bounds.top >= appBar.bottom) + assertTrue( + "$title is below the safe viewport: $bounds, safe bottom $safeBottom, app bar scroll range ${appBar.totalScrollRange}", + bounds.bottom <= safeBottom + ) + assertTrue("$title is clipped horizontally: $bounds", bounds.left >= root.paddingLeft && bounds.right <= root.width - root.paddingRight) + } + + private fun dp(value: Int) = (value * activity.resources.displayMetrics.density).toInt() + } +} diff --git a/docs/changelogs.md b/docs/changelogs.md index 3e9da3f56c3..48b559aafb2 100644 --- a/docs/changelogs.md +++ b/docs/changelogs.md @@ -4,6 +4,11 @@ Release history is listed newest first. The number beside each release is its An ## Unreleased +### Settings layout + +- Use a compact toolbar with the settings title beside the back button, leaving more space for preferences. +- Keep the last settings rows fully reachable above the system navigation buttons, including after restoring a scrolled page. + ### Single-channel feed refresh - Add Refresh channel feed to subscribed channels' menus to fetch their saved feed immediately, including muted channels, while keeping the mute setting enabled for regular updates.