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.deletion
17 
18 import android.os.Parcel
19 import android.os.Parcelable
20 import com.android.healthconnect.controller.permissions.data.HealthPermissionType
21 import com.android.healthconnect.controller.shared.DataType
22 import com.android.healthconnect.controller.shared.HealthDataCategoryInt
23 
24 /** Represents the types of deletion that the user can perform. */
25 sealed class DeletionType : Parcelable {
26     class DeletionTypeAllData() : DeletionType() {
27 
28         @Suppress(
29             "UNUSED_PARAMETER") // the class has no data to write but inherits from a Parcelable
30         constructor(parcel: Parcel) : this() {}
31 
writeToParcelnull32         override fun writeToParcel(parcel: Parcel, flags: Int) {}
33 
describeContentsnull34         override fun describeContents(): Int = 0
35 
36         companion object CREATOR : Parcelable.Creator<DeletionTypeAllData> {
37             override fun createFromParcel(parcel: Parcel): DeletionTypeAllData {
38                 return DeletionTypeAllData(parcel)
39             }
40 
41             override fun newArray(size: Int): Array<DeletionTypeAllData?> {
42                 return arrayOfNulls(size)
43             }
44         }
45     }
46 
47     data class DeletionTypeHealthPermissionTypeData(
48         val healthPermissionType: HealthPermissionType
49     ) : DeletionType() {
50         constructor(
51             parcel: Parcel
52         ) : this(
53             HealthPermissionType.valueOf(
54                 parcel.readString() ?: HealthPermissionType.ACTIVE_CALORIES_BURNED.toString())) {}
55 
writeToParcelnull56         override fun writeToParcel(parcel: Parcel, flags: Int) {
57             parcel.writeString(healthPermissionType.toString())
58         }
59 
describeContentsnull60         override fun describeContents(): Int {
61             return 0
62         }
63 
64         companion object CREATOR : Parcelable.Creator<DeletionTypeHealthPermissionTypeData> {
createFromParcelnull65             override fun createFromParcel(parcel: Parcel): DeletionTypeHealthPermissionTypeData {
66                 return DeletionTypeHealthPermissionTypeData(parcel)
67             }
68 
newArraynull69             override fun newArray(size: Int): Array<DeletionTypeHealthPermissionTypeData?> {
70                 return arrayOfNulls(size)
71             }
72         }
73     }
74 
75     data class DeletionTypeCategoryData(val category: @HealthDataCategoryInt Int) : DeletionType() {
76         constructor(parcel: Parcel) : this(parcel.readInt()) {}
77 
writeToParcelnull78         override fun writeToParcel(parcel: Parcel, flags: Int) {
79             parcel.writeString(category.toString())
80         }
81 
describeContentsnull82         override fun describeContents(): Int {
83             return 0
84         }
85 
86         companion object CREATOR : Parcelable.Creator<DeletionTypeCategoryData> {
createFromParcelnull87             override fun createFromParcel(parcel: Parcel): DeletionTypeCategoryData {
88                 return DeletionTypeCategoryData(parcel)
89             }
90 
newArraynull91             override fun newArray(size: Int): Array<DeletionTypeCategoryData?> {
92                 return arrayOfNulls(size)
93             }
94         }
95     }
96 
97     data class DeletionTypeAppData(val packageName: String, val appName: String) : DeletionType() {
98         constructor(parcel: Parcel) : this(parcel.readString() ?: "", parcel.readString() ?: "") {}
99 
writeToParcelnull100         override fun writeToParcel(parcel: Parcel, flags: Int) {
101             parcel.writeString(packageName)
102             parcel.writeString(appName)
103         }
104 
describeContentsnull105         override fun describeContents(): Int {
106             return 0
107         }
108 
109         companion object CREATOR : Parcelable.Creator<DeletionTypeAppData> {
createFromParcelnull110             override fun createFromParcel(parcel: Parcel): DeletionTypeAppData {
111                 return DeletionTypeAppData(parcel)
112             }
113 
newArraynull114             override fun newArray(size: Int): Array<DeletionTypeAppData?> {
115                 return arrayOfNulls(size)
116             }
117         }
118     }
119 
120     data class DeletionTypeHealthPermissionTypeFromApp(
121         val healthPermissionType: HealthPermissionType,
122         val packageName: String,
123         val appName: String
124     ) : DeletionType() {
125         constructor(
126             parcel: Parcel
127         ) : this(
128             HealthPermissionType.valueOf(
129                 parcel.readString() ?: HealthPermissionType.ACTIVE_CALORIES_BURNED.toString()),
130             parcel.readString() ?: "",
131             parcel.readString() ?: "") {}
132 
writeToParcelnull133         override fun writeToParcel(parcel: Parcel, flags: Int) {
134             parcel.writeString(healthPermissionType.toString())
135             parcel.writeString(packageName)
136             parcel.writeString(appName)
137         }
138 
describeContentsnull139         override fun describeContents(): Int {
140             return 0
141         }
142 
143         companion object CREATOR : Parcelable.Creator<DeletionTypeAppData> {
createFromParcelnull144             override fun createFromParcel(parcel: Parcel): DeletionTypeAppData {
145                 return DeletionTypeAppData(parcel)
146             }
147 
newArraynull148             override fun newArray(size: Int): Array<DeletionTypeAppData?> {
149                 return arrayOfNulls(size)
150             }
151         }
152     }
153 
154     data class DeleteDataEntry(val id: String, val dataType: DataType, val index: Int) :
155         DeletionType() {
156 
157         constructor(
158             parcel: Parcel
159         ) : this(
160             parcel.readString().orEmpty(),
161             DataType.valueOf(parcel.readString().orEmpty()),
162             parcel.readInt())
163 
describeContentsnull164         override fun describeContents(): Int = 0
165 
166         override fun writeToParcel(parcel: Parcel, flags: Int) {
167             parcel.writeString(id)
168             parcel.writeString(dataType.name)
169             parcel.writeInt(index)
170         }
171 
172         companion object CREATOR : Parcelable.Creator<DeleteDataEntry> {
createFromParcelnull173             override fun createFromParcel(parcel: Parcel): DeleteDataEntry {
174                 return DeleteDataEntry(parcel)
175             }
176 
newArraynull177             override fun newArray(size: Int): Array<DeleteDataEntry?> {
178                 return arrayOfNulls(size)
179             }
180         }
181     }
182 
183     val hasPermissionType: Boolean
184         get() {
185             return when (this) {
186                 is DeletionTypeHealthPermissionTypeData,
187                 is DeletionTypeHealthPermissionTypeFromApp -> true
188                 else -> false
189             }
190         }
191 
192     val hasCategory: Boolean
193         get() {
194             return when (this) {
195                 is DeletionTypeCategoryData -> true
196                 else -> false
197             }
198         }
199 
200     val hasAppData: Boolean
201         get() {
202             return when (this) {
203                 is DeletionTypeAppData -> true
204                 is DeletionTypeHealthPermissionTypeFromApp -> true
205                 else -> false
206             }
207         }
208 }
209