app: Switch to toolbar APIs for working with menu

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta 2024-10-13 15:07:47 +05:30
parent a5fe52a01f
commit 82267f6844
18 changed files with 372 additions and 303 deletions

View file

@ -11,6 +11,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.transport.backup.PackageService
@ -41,12 +42,11 @@ class AboutDialogFragment : Fragment() {
contributorsView.movementMethod = linkMovementMethod
orgsView.movementMethod = linkMovementMethod
v.requireViewById<Toolbar>(R.id.toolbar).setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
return v
}
override fun onStart() {
super.onStart()
activity?.setTitle(R.string.about_title)
}
}

View file

@ -7,14 +7,13 @@ package com.stevesoltys.seedvault.settings
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.ProgressBar
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -32,7 +31,6 @@ class AppStatusFragment : Fragment(), AppStatusToggleListener {
private val layoutManager = LinearLayoutManager(context)
private val adapter = AppStatusAdapter(this)
private lateinit var appEditMenuItem: MenuItem
private lateinit var list: RecyclerView
private lateinit var progressBar: ProgressBar
@ -41,7 +39,6 @@ class AppStatusFragment : Fragment(), AppStatusToggleListener {
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
setHasOptionsMenu(true)
val v: View = inflater.inflate(R.layout.fragment_app_status, container, false)
progressBar = v.requireViewById(R.id.progressBar)
@ -53,7 +50,17 @@ class AppStatusFragment : Fragment(), AppStatusToggleListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.setTitle(R.string.settings_backup_status_title)
val toolbar = view.requireViewById<Toolbar>(R.id.toolbar).apply {
setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
viewModel.appEditMode.observe(viewLifecycleOwner) { enabled ->
toolbar.menu.findItem(R.id.edit_app_blacklist)?.isChecked = enabled
adapter.setEditMode(enabled)
}
list.apply {
layoutManager = this@AppStatusFragment.layoutManager
@ -67,24 +74,12 @@ class AppStatusFragment : Fragment(), AppStatusToggleListener {
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.app_status_menu, menu)
appEditMenuItem = menu.findItem(R.id.edit_app_blacklist)
// observe edit mode changes here where we are sure to have the MenuItem
viewModel.appEditMode.observe(viewLifecycleOwner) { enabled ->
appEditMenuItem.isChecked = enabled
adapter.setEditMode(enabled)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
private fun onMenuItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.edit_app_blacklist -> {
viewModel.setEditMode(!item.isChecked)
true
}
else -> super.onOptionsItemSelected(item)
else -> false
}
override fun onAppStatusToggled(status: AppStatus) {

View file

@ -7,7 +7,9 @@ package com.stevesoltys.seedvault.settings
import android.app.backup.IBackupManager
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
import androidx.appcompat.widget.Toolbar
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
import androidx.preference.PreferenceFragmentCompat
@ -66,9 +68,19 @@ class ExpertSettingsFragment : PreferenceFragmentCompat() {
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.settings_expert_title)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
override fun onStart() {
super.onStart()
activity?.setTitle(R.string.settings_expert_title)
apkBackup.isEnabled = backupManager.isBackupEnabled
}
}

View file

@ -8,6 +8,7 @@ package com.stevesoltys.seedvault.settings
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
@ -39,6 +40,13 @@ class SchedulingFragment : PreferenceFragmentCompat(),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.settings_backup_scheduling_title)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
val storage = backendManager.backendProperties
if (storage?.isUsb == true) {
findPreference<PreferenceCategory>("scheduling_category_conditions")?.isEnabled = false
@ -60,12 +68,6 @@ class SchedulingFragment : PreferenceFragmentCompat(),
}
}
override fun onStart() {
super.onStart()
activity?.setTitle(R.string.settings_backup_scheduling_title)
}
override fun onResume() {
super.onResume()
settingsManager.registerOnSharedPreferenceChangeListener(this)

View file

@ -30,9 +30,6 @@ class SettingsActivity : RequireProvisioningActivity(), OnPreferenceStartFragmen
setContentView(R.layout.activity_fragment_container)
setSupportActionBar(requireViewById(R.id.toolbar))
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
if (savedInstanceState == null && viewModel.isFirstStart) {
// let user choose whether to restore on first start
FirstRunFragment().show(supportFragmentManager, null)

View file

@ -11,12 +11,11 @@ import android.os.Bundle
import android.os.PowerManager
import android.os.RemoteException
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.appcompat.widget.Toolbar
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
import androidx.preference.PreferenceFragmentCompat
@ -53,9 +52,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
private lateinit var backupStorage: TwoStatePreference
private lateinit var backupRecoveryCode: Preference
private var menuBackupNow: MenuItem? = null
private var menuRestore: MenuItem? = null
private val backendProperties: BackendProperties<*>?
get() = backendManager.backendProperties
@ -63,7 +59,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
permitDiskReads {
setPreferencesFromResource(R.xml.settings, rootKey)
}
setHasOptionsMenu(true)
backup = findPreference("backup")!!
backup.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
@ -141,6 +136,20 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val toolbar = view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.backup)
inflateMenu(R.menu.settings_menu)
setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
viewModel.backupPossible.observe(viewLifecycleOwner) { possible ->
toolbar.menu.findItem(R.id.action_backup)?.isEnabled = possible
toolbar.menu.findItem(R.id.action_restore)?.isEnabled = possible
}
viewModel.lastBackupTime.observe(viewLifecycleOwner) { time ->
setAppBackupStatusSummary(time)
}
@ -157,9 +166,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onStart() {
super.onStart()
// we need to re-set the title when returning to this fragment
activity?.setTitle(R.string.backup)
setBackupEnabledState()
setBackupLocationSummary()
setAutoRestoreState()
@ -185,18 +191,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.settings_menu, menu)
menuBackupNow = menu.findItem(R.id.action_backup)
menuRestore = menu.findItem(R.id.action_restore)
viewModel.backupPossible.observe(viewLifecycleOwner) { possible ->
menuBackupNow?.isEnabled = possible
menuRestore?.isEnabled = possible
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
private fun onMenuItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.action_backup -> {
viewModel.backupNow()
true
@ -219,7 +214,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
.commit()
true
}
else -> super.onOptionsItemSelected(item)
else -> false
}
private fun trySetBackupEnabled(enabled: Boolean): Boolean {

View file

@ -6,8 +6,6 @@
package com.stevesoltys.seedvault.ui
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.stevesoltys.seedvault.R
@ -19,15 +17,6 @@ abstract class BackupActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
}
@CallSuper
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
android.R.id.home -> {
onBackPressed()
true
}
else -> super.onOptionsItemSelected(item)
}
protected fun showFragment(f: Fragment, addToBackStack: Boolean = false, tag: String? = null) {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragment, f, tag)

View file

@ -6,9 +6,8 @@
package com.stevesoltys.seedvault.ui.files
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.settings.SettingsViewModel
import org.calyxos.backup.storage.ui.backup.BackupContentFragment
@ -20,13 +19,15 @@ class FileSelectionFragment : BackupContentFragment() {
override val viewModel by viewModel<FileSelectionViewModel>()
private val settingsViewModel by sharedViewModel<SettingsViewModel>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
requireActivity().setTitle(R.string.settings_backup_files_title)
return super.onCreateView(inflater, container, savedInstanceState)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.settings_backup_files_title)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}
override fun onDestroy() {

View file

@ -6,7 +6,6 @@
package com.stevesoltys.seedvault.ui.recoverycode
import android.os.Bundle
import android.view.MenuItem
import android.view.WindowManager.LayoutParams.FLAG_SECURE
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.isDebugBuild
@ -42,16 +41,6 @@ class RecoveryCodeActivity : BackupActivity() {
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
onBackPressed()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun showOutput() {
supportFragmentManager.beginTransaction()
.add(R.id.fragment, RecoveryCodeOutputFragment(), "Code")

View file

@ -26,6 +26,7 @@ import android.widget.TextView
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat.getMainExecutor
import androidx.fragment.app.Fragment
@ -105,7 +106,11 @@ class RecoveryCodeInputFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.setTitle(R.string.recovery_code_title)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
if (viewModel.isRestore) {
introText.setText(R.string.recovery_code_input_intro)

View file

@ -12,6 +12,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager.LayoutParams.FLAG_SECURE
import android.widget.Button
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -44,6 +45,12 @@ class RecoveryCodeOutputFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
setGridParameters(wordList)
wordList.adapter = RecoveryCodeAdapter(viewModel.wordList)

View file

@ -8,13 +8,6 @@
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"

View file

@ -3,9 +3,23 @@
SPDX-FileCopyrightText: 2020 The Calyx Institute
SPDX-License-Identifier: Apache-2.0
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="@string/about_title" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -151,3 +165,4 @@
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</LinearLayout>

View file

@ -8,6 +8,18 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/app_status_menu"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="@string/settings_backup_status_title" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
@ -15,7 +27,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<ProgressBar
android:id="@+id/progressBar"

View file

@ -3,9 +3,23 @@
SPDX-FileCopyrightText: 2020 The Calyx Institute
SPDX-License-Identifier: Apache-2.0
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="@string/recovery_code_title" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
@ -85,5 +99,5 @@
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</LinearLayout>

View file

@ -10,6 +10,17 @@
android:layout_height="match_parent"
tools:context=".ui.recoverycode.RecoveryCodeActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="@string/recovery_code_title" />
<ImageView
android:id="@+id/introIcon"
android:layout_width="wrap_content"
@ -18,7 +29,7 @@
android:layout_marginTop="24dp"
android:src="@drawable/ic_info_outline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:ignore="ContentDescription" />
<TextView
@ -32,7 +43,7 @@
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/introIcon"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<TextView
android:id="@+id/introText2"
@ -78,8 +89,8 @@
style="@style/SudPrimaryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recovery_code_confirm_button"
android:layout_marginEnd="40dp"
android:text="@string/recovery_code_confirm_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/wordList" />

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 The Calyx Institute
SPDX-License-Identifier: Apache-2.0
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".settings.SettingsFragment">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:navigationIcon="?attr/homeAsUpIndicator" />
<!-- Required ViewGroup for PreferenceFragmentCompat -->
<FrameLayout
android:id="@android:id/list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View file

@ -10,13 +10,17 @@
<item name="colorError">@color/red</item>
<item name="android:windowBackground">@color/background</item>
<item name="fontFamily">@*android:string/config_bodyFontFamily</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
<item name="preferenceTheme">@style/PreferenceTheme</item>
<item name="android:windowLightStatusBar">@bool/isLight</item>
<item name="android:statusBarColor">@color/background</item>
<item name="android:windowLightNavigationBar">@bool/isLight</item>
<item name="android:navigationBarColor">@color/background</item>
</style>
<style name="PreferenceTheme" parent="PreferenceThemeOverlay">
<item name="android:layout">@layout/fragment_settings</item>
</style>
<!-- Copied from Settings -->
<style name="ActionPrimaryButton" parent="android:Widget.DeviceDefault.Button.Colored" />