/*
- * Copyright © 2017-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2017-2020 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
*
package com.stoutner.privacybrowser.activities;
import android.app.Activity;
-import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.res.Configuration;
import android.os.Bundle;
+import android.preference.PreferenceManager;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
+import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
+import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; // The AndroidX toolbar must be used until the minimum API is >= 21.
import androidx.core.app.NavUtils;
+import androidx.fragment.app.DialogFragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.stoutner.privacybrowser.R;
@Override
protected void onCreate(Bundle savedInstanceState) {
+ // Get a handle for the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+
+ // Get the screenshot preference.
+ boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
// Disable screenshots if not allowed.
- if (!MainWebViewActivity.allowScreenshots) {
+ if (!allowScreenshots) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
// Set the theme.
- if (MainWebViewActivity.darkTheme) {
- setTheme(R.style.PrivacyBrowserDark);
- } else {
- setTheme(R.style.PrivacyBrowserLight);
- }
+ setTheme(R.style.PrivacyBrowser);
// Run the default commands.
super.onCreate(savedInstanceState);
// Get the launching intent
Intent intent = getIntent();
- // Get the user agent.
+ // Get the information from the intent.
String userAgent = intent.getStringExtra("user_agent");
+ String currentUrl = intent.getStringExtra("current_url");
// Store a handle for the current activity.
activity = this;
// Get a handle for the url text box.
EditText urlEditText = findViewById(R.id.url_edittext);
- // Get the formatted URL string from the main activity.
- String formattedUrlString = MainWebViewActivity.formattedUrlString;
-
// Populate the URL text box.
- urlEditText.setText(formattedUrlString);
+ urlEditText.setText(currentUrl);
// Initialize the foreground color spans for highlighting the URLs. We have to use the deprecated `getColor()` until API >= 23.
redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
// Get new source data for the current URL if it beings with `http`.
if (url.startsWith("http")) {
- new GetSource(this, userAgent).execute(url);
+ new GetSource(this, this, userAgent).execute(url);
}
// Consume the key press.
}
});
- // Implement swipe to refresh.
+ // Get a handle for the swipe refresh layout.
SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.view_source_swiperefreshlayout);
+
+ // Implement swipe to refresh.
swipeRefreshLayout.setOnRefreshListener(() -> {
// Get the URL.
String url = urlEditText.getText().toString();
// Get new source data for the URL if it begins with `http`.
if (url.startsWith("http")) {
- new GetSource(this, userAgent).execute(url);
+ new GetSource(this, this, userAgent).execute(url);
} else {
// Stop the refresh animation.
swipeRefreshLayout.setRefreshing(false);
}
});
- // Set the swipe to refresh color according to the theme.
- if (MainWebViewActivity.darkTheme) {
- swipeRefreshLayout.setColorSchemeResources(R.color.blue_600);
- swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.gray_800);
+ // Get the current theme status.
+ int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
+ // Set the refresh color scheme according to the theme.
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ swipeRefreshLayout.setColorSchemeResources(R.color.blue_500);
} else {
swipeRefreshLayout.setColorSchemeResources(R.color.blue_700);
}
+ // Initialize a color background typed value.
+ TypedValue colorBackgroundTypedValue = new TypedValue();
+
+ // Get the color background from the theme.
+ getTheme().resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true);
+
+ // Get the color background int from the typed value.
+ int colorBackgroundInt = colorBackgroundTypedValue.data;
+
+ // Set the swipe refresh background color.
+ swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt);
+
// Get the source using an AsyncTask if the URL begins with `http`.
- if (formattedUrlString.startsWith("http")) {
- new GetSource(this, userAgent).execute(formattedUrlString);
+ if ((currentUrl != null) && currentUrl.startsWith("http")) {
+ new GetSource(this, this, userAgent).execute(currentUrl);
}
}
}
@Override
- public boolean onOptionsItemSelected(MenuItem menuItem) {
+ public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
// Get a handle for the about alert dialog.
DialogFragment aboutDialogFragment = new AboutViewSourceDialog();
// Show the about alert dialog.
- aboutDialogFragment.show(getFragmentManager(), getString(R.string.about));
+ aboutDialogFragment.show(getSupportFragmentManager(), getString(R.string.about));
// Consume the event.
return true;