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

Network awareness #168

Merged
merged 10 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
8 changes: 8 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ junit = "4.13.2"
kotlin = "1.6.21"
kotlinxCoroutine = "1.6.1"
ktlint = "0.42.1"
com-squareup-okhttp3 = "4.9.3"
metalava = "0.2.1"
room = "2.4.2"
truth = "1.1.3"
wearcompose = "1.0.0-beta01"

Expand Down Expand Up @@ -50,6 +52,9 @@ androidx-test-rules = "androidx.test:rules:1.4.0"
androidx-wear = { module = "androidx.wear:wear", version.ref = "androidxWear" }
androidx-wear-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "androidxtiles" }
androidx-wear-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "androidxtiles" }
com-squareup-okhttp3-logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "com-squareup-okhttp3" }
com-squareup-okhttp3-okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "com-squareup-okhttp3" }
com-squareup-okhttp3-okhttp-bom = { module = "com.squareup.okhttp3:okhttp-bom", version.ref = "com-squareup-okhttp3" }
compose-foundation-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material-iconscore = { module = "androidx.compose.material:material-icons-core", version.ref = "compose" }
compose-material-iconsext = { module = "androidx.compose.material:material-icons-extended", version.ref = "compose" }
Expand All @@ -68,6 +73,9 @@ kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-c
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutine" }
metalavaGradle = { module = "me.tylerbwong.gradle:metalava-gradle", version.ref = "metalava" }
robolectric = "org.robolectric:robolectric:4.8.1"
room-common = { module = "androidx.room:room-common", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }
wearcompose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "wearcompose" }
wearcompose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearcompose" }
Expand Down
21 changes: 21 additions & 0 deletions network-awareness/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Composables library

[![Maven Central](https://img.shields.io/maven-central/v/com.google.android.horologist/horologist-network-awareness)](https://search.maven.org/search?q=g:com.google.android.horologist)

For more information, visit the documentation: https://google.github.io/horologist/network-awareness

## Download

```groovy
repositories {
mavenCentral()
}

dependencies {
implementation "com.google.android.horologist:horologist-network-awareness:<version>"
}
```

Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. These are updated on every commit.

[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/android/horologist/horologist-network-awareness/
511 changes: 511 additions & 0 deletions network-awareness/api/current.api

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions network-awareness/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2022 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.
*/

plugins {
id 'com.android.library'
id 'kotlin-android'
id 'org.jetbrains.dokka'
id "org.jetbrains.kotlin.kapt"
}

android {
compileSdkVersion 31

defaultConfig {
minSdk 26
targetSdk 30

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
buildConfig false
compose true
}

kotlinOptions {
jvmTarget = '1.8'
}

composeOptions {
kotlinCompilerExtensionVersion libs.versions.compose.get()
}

lintOptions {
textReport true
textOutput 'stdout'
// We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks
checkReleaseBuilds false
}

packagingOptions {
// Some of the META-INF files conflict with coroutines-test. Exclude them to enable
// our test APK to build (has no effect on our AARs)
excludes += "/META-INF/AL2.0"
excludes += "/META-INF/LGPL2.1"
}

testOptions {
unitTests {
includeAndroidResources = true
}
animationsDisabled true
}
}

project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach { task ->
// Workaround for https://youtrack.jetbrains.com/issue/KT-37652
if (!task.name.endsWith("TestKotlin") && !task.name.startsWith("compileDebug")) {
task.kotlinOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
}
}

dependencies {
implementation libs.kotlin.stdlib
implementation libs.androidx.wear
implementation libs.wearcompose.material
implementation libs.wearcompose.foundation
implementation libs.compose.ui.tooling
implementation libs.compose.material.iconscore
implementation libs.compose.material.iconsext

implementation libs.com.squareup.okhttp3.okhttp

implementation libs.room.common
implementation libs.room.ktx
kapt libs.room.compiler

debugImplementation libs.compose.ui.test.manifest
debugImplementation libs.compose.ui.toolingpreview

testImplementation libs.junit

androidTestImplementation libs.compose.ui.test.junit4
androidTestImplementation libs.truth
}

apply plugin: "com.vanniktech.maven.publish"
3 changes: 3 additions & 0 deletions network-awareness/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=horologist-network-awareness
POM_NAME=Horologist Network Awareness library
POM_PACKAGING=aar
22 changes: 22 additions & 0 deletions network-awareness/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2022 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.horologist.network.awareness">

<uses-feature android:name="android.hardware.type.watch" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 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.networks.data

import java.time.Instant

public data class DataRequest(
public val requestType: RequestType,
public val networkType: NetworkType,
public val dataBytes: Long,
public val time: Instant = Instant.now()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 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.networks.data

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import java.time.Instant

public interface DataRequestRepository {
public fun storeRequest(dataRequest: DataRequest)
public fun currentPeriodUsage(): Flow<DataUsageReport>

public object InMemoryDataRequestRepository : DataRequestRepository {
private val from = Instant.now()
private var ble = 0L
private var wifi = 0L
private var cell = 0L
private var unknown = 0L

public val _currentPeriodUsage: MutableStateFlow<DataUsageReport> =
yschimke marked this conversation as resolved.
Show resolved Hide resolved
MutableStateFlow(DataUsageReport(dataByType = mapOf(), from = from, to = from))

override fun currentPeriodUsage(): Flow<DataUsageReport> = _currentPeriodUsage

override fun storeRequest(dataRequest: DataRequest) {
when (dataRequest.networkType) {
is NetworkType.Cellular -> cell += dataRequest.dataBytes
is NetworkType.Bluetooth -> ble += dataRequest.dataBytes
is NetworkType.Wifi -> wifi += dataRequest.dataBytes
is NetworkType.Unknown -> unknown += dataRequest.dataBytes
}

_currentPeriodUsage.value =
DataUsageReport(
dataByType = mapOf(
NetworkType.cell to cell,
NetworkType.ble to ble,
NetworkType.wifi to wifi,
NetworkType.unknown to unknown,
),
from = from, to = Instant.now()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2022 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.networks.data

import java.time.Instant
import java.time.temporal.ChronoUnit

public data class DataUsageReport(
public val dataByType: Map<String, Long>,
public val from: Instant,
public val to: Instant,
) {
public companion object {
public val Empty: DataUsageReport = DataUsageReport(
dataByType = mapOf(),
from = Instant.now().minus(1, ChronoUnit.DAYS),
to = Instant.now()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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.networks.data

import android.net.LinkProperties
import android.net.NetworkCapabilities
import java.net.Inet6Address
import java.net.InetAddress
import java.net.Socket

public data class NetworkStatus(
public val id: String,
public val status: Status,
public val type: NetworkType,
public val addresses: List<InetAddress>,
public val capabilities: NetworkCapabilities?,
public val linkProperties: LinkProperties?,
public val bindSocket: (Socket) -> Unit
) {
public val firstAddress: InetAddress?
get() = addresses.minByOrNull { it is Inet6Address }

override fun toString(): String {
return "NetworkStatus(id=$id, status=$status, type=$type, addresses=$addresses)"
}
}
Loading