1 /*
2  * Copyright (C) 2023 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 package com.google.jetpackcamera.settings
17 
18 import androidx.datastore.core.CorruptionException
19 import androidx.datastore.core.Serializer
20 import com.google.protobuf.InvalidProtocolBufferException
21 import java.io.InputStream
22 import java.io.OutputStream
23 
24 object JcaSettingsSerializer : Serializer<JcaSettings> {
25 
26     override val defaultValue: JcaSettings = JcaSettings.newBuilder()
27         .setDarkModeStatus(DarkMode.DARK_MODE_SYSTEM)
28         .setDefaultLensFacing(LensFacing.LENS_FACING_BACK)
29         .setFlashModeStatus(FlashMode.FLASH_MODE_OFF)
30         .setAspectRatioStatus(AspectRatio.ASPECT_RATIO_NINE_SIXTEEN)
31         .setCaptureModeStatus(CaptureMode.CAPTURE_MODE_MULTI_STREAM)
32         .setStabilizePreview(PreviewStabilization.PREVIEW_STABILIZATION_UNDEFINED)
33         .setStabilizeVideo(VideoStabilization.VIDEO_STABILIZATION_UNDEFINED)
34         .setDynamicRangeStatus(DynamicRange.DYNAMIC_RANGE_UNSPECIFIED)
35         .setImageFormatStatus(ImageOutputFormat.IMAGE_OUTPUT_FORMAT_JPEG)
36         .build()
37 
readFromnull38     override suspend fun readFrom(input: InputStream): JcaSettings {
39         try {
40             return JcaSettings.parseFrom(input)
41         } catch (exception: InvalidProtocolBufferException) {
42             throw CorruptionException("Cannot read proto.", exception)
43         }
44     }
45 
writeTonull46     override suspend fun writeTo(t: JcaSettings, output: OutputStream) = t.writeTo(output)
47 }
48