<lambda>null1 package com.android.healthconnect.controller.managedata
2 
3 import android.health.connect.HealthDataCategory
4 import android.icu.text.MessageFormat
5 import android.os.Bundle
6 import android.view.View
7 import androidx.core.os.bundleOf
8 import androidx.fragment.app.activityViewModels
9 import androidx.navigation.fragment.findNavController
10 import com.android.healthconnect.controller.R
11 import com.android.healthconnect.controller.autodelete.AutoDeleteRange
12 import com.android.healthconnect.controller.autodelete.AutoDeleteViewModel
13 import com.android.healthconnect.controller.categories.HealthDataCategoriesFragment
14 import com.android.healthconnect.controller.shared.preference.HealthPreference
15 import com.android.healthconnect.controller.shared.preference.HealthPreferenceFragment
16 import com.android.healthconnect.controller.utils.FeatureUtils
17 import com.android.healthconnect.controller.utils.logging.ManageDataElement
18 import com.android.healthconnect.controller.utils.logging.PageName
19 import com.android.healthfitness.flags.Flags
20 import dagger.hilt.android.AndroidEntryPoint
21 import javax.inject.Inject
22 
23 @AndroidEntryPoint(HealthPreferenceFragment::class)
24 class ManageDataFragment : Hilt_ManageDataFragment() {
25 
26     companion object {
27         private const val AUTO_DELETE_PREFERENCE_KEY = "auto_delete"
28         private const val DATA_SOURCES_AND_PRIORITY_PREFERENCE_KEY = "data_sources_and_priority"
29         private const val SET_UNITS_PREFERENCE_KEY = "set_units"
30         private const val BACKUP_AND_RESTORE_PREFERENCE_KEY = "backup_and_restore"
31     }
32 
33     init {
34         this.setPageName(PageName.MANAGE_DATA_PAGE)
35     }
36 
37     private val autoDeleteViewModel: AutoDeleteViewModel by activityViewModels()
38     @Inject lateinit var featureUtils: FeatureUtils
39 
40     private val mAutoDeletePreference: HealthPreference? by lazy {
41         preferenceScreen.findPreference(AUTO_DELETE_PREFERENCE_KEY)
42     }
43 
44     private val mDataSourcesPreference: HealthPreference? by lazy {
45         preferenceScreen.findPreference(DATA_SOURCES_AND_PRIORITY_PREFERENCE_KEY)
46     }
47 
48     private val mSetUnitsPreference: HealthPreference? by lazy {
49         preferenceScreen.findPreference(SET_UNITS_PREFERENCE_KEY)
50     }
51 
52     private val backupAndRestorePreference: HealthPreference? by lazy {
53         preferenceScreen.findPreference(BACKUP_AND_RESTORE_PREFERENCE_KEY)
54     }
55 
56     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
57         super.onCreatePreferences(savedInstanceState, rootKey)
58         if (Flags.exportImport()) {
59             setPreferencesFromResource(R.xml.new_manage_data_screen, rootKey)
60         } else {
61             setPreferencesFromResource(R.xml.manage_data_screen, rootKey)
62         }
63 
64         mAutoDeletePreference?.logName = ManageDataElement.AUTO_DELETE_BUTTON
65         mAutoDeletePreference?.setOnPreferenceClickListener {
66             findNavController().navigate(R.id.action_manageData_to_autoDelete)
67             true
68         }
69 
70         if (featureUtils.isNewAppPriorityEnabled()) {
71             mDataSourcesPreference?.logName = ManageDataElement.DATA_SOURCES_AND_PRIORITY_BUTTON
72             mDataSourcesPreference?.setOnPreferenceClickListener {
73                 findNavController()
74                     .navigate(
75                         R.id.action_manageData_to_dataSources,
76                         bundleOf(
77                             HealthDataCategoriesFragment.CATEGORY_KEY to
78                                 HealthDataCategory.UNKNOWN))
79                 true
80             }
81         } else {
82             preferenceScreen.removePreferenceRecursively(DATA_SOURCES_AND_PRIORITY_PREFERENCE_KEY)
83         }
84 
85         mSetUnitsPreference?.logName = ManageDataElement.SET_UNITS_BUTTON
86         mSetUnitsPreference?.setOnPreferenceClickListener {
87             findNavController().navigate(R.id.action_manageData_to_setUnits)
88             true
89         }
90 
91         if (Flags.exportImport()) {
92             // TODO: b/325917291 - Add proper log name for the backup and restore button.
93             backupAndRestorePreference?.setOnPreferenceClickListener {
94                 findNavController().navigate(R.id.action_manageData_to_backupAndRestore)
95                 true
96             }
97         }
98     }
99 
100     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
101         super.onViewCreated(view, savedInstanceState)
102 
103         autoDeleteViewModel.storedAutoDeleteRange.observe(viewLifecycleOwner) { state ->
104             when (state) {
105                 AutoDeleteViewModel.AutoDeleteState.Loading -> {
106                     mAutoDeletePreference?.summary = ""
107                 }
108                 is AutoDeleteViewModel.AutoDeleteState.LoadingFailed -> {
109                     mAutoDeletePreference?.summary = ""
110                 }
111                 is AutoDeleteViewModel.AutoDeleteState.WithData -> {
112                     mAutoDeletePreference?.summary = buildSummary(state.autoDeleteRange)
113                 }
114             }
115         }
116     }
117 
118     private fun buildSummary(autoDeleteRange: AutoDeleteRange): String {
119         return when (autoDeleteRange) {
120             AutoDeleteRange.AUTO_DELETE_RANGE_NEVER -> getString(R.string.range_off)
121             AutoDeleteRange.AUTO_DELETE_RANGE_THREE_MONTHS -> {
122                 val count = AutoDeleteRange.AUTO_DELETE_RANGE_THREE_MONTHS.numberOfMonths
123                 MessageFormat.format(
124                     getString(R.string.range_after_x_months), mapOf("count" to count))
125             }
126             AutoDeleteRange.AUTO_DELETE_RANGE_EIGHTEEN_MONTHS -> {
127                 val count = AutoDeleteRange.AUTO_DELETE_RANGE_EIGHTEEN_MONTHS.numberOfMonths
128                 MessageFormat.format(
129                     getString(R.string.range_after_x_months), mapOf("count" to count))
130             }
131         }
132     }
133 }
134