public class MainWebViewActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener {
// favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut.
public static Bitmap favoriteIcon;
- // mainWebView is public static so it can be accessed from AboutDialog. It is also used in onCreate(), onOptionsItemSelected(), onNavigationItemSelected(), and loadUrlFromTextBox().
+ // mainWebView is public static so it can be accessed from AboutDialog and SettingsFragment. It is also used in onCreate(), onOptionsItemSelected(), onNavigationItemSelected(), and loadUrlFromTextBox().
public static WebView mainWebView;
+ // mainMenu is public static so it can be accessed from SettingsFragment. It is also used in onCreateOptionsMenu() and onOptionsItemSelected().
+ public static Menu mainMenu;
+ // cookieManager is public static so it can be accessed from SettingsFragment. It is also used in onCreate(), onOptionsItemSelected(), and onNavigationItemSelected().
+ public static CookieManager cookieManager;
+ // javaScriptEnabled is public static so it can be accessed from SettingsFragment. It is also used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
+ public static boolean javaScriptEnabled;
+ // firstPartyCookiesEnabled is public static so it can be accessed from SettingsFragment. It is also used in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
+ public static boolean firstPartyCookiesEnabled;
+ // thirdPartyCookiesEnabled is uesd in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
+ public static boolean thirdPartyCookiesEnabled;
+ // domStorageEnabled is public static so it can be accessed from SettingsFragment. It is also used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
+ public static boolean domStorageEnabled;
+ // javaScriptDisabledSearchURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox().
+ public static String javaScriptDisabledSearchURL;
+ // javaScriptDisabledSearchCustomURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox().
+ public static String javaScriptDisabledSearchCustomURL;
+ // javaScriptEnabledSearchURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox().
+ public static String javaScriptEnabledSearchURL;
+ // javaScriptEnabledSearchCustomURL is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and loadURLFromTextBox().
+ public static String javaScriptEnabledSearchCustomURL;
+ // homepage is public static so it can be accessed from SettingsFragment. It is also used in onCreate() and onOptionsItemSelected().
+ public static String homepage;
+ // swipeToRefresh is public static so it can be accessed from SettingsFragment. It is also used in onCreate().
+ public static SwipeRefreshLayout swipeToRefresh;
+ // swipeToRefreshEnabled is public static so it can be accessed from SettingsFragment. It is also used in onCreate().
+ public static boolean swipeToRefreshEnabled;
+
// drawerToggle is used in onCreate(), onPostCreate(), onConfigurationChanged(), onNewIntent(), and onNavigationItemSelected().
private ActionBarDrawerToggle drawerToggle;
// drawerLayout is used in onCreate(), onNewIntent(), and onBackPressed().
private DrawerLayout drawerLayout;
- // mainMenu is used in onCreateOptionsMenu() and onOptionsItemSelected().
- private Menu mainMenu;
// formattedUrlString is used in onCreate(), onOptionsItemSelected(), onCreateHomeScreenShortcutCreate(), and loadUrlFromTextBox().
private String formattedUrlString;
- // homepage is used in onCreate() and onOptionsItemSelected().
- private String homepage;
- // javaScriptEnabled is used in onCreate(), onCreateOptionsMenu(), onOptionsItemSelected(), and loadUrlFromTextBox().
- private boolean javaScriptEnabled;
- // domStorageEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
- private boolean domStorageEnabled;
-
- /* saveFormDataEnabled does nothing until database storage is implemented.
- // saveFormDataEnabled is used in onCreate(), onCreateOptionsMenu(), and onOptionsItemSelected().
- private boolean saveFormDataEnabled;
- */
-
- // cookieManager is used in onCreate(), onOptionsItemSelected(), and onNavigationItemSelected().
- private CookieManager cookieManager;
- // firstPartyCookiesEnabled is used in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
- private boolean firstPartyCookiesEnabled;
- // thirdPartyCookiesEnabled is uesd in onCreate(), onCreateOptionsMenu(), onPrepareOptionsMenu(), and onOptionsItemSelected().
- private boolean thirdPartyCookiesEnabled;
// urlTextBox is used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox().
private EditText urlTextBox;
final View adView = findViewById(R.id.adView);
// Implement swipe to refresh
- final SwipeRefreshLayout swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
+ swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeToRefresh.setColorSchemeResources(R.color.blue);
swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
// Hide zoom controls.
mainWebView.getSettings().setDisplayZoomControls(false);
+
// Initialize the default preference values the first time the program is run.
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
// Get the shared preference values.
SharedPreferences savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- // Set JavaScript initial status.
+ // Set JavaScript initial status. The default value is false.
javaScriptEnabled = savedPreferences.getBoolean("javascript_enabled", false);
mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
// Initialize cookieManager.
cookieManager = CookieManager.getInstance();
- // Set cookies initial status.
+ // Set cookies initial status. The default value is false.
firstPartyCookiesEnabled = savedPreferences.getBoolean("first_party_cookies_enabled", false);
cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
- // Set third-party cookies initial status if API >= 21.
+ // Set third-party cookies initial status if API >= 21. The default value is false.
if (Build.VERSION.SDK_INT >= 21) {
thirdPartyCookiesEnabled = savedPreferences.getBoolean("third_party_cookies_enabled", false);
cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled);
}
- // Set DOM storage initial status.
+ // Set DOM storage initial status. The default value is false.
domStorageEnabled = savedPreferences.getBoolean("dom_storage_enabled", false);
mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
- // Set homepage initial status.
+ // Set the user agent initial status.
+ String userAgentString = savedPreferences.getString("user_agent", "Default user agent");
+ switch (userAgentString) {
+ case "Default user agent":
+ // Do nothing.
+ break;
+
+ case "Custom user agent":
+ // Set the custom user agent on mainWebView, The default is "PrivacyBrowser/1.0".
+ mainWebView.getSettings().setUserAgentString(savedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0"));
+ break;
+
+ default:
+ // Set the selected user agent on mainWebView. The default is "PrivacyBrowser/1.0".
+ mainWebView.getSettings().setUserAgentString(savedPreferences.getString("user_agent", "PrivacyBrowser/1.0"));
+ break;
+ }
+
+ // Set the initial status for the search URLs.
+ javaScriptDisabledSearchURL = savedPreferences.getString("javascript_disabled_search", "https://duckduckgo.com/html/?q=");
+ javaScriptDisabledSearchCustomURL = savedPreferences.getString("javascript_disabled_search_custom_url", "");
+ javaScriptEnabledSearchURL = savedPreferences.getString("javascript_enabled_search", "https://duckduckgo.com/?q=");
+ javaScriptEnabledSearchCustomURL = savedPreferences.getString("javascript_enabled_search_custom_url", "");
+
+ // Set homepage initial status. The default value is "https://www.duckduckgo.com".
homepage = savedPreferences.getString("homepage", "https://www.duckduckgo.com");
+ // Set swipe to refresh initial status. The default is true.
+ swipeToRefreshEnabled = savedPreferences.getBoolean("swipe_to_refresh_enabled", true);
+ swipeToRefresh.setEnabled(swipeToRefreshEnabled);
+
+
// Get the intent information that started the app.
final Intent intent = getIntent();
//Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
return true;
+ case R.id.refresh:
+ mainWebView.reload();
+ return true;
+
default:
+ // Don't consume the event.
return super.onOptionsItemSelected(menuItem);
}
}
// Use the correct search URL based on javaScriptEnabled.
if (javaScriptEnabled) {
- formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
- } else {
- formattedUrlString = "https://duckduckgo.com/html/?q=" + encodedUrlString;
+ if (javaScriptEnabledSearchURL.equals("Custom URL")) {
+ formattedUrlString = javaScriptEnabledSearchCustomURL + encodedUrlString;
+ } else {
+ formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString;
+ }
+ } else { // JavaScript is disabled.
+ if (javaScriptDisabledSearchURL.equals("Custom URL")) {
+ formattedUrlString = javaScriptDisabledSearchCustomURL + encodedUrlString;
+ } else {
+ formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString;
+ }
}
}