]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
Fix audio and video sometimes playing after a tab is closed. https://redmine.stoutne...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / WebViewPagerAdapter.java
1 /*
2  * Copyright © 2019-2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.adapters;
21
22 import android.os.Bundle;
23 import android.widget.FrameLayout;
24
25 import androidx.annotation.NonNull;
26 import androidx.fragment.app.Fragment;
27 import androidx.fragment.app.FragmentManager;
28 import androidx.fragment.app.FragmentPagerAdapter;
29 import androidx.viewpager.widget.ViewPager;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
33 import com.stoutner.privacybrowser.views.NestedScrollWebView;
34
35 import java.util.LinkedList;
36
37 public class WebViewPagerAdapter extends FragmentPagerAdapter {
38     // The WebView fragments list contains all the WebViews.
39     private final LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
40
41     // Define the constructor.
42     public WebViewPagerAdapter(FragmentManager fragmentManager) {
43         // Run the default commands.
44         super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
45     }
46
47     @Override
48     public int getCount() {
49         // Return the number of pages.
50         return webViewFragmentsList.size();
51     }
52
53     @Override
54     public int getItemPosition(@NonNull Object object) {
55         //noinspection SuspiciousMethodCalls
56         if (webViewFragmentsList.contains(object)) {
57             // Return the current page position.
58             //noinspection SuspiciousMethodCalls
59             return webViewFragmentsList.indexOf(object);
60         } else {
61             // The tab has been deleted.
62             return POSITION_NONE;
63         }
64     }
65
66     @Override
67     @NonNull
68     public Fragment getItem(int pageNumber) {
69         // Get the fragment for a particular page.  Page numbers are 0 indexed.
70         return webViewFragmentsList.get(pageNumber);
71     }
72
73     @Override
74     public long getItemId(int position) {
75         // Return the unique ID for this page.
76         return webViewFragmentsList.get(position).fragmentId;
77     }
78
79     public int getPositionForId(long fragmentId) {
80         // Initialize the position variable.
81         int position = -1;
82
83         // Initialize the while counter.
84         int i = 0;
85
86         // Find the current position of the WebView fragment with the given ID.
87         while (position < 0 && i < webViewFragmentsList.size()) {
88             // Check to see if the tab ID of this WebView matches the page ID.
89             if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
90                 // Store the position if they are a match.
91                 position = i;
92             }
93
94             // Increment the counter.
95             i++;
96         }
97
98         // Set the position to be the last tab if it is not found.
99         // Sometimes there is a race condition in populating the webView fragments list when resuming Privacy Browser and displaying an SSL certificate error while loading a new intent.
100         // In that case, the last tab should be the one it is looking for.
101         if (position == -1) {
102             position = webViewFragmentsList.size() - 1;
103         }
104
105         // Return the position.
106         return position;
107     }
108
109     public void addPage(int pageNumber, ViewPager webViewPager, String url, boolean moveToNewPage) {
110         // Add a new page.
111         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
112
113         // Update the view pager.
114         notifyDataSetChanged();
115
116         // Move to the new page if indicated.
117         if (moveToNewPage) {
118             webViewPager.setCurrentItem(pageNumber);
119         }
120     }
121
122     public void restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
123         // Restore the page.
124         webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState));
125
126         // Update the view pager.
127         notifyDataSetChanged();
128     }
129
130     public boolean deletePage(int pageNumber, ViewPager webViewPager) {
131         // Get the WebView tab fragment.
132         WebViewTabFragment webViewTabFragment = webViewFragmentsList.get(pageNumber);
133
134         // Get the WebView frame layout.
135         FrameLayout webViewFrameLayout = (FrameLayout) webViewTabFragment.getView();
136
137         // Get a handle for the nested scroll WebView.
138         NestedScrollWebView nestedScrollWebView = webViewFrameLayout.findViewById(R.id.nestedscroll_webview);
139
140         // Pause the current WebView.
141         nestedScrollWebView.onPause();
142
143         // Remove all the views from the frame layout.
144         webViewFrameLayout.removeAllViews();
145
146         // Destroy the current WebView.
147         nestedScrollWebView.destroy();
148
149         // Delete the page.
150         webViewFragmentsList.remove(pageNumber);
151
152         // Update the view pager.
153         notifyDataSetChanged();
154
155         // Return true if the selected page number did not change after the delete (because the newly selected tab has has same number as the previously deleted tab).
156         // This will cause the calling method to reset the current WebView to the new contents of this page number.
157         return (webViewPager.getCurrentItem() == pageNumber);
158     }
159
160     public WebViewTabFragment getPageFragment(int pageNumber) {
161         // Return the page fragment.
162         return webViewFragmentsList.get(pageNumber);
163     }
164 }