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 com.android.healthconnect.controller.R
30 import com.android.healthconnect.controller.shared.dialog.ProgressDialogFragment
31 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
32 import com.android.healthconnect.controller.utils.logging.ToolbarElement
33 import dagger.hilt.android.EntryPointAccessors
34
35 private lateinit var deviceInfoUtils: DeviceInfoUtils
36
37 /** Sets fragment title on the collapsing layout, delegating to host if needed. */
setTitlenull38 fun Fragment.setTitle(@StringRes title: Int) {
39 (requireActivity() as Activity).setTitle(title)
40 }
41
setupMenunull42 fun Fragment.setupMenu(
43 @MenuRes menuRes: Int,
44 viewLifecycleOwner: LifecycleOwner,
45 logger: HealthConnectLogger? = null,
46 onMenuItemSelected: (MenuItem) -> Boolean,
47 ) {
48
49 val hiltEntryPoint =
50 EntryPointAccessors.fromApplication(
51 requireContext().applicationContext, DeviceInfoUtilsEntryPoint::class.java)
52
53 deviceInfoUtils = hiltEntryPoint.deviceInfoUtils()
54
55 val menuProvider =
56 object : MenuProvider {
57 override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
58 menu.clear()
59 menuInflater.inflate(menuRes, menu)
60 menu.findItem(R.id.menu_send_feedback).isVisible =
61 deviceInfoUtils.isSendFeedbackAvailable(requireContext())
62 }
63
64 override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
65 return when (menuItem.itemId) {
66 R.id.menu_send_feedback -> {
67 deviceInfoUtils.openSendFeedbackActivity(requireActivity())
68 true
69 }
70 R.id.menu_help -> {
71 // TODO (b/270864219) might be able to move impression out of this method
72 logger?.logImpression(ToolbarElement.TOOLBAR_HELP_BUTTON)
73 logger?.logInteraction(ToolbarElement.TOOLBAR_HELP_BUTTON)
74 deviceInfoUtils.openHCGetStartedLink(requireActivity())
75 true
76 }
77 else -> onMenuItemSelected.invoke(menuItem)
78 }
79 }
80 }
81
82 (requireActivity() as MenuHost).addMenuProvider(
83 menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
84 }
85
setupSharedMenunull86 fun Fragment.setupSharedMenu(
87 viewLifecycleOwner: LifecycleOwner,
88 logger: HealthConnectLogger? = null,
89 @MenuRes menuRes: Int = R.menu.send_feedback_and_help,
90 onMenuItemSelected: (MenuItem) -> Boolean = { false }
91 ) {
92 setupMenu(menuRes, viewLifecycleOwner, logger, onMenuItemSelected)
93 }
94
showLoadingDialognull95 fun Fragment.showLoadingDialog() {
96 ProgressDialogFragment().show(childFragmentManager, ProgressDialogFragment.TAG)
97 }
98
Fragmentnull99 fun Fragment.dismissLoadingDialog() {
100 val dialog = childFragmentManager.findFragmentByTag(ProgressDialogFragment.TAG)
101 if (dialog != null && dialog is ProgressDialogFragment) {
102 dialog.dismiss()
103 }
104 }
105