From: Soren Stoutner Date: Tue, 30 Apr 2019 23:09:18 +0000 (-0700) Subject: Make the back button close tabs that are at the beginning of their history. https... X-Git-Tag: v3.0.1~2 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=a35a6e396b61579e00f1954801720613dd6685f7;p=PrivacyBrowserAndroid.git Make the back button close tabs that are at the beginning of their history. https://redmine.stoutner.com/issues/421 --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 5356c420..b6ba8097 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -2077,24 +2077,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Run the commands that correspond to the selected menu item. switch (menuItemId) { case R.id.close_tab: - // Get handles for the views. - AppBarLayout appBarLayout = findViewById(R.id.appbar_layout); - TabLayout tabLayout = findViewById(R.id.tablayout); - ViewPager webViewPager = findViewById(R.id.webviewpager); - - // Get the current tab number. - int currentTabNumber = tabLayout.getSelectedTabPosition(); - - // Delete the current tab. - tabLayout.removeTabAt(currentTabNumber); - - // Delete the current page. If the selected page number did not change during the delete, it will return true, meaning that the current WebView must be reset. - if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) { - setCurrentWebView(currentTabNumber); - } - - // Expand the app bar if it is currently collapsed. - appBarLayout.setExpanded(true); + // Close the current tab. + closeCurrentTab(); break; case R.id.clear_and_exit: @@ -3143,8 +3127,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Override `onBackPressed` to handle the navigation drawer and and the WebView. @Override public void onBackPressed() { - // Get a handle for the drawer layout. + // Get a handle for the drawer layout and the tab layout. DrawerLayout drawerLayout = findViewById(R.id.drawerlayout); + TabLayout tabLayout = findViewById(R.id.tablayout); if (drawerLayout.isDrawerVisible(GravityCompat.START)) { // The navigation drawer is open. // Close the navigation drawer. @@ -3169,9 +3154,12 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Go back. currentWebView.goBack(); - } else { // There is nothing else to do. - // Load a blank website. - loadUrl(""); + } else if (tabLayout.getTabCount() > 1) { // There are at least two tabs. + // Close the current tab. + closeCurrentTab(); + } else { // There isn't anything to do in Privacy Browser. + // Run the default commands. + super.onBackPressed(); } } @@ -4240,6 +4228,27 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook webViewPagerAdapter.addPage(newTabNumber, webViewPager); } + private void closeCurrentTab() { + // Get handles for the views. + AppBarLayout appBarLayout = findViewById(R.id.appbar_layout); + TabLayout tabLayout = findViewById(R.id.tablayout); + ViewPager webViewPager = findViewById(R.id.webviewpager); + + // Get the current tab number. + int currentTabNumber = tabLayout.getSelectedTabPosition(); + + // Delete the current tab. + tabLayout.removeTabAt(currentTabNumber); + + // Delete the current page. If the selected page number did not change during the delete, it will return true, meaning that the current WebView must be reset. + if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) { + setCurrentWebView(currentTabNumber); + } + + // Expand the app bar if it is currently collapsed. + appBarLayout.setExpanded(true); + } + private void setCurrentWebView(int pageNumber) { // Get a handle for the shared preferences. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -5391,6 +5400,24 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook highlightUrlText(); } } + + // Get the current tab. + TabLayout.Tab tab = tabLayout.getTabAt(currentPagePosition); + + // Only populate the title text view if the tab has been fully created. + if (tab != null) { + // Get the custom view from the tab. + View tabView = tab.getCustomView(); + + // Remove the incorrect warning below that the current tab view might be null. + assert tabView != null; + + // Get the title text view from the tab. + TextView tabTitleTextView = tabView.findViewById(R.id.title_textview); + + // Set the title as the tab text. Sometimes `onReceivedTitle()` is not called, especially when navigating history. + tabTitleTextView.setText(nestedScrollWebView.getTitle()); + } } }