/*
- * Copyright 2017-2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2017-2024 Soren Stoutner <soren@stoutner.com>.
*
- * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
+ * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android/>.
*
* Privacy Browser Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
import android.view.View.OnFocusChangeListener
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
-import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
-import androidx.core.app.NavUtils
+import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
private lateinit var urlEditText: EditText
private lateinit var sslInformationTitleTextView: TextView
private lateinit var sslInformationTextView: TextView
- private lateinit var ciphersButton: Button
- private lateinit var certificateButton: Button
+ private lateinit var sslButtonsConstraintLayout: ConstraintLayout
private lateinit var requestHeadersTitleTextView: TextView
private lateinit var requestHeadersTextView: TextView
private lateinit var responseMessageTitleTextView: TextView
private val saveTextActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { fileUri ->
// Only save the file if the URI is not null, which happens if the user exited the file picker by pressing back.
if (fileUri != null) {
- // Initialize the file name string from the file URI last path segment.
- var fileNameString = fileUri.lastPathSegment
+ // Get a cursor from the content resolver.
+ val contentResolverCursor = contentResolver.query(fileUri, null, null, null)!!
- // Query the exact file name if the API >= 26.
- if (Build.VERSION.SDK_INT >= 26) {
- // Get a cursor from the content resolver.
- val contentResolverCursor = contentResolver.query(fileUri, null, null, null)!!
+ // Move to the first row.
+ contentResolverCursor.moveToFirst()
- // Move to the first row.
- contentResolverCursor.moveToFirst()
+ // Get the file name from the cursor.
+ val fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
- // Get the file name from the cursor.
- fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
-
- // Close the cursor.
- contentResolverCursor.close()
- }
+ // Close the cursor.
+ contentResolverCursor.close()
try {
// Get the about version string.
val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
// Disable screenshots if not allowed.
- if (!allowScreenshots) {
+ if (!allowScreenshots)
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
- }
// Run the default commands.
super.onCreate(savedInstanceState)
val userAgent = intent.getStringExtra(USER_AGENT)!!
// Set the content view.
- if (bottomAppBar) {
+ if (bottomAppBar)
setContentView(R.layout.view_headers_bottom_appbar)
- } else {
+ else
setContentView(R.layout.view_headers_top_appbar)
- }
// Get a handle for the toolbar.
val toolbar = findViewById<Toolbar>(R.id.toolbar)
actionBar.setCustomView(R.layout.view_headers_appbar_custom_view)
// Instruct the action bar to display a custom layout.
- actionBar.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
+ actionBar.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM or ActionBar.DISPLAY_HOME_AS_UP
// Get handles for the views.
urlEditText = findViewById(R.id.url_edittext)
val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swiperefreshlayout)
sslInformationTitleTextView = findViewById(R.id.ssl_information_title_textview)
sslInformationTextView = findViewById(R.id.ssl_information_textview)
- ciphersButton = findViewById(R.id.ciphers_button)
- certificateButton = findViewById(R.id.certificate_button)
+ sslButtonsConstraintLayout = findViewById(R.id.ssl_buttons_constraintlayout)
requestHeadersTitleTextView = findViewById(R.id.request_headers_title_textview)
requestHeadersTextView = findViewById(R.id.request_headers_textview)
responseMessageTitleTextView = findViewById(R.id.response_message_title_textview)
}
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
- // Run the appropriate commands.
+ // Run the commands that correlate to the selected menu item.
when (menuItem.itemId) {
R.id.copy_headers -> { // Copy the headers.
// Get the headers string.
// Display a snackbar if the API <= 32 (Android 12L). Beginning in Android 13 the OS displays a notification that covers up the snackbar.
if (Build.VERSION.SDK_INT <= 32)
- Snackbar.make(urlEditText, R.string.version_info_copied, Snackbar.LENGTH_SHORT).show()
+ Snackbar.make(urlEditText, R.string.headers_copied, Snackbar.LENGTH_SHORT).show()
// Consume the event.
return true
}
else -> { // The home button was selected.
- // Run the parents class on return.
+ // Do not consume the event. The system will process the home command.
return super.onOptionsItemSelected(menuItem)
}
}
}
- // This method must be named `goBack()` and must have a View argument to match the default back arrow in the app bar or a crash occurs.
- fun goBack(@Suppress("UNUSED_PARAMETER") view: View) {
- // Go home.
- NavUtils.navigateUpFromSameTask(this)
- }
-
private fun getHeadersString(): String {
// Initialize a headers string builder.
val headersStringBuilder = StringBuilder()
// Hide the unused views.
sslInformationTitleTextView.visibility = View.GONE
sslInformationTextView.visibility = View.GONE
- ciphersButton.visibility = View.GONE
- certificateButton.visibility = View.GONE
+ sslButtonsConstraintLayout.visibility = View.GONE
requestHeadersTitleTextView.visibility = View.GONE
requestHeadersTextView.visibility = View.GONE
responseMessageTitleTextView.visibility = View.GONE
// Hide the SSL information views.
sslInformationTitleTextView.visibility = View.GONE
sslInformationTextView.visibility = View.GONE
- ciphersButton.visibility = View.GONE
- certificateButton.visibility = View.GONE
+ sslButtonsConstraintLayout.visibility = View.GONE
} else { // This is not an HTTP URL.
// Show the SSL information views.
sslInformationTitleTextView.visibility = View.VISIBLE
sslInformationTextView.visibility = View.VISIBLE
- ciphersButton.visibility = View.VISIBLE
- certificateButton.visibility = View.VISIBLE
+ sslButtonsConstraintLayout.visibility = View.VISIBLE
}
// Show the other views.