import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
+import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
// We need to use AppCompatActivity from android.support.v7.app.AppCompatActivity to have access to the SupportActionBar until the minimum API is >= 21.
public class MainWebViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener,
- SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener, DownloadImage.DownloadImageListener {
+ SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener, DownloadImage.DownloadImageListener, UrlHistory.UrlHistoryListener {
// `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
// It is also used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`.
// Create the navigation drawer.
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
- // The `DrawerTitle` identifies the drawer in accessibility mode.
+ // `DrawerTitle` identifies the drawer in accessibility mode.
drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
// Listen for touches on the navigation menu.
final Menu navigationMenu = navigationView.getMenu();
final MenuItem navigationBackMenuItem = navigationMenu.getItem(1);
final MenuItem navigationForwardMenuItem = navigationMenu.getItem(2);
+ final MenuItem navigationHistoryMenuItem = navigationMenu.getItem(3);
// The `DrawerListener` allows us to update the Navigation Menu.
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerStateChanged(int newState) {
- // Update the back and forward menu items every time the drawer opens.
+ // Update the `Back`, `Forward`, and `History` menu items every time the drawer opens.
navigationBackMenuItem.setEnabled(mainWebView.canGoBack());
navigationForwardMenuItem.setEnabled(mainWebView.canGoForward());
+ navigationHistoryMenuItem.setEnabled((mainWebView.canGoBack() || mainWebView.canGoForward()));
+
+ // Hide the keyboard so we can see the navigation menu. `0` indicates no additional flags.
+ inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
}
});
return true;
case R.id.addToHomescreen:
- // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `@string/create_shortcut`.
+ // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `R.string.create_shortcut`.
AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut();
createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut));
}
break;
+ case R.id.history:
+ // Gte the `WebBackForwardList`.
+ WebBackForwardList webBackForwardList = mainWebView.copyBackForwardList();
+
+ // Show the `UrlHistory` `AlertDialog` and name this instance `R.string.history`. `this` is the `Context`.
+ AppCompatDialogFragment urlHistoryDialogFragment = UrlHistory.loadBackForwardList(this, webBackForwardList);
+ urlHistoryDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.history));
+ break;
+
case R.id.bookmarks:
// Launch BookmarksActivity.
Intent bookmarksIntent = new Intent(this, BookmarksActivity.class);
sslErrorHandler.proceed();
}
+ @Override
+ public void onUrlHistoryEntrySelected(int moveBackOrForwardSteps) {
+ // Load the history entry.
+ mainWebView.goBackOrForward(moveBackOrForwardSteps);
+ }
+
+ @Override
+ public void onClearHistory() {
+ // Clear the history.
+ mainWebView.clearHistory();
+ }
+
// Override onBackPressed to handle the navigation drawer and mainWebView.
@Override
public void onBackPressed() {
@Override
public void onPause() {
+ // Pause `mainWebView`.
+ mainWebView.onPause();
+ mainWebView.pauseTimers();
+
// We need to pause the adView or it will continue to consume resources in the background on the free flavor.
BannerAd.pauseAd(adView);
public void onResume() {
super.onResume();
+ // Resume `mainWebView`.
+ mainWebView.resumeTimers();
+ mainWebView.onResume();
+
// We need to resume the adView for the free flavor.
BannerAd.resumeAd(adView);
}
mainWebView.loadUrl(formattedUrlString, customHeaders);
- // Hides the keyboard so we can see the webpage. `0` indicates no additional flags.
+ // Hide the keyboard so we can see the webpage. `0` indicates no additional flags.
inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
}
Toolbar appBarToolbar = (Toolbar) findViewById(R.id.appBar);
appBarToolbar.setVisibility(View.VISIBLE);
- // Hides the keyboard so we can see the webpage. `0` indicates no additional flags.
+ // Hide the keyboard so we can see the webpage. `0` indicates no additional flags.
inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0);
}