Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API change for Version #2151

Merged
merged 9 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
19 changes: 15 additions & 4 deletions datalayer/phone/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ package com.google.android.horologist.datalayer.phone {
method public suspend Object? installOnNode(String nodeId, kotlin.coroutines.Continuation<? super com.google.android.horologist.data.AppHelperResultCode>);
method @CheckResult public suspend Object? startCompanion(String nodeId, kotlin.coroutines.Continuation<? super com.google.android.horologist.data.AppHelperResultCode>);
property public kotlinx.coroutines.flow.Flow<java.util.Set<com.google.android.gms.wearable.Node>> connectedAndInstalledNodes;
field public static final com.google.android.horologist.datalayer.phone.PhoneDataLayerAppHelper.Companion Companion;
}

public static final class PhoneDataLayerAppHelper.Companion {
method public com.google.android.horologist.datalayer.phone.Version getRequiredCompanionVersion();
property public final com.google.android.horologist.datalayer.phone.Version RequiredCompanionVersion;
}

public final class PhoneDataLayerListenerService extends com.google.android.horologist.data.apphelper.DataLayerAppHelperService {
Expand All @@ -18,11 +24,16 @@ package com.google.android.horologist.datalayer.phone {
}

public final class Version implements java.lang.Comparable<com.google.android.horologist.datalayer.phone.Version> {
ctor public Version(String inputVersion);
method public int compareTo(com.google.android.horologist.datalayer.phone.Version other);
method public String getVersion();
method public void setVersion(String);
property public final String version;
method public java.util.List<java.lang.Integer> component1();
method public com.google.android.horologist.datalayer.phone.Version copy(java.util.List<java.lang.Integer> inputVersion);
method public java.util.List<java.lang.Integer> getInputVersion();
property public final java.util.List<java.lang.Integer> inputVersion;
field public static final com.google.android.horologist.datalayer.phone.Version.Companion Companion;
}

public static final class Version.Companion {
method public com.google.android.horologist.datalayer.phone.Version? parse(String version);
}

}
Expand Down
3 changes: 3 additions & 0 deletions datalayer/phone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ dependencies {
implementation(libs.kotlinx.coroutines.playservices)
implementation(libs.androidx.lifecycle.runtime)
implementation(libs.androidx.wear.remote.interactions)

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

tasks.withType<org.jetbrains.dokka.gradle.DokkaTaskPartial>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,19 @@ public class PhoneDataLayerAppHelper(
* Checks that the companion app supports deep linking to Tile editor setting.
*/
public fun checkCompanionVersionSupportTileEditing(): AppHelperResultCode? {
try {
return try {
val packageInfo: PackageInfo =
context.packageManager.getPackageInfo("com.google.android.apps.wear.companion", 0)
val version = packageInfo.versionName

if (Version(version).compareTo(Version("2.1.0.576785526")) >= 0) {
return AppHelperResultCode.APP_HELPER_RESULT_SUCCESS
val companionVersion = Version.parse(version)
if (companionVersion != null && companionVersion >= RequiredCompanionVersion) {
AppHelperResultCode.APP_HELPER_RESULT_SUCCESS
} else {
return AppHelperResultCode.APP_HELPER_RESULT_INVALID_COMPANION
}
} catch (ex: Exception) {
when (ex) {
is PackageManager.NameNotFoundException, is IllegalArgumentException -> {
return AppHelperResultCode.APP_HELPER_RESULT_NO_COMPANION_FOUND
}

else -> throw ex
AppHelperResultCode.APP_HELPER_RESULT_INVALID_COMPANION
}
} catch (nnfe: PackageManager.NameNotFoundException) {
AppHelperResultCode.APP_HELPER_RESULT_NO_COMPANION_FOUND
}
}

Expand All @@ -146,4 +141,8 @@ public class PhoneDataLayerAppHelper(
companionPackage
}
}

public companion object {
public val RequiredCompanionVersion: Version = Version.parse("2.1.0.576785526")!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ package com.google.android.horologist.datalayer.phone

import kotlin.math.max

public class Version(inputVersion: String) : Comparable<Version> {
public data class Version internal constructor(val inputVersion: List<Int>) :
Comparable<Version> {

public var version: String
public override fun compareTo(other: Version): Int =
(inputVersion to other.inputVersion).let { (thisParts, thatParts) ->
val length = max(thisParts.size, thatParts.size)
for (i in 0 until length) {
val thisPart = if (i < thisParts.size) thisParts[i] else 0
val thatPart = if (i < thatParts.size) thatParts[i] else 0
if (thisPart < thatPart) return -1
if (thisPart > thatPart) return 1
}
0
}

public companion object {
public fun parse(version: String): Version? {
if (!version.matches("[0-9]+(\\.[0-9]+)*".toRegex())) {
return null
}

public override fun compareTo(other: Version): Int =
(split() to other.split()).let { (thisParts, thatParts) ->
val length = max(thisParts.size, thatParts.size)
for (i in 0 until length) {
val thisPart = if (i < thisParts.size) thisParts[i].toInt() else 0
val thatPart = if (i < thatParts.size) thatParts[i].toInt() else 0
if (thisPart < thatPart) return -1
if (thisPart > thatPart) return 1
return Version(version.split(".").toList().map { it.toInt() })
}
0
}

init {
require(inputVersion.matches("[0-9]+(\\.[0-9]+)*".toRegex())) { "Invalid version format" }
version = inputVersion
}
}

private fun Version.split() = version.split(".").toTypedArray()
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.datalayer.phone

import com.google.common.truth.Truth.assertThat
import org.junit.Test

class VersionTest {
@Test
fun testValid() {
assertThat(Version.parse("1.2")!!.inputVersion).containsExactly(1, 2).inOrder()
assertThat(Version.parse("2.1.0.576785526")!!.inputVersion).containsExactly(
2,
1,
0,
576785526,
).inOrder()
}

@Test
fun testNotValid() {
assertThat(Version.parse("XYZ")).isNull()
assertThat(Version.parse("2.1.0.X")).isNull()
}

@Test
fun testComparison() {
val known = Version.parse("2.1.0.576785526")
val knownPlusOne = Version.parse("2.1.1")
val old = Version.parse("1.1.0")

assertThat(known).isGreaterThan(old)
assertThat(known).isLessThan(knownPlusOne)
assertThat(known).isEquivalentAccordingToCompareTo(known)
}
}