1 /*
2  * Copyright 2021 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 androidx.glance.appwidget.proto
18 
19 import androidx.datastore.core.CorruptionException
20 import androidx.datastore.core.Serializer
21 import androidx.glance.appwidget.proto.LayoutProto.LayoutConfig
22 import com.google.protobuf.InvalidProtocolBufferException
23 import java.io.InputStream
24 import java.io.OutputStream
25 
26 /** Serializes the proto for use with DataStore. */
27 public object LayoutProtoSerializer : Serializer<LayoutConfig> {
28 
29     override val defaultValue: LayoutConfig = LayoutConfig.getDefaultInstance()
30 
31     @Throws(CorruptionException::class)
readFromnull32     override suspend fun readFrom(input: InputStream): LayoutConfig {
33         try {
34             return LayoutConfig.parseFrom(input)
35         } catch (e: InvalidProtocolBufferException) {
36             throw CorruptionException("Cannot read proto.", e)
37         }
38     }
39 
writeTonull40     override suspend fun writeTo(t: LayoutConfig, output: OutputStream) = t.writeTo(output)
41 }
42