1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.photopicker.data.model
18 
19 import android.net.Uri
20 import android.os.Parcel
21 import android.os.Parcelable
22 import com.android.photopicker.core.glide.GlideLoadable
23 import com.android.photopicker.core.glide.Resolution
24 import com.bumptech.glide.load.DataSource
25 import com.bumptech.glide.signature.ObjectKey
26 
27 /** Holds metadata for a group of media items. */
28 sealed interface Group : GlideLoadable, Parcelable {
29     /** Unique identifier for this group */
30     val id: String
31 
32     /**
33      * Holds metadata for a album item. It is a type of a [Group] object because it represents a
34      * collection of media items.
35      */
36     data class Album(
37         /** This is the ID provided by the [Provider] of this data */
38         override val id: String,
39 
40         /** This is the Picker ID auto-generated in Picker DB */
41         val pickerId: Long,
42         val authority: String,
43         val dateTakenMillisLong: Long,
44         val displayName: String,
45         val coverUri: Uri,
46         val coverMediaSource: MediaSource,
47     ) : Group {
getSignaturenull48         override fun getSignature(resolution: Resolution): ObjectKey {
49             return ObjectKey("${coverUri}_$resolution")
50         }
51 
getLoadableUrinull52         override fun getLoadableUri(): Uri {
53             return coverUri
54         }
55 
getDataSourcenull56         override fun getDataSource(): DataSource {
57             return when (coverMediaSource) {
58                 MediaSource.LOCAL -> DataSource.LOCAL
59                 MediaSource.REMOTE -> DataSource.REMOTE
60             }
61         }
62 
getTimestampnull63         override fun getTimestamp(): Long {
64             return dateTakenMillisLong
65         }
66 
describeContentsnull67         override fun describeContents(): Int {
68             return 0
69         }
70 
71         /** Implemented for [Parcelable], and handles all the common attributes. */
writeToParcelnull72         override fun writeToParcel(out: Parcel, flags: Int) {
73             out.writeString(id)
74             out.writeLong(pickerId)
75             out.writeString(authority)
76             out.writeLong(dateTakenMillisLong)
77             out.writeString(displayName)
78             out.writeString(coverUri.toString())
79             out.writeString(coverMediaSource.name)
80         }
81 
82         companion object CREATOR : Parcelable.Creator<Album> {
83 
createFromParcelnull84             override fun createFromParcel(parcel: Parcel): Album {
85                 val album =
86                     Album(
87                         /* id =*/ parcel.readString() ?: "",
88                         /* pickerId=*/ parcel.readLong(),
89                         /* authority=*/ parcel.readString() ?: "",
90                         /* dateTakenMillisLong=*/ parcel.readLong(),
91                         /* displayName =*/ parcel.readString() ?: "",
92                         /* uri= */ Uri.parse(parcel.readString() ?: ""),
93                         /* coverUriMediaSource =*/ MediaSource.valueOf(
94                             parcel.readString() ?: "LOCAL"
95                         ),
96                     )
97                 return album
98             }
99 
newArraynull100             override fun newArray(size: Int): Array<Album?> {
101                 return arrayOfNulls(size)
102             }
103         }
104     }
105 }
106