Skip to content

Commit

Permalink
Properly support predictive back
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Feb 5, 2023
1 parent 724f5cc commit f3dcec5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 44 deletions.
30 changes: 18 additions & 12 deletions mobile/src/main/java/com/github/shadowsocks/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import android.os.RemoteException
import android.view.KeyCharacterMap
import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
Expand Down Expand Up @@ -54,6 +56,7 @@ import com.google.android.material.snackbar.Snackbar
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.ktx.logEvent
import com.google.firebase.ktx.Firebase
import timber.log.Timber

class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPreferenceDataStoreChangeListener,
NavigationView.OnNavigationItemSelectedListener {
Expand Down Expand Up @@ -141,6 +144,21 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPref
stats = findViewById(R.id.stats)
stats.setOnClickListener { if (state == BaseService.State.Connected) stats.testConnection() }
drawer = findViewById(R.id.drawer)
val drawerHandler = object : OnBackPressedCallback(drawer.isOpen), DrawerLayout.DrawerListener {
override fun handleOnBackPressed() = drawer.closeDrawers()
override fun onDrawerSlide(drawerView: View, slideOffset: Float) { }
override fun onDrawerOpened(drawerView: View) {
isEnabled = true
}
override fun onDrawerClosed(drawerView: View) {
isEnabled = false
}
override fun onDrawerStateChanged(newState: Int) {
isEnabled = newState == DrawerLayout.STATE_IDLE == drawer.isOpen
}
}
onBackPressedDispatcher.addCallback(drawerHandler)
drawer.addDrawerListener(drawerHandler)
navigation = findViewById(R.id.navigation)
navigation.setNavigationItemSelectedListener(this)
if (savedInstanceState == null) {
Expand Down Expand Up @@ -208,18 +226,6 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Callback, OnPref
connection.bandwidthTimeout = 500
}

override fun onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) drawer.closeDrawers() else {
val currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_holder) as ToolbarFragment
if (!currentFragment.onBackPressed()) {
if (currentFragment is ProfilesFragment) super.onBackPressed() else {
navigation.menu.findItem(R.id.profiles).isChecked = true
displayFragment(ProfilesFragment())
}
}
}
}

