storage: demo: Switch to toolbar APIs for working with menu

Activity methods have been deprecated

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta 2024-09-19 13:25:18 +05:30
parent f5c2f0bd3a
commit a5fe52a01f
9 changed files with 98 additions and 58 deletions

View file

@ -11,8 +11,6 @@ import android.os.Bundle
import android.os.Environment import android.os.Environment
import android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION import android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.View.INVISIBLE import android.view.View.INVISIBLE
@ -22,6 +20,7 @@ import android.widget.Button
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.Toast import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -48,7 +47,6 @@ open class LogFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle?, savedInstanceState: Bundle?,
): View { ): View {
setHasOptionsMenu(true)
val v = inflater.inflate(R.layout.fragment_log, container, false) val v = inflater.inflate(R.layout.fragment_log, container, false)
list = v.findViewById(R.id.listView) list = v.findViewById(R.id.listView)
list.adapter = adapter list.adapter = adapter
@ -75,16 +73,16 @@ open class LogFragment : Fragment() {
return v return v
} }
override fun onStart() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onStart() super.onViewCreated(view, savedInstanceState)
requireActivity().setTitle(R.string.app_name)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = getString(R.string.app_name)
setOnMenuItemClickListener(::onMenuItemSelected)
}
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { private fun onMenuItemSelected(item: MenuItem): Boolean {
inflater.inflate(R.menu.fragment_main, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.settings -> { R.id.settings -> {
if (!checkPermission()) return false if (!checkPermission()) return false
@ -108,7 +106,7 @@ open class LogFragment : Fragment() {
startActivity(shareIntent) startActivity(shareIntent)
true true
} }
else -> super.onOptionsItemSelected(item) else -> false
} }
} }

View file

@ -31,7 +31,7 @@ class DemoSnapshotFragment : SnapshotFragment() {
bottomStub.layoutResource = R.layout.footer_snapshot bottomStub.layoutResource = R.layout.footer_snapshot
val footer = bottomStub.inflate() val footer = bottomStub.inflate()
footer.findViewById<Button>(R.id.button).setOnClickListener { footer.findViewById<Button>(R.id.button).setOnClickListener {
requireActivity().onBackPressed() requireActivity().onBackPressedDispatcher.onBackPressed()
} }
return v return v
} }

View file

@ -10,8 +10,6 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.View.FOCUS_DOWN import android.view.View.FOCUS_DOWN
@ -21,6 +19,7 @@ import android.view.ViewGroup
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import androidx.annotation.UiThread import androidx.annotation.UiThread
import androidx.appcompat.widget.Toolbar
import androidx.core.widget.NestedScrollView import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
@ -53,8 +52,6 @@ open class MediaScanFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle?, savedInstanceState: Bundle?,
): View { ): View {
setHasOptionsMenu(true)
requireActivity().title = arguments?.getString("name")
val v = inflater.inflate(R.layout.fragment_scan, container, false) val v = inflater.inflate(R.layout.fragment_scan, container, false)
scrollView = v.findViewById(R.id.scrollView) scrollView = v.findViewById(R.id.scrollView)
logView = v.findViewById(R.id.logView) logView = v.findViewById(R.id.logView)
@ -63,11 +60,19 @@ open class MediaScanFragment : Fragment() {
return v return v
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
inflater.inflate(R.menu.fragment_scan, menu) super.onViewCreated(view, savedInstanceState)
view.requireViewById<Toolbar>(R.id.toolbar).apply {
title = arguments?.getString("name")
setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.refresh -> { R.id.refresh -> {
loadText() loadText()
@ -85,7 +90,7 @@ open class MediaScanFragment : Fragment() {
startActivity(shareIntent) startActivity(shareIntent)
true true
} }
else -> super.onOptionsItemSelected(item) else -> false
} }
} }

View file

@ -9,14 +9,12 @@ import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.grobox.storagebackuptester.MainViewModel import de.grobox.storagebackuptester.MainViewModel
@ -44,18 +42,21 @@ class SettingsFragment : BackupContentFragment() {
onBackupUriReceived(uri) onBackupUriReceived(uri)
} }
override fun onCreateView( override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
inflater: LayoutInflater, super.onViewCreated(view, savedInstanceState)
container: ViewGroup?,
savedInstanceState: Bundle?, view.requireViewById<Toolbar>(R.id.toolbar).apply {
): View { title = "Settings"
setHasOptionsMenu(true) inflateMenu(R.menu.fragment_settings)
requireActivity().title = "Settings" onCreateMenu(menu)
return super.onCreateView(inflater, container, savedInstanceState) setOnMenuItemClickListener(::onMenuItemSelected)
setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { private fun onCreateMenu(menu: Menu) {
inflater.inflate(R.menu.fragment_settings, menu)
backupLocationItem = menu.findItem(R.id.backup_location) backupLocationItem = menu.findItem(R.id.backup_location)
backupLocationItem.isChecked = viewModel.hasBackupLocation() backupLocationItem.isChecked = viewModel.hasBackupLocation()
jobItem = menu.findItem(R.id.backup_job) jobItem = menu.findItem(R.id.backup_job)
@ -65,7 +66,7 @@ class SettingsFragment : BackupContentFragment() {
restoreItem.isEnabled = backupLocationItem.isChecked restoreItem.isEnabled = backupLocationItem.isChecked
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { private fun onMenuItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.info -> { R.id.info -> {
parentFragmentManager.beginTransaction() parentFragmentManager.beginTransaction()
@ -102,7 +103,7 @@ class SettingsFragment : BackupContentFragment() {
Toast.makeText(requireContext(), "Cache cleared", LENGTH_SHORT).show() Toast.makeText(requireContext(), "Cache cleared", LENGTH_SHORT).show()
true true
} }
else -> super.onOptionsItemSelected(item) else -> false
} }
} }

View file

@ -9,6 +9,17 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/fragment_main" />
<ProgressBar <ProgressBar
android:id="@+id/horizontalProgressBar" android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleHorizontal"
@ -16,7 +27,7 @@
android:layout_height="5dp" android:layout_height="5dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:max="10" tools:max="10"
tools:progress="7" /> tools:progress="7" />

View file

@ -9,13 +9,25 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/fragment_scan"
app:navigationIcon="?attr/homeAsUpIndicator" />
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView
android:id="@+id/scrollView" android:id="@+id/scrollView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="0dp"
android:fadeScrollbars="false" android:fadeScrollbars="false"
android:scrollbars="vertical" android:scrollbars="vertical"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toBottomOf="@id/toolbar">
<TextView <TextView
android:id="@+id/logView" android:id="@+id/logView"

View file

@ -4,7 +4,7 @@
--> -->
<resources xmlns:tools="http://schemas.android.com/tools"> <resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. --> <!-- Primary brand color. -->
<item name="colorPrimary">@color/matrix</item> <item name="colorPrimary">@color/matrix</item>
<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorPrimaryVariant">@color/purple_700</item>

View file

@ -4,7 +4,7 @@
--> -->
<resources> <resources>
<style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <style name="Theme.StorageBackupTester" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/matrix</item> <item name="colorPrimary">@color/matrix</item>
<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item> <item name="colorOnPrimary">@color/white</item>

View file

@ -3,28 +3,41 @@
SPDX-FileCopyrightText: 2021 The Calyx Institute SPDX-FileCopyrightText: 2021 The Calyx Institute
SPDX-License-Identifier: Apache-2.0 SPDX-License-Identifier: Apache-2.0
--> -->
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView <androidx.appcompat.widget.Toolbar
android:id="@+id/list" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_media" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:minHeight="?attr/actionBarSize"
android:layout_margin="16dp" android:theme="?attr/actionBarTheme"
android:contentDescription="@string/content_add" app:navigationIcon="?attr/homeAsUpIndicator" />
android:src="@drawable/ic_add"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> <androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_media" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:contentDescription="@string/content_add"
android:src="@drawable/ic_add"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>