• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
<lambda>null2  * 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.deletion
17 
18 import android.app.Dialog
19 import android.os.Bundle
20 import android.view.View
21 import android.widget.ImageView
22 import android.widget.RadioGroup
23 import android.widget.TextView
24 import androidx.core.view.children
25 import androidx.fragment.app.DialogFragment
26 import androidx.fragment.app.activityViewModels
27 import androidx.fragment.app.setFragmentResult
28 import com.android.healthconnect.controller.R
29 import com.android.healthconnect.controller.deletion.DeletionConstants.TIME_RANGE_SELECTION_EVENT
30 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder
31 import com.android.healthconnect.controller.utils.AttributeResolver
32 import com.android.healthconnect.controller.utils.logging.DeletionDialogTimeRangeElement
33 import com.android.healthconnect.controller.utils.logging.ErrorPageElement
34 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
35 import dagger.hilt.android.AndroidEntryPoint
36 import java.time.Instant
37 import javax.inject.Inject
38 
39 /** A {@link DialogFragment} for choosing the deletion time range. */
40 @Deprecated("This won't be used once the NEW_INFORMATION_ARCHITECTURE feature is enabled.")
41 @AndroidEntryPoint(DialogFragment::class)
42 class TimeRangeDialogFragment : Hilt_TimeRangeDialogFragment() {
43 
44     private val viewModel: DeletionViewModel by activityViewModels()
45     @Inject lateinit var logger: HealthConnectLogger
46 
47     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
48         val view: View = layoutInflater.inflate(R.layout.dialog_message_time_range_picker, null)
49         val radioGroup: RadioGroup = view.findViewById(R.id.radio_group_time_range)
50         val messageView: TextView = view.findViewById(R.id.time_range_message)
51         val iconView = view.findViewById(R.id.dialog_icon) as ImageView
52         val title = view.findViewById(R.id.dialog_title) as TextView
53         messageView.text = buildMessage()
54         val iconDrawable =
55             AttributeResolver.getNullableDrawable(view.context, R.attr.deletionSettingsIcon)
56         iconDrawable?.let {
57             iconView.setImageDrawable(it)
58             iconView.visibility = View.VISIBLE
59         }
60         title.text = getString(R.string.time_range_title)
61 
62         return AlertDialogBuilder(
63                 this, DeletionDialogTimeRangeElement.DELETION_DIALOG_TIME_RANGE_CONTAINER)
64             .setView(view)
65             .setNeutralButton(
66                 android.R.string.cancel,
67                 DeletionDialogTimeRangeElement.DELETION_DIALOG_TIME_RANGE_CANCEL_BUTTON) { _, _ ->
68                 }
69             .setPositiveButton(
70                 R.string.time_range_next_button,
71                 DeletionDialogTimeRangeElement.DELETION_DIALOG_TIME_RANGE_NEXT_BUTTON) { _, _ ->
72                     val chosenRange = getChosenRange(radioGroup)
73                     viewModel.setChosenRange(chosenRange)
74                     viewModel.setEndTime(Instant.now())
75                     setFragmentResult(TIME_RANGE_SELECTION_EVENT, Bundle())
76                 }
77             .setAdditionalLogging { radioGroupLogging(radioGroup) }
78             .create()
79     }
80 
81     private fun buildMessage(): String {
82         val deletionParameters = viewModel.deletionParameters.value ?: DeletionParameters()
83         val deletionType: DeletionType = deletionParameters.deletionType
84 
85         return when (deletionType) {
86             is DeletionType.DeletionTypeAllData ->
87                 resources.getString(R.string.time_range_message_all)
88             is DeletionType.DeletionTypeHealthPermissionTypeData,
89             is DeletionType.DeletionTypeHealthPermissionTypeFromApp -> {
90                 val permissionTypeLabel = getString(deletionParameters.getPermissionTypeLabel())
91                 resources.getString(R.string.time_range_message_data_type, permissionTypeLabel)
92             }
93             is DeletionType.DeletionTypeCategoryData -> {
94                 val categoryLabel = getString(deletionParameters.getCategoryLabel())
95                 resources.getString(R.string.time_range_message_category, categoryLabel)
96             }
97             is DeletionType.DeletionTypeAppData -> {
98                 val appName = deletionParameters.getAppName()
99                 resources.getString(R.string.time_range_message_app_data, appName)
100             }
101             else ->
102                 throw UnsupportedOperationException(
103                     "This Deletion type does not support configurable time range. DataTypeFromApp automatically" +
104                         " deletes from all time.")
105         }
106     }
107 
108     private fun getChosenRange(radioGroup: RadioGroup): ChosenRange {
109         return when (radioGroup.checkedRadioButtonId) {
110             R.id.radio_button_one_day -> ChosenRange.DELETE_RANGE_LAST_24_HOURS
111             R.id.radio_button_one_week -> ChosenRange.DELETE_RANGE_LAST_7_DAYS
112             R.id.radio_button_one_month -> ChosenRange.DELETE_RANGE_LAST_30_DAYS
113             else -> ChosenRange.DELETE_RANGE_ALL_DATA
114         }
115     }
116 
117     private fun radioGroupLogging(radioGroup: RadioGroup) {
118         radioGroup.children.forEach {
119             val elementName =
120                 when (it.id) {
121                     R.id.radio_button_one_day ->
122                         DeletionDialogTimeRangeElement
123                             .DELETION_DIALOG_TIME_RANGE_LAST_24_HOURS_BUTTON
124                     R.id.radio_button_one_week ->
125                         DeletionDialogTimeRangeElement.DELETION_DIALOG_TIME_RANGE_LAST_7_DAYS_BUTTON
126                     R.id.radio_button_one_month ->
127                         DeletionDialogTimeRangeElement
128                             .DELETION_DIALOG_TIME_RANGE_LAST_30_DAYS_BUTTON
129                     R.id.radio_button_all ->
130                         DeletionDialogTimeRangeElement.DELETION_DIALOG_TIME_RANGE_ALL_DATA_BUTTON
131                     else -> ErrorPageElement.UNKNOWN_ELEMENT
132                 }
133             it.setOnClickListener { logger.logInteraction(elementName) }
134             logger.logImpression(elementName)
135         }
136     }
137 
138     companion object {
139         const val TAG = "TimeRangeDialogFragment"
140     }
141 }
142