X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=50f6a2cca239d85c35f75f280371f151ca58c8be;hp=658a8dd8a6ccd12dbb49e0e292b9983fa39e5a09;hb=8b69108b214333167fc16cb897143005a7dfbad7;hpb=aedc35976f8eda7c00bdd822c172e19cad0fc485 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 658a8dd8..50f6a2cc 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -810,11 +810,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook String intentAction = intent.getAction(); Uri intentUriData = intent.getData(); - // Only process the URI if it contains data. If the user pressed the desktop icon after the app was already running the URI will be null. - if (intentUriData != null) { - // Sets the new intent as the activity intent, which replaces the one that originally started the app. - setIntent(intent); + // Determine if this is a web search. + boolean isWebSearch = ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH)); + // Only process the URI if it contains data or it is a web search. If the user pressed the desktop icon after the app was already running the URI will be null. + if (intentUriData != null || isWebSearch) { // Get the shared preferences. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -831,7 +831,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook String url; // If the intent action is a web search, perform the search. - if ((intentAction != null) && intentAction.equals(Intent.ACTION_WEB_SEARCH)) { + if (isWebSearch) { // Create an encoded URL string. String encodedUrlString; @@ -2077,20 +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 a handle for the tab layout and the view pager. - 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); - } + // Close the current tab. + closeCurrentTab(); break; case R.id.clear_and_exit: @@ -3139,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. @@ -3165,9 +3154,15 @@ 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(); + + // Manually kill Privacy Browser. Otherwise, it is glitchy when restarted. + System.exit(0); } } @@ -3315,6 +3310,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook FrameLayout rootFrameLayout = findViewById(R.id.root_framelayout); AppBarLayout appBarLayout = findViewById(R.id.appbar_layout); ActionBar actionBar = getSupportActionBar(); + Toolbar toolbar = findViewById(R.id.toolbar); + LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout); LinearLayout tabsLinearLayout = findViewById(R.id.tabs_linearlayout); SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout); @@ -3331,23 +3328,35 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook customHeaders.remove("DNT"); } - // Get the current layout parameters. Using coordinator layout parameters allows the `setBehavior()` command. - CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams(); + // Get the current layout parameters. Using coordinator layout parameters allows the `setBehavior()` command and using app bar layout parameters allows the `setScrollFlags()` command. + CoordinatorLayout.LayoutParams swipeRefreshLayoutParams = (CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams(); + AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); + AppBarLayout.LayoutParams findOnPageLayoutParams = (AppBarLayout.LayoutParams) findOnPageLinearLayout.getLayoutParams(); + AppBarLayout.LayoutParams tabsLayoutParams = (AppBarLayout.LayoutParams) tabsLinearLayout.getLayoutParams(); // Add the scrolling behavior to the layout parameters. if (scrollAppBar) { // Enable scrolling of the app bar. - layoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior()); + swipeRefreshLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior()); + toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); + findOnPageLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); + tabsLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); } else { // Disable scrolling of the app bar. - layoutParams.setBehavior(null); + swipeRefreshLayoutParams.setBehavior(null); + toolbarLayoutParams.setScrollFlags(0); + findOnPageLayoutParams.setScrollFlags(0); + tabsLayoutParams.setScrollFlags(0); // Expand the app bar if it is currently collapsed. appBarLayout.setExpanded(true); } - // Apply the modified layout parameters to the swipe refresh layout. - swipeRefreshLayout.setLayoutParams(layoutParams); + // Apply the modified layout parameters. + swipeRefreshLayout.setLayoutParams(swipeRefreshLayoutParams); + toolbar.setLayoutParams(toolbarLayoutParams); + findOnPageLinearLayout.setLayoutParams(findOnPageLayoutParams); + tabsLinearLayout.setLayoutParams(tabsLayoutParams); // Set the app bar scrolling for each WebView. for (int i = 0; i < webViewPagerAdapter.getCount(); i++) { @@ -4222,6 +4231,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); @@ -5347,10 +5377,15 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook CheckPinnedMismatchHelper.checkPinnedMismatch(getSupportFragmentManager(), nestedScrollWebView); } + // Get the current URL from the nested scroll WebView. This is more accurate than using the URL passed into the method, which is sometimes not the final one. + String currentUrl = nestedScrollWebView.getUrl(); + // Update the URL text bar if the page is currently selected and the user is not currently typing in the URL edit text. - if ((tabLayout.getSelectedTabPosition() == currentPagePosition) && !urlEditText.hasFocus()) { + // Crash records show that, in some crazy way, it is possible for the current URL to be blank at this point. + // Probably some sort of race condition when Privacy Browser is being resumed. + if ((tabLayout.getSelectedTabPosition() == currentPagePosition) && !urlEditText.hasFocus() && (currentUrl != null)) { // Check to see if the URL is `about:blank`. - if (nestedScrollWebView.getUrl().equals("about:blank")) { // The WebView is blank. + if (currentUrl.equals("about:blank")) { // The WebView is blank. // Display the hint in the URL edit text. urlEditText.setText(""); @@ -5367,12 +5402,30 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook applyDomainSettings(nestedScrollWebView, "", true, false); } else { // The WebView has loaded a webpage. // Display the final URL. Getting the URL from the WebView instead of using the one provided by `onPageFinished()` makes websites like YouTube function correctly. - urlEditText.setText(nestedScrollWebView.getUrl()); + urlEditText.setText(currentUrl); // Apply text highlighting to the URL. 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()); + } } }