Skip to content

Commit

Permalink
Move security error to domain settings
Browse files Browse the repository at this point in the history
Improve SSL error handling.
Part of #159.
Close #72.
  • Loading branch information
Slion committed Sep 24, 2023
1 parent 44e712b commit 4a4774c
Show file tree
Hide file tree
Showing 25 changed files with 249 additions and 167 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/acr/browser/lightning/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ class MainActivity @Inject constructor(): BrowserActivity(), PreferenceFragmentC

// No actual fragment specified, just a back action
if (preference.fragment == "back") {
fragmentManager.popBackStack()
if (fragmentManager.backStackEntryCount >=1) {
// Go back to previous fragment if any
fragmentManager.popBackStack()
} else {
// Close our bottom sheet if not previous fragment
// Needed for the case where we jump directly to a domain settings without going through option
// Notably happening when security error is set to no and snackbar action is shown
// Actually should not be needed now that we hide the back button in that case.
iBottomSheet.dismiss()
}

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
private lateinit var bookmarksDialog: BottomSheetDialog

// Options settings menu
private val iBottomSheet = BottomSheetDialogFragment(supportFragmentManager)
val iBottomSheet = BottomSheetDialogFragment(supportFragmentManager)

// Binding
lateinit var iBinding: ActivityMainBinding
Expand Down Expand Up @@ -538,7 +538,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
//
onMenuItemClicked(iBinding.menuItemSettings) { dismiss(); executeAction(R.id.action_settings) }
onMenuItemClicked(iBinding.menuItemOptions) {
dismiss();
dismiss()
app.domain = currentHost()
iBottomSheet.setLayout(R.layout.fragment_settings_options).show()
}
Expand All @@ -556,6 +556,14 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
}
}

/**
* Show settings for the provided domain
*/
fun showDomainSettings(aDomain: String) {
app.domain = aDomain
iBottomSheet.setLayout(R.layout.fragment_settings_domain).show()
}

/**
*
*/
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/acr/browser/lightning/di/AppBindsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import acr.browser.lightning.database.downloads.DownloadsDatabase
import acr.browser.lightning.database.downloads.DownloadsRepository
import acr.browser.lightning.database.history.HistoryDatabase
import acr.browser.lightning.database.history.HistoryRepository
import acr.browser.lightning.ssl.SessionSslWarningPreferences
import acr.browser.lightning.ssl.SslWarningPreferences
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -37,9 +35,6 @@ interface AppBindsModule {
@Binds
fun bindsHistoryModel(historyDatabase: HistoryDatabase): HistoryRepository

@Binds
fun bindsSslWarningPreferences(sessionSslWarningPreferences: SessionSslWarningPreferences): SslWarningPreferences

@Binds
fun bindsAbpRulesRepository(apbRulesDatabase: UserRulesDatabase): UserRulesRepository

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/acr/browser/lightning/di/EntryPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import acr.browser.lightning.js.TextReflow
import acr.browser.lightning.network.NetworkConnectivityModel
import acr.browser.lightning.search.SearchEngineProvider
import acr.browser.lightning.settings.preferences.UserPreferences
import acr.browser.lightning.ssl.SslWarningPreferences
import acr.browser.lightning.utils.ProxyUtils
import acr.browser.lightning.view.webrtc.WebRtcPermissionsModel
import android.app.DownloadManager
Expand Down Expand Up @@ -52,7 +51,6 @@ interface HiltEntryPoint {
@MainScheduler fun mainScheduler(): Scheduler
val searchEngineProvider: SearchEngineProvider
val proxyUtils: ProxyUtils
val sslWarningPreferences: SslWarningPreferences
val textReflowJs: TextReflow
val invertPageJs: InvertPage
val setMetaViewport: SetMetaViewport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

package acr.browser.lightning.extensions

// For comments links

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.LayoutInflater
import android.widget.Toast
import androidx.annotation.*
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import java.util.*

// For comments links
import androidx.preference.PreferenceManager

/**
* Returns the dimension in pixels.
Expand Down Expand Up @@ -82,4 +83,9 @@ fun Context.landscapeSharedPreferencesName(): String {
return packageName + "_preferences_landscape"
}


/**
* Allows us to have rich text and variable interpolation.
*/
fun Context.getText(@StringRes id: Int, vararg args: Any?): CharSequence? {
return ContextUtils.getText(this,id,*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package acr.browser.lightning.extensions;


import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_COMPACT;
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY;
import static androidx.core.text.HtmlCompat.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL;

import android.content.Context;
import android.text.SpannedString;
import android.text.TextUtils;

import androidx.annotation.StringRes;
import androidx.core.text.HtmlCompat;

public class ContextUtils {


/**
* Allows us to have rich text and variable interpolation.
* New line and white space handling is a mess though.
* Kept it in Java cause we could not get varargs to work as we wanted in Kotlin.
* Modded from: https://stackoverflow.com/a/23562910/3969362
*
* @param context
* @param id
* @param args
* @return
*/
public static CharSequence getText(Context context, @StringRes int id, Object... args) {
for (int i = 0; i < args.length; ++i)
args[i] = args[i] instanceof String ? HtmlCompat.toHtml(new SpannedString((String)args[i]),TO_HTML_PARAGRAPH_LINES_INDIVIDUAL) : args[i];
return HtmlCompat.fromHtml(String.format(HtmlCompat.toHtml(new SpannedString(context.getText(id)),TO_HTML_PARAGRAPH_LINES_INDIVIDUAL), args),FROM_HTML_MODE_COMPACT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
package acr.browser.lightning.extensions

import acr.browser.lightning.R
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.Snackbar

/**
Expand Down Expand Up @@ -65,3 +71,29 @@ fun Snackbar.addAction(@LayoutRes aLayoutId: Int, aLabel: String, aListener: Vie
button.setOnClickListener {this.dismiss(); aListener?.onClick(it)}
return this;
}

/**
* Add an icon to this snackbar.
* See: https://stackoverflow.com/a/31829381/3969362
*/
fun Snackbar.setIcon(drawable: Drawable): Snackbar {
return this.apply {
//setAction(" ") {}
val textView = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
textView.compoundDrawablePadding = context.resources.getDimensionPixelOffset(com.google.android.material.R.dimen.m3_navigation_item_icon_padding);
}
}

/**
* Add an icon to this snackbar.
*/
fun Snackbar.setIcon(@DrawableRes aIcon: Int): Snackbar {
return this.apply {
AppCompatResources.getDrawable(context, aIcon)?.let {
// Apply proper tint so that it works regardless of the theme
it.setTint(MaterialColors.getColor(context, com.google.android.material.R.attr.colorOnSurfaceInverse, Color.BLACK))
setIcon(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ abstract class AbstractSettingsFragment : PreferenceFragmentCompat() {
setPreferencesFromResource(providePreferencesXmlResource(),rootKey)
prefGroup = preferenceScreen

if (activity is SettingsActivity) {
// Hide back button preference in settings activity
// Hide back button preference in settings activity
if (activity is SettingsActivity
// Also hide back button when there is nothing to go back to
// Notably the case when SSL error settings is set to abort and snackbar is shown with direct access to domain settings
|| parentFragmentManager.backStackEntryCount == 0) {
// Back buttons are there for navigation in options menu bottom sheet
findPreference<Preference>(getString(R.string.pref_key_back))?.isVisible = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ class GeneralSettingsFragment : AbstractSettingsFragment() {
onClick = ::showHomePageDialog
)

switchPreference(
preference = SETTINGS_SHOW_SSL,
isChecked = userPreferences.ssl,
onCheckChange = { userPreferences.ssl = it }
)

clickableDynamicPreference(
preference = SETTINGS_SEARCH_ENGINE,
summary = getSearchEngineSummary(searchEngineProvider.provideSearchEngine()),
Expand Down Expand Up @@ -593,6 +587,5 @@ class GeneralSettingsFragment : AbstractSettingsFragment() {
private const val SETTINGS_SEARCH_ENGINE = "search"
private const val SETTINGS_SUGGESTIONS = "suggestions_choice"
private const val SETTINGS_FORCE_ZOOM = "force_zoom"
private const val SETTINGS_SHOW_SSL = "show_ssl"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ class DomainPreferences constructor(
val launchAppParent: NoYesAsk get() { return parent?.launchApp ?: launchAppLocal}
val launchApp: NoYesAsk get() { return if (isDefault || !launchAppOverride) { launchAppParent } else { launchAppLocal } }

/**
* Define what to do when an SSL error is detected
* - Yes: Proceed anyway
* - No: Abort
* - Ask: Ask the user whether to proceed or abort
*/
var sslErrorOverride by preferences.booleanPreference(R.string.pref_key_ssl_error_override, false)
var sslErrorLocal by preferences.enumPreference(R.string.pref_key_ssl_error, NoYesAsk.ASK)
val sslErrorParent: NoYesAsk get() { return parent?.sslError ?: sslErrorLocal}
val sslError: NoYesAsk get() { return if (isDefault || !sslErrorOverride) { sslErrorParent } else { sslErrorLocal } }

/**
* Is this the default domain settings?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,8 @@ class UserPreferences @Inject constructor(
var incognito by preferences.booleanPreference(R.string.pref_key_always_incognito, R.bool.pref_default_always_incognito)

/**
* SSL Warn Dialog
*
*/
var ssl by preferences.booleanPreference(R.string.pref_key_ssl_dialog, R.bool.pref_default_ssl_dialog)

var imageUrlString by preferences.stringPreference(R.string.pref_key_image_url, "")

/**
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/acr/browser/lightning/ssl/SslState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ sealed class SslState {
*
* @param sslError The error that is causing the invalid SSL state.
*/
class Invalid(val sslError: SslError) : SslState()
object Invalid : SslState()

}

This file was deleted.

Loading

0 comments on commit 4a4774c

Please sign in to comment.