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 androidx.fragment.app.DialogFragment
21 import androidx.fragment.app.activityViewModels
22 import androidx.fragment.app.setFragmentResult
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.deletion.DeletionConstants.CONFIRMATION_EVENT
25 import com.android.healthconnect.controller.deletion.DeletionConstants.GO_BACK_EVENT
26 import com.android.healthconnect.controller.shared.DataType
27 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder
28 import com.android.healthconnect.controller.utils.LocalDateTimeFormatter
29 import com.android.healthconnect.controller.utils.logging.DeletionDialogConfirmationElement
30 import com.android.healthconnect.controller.utils.toInstant
31 import dagger.hilt.android.AndroidEntryPoint
32 
33 /**
34  * A deletion {@link DialogFragment} asking confirmation from user for deleting data from from the
35  * time range chosen on the previous dialog.
36  */
37 @Deprecated("This won't be used once the NEW_INFORMATION_ARCHITECTURE feature is enabled.")
38 @AndroidEntryPoint(DialogFragment::class)
39 class DeletionConfirmationDialogFragment : Hilt_DeletionConfirmationDialogFragment() {
40 
41     private val viewModel: DeletionViewModel by activityViewModels()
42     private val separator = " "
43 
44     private val dateFormatter: LocalDateTimeFormatter by lazy {
45         LocalDateTimeFormatter(requireContext())
46     }
47 
48     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
49         val alertDialogBuilder =
50             AlertDialogBuilder(
51                     this, DeletionDialogConfirmationElement.DELETION_DIALOG_CONFIRMATION_CONTAINER)
52                 .setTitle(buildTitle())
53                 .setIcon(R.attr.deleteIcon)
54                 .setMessage(buildMessage())
55                 .setPositiveButton(
56                     R.string.confirming_question_delete_button,
57                     DeletionDialogConfirmationElement.DELETION_DIALOG_CONFIRMATION_DELETE_BUTTON) {
58                         _,
59                         _ ->
60                         setFragmentResult(CONFIRMATION_EVENT, Bundle())
61                     }
62 
63         if (viewModel.showTimeRangeDialogFragment) {
64             alertDialogBuilder.setNeutralButton(
65                 R.string.confirming_question_go_back_button,
66                 DeletionDialogConfirmationElement.DELETION_DIALOG_CONFIRMATION_GO_BACK_BUTTON) {
67                     _,
68                     _ ->
69                     setFragmentResult(GO_BACK_EVENT, Bundle())
70                 }
71         } else {
72             alertDialogBuilder.setNeutralButton(
73                 android.R.string.cancel,
74                 DeletionDialogConfirmationElement.DELETION_DIALOG_CONFIRMATION_CANCEL_BUTTON) { _, _
75                     ->
76                 }
77         }
78 
79         return alertDialogBuilder.create()
80     }
81 
82     private fun buildTitle(): String {
83         val deletionParameters = viewModel.deletionParameters.value ?: DeletionParameters()
84         val deletionType = deletionParameters.deletionType
85         val chosenRange = deletionParameters.chosenRange
86 
87         when (deletionType) {
88             is DeletionType.DeletionTypeAllData -> {
89                 return when (chosenRange) {
90                     ChosenRange.DELETE_RANGE_LAST_24_HOURS ->
91                         getString(R.string.confirming_question_one_day)
92                     ChosenRange.DELETE_RANGE_LAST_7_DAYS ->
93                         getString(R.string.confirming_question_one_week)
94                     ChosenRange.DELETE_RANGE_LAST_30_DAYS ->
95                         getString(R.string.confirming_question_one_month)
96                     ChosenRange.DELETE_RANGE_ALL_DATA -> getString(R.string.confirming_question_all)
97                 }
98             }
99             is DeletionType.DeletionTypeHealthPermissionTypeData -> {
100                 val permissionTypeLabel = getString(deletionParameters.getPermissionTypeLabel())
101                 return when (chosenRange) {
102                     ChosenRange.DELETE_RANGE_LAST_24_HOURS ->
103                         getString(
104                             R.string.confirming_question_data_type_one_day, permissionTypeLabel)
105                     ChosenRange.DELETE_RANGE_LAST_7_DAYS ->
106                         getString(
107                             R.string.confirming_question_data_type_one_week, permissionTypeLabel)
108                     ChosenRange.DELETE_RANGE_LAST_30_DAYS ->
109                         getString(
110                             R.string.confirming_question_data_type_one_month, permissionTypeLabel)
111                     ChosenRange.DELETE_RANGE_ALL_DATA ->
112                         getString(R.string.confirming_question_data_type_all, permissionTypeLabel)
113                 }
114             }
115             is DeletionType.DeletionTypeCategoryData -> {
116                 val categoryLabel = getString(deletionParameters.getCategoryLabel())
117                 return when (chosenRange) {
118                     ChosenRange.DELETE_RANGE_LAST_24_HOURS ->
119                         getString(R.string.confirming_question_category_one_day, categoryLabel)
120                     ChosenRange.DELETE_RANGE_LAST_7_DAYS ->
121                         getString(R.string.confirming_question_category_one_week, categoryLabel)
122                     ChosenRange.DELETE_RANGE_LAST_30_DAYS ->
123                         getString(R.string.confirming_question_category_one_month, categoryLabel)
124                     ChosenRange.DELETE_RANGE_ALL_DATA ->
125                         getString(R.string.confirming_question_category_all, categoryLabel)
126                 }
127             }
128             is DeletionType.DeleteDataEntry -> {
129                 return getString(R.string.confirming_question_single_entry)
130             }
131             is DeletionType.DeletionTypeAppData -> {
132                 val appName = deletionParameters.getAppName()
133                 return when (chosenRange) {
134                     ChosenRange.DELETE_RANGE_LAST_24_HOURS ->
135                         getString(R.string.confirming_question_app_data_one_day, appName)
136                     ChosenRange.DELETE_RANGE_LAST_7_DAYS ->
137                         getString(R.string.confirming_question_app_data_one_week, appName)
138                     ChosenRange.DELETE_RANGE_LAST_30_DAYS ->
139                         getString(R.string.confirming_question_app_data_one_month, appName)
140                     ChosenRange.DELETE_RANGE_ALL_DATA ->
141                         getString(R.string.confirming_question_app_data_all, appName)
142                 }
143             }
144             else -> {
145                 // TODO implement other flows
146                 throw UnsupportedOperationException("")
147             }
148         }
149     }
150 
151     private fun buildMessage(): String {
152         val deletionParameters = viewModel.deletionParameters.value ?: DeletionParameters()
153         val deletionType = deletionParameters.deletionType
154         var message = getString(R.string.confirming_question_message)
155 
156         if (deletionType is DeletionType.DeleteDataEntry &&
157             deletionType.dataType == DataType.MENSTRUATION_PERIOD) {
158             message =
159                 getString(
160                     R.string.confirming_question_message_menstruation,
161                     dateFormatter.formatLongDate(deletionParameters.startTimeMs.toInstant()),
162                     dateFormatter.formatLongDate(deletionParameters.endTimeMs.toInstant())) +
163                     separator +
164                     message
165         }
166 
167         return message
168     }
169 
170     companion object {
171         const val TAG = "ConfirmationDialogFragment"
172     }
173 }
174