Skip to content

Commit

Permalink
Fix incognito mode options menu crash
Browse files Browse the repository at this point in the history
There was a crash when going back from domain or other settings in options menu.
  • Loading branch information
Slion committed Sep 29, 2023
1 parent 3242de0 commit d9bd571
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
53 changes: 4 additions & 49 deletions app/src/main/java/acr/browser/lightning/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package acr.browser.lightning

import acr.browser.lightning.browser.activity.BrowserActivity
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.CookieManager
import android.webkit.CookieSyncManager
import androidx.fragment.app.FragmentManager
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Completable
import javax.inject.Inject


/**
* Not used in incognito mode
*/
@AndroidEntryPoint
class MainActivity @Inject constructor(): BrowserActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
class MainActivity @Inject constructor(): BrowserActivity() {

@Suppress("DEPRECATION")
public override fun updateCookiePreference(): Completable = Completable.fromAction {
Expand Down Expand Up @@ -64,45 +60,4 @@ class MainActivity @Inject constructor(): BrowserActivity(), PreferenceFragmentC
return super.dispatchKeyEvent(event)
}

/**
* Needed to have animations while navigating our settings.
* Also used to back up our stack.
* See [PreferenceFragmentCompat.onPreferenceTreeClick].
*/
@SuppressLint("PrivateResource")
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, preference: Preference): Boolean {
val fragmentManager: FragmentManager = caller.parentFragmentManager

// No actual fragment specified, just a back action
if (preference.fragment == "back") {
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
}

// Launch specified fragment
val args: Bundle = preference.extras
val fragment = fragmentManager.fragmentFactory.instantiate(classLoader, preference.fragment!!)
fragment.arguments = args
fragmentManager.beginTransaction()
// Use standard bottom sheet animations
.setCustomAnimations(com.google.android.material.R.anim.design_bottom_sheet_slide_in,
com.google.android.material.R.anim.design_bottom_sheet_slide_out,
com.google.android.material.R.anim.design_bottom_sheet_slide_in,
com.google.android.material.R.anim.design_bottom_sheet_slide_out)
.replace((caller.requireView().parent as View).id, fragment)
.addToBackStack(null)
.commit()
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ import androidx.core.view.isVisible
import androidx.customview.widget.ViewDragHelper
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentManager
import androidx.palette.graphics.Palette
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.*
Expand Down Expand Up @@ -126,7 +129,7 @@ import kotlin.system.exitProcess
*
*/
@AndroidEntryPoint
abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIController, OnClickListener {
abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIController, OnClickListener, PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

// Notifications
lateinit var CHANNEL_ID: String
Expand Down Expand Up @@ -4369,6 +4372,47 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
super.onProvideKeyboardShortcuts(data, menu, deviceId)
}

/**
* Needed to have animations while navigating our settings.
* Also used to back up our stack.
* See [PreferenceFragmentCompat.onPreferenceTreeClick].
*/
@SuppressLint("PrivateResource")
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, preference: Preference): Boolean {
val fragmentManager: FragmentManager = caller.parentFragmentManager

// No actual fragment specified, just a back action
if (preference.fragment == "back") {
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
}

// Launch specified fragment
val args: Bundle = preference.extras
val fragment = fragmentManager.fragmentFactory.instantiate(classLoader, preference.fragment!!)
fragment.arguments = args
fragmentManager.beginTransaction()
// Use standard bottom sheet animations
.setCustomAnimations(com.google.android.material.R.anim.design_bottom_sheet_slide_in,
com.google.android.material.R.anim.design_bottom_sheet_slide_out,
com.google.android.material.R.anim.design_bottom_sheet_slide_in,
com.google.android.material.R.anim.design_bottom_sheet_slide_out)
.replace((caller.requireView().parent as View).id, fragment)
.addToBackStack(null)
.commit()
return true;
}

companion object {

private const val TAG = "BrowserActivity"
Expand Down

0 comments on commit d9bd571

Please sign in to comment.