override fun onKeyShortcut(keyCode: Int, event: KeyEvent) = when {
keyCode == KeyEvent.KEYCODE_G && event.hasModifiers(KeyEvent.META_CTRL_ON) -> {
toggle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.content.DialogInterface
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.component1
import androidx.activity.result.component2
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -47,6 +48,11 @@ class ProfileConfigActivity : AppCompatActivity() {
}

private val child by lazy { supportFragmentManager.findFragmentById(R.id.content) as ProfileConfigFragment }
val unsavedChangesHandler = object : OnBackPressedCallback(DataStore.dirty) {
override fun handleOnBackPressed() = UnsavedChangesDialogFragment().apply {
key()
}.show(supportFragmentManager, null)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -57,6 +63,12 @@ class ProfileConfigActivity : AppCompatActivity() {
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(R.drawable.ic_navigation_close)
}
onBackPressedDispatcher.addCallback(unsavedChangesHandler)
}

override fun onResume() {
super.onResume()
unsavedChangesHandler.isEnabled = DataStore.dirty
}

override fun onSupportNavigateUp(): Boolean {
Expand All @@ -70,12 +82,6 @@ class ProfileConfigActivity : AppCompatActivity() {
}
override fun onOptionsItemSelected(item: MenuItem) = child.onOptionsItemSelected(item)

override fun onBackPressed() {
if (DataStore.dirty) UnsavedChangesDialogFragment().apply {
key()
}.show(supportFragmentManager, null) else super.onBackPressed()
}

val pluginHelp = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
(resultCode, data) ->
if (resultCode == Activity.RESULT_OK) AlertDialog.Builder(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
private lateinit var receiver: BroadcastReceiver
private lateinit var udpFallback: Preference

private fun makeDirt() {
DataStore.dirty = true
(activity as ProfileConfigActivity).unsavedChangesHandler.isEnabled = true
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.privateStore
val activity = requireActivity()
Expand All @@ -97,7 +102,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
isProxyApps.isEnabled = serviceMode == Key.modeVpn
isProxyApps.setOnPreferenceChangeListener { _, newValue ->
startActivity(Intent(activity, AppManager::class.java))
if (newValue as Boolean) DataStore.dirty = true
if (newValue as Boolean) makeDirt()
newValue
}
findPreference<Preference>(Key.metered)!!.apply {
Expand Down Expand Up @@ -136,7 +141,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
}
pluginConfiguration = PluginConfiguration(pluginConfiguration.pluginsOptions, override ?: selected.id)
DataStore.plugin = pluginConfiguration.toString()
DataStore.dirty = true
makeDirt()
plugin.value = pluginConfiguration.selected
pluginConfigure.isEnabled = selected !is NoPlugin
pluginConfigure.text = pluginConfiguration.getOptions().toString()
Expand Down Expand Up @@ -199,15 +204,15 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
pluginConfiguration = PluginConfiguration(pluginConfiguration.pluginsOptions +
(pluginConfiguration.selected to PluginOptions(selected, newValue as? String?)), selected)
DataStore.plugin = pluginConfiguration.toString()
DataStore.dirty = true
makeDirt()
true
} catch (exc: RuntimeException) {
Snackbar.make(requireView(), exc.readableMessage, Snackbar.LENGTH_LONG).show()
false
}

override fun onPreferenceDataStoreChanged(store: PreferenceDataStore, key: String) {
if (key != Key.proxyApps && findPreference<Preference>(key) != null) DataStore.dirty = true
if (key != Key.proxyApps && findPreference<Preference>(key) != null) makeDirt()
}

override fun onDisplayPreferenceDialog(preference: Preference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ open class ToolbarFragment : Fragment() {
toolbar.setNavigationIcon(R.drawable.ic_navigation_menu)
toolbar.setNavigationOnClickListener { (activity as MainActivity).drawer.openDrawer(GravityCompat.START) }
}

open fun onBackPressed(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.github.shadowsocks.acl

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.os.Parcelable
Expand All @@ -31,6 +32,7 @@ import android.widget.AdapterView
import android.widget.EditText
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -371,7 +373,10 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener,
private lateinit var undoManager: UndoSnackbarManager<Any>

private fun onSelectedItemsUpdated() {
if (selectedItems.isEmpty()) mode?.finish() else if (mode == null) mode = toolbar.startActionMode(this)
if (selectedItems.isEmpty()) mode?.finish() else if (mode == null) {
mode = toolbar.startActionMode(this)
backHandler.isEnabled = true
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
Expand Down Expand Up @@ -418,12 +423,15 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener,
}).attachToRecyclerView(list)
}

override fun onBackPressed(): Boolean {
val mode = mode
return if (mode != null) {
mode.finish()
true
} else super.onBackPressed()
private val backHandler = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
mode?.finish()
}
}

override fun onAttach(context: Context) {
super.onAttach(context)
requireActivity().onBackPressedDispatcher.addCallback(backHandler)
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -477,6 +485,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener,
}

override fun onDetach() {
backHandler.remove()
undoManager.flush()
mode?.finish()
super.onDetach()
Expand Down Expand Up @@ -518,6 +527,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener,
selectedItems.clear()
onSelectedItemsUpdated()
adapter.notifyDataSetChanged()
backHandler.isEnabled = false
this.mode = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import android.os.Bundle
import android.os.Parcelable
import android.text.Editable
import android.text.TextWatcher
import android.view.*
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.EditText
import android.widget.TextView
Expand Down Expand Up @@ -115,8 +118,7 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener
}
}

private inner class SubViewHolder(view: View) : RecyclerView.ViewHolder(view),
View.OnClickListener {
private inner class SubViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
lateinit var item: URL
private val text = view.findViewById<TextView>(android.R.id.text1)

Expand Down Expand Up @@ -197,7 +199,6 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener

private val adapter by lazy { SubscriptionAdapter() }
private lateinit var list: RecyclerView
private var mode: ActionMode? = null
private lateinit var undoManager: UndoSnackbarManager<URL>

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
Expand Down Expand Up @@ -238,14 +239,6 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener
}).attachToRecyclerView(list)
}

override fun onBackPressed(): Boolean {
val mode = mode
return if (mode != null) {
mode.finish()
true
} else super.onBackPressed()
}

override fun onMenuItemClick(item: MenuItem): Boolean = when (item.itemId) {
R.id.action_manual_settings -> {
SubDialogFragment().apply {
Expand All @@ -264,7 +257,6 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener

override fun onDetach() {
undoManager.flush()
mode?.finish()
super.onDetach()
}
}

0 comments on commit f3dcec5

Please sign in to comment.