Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions core/model/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.google.protobuf)
}

android {
Expand Down Expand Up @@ -72,35 +71,12 @@ dependencies {

// proto datastore
implementation(libs.androidx.datastore)
implementation(libs.protobuf.kotlin.lite)

// Testing
testImplementation(libs.junit)
testImplementation(libs.truth)
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.21.12"
}

generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
}

task.builtins {
create("kotlin") {
option("lite")
}
}
}
}
}

// Allow references to generated code
kapt {
correctErrorTypes = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.jetpackcamera.model

import com.google.jetpackcamera.model.proto.AspectRatio as AspectRatioProto

enum class AspectRatio(val numerator: Int, val denominator: Int) {
THREE_FOUR(3, 4),
NINE_SIXTEEN(9, 16),
Expand All @@ -31,21 +29,4 @@ enum class AspectRatio(val numerator: Int, val denominator: Int) {
* Returns the landscape aspect ratio as a [Float].
*/
fun toLandscapeFloat(): Float = denominator.toFloat() / numerator

companion object {

/** returns the AspectRatio enum equivalent of a provided AspectRatioProto */
fun fromProto(aspectRatioProto: AspectRatioProto): AspectRatio {
return when (aspectRatioProto) {
AspectRatioProto.ASPECT_RATIO_NINE_SIXTEEN -> NINE_SIXTEEN
AspectRatioProto.ASPECT_RATIO_ONE_ONE -> ONE_ONE

// defaults to 3:4 aspect ratio
AspectRatioProto.ASPECT_RATIO_THREE_FOUR,
AspectRatioProto.ASPECT_RATIO_UNDEFINED,
AspectRatioProto.UNRECOGNIZED
-> THREE_FOUR
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
*/
package com.google.jetpackcamera.model

import android.util.Base64
import com.google.jetpackcamera.model.LensFacing.Companion.toProto
import com.google.jetpackcamera.model.TestPattern.Companion.toProto
import com.google.jetpackcamera.model.proto.DebugSettings as DebugSettingsProto
import com.google.jetpackcamera.model.proto.debugSettings as debugSettingsProto

/**
* Data class for defining settings used in debug flows within the app.
*
Expand All @@ -38,64 +32,81 @@ data class DebugSettings(
) {
companion object {
/**
* Creates a [DebugSettings] domain model from its protobuf representation.
*
* @param proto The [DebugSettingsProto] instance.
* @return The corresponding [DebugSettings] instance.
* Parses the string into a [DebugSettings] instance.
*/
fun fromProto(proto: DebugSettingsProto): DebugSettings {
return DebugSettings(
isDebugModeEnabled = proto.isDebugModeEnabled,
singleLensMode = if (proto.hasSingleLensMode()) {
LensFacing.fromProto(proto.singleLensMode)
} else {
null
},
testPattern = TestPattern.fromProto(proto.testPattern)
)
}
fun parseFromString(value: String): DebugSettings {
val parts = value.split(";")
var isDebugModeEnabled = false
var singleLensMode: LensFacing? = null
var testPattern: TestPattern = TestPattern.Off

/**
* Converts a [DebugSettings] domain model to its protobuf representation.
*
* @receiver The [DebugSettings] instance to convert.
* @return The corresponding [DebugSettingsProto] instance.
*/
fun DebugSettings.toProto(): DebugSettingsProto = debugSettingsProto {
isDebugModeEnabled = this@toProto.isDebugModeEnabled
this@toProto.singleLensMode?.let { lensFacing ->
singleLensMode = lensFacing.toProto()
for (part in parts) {
val kv = part.split(":")
if (kv.size == 2) {
when (kv[0]) {
"debug" -> isDebugModeEnabled = kv[1].toBoolean()
"lens" -> singleLensMode = enumValues<LensFacing>()
.firstOrNull { it.name == kv[1] }
"pattern" -> {
testPattern = when (kv[1]) {
"Off" -> TestPattern.Off
"ColorBars" -> TestPattern.ColorBars
"ColorBarsFadeToGray" -> TestPattern.ColorBarsFadeToGray
"PN9" -> TestPattern.PN9
"Custom1" -> TestPattern.Custom1
else -> {
if (kv[1].startsWith("SolidColor(") &&
kv[1].endsWith(")")
) {
val channels = kv[1]
.removePrefix("SolidColor(")
.removeSuffix(")")
.split(",")
if (channels.size == 4) {
val red = channels[0].toUIntOrNull()
val greenEven = channels[1].toUIntOrNull()
val greenOdd = channels[2].toUIntOrNull()
val blue = channels[3].toUIntOrNull()
if (red != null &&
greenEven != null &&
greenOdd != null &&
blue != null
) {
TestPattern.SolidColor(
red,
greenEven,
greenOdd,
blue
)
} else {
TestPattern.Off
}
} else {
TestPattern.Off
}
} else {
TestPattern.Off
}
}
}
}
}
}
}
testPattern = this@toProto.testPattern.toProto()
}

/**
* Parses the encoded byte array into a [DebugSettings] instance.
*/
fun parseFromByteArray(value: ByteArray): DebugSettings {
val protoValue = DebugSettingsProto.parseFrom(value)
return fromProto(protoValue)
return DebugSettings(isDebugModeEnabled, singleLensMode, testPattern)
}
Comment thread
davidjiagoogle marked this conversation as resolved.

/**
* Parses the Base64 encoded string into a [DebugSettings] instance.
*/
fun parseFromString(value: String): DebugSettings {
val decodedBytes = Base64.decode(value, Base64.NO_WRAP)
return parseFromByteArray(decodedBytes)
}

/**
* Encodes the [DebugSettings] data class into a byte array.
*/
fun DebugSettings.encodeAsByteArray(): ByteArray = this.toProto().toByteArray()

/**
* Encodes the [DebugSettings] data class to a Base64 string.
* Encodes the [DebugSettings] data class to a string.
*/
fun DebugSettings.encodeAsString(): String {
val protoValue = this.toProto() // Data class -> Proto
return Base64.encodeToString(protoValue.toByteArray(), Base64.NO_WRAP)
val lensStr = singleLensMode?.name ?: ""
val patternStr = when (val pattern = testPattern) {
is TestPattern.SolidColor ->
"SolidColor(${pattern.red},${pattern.greenEven},${pattern.greenOdd},${pattern.blue})"
else -> pattern.toString()
}
return "debug:$isDebugModeEnabled;lens:$lensStr;pattern:$patternStr"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,9 @@
* limitations under the License.
*/
package com.google.jetpackcamera.model
import com.google.jetpackcamera.model.proto.DynamicRange as DynamicRangeProto

val DEFAULT_HDR_DYNAMIC_RANGE = DynamicRange.HLG10

enum class DynamicRange {
SDR,
HLG10;

companion object {

/** returns the DynamicRangeType enum equivalent of a provided DynamicRangeTypeProto */
fun fromProto(dynamicRangeProto: DynamicRangeProto): DynamicRange {
return when (dynamicRangeProto) {
DynamicRangeProto.DYNAMIC_RANGE_HLG10 -> HLG10

// Treat unrecognized and unspecified as SDR as a fallback
DynamicRangeProto.DYNAMIC_RANGE_SDR,
DynamicRangeProto.DYNAMIC_RANGE_UNSPECIFIED,
DynamicRangeProto.UNRECOGNIZED -> SDR
}
}

fun DynamicRange.toProto(): DynamicRangeProto {
return when (this) {
SDR -> DynamicRangeProto.DYNAMIC_RANGE_SDR
HLG10 -> DynamicRangeProto.DYNAMIC_RANGE_HLG10
}
}
}
HLG10
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,9 @@
*/
package com.google.jetpackcamera.model

import com.google.jetpackcamera.model.proto.ImageOutputFormat as ImageOutputFormatProto

val DEFAULT_HDR_IMAGE_OUTPUT = ImageOutputFormat.JPEG_ULTRA_HDR

enum class ImageOutputFormat {
JPEG,
JPEG_ULTRA_HDR;

companion object {

/** returns the DynamicRangeType enum equivalent of a provided DynamicRangeTypeProto */
fun fromProto(imageOutputFormatProto: ImageOutputFormatProto): ImageOutputFormat {
return when (imageOutputFormatProto) {
ImageOutputFormatProto.IMAGE_OUTPUT_FORMAT_JPEG_ULTRA_HDR -> JPEG_ULTRA_HDR

// Treat unrecognized as JPEG as a fallback
ImageOutputFormatProto.IMAGE_OUTPUT_FORMAT_JPEG,
ImageOutputFormatProto.UNRECOGNIZED -> JPEG
}
}

fun ImageOutputFormat.toProto(): ImageOutputFormatProto {
return when (this) {
JPEG -> ImageOutputFormatProto.IMAGE_OUTPUT_FORMAT_JPEG
JPEG_ULTRA_HDR -> ImageOutputFormatProto.IMAGE_OUTPUT_FORMAT_JPEG_ULTRA_HDR
}
}
}
JPEG_ULTRA_HDR
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.google.jetpackcamera.model

import com.google.jetpackcamera.model.proto.LensFacing as LensFacingProto

enum class LensFacing {
BACK,
FRONT;
Expand All @@ -27,25 +25,4 @@ enum class LensFacing {
BACK -> FRONT
}
}

companion object {

/** returns the LensFacing enum equivalent of a provided LensFacingProto */
fun fromProto(lensFacingProto: LensFacingProto): LensFacing {
return when (lensFacingProto) {
LensFacingProto.LENS_FACING_BACK -> BACK

// Treat unrecognized as front as a fallback
LensFacingProto.LENS_FACING_FRONT,
LensFacingProto.UNRECOGNIZED -> FRONT
}
}

fun LensFacing.toProto(): LensFacingProto {
return when (this) {
BACK -> LensFacingProto.LENS_FACING_BACK
FRONT -> LensFacingProto.LENS_FACING_FRONT
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,7 @@
*/
package com.google.jetpackcamera.model

import com.google.jetpackcamera.model.proto.LowLightBoostPriority as LowLightBoostPriorityProto

enum class LowLightBoostPriority {
PRIORITIZE_AE_MODE,
PRIORITIZE_GOOGLE_PLAY_SERVICES;

companion object {
/**
* Returns the [LowLightBoostPriority] enum equivalent of a provided [LowLightBoostPriorityProto].
*
* @param lowLightBoostPriorityProto The proto to convert from.
* @return The converted [LowLightBoostPriority].
*/
fun fromProto(
lowLightBoostPriorityProto: LowLightBoostPriorityProto
): LowLightBoostPriority {
return when (lowLightBoostPriorityProto) {
LowLightBoostPriorityProto.LOW_LIGHT_BOOST_PRIORITY_AE_MODE -> PRIORITIZE_AE_MODE
LowLightBoostPriorityProto.LOW_LIGHT_BOOST_PRIORITY_GOOGLE_PLAY_SERVICES ->
PRIORITIZE_GOOGLE_PLAY_SERVICES
LowLightBoostPriorityProto.UNRECOGNIZED -> PRIORITIZE_AE_MODE // Default to AE mode
}
}

fun LowLightBoostPriority.toProto(): LowLightBoostPriorityProto {
return when (this) {
PRIORITIZE_AE_MODE -> LowLightBoostPriorityProto.LOW_LIGHT_BOOST_PRIORITY_AE_MODE
PRIORITIZE_GOOGLE_PLAY_SERVICES ->
LowLightBoostPriorityProto.LOW_LIGHT_BOOST_PRIORITY_GOOGLE_PLAY_SERVICES
}
}
}
PRIORITIZE_GOOGLE_PLAY_SERVICES
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package com.google.jetpackcamera.model

import com.google.jetpackcamera.model.proto.StabilizationMode as StabilizationModeProto

/** Enum class representing the device's supported stabilization configurations. */
enum class StabilizationMode {
/** Stabilization off */
OFF,
Expand All @@ -36,22 +33,5 @@ enum class StabilizationMode {
HIGH_QUALITY,

/** Optical Stabilization (OIS) */
OPTICAL;

companion object {
/** returns the AspectRatio enum equivalent of a provided AspectRatioProto */
fun fromProto(stabilizationModeProto: StabilizationModeProto): StabilizationMode =
when (stabilizationModeProto) {
StabilizationModeProto.STABILIZATION_MODE_OFF -> OFF
StabilizationModeProto.STABILIZATION_MODE_ON -> ON
StabilizationModeProto.STABILIZATION_MODE_HIGH_QUALITY -> HIGH_QUALITY
StabilizationModeProto.STABILIZATION_MODE_OPTICAL -> OPTICAL

// Default to AUTO
StabilizationModeProto.STABILIZATION_MODE_UNDEFINED,
StabilizationModeProto.UNRECOGNIZED,
StabilizationModeProto.STABILIZATION_MODE_AUTO
-> AUTO
}
}
OPTICAL
}
Loading
Loading