Skip to content

Commit

Permalink
Merge pull request #793 from dariuszseweryn/develop
Browse files Browse the repository at this point in the history
RxJava 3 modules with source generation
  • Loading branch information
dariuszseweryn authored Jun 12, 2022
2 parents 979791e + e1a53a2 commit 10c3bb7
Show file tree
Hide file tree
Showing 37 changed files with 580 additions and 354 deletions.
46 changes: 46 additions & 0 deletions .buildscript/update_version_in_readmes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
# Recommended pre-commit hook for people releasing RxAndroidBle
# This script updates all README.md files with the current value of VERSION_NAME specified in
# $root/gradle.properties when following conditions are met:
# - this script is run on the release branch
# - the VERSION_NAME has changed
# - the VERSION_NAME is not a "SNAPSHOT" release

# Check if on release branch
current_branch="$(git branch --show-current)"
release_branch='master'
echo current_branch: "$current_branch", release_branch: "$release_branch"
if [[ $current_branch != "$release_branch" ]]; then
exit 0
fi
echo not equal

# Check if VERSION_NAME changed
this_script_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_project_path="${this_script_path%/*}"
git stash -m "PRE-COMMIT-VERSIONS-CHANGE-CHECK-PREPARE" --keep-index --include-untracked
git stash -m "PRE-COMMIT-VERSIONS-CHANGE-CHECK"
version_name_line=$(grep ^VERSION_NAME= "$root_project_path"/gradle.properties)
version_name_old=${version_name_line##*=}
git stash pop
git add .
git stash pop
version_name_line=$(grep ^VERSION_NAME= "$root_project_path"/gradle.properties)
version_name_new=${version_name_line##*=}
if [[ $version_name_old -eq $version_name_new ]]; then
exit 0
fi

# Check if VERSION_NAME is not SNAPSHOT
if [[ $version_name_new == *"SNAPSHOT"* ]]; then
exit 0
fi

# Update version for gradle and maven in all README.md files
git stash -m "PRE-COMMIT-HOOK-STASH" --keep-index
find "$root_project_path" -name README.md \
-exec sed -i '' \
-e "s~\(.*implementation .*:\)\(.*\)\(\\\"\)~\1$version_name_new\3~g" \
-e "s~\(.*<version>\)\(.*\)\(<\/version>\)~\1$version_name_new\3~g" {} ";" \
-exec git add {} ";"
git stash pop
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ local.properties
/build
dagger-compiler-shadow/build
dagger-library-shadow/build
rxandroidble3/src
mockrxandroidble3/src
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ The first step is to include RxAndroidBle into your project.
### Gradle
If you use Gradle to build your project — as a Gradle project implementation dependency:
```groovy
implementation "com.polidea.rxandroidble3:rxandroidble:1.14.1"
```
or for RxJava 2 based artifact
```groovy
implementation "com.polidea.rxandroidble2:rxandroidble:1.14.1"
```
### Maven
If you use Maven to build your project — as a Maven project dependency:
```xml
<dependency>
<groupId>com.polidea.rxandroidble3</groupId>
<artifactId>rxandroidble</artifactId>
<version>1.14.1</version>
<type>aar</type>
</dependency>
```
or for RxJava 2 based artifact
```xml
<dependency>
<groupId>com.polidea.rxandroidble2</groupId>
<artifactId>rxandroidble</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions dagger-library-shadow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'com.vanniktech.maven.publish'
apply from: rootProject.file('gradle/shadow.gradle')
apply from: rootProject.file('gradle/gradle-mvn-config.gradle')

import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
shadowJar {
Expand All @@ -28,3 +29,18 @@ java {
sourceCompatibility = rootProject.ext.sourceCompatibilityVersion
targetCompatibility = rootProject.ext.targetCompatibilityVersion
}

task javadocs(type: Javadoc) {
source = sourceSets.main.java.source
classpath += configurations.compileClasspath
}

task javadocsJar(type: Jar, dependsOn: javadocs) {
archiveClassifier.set('javadoc')
from javadocs.destinationDir
}

task sourcesJar(type: Jar) {
archiveClassifier.set('sources')
from sourceSets.main.allSource
}
9 changes: 6 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
ext {
kotlinVersion = '1.6.21'
rxJavaVersion = '2.2.17'
rxJava2Version = '2.2.17'
rxJava3Version = '3.1.4'
sourceCompatibilityVersion = JavaVersion.VERSION_1_8
targetCompatibilityVersion = JavaVersion.VERSION_1_8
shadowPluginVersion = '7.1.2'
daggerVersion = '2.42'
libs = [
/* Library-only dependencies */
rxjava : "io.reactivex.rxjava2:rxjava:$rxJavaVersion",
rxjava2 : "io.reactivex.rxjava2:rxjava:$rxJava2Version",
rxjava3 : "io.reactivex.rxjava3:rxjava:$rxJava3Version",
rxandroid : 'io.reactivex.rxjava2:rxandroid:2.1.1',
rxrelay : 'com.jakewharton.rxrelay2:rxrelay:2.1.1',
rxrelay2 : 'com.jakewharton.rxrelay2:rxrelay:2.1.1',
rxrelay3 : 'com.jakewharton.rxrelay3:rxrelay:3.0.1',
junit : 'org.junit.jupiter:junit-jupiter:5.8.1',
groovy : ['org.codehaus.groovy:groovy:3.0.9', 'org.codehaus.groovy:groovy-test:3.0.9'],
spock : 'org.spockframework:spock-core:2.0-groovy-3.0',
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.polidea.rxandroidble2
VERSION_NAME=1.14.1
VERSION_NAME=1.15.0-SNAPSHOT

POM_DESCRIPTION=RxJava backed support for Bluetooth Low Energy in Android

Expand Down
13 changes: 9 additions & 4 deletions gradle/gradle-mvn-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def getRepositoryPassword() {
return hasProperty('SONATYPE_NEXUS_PASSWORD') ? property('SONATYPE_NEXUS_PASSWORD') : ""
}

def isAndroidProject() {
return project.getPlugins().hasPlugin('com.android.application') || project.getPlugins().hasPlugin('com.android.library')
def isAndroidProject(Project maybeProject = null) {
return (maybeProject ?: project).getPlugins().hasPlugin('com.android.application') || project.getPlugins().hasPlugin('com.android.library')
}

if (isAndroidProject()) {
Expand Down Expand Up @@ -71,8 +71,13 @@ afterEvaluate { project ->
publications {
release(MavenPublication) {
afterEvaluate {
if (isAndroidProject()) from components.release
else from components.java
if (isAndroidProject(project)) {
from components.release
} else {
from components.java
artifact sourcesJar
artifact javadocsJar
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions gradle/src-gen-rxjava3-from-rxjava2.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// generates sources compatible with RxJava 2 to RxJava 3

copy {
from "../${project.name.substring(0, project.name.length() - 1)}/src"
into 'src'
includeEmptyDirs = false
eachFile {
name = name.replace('rxandroidble2', 'rxandroidble3')
path = path.replace('rxandroidble2', 'rxandroidble3')
filter {
it.replace('rxandroidble2', 'rxandroidble3')
.replaceAll('io\\.reactivex\\.([a-z]+)', 'io.reactivex.rxjava3.$1')
.replaceAll('io\\.reactivex\\.([A-Z][a-z]+)', 'io.reactivex.rxjava3.core.$1')
.replace('import io.reactivex.rxjava3.disposables.Disposables', 'import io.reactivex.rxjava3.disposables.Disposable')
.replace('Disposables.fromAction', 'Disposable.fromAction')
.replace('Disposables.empty()', 'Disposable.empty()')
.replace('com.jakewharton.rxrelay2.BehaviorRelay', 'com.jakewharton.rxrelay3.BehaviorRelay')
.replace('.startWith(', '.startWithItem(')
.replace('import com.jakewharton.rxrelay2', 'import com.jakewharton.rxrelay3')
}
}
}
2 changes: 1 addition & 1 deletion mockrxandroidble/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ dependencies {
testImplementation rootProject.ext.libs.groovy
testImplementation rootProject.ext.libs.spock
api project(path: ':rxandroidble')
api rootProject.ext.libs.rxjava
api rootProject.ext.libs.rxjava2
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@
import com.polidea.rxandroidble2.Timeout;
import com.polidea.rxandroidble2.exceptions.BleAlreadyConnectedException;
import com.polidea.rxandroidble2.exceptions.BleException;
import com.polidea.rxandroidble2.mockrxandroidble.callbacks.RxBleCharacteristicReadCallback;
import com.polidea.rxandroidble2.mockrxandroidble.callbacks.RxBleCharacteristicWriteCallback;
import com.polidea.rxandroidble2.mockrxandroidble.callbacks.RxBleDescriptorReadCallback;
import com.polidea.rxandroidble2.mockrxandroidble.callbacks.RxBleDescriptorWriteCallback;
import com.polidea.rxandroidble2.scan.ScanRecord;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.ReplaySubject;

Expand Down Expand Up @@ -81,10 +73,10 @@ public RxBleDeviceMock(String name,
rxBleDeviceServices,
rssi,
characteristicNotificationSources,
new HashMap<UUID, RxBleCharacteristicReadCallback>(),
new HashMap<UUID, RxBleCharacteristicWriteCallback>(),
new HashMap<UUID, Map<UUID, RxBleDescriptorReadCallback>>(),
new HashMap<UUID, Map<UUID, RxBleDescriptorWriteCallback>>())
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>())
);
this.legacyScanRecord = scanRecord;
}
Expand Down Expand Up @@ -252,33 +244,17 @@ public void addAdvertisedUUID(UUID advertisedUUID) {

@Override
public Observable<RxBleConnection> establishConnection(boolean autoConnect) {
return Observable.defer(new Callable<Observable<RxBleConnection>>() {
@Override
public Observable<RxBleConnection> call() {
if (isConnected.compareAndSet(false, true)) {
return RxBleDeviceMock.this.emitConnectionWithoutCompleting()
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
connectionStateBehaviorSubject.onNext(CONNECTING);
}
})
.doOnNext(new Consumer<RxBleConnection>() {
@Override
public void accept(RxBleConnection rxBleConnection) throws Exception {
connectionStateBehaviorSubject.onNext(CONNECTED);
}
})
.doFinally(new Action() {
@Override
public void run() {
connectionStateBehaviorSubject.onNext(DISCONNECTED);
isConnected.set(false);
}
});
} else {
return Observable.error(new BleAlreadyConnectedException(macAddress));
}
return Observable.defer(() -> {
if (isConnected.compareAndSet(false, true)) {
return RxBleDeviceMock.this.emitConnectionWithoutCompleting()
.doOnSubscribe(disposable -> connectionStateBehaviorSubject.onNext(CONNECTING))
.doOnNext(rxBleConnection -> connectionStateBehaviorSubject.onNext(CONNECTED))
.doFinally(() -> {
connectionStateBehaviorSubject.onNext(DISCONNECTED);
isConnected.set(false);
});
} else {
return Observable.error(new BleAlreadyConnectedException(macAddress));
}
});
}
Expand All @@ -291,12 +267,7 @@ public Observable<RxBleConnection> establishConnection(boolean autoConnect, Time
private Observable<RxBleConnection> emitConnectionWithoutCompleting() {
connectionSubject = ReplaySubject.createWithSize(1);
connectionSubject.onNext(rxBleConnection);
return connectionSubject.doFinally(new Action() {
@Override
public void run() throws Exception {
connectionSubject = null;
}
});
return connectionSubject.doFinally(() -> connectionSubject = null);
}

public void disconnectWithException(BleException exception) {
Expand Down
1 change: 1 addition & 0 deletions mockrxandroidble3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
94 changes: 94 additions & 0 deletions mockrxandroidble3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'src' is generated from 'mockrxandroidble' by script in 'gradle/src-gen-rxjava3-from-rxjava2.gradle'

MockRxAndroidBle
=============

This project allows to mock a Bluetooth LE device. It's supposed to be used with RxAndroidBle library.


### Example

Using MockRxAndroidBle you can start writing and testing your Bluetooth code having just the device's specification.

Here's an example:

```java
RxBleClient rxBleClientMock = new RxBleClientMock.Builder()
.addDevice(new RxBleDeviceMock.Builder() // <-- creating device mock, there can me multiple of them
.deviceMacAddress(macAddress)
.deviceName(deviceName)
.scanRecord(new RxBleScanRecordMock.Builder()
.setAdvertiseFlags(1)
.addServiceUuid(new ParcelUuid(serviceUUID))
.addServiceUuid(new ParcelUuid(serviceUUID2))
.addManufacturerSpecificData(0x2211, [0x33, 0x44] as byte[])
.addServiceData(new ParcelUuid(serviceUUID), [0x11, 0x22] as byte[])
.setTxPowerLevel(12)
.setDeviceName("TestDeviceAdv")
.build()
)
.connection(new RxBleConnectionMock.Builder() // <-- creating connection mock
.rssi(rssi)
.notificationSource(characteristicNotifiedUUID, characteristicNotificationSubject)
.addService( // <-- adding service mocks to the device, there can be multiple of them
serviceUUID,
new RxBleClientMock.CharacteristicsBuilder()
.addCharacteristic( // <-- adding characteristic mocks to the service, there can be multiple of them
characteristicUUID,
characteristicData,
new RxBleClientMock.DescriptorsBuilder()
.addDescriptor(descriptorUUID, descriptorData) // <-- adding descriptor mocks
.build() // to the characteristic, there can be multiple of them
).build()
).characteristicReadCallback(characteristicUUID, (device, characteristic, result) -> {
result.success(characteristicValue);
})
.characteristicWriteCallback(characteristicUUID, (device, characteristic, bytes, result) -> {
if(writeData(characteristic, bytes)) {
result.success();
} else {
result.failure(0x80);
// can also use result.disconnect(0x80);
}
}).build()
).build()
).build();

// Now mocked client can be used the same way as RxAndroidBle client
```

### Download

Get MockRxAndroidBle via Maven:

```xml
<dependency>
<groupId>com.polidea.rxandroidble3</groupId>
<artifactId>mockclient</artifactId>
<version>1.14.1</version>
<type>aar</type>
</dependency>
```

or via Gradle

```groovy
implementation "com.polidea.rxandroidble3:mockclient:1.14.1"
```

### License

Copyright 2016 Polidea Sp. z o.o
Copyright 2021 Dariusz Seweryn

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

http://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.
Loading

0 comments on commit 10c3bb7

Please sign in to comment.