1 /**
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * ```
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * ```
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
14 * the License.
15 */
16 package com.android.healthconnect.controller.utils
17
18 import android.app.Activity
19 import android.view.Menu
20 import android.view.MenuInflater
21 import android.view.MenuItem
22 import androidx.annotation.MenuRes
23 import androidx.annotation.StringRes
24 import androidx.core.view.MenuHost
25 import androidx.core.view.MenuProvider
26 import androidx.fragment.app.Fragment
27 import androidx.lifecycle.Lifecycle
28 import androidx.lifecycle.LifecycleOwner
29 import androidx.preference.Preference
30 import androidx.preference.PreferenceFragmentCompat
31 import com.android.healthconnect.controller.R
32 import com.android.healthconnect.controller.shared.dialog.ProgressDialogFragment
33 import com.android.healthconnect.controller.shared.preference.HealthPreferenceFragment
34 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
35 import com.android.healthconnect.controller.utils.logging.ToolbarElement
36 import dagger.hilt.android.EntryPointAccessors
37
38 private lateinit var deviceInfoUtils: DeviceInfoUtils
39
40 /** Sets fragment title on the collapsing layout, delegating to host if needed. */
setTitlenull41 fun Fragment.setTitle(@StringRes title: Int) {
42 (requireActivity() as Activity).setTitle(title)
43 }
44
setupMenunull45 fun Fragment.setupMenu(
46 @MenuRes menuRes: Int,
47 viewLifecycleOwner: LifecycleOwner,
48 logger: HealthConnectLogger? = null,
49 onMenuItemSelected: (MenuItem) -> Boolean,
50 ) {
51
52 val hiltEntryPoint =
53 EntryPointAccessors.fromApplication(
54 requireContext().applicationContext, DeviceInfoUtilsEntryPoint::class.java)
55
56 deviceInfoUtils = hiltEntryPoint.deviceInfoUtils()
57
58 val menuProvider =
59 object : MenuProvider {
60 override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
61 menu.clear()
62 menuInflater.inflate(menuRes, menu)
63 menu.findItem(R.id.menu_send_feedback).isVisible =
64 deviceInfoUtils.isSendFeedbackAvailable(requireContext())
65 }
66
67 override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
68 return when (menuItem.itemId) {
69 R.id.menu_send_feedback -> {
70 deviceInfoUtils.openSendFeedbackActivity(requireActivity())
71 true
72 }
73 R.id.menu_help -> {
74 // TODO (b/270864219) might be able to move impression out of this method
75 logger?.logImpression(ToolbarElement.TOOLBAR_HELP_BUTTON)
76 logger?.logInteraction(ToolbarElement.TOOLBAR_HELP_BUTTON)
77 deviceInfoUtils.openHCGetStartedLink(requireActivity())
78 true
79 }
80 else -> onMenuItemSelected.invoke(menuItem)
81 }
82 }
83 }
84
85 (requireActivity() as MenuHost).addMenuProvider(
86 menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
87 }
88
setupSharedMenunull89 fun Fragment.setupSharedMenu(
90 viewLifecycleOwner: LifecycleOwner,
91 logger: HealthConnectLogger? = null,
92 @MenuRes menuRes: Int = R.menu.send_feedback_and_help,
93 onMenuItemSelected: (MenuItem) -> Boolean = { false }
94 ) {
95 setupMenu(menuRes, viewLifecycleOwner, logger, onMenuItemSelected)
96 }
97
showLoadingDialognull98 fun Fragment.showLoadingDialog() {
99 ProgressDialogFragment().show(childFragmentManager, ProgressDialogFragment.TAG)
100 }
101
Fragmentnull102 fun Fragment.dismissLoadingDialog() {
103 val dialog = childFragmentManager.findFragmentByTag(ProgressDialogFragment.TAG)
104 if (dialog != null && dialog is ProgressDialogFragment) {
105 dialog.dismiss()
106 }
107 }
108
109 /** Returns a [Lazy] delegate to load the PreferenceFragment's preferences. */
prefnull110 inline fun <P : Preference> HealthPreferenceFragment.pref(key: String): Lazy<P> {
111 return lazy { findPreference(key)!! }
112 }
113
114 /** Returns a [Lazy] delegate to load the PreferenceFragment's preferences. */
prefnull115 inline fun <P : Preference> PreferenceFragmentCompat.pref(key: String): Lazy<P> {
116 return lazy { findPreference(key)!! }
117 }
118