Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Dev: replace Timer with Handler in embedded version
Browse files Browse the repository at this point in the history
  • Loading branch information
andriydruk committed Jan 30, 2019
1 parent aecc21a commit 147b292
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ Embedded version:
My dnssd library:

```groovy
compile 'com.github.andriydruk:dnssd:0.9.12'
compile 'com.github.andriydruk:dnssd:0.9.13'
```

My rxdnssd library:

```groovy
compile 'com.github.andriydruk:rxdnssd:0.9.12'
compile 'com.github.andriydruk:rxdnssd:0.9.13'
```

My rx2dnssd library:

```
compile 'com.github.andriydruk:rx2dnssd:0.9.12'
compile 'com.github.andriydruk:rx2dnssd:0.9.13'
```

* It's built with Andorid NDK r18b for all platforms (1.7 MB). If you prefer another NDK version or subset of platforms, please build it from source with command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.druk.rx2dnssd.BonjourService;
import com.github.druk.rx2dnssd.Rx2Dnssd;
import com.github.druk.rx2dnssd.Rx2DnssdBindable;
import com.github.druk.rx2dnssd.Rx2DnssdEmbedded;

import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -39,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rxDnssd = new Rx2DnssdBindable(this);
rxDnssd = new Rx2DnssdEmbedded(this);

findViewById(R.id.check_threads).setOnClickListener(v -> {
/*
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
Expand Down Expand Up @@ -43,7 +43,7 @@ ext {
bintrayConfigurations = ['archives']

bintrayGroup = 'com.github.andriydruk'
bintrayVersion = '0.9.12'
bintrayVersion = '0.9.13'

bintrayLibraryDescription = 'Android version of Apple DNSSD Java API'

Expand Down
26 changes: 8 additions & 18 deletions dnssd/src/main/java/com/github/druk/dnssd/DNSSDEmbedded.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package com.github.druk.dnssd;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

/**
* RxDnssd is implementation of RxDnssd with embedded DNS-SD {@link InternalDNSSD}
*/
Expand All @@ -31,7 +30,7 @@ public class DNSSDEmbedded extends DNSSD {

private static final String TAG = "DNSSDEmbedded";
private final long mStopTimerDelay;
private Timer mStopTimer;
private final Handler handler = new Handler(Looper.getMainLooper());
private Thread mThread;
private volatile boolean isStarted = false;
private int serviceCount = 0;
Expand All @@ -58,12 +57,10 @@ public DNSSDEmbedded(Context context, long stopTimerDelay) {
* Note: This method will block thread until DNS-SD initialization finish.
*/
public void init() {
if (mStopTimer != null) {
mStopTimer.cancel();
mStopTimer.purge();
}
handler.removeCallbacks(DNSSDEmbedded::nativeExit);

if (mThread != null && mThread.isAlive()) {
Log.i(TAG, "already started");
waitUntilStarted();
return;
}
Expand All @@ -90,7 +87,7 @@ public void run() {
}
};
mThread.setPriority(Thread.MAX_PRIORITY);
mThread.setName("DNS-SD");
mThread.setName("DNS-SDEmbedded");
mThread.start();

waitUntilStarted();
Expand All @@ -103,18 +100,11 @@ public void run() {
*/
public void exit() {
synchronized (DNSSDEmbedded.class) {
mStopTimer = new Timer();
mStopTimer.schedule(exitTask, mStopTimerDelay);
Log.i(TAG, "post exit");
handler.postDelayed(DNSSDEmbedded::nativeExit, mStopTimerDelay);
}
}

private static final TimerTask exitTask = new TimerTask() {
@Override
public void run() {
nativeExit();
}
};

private void waitUntilStarted() {
synchronized (DNSSDEmbedded.class) {
while (!isStarted) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

0 comments on commit 147b292

Please sign in to comment.