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

Commit

Permalink
update rxdnssd to 0.7.0 version
Browse files Browse the repository at this point in the history
add support of ICS
fix issue on Samsungs 4.4.2
  • Loading branch information
andriydruk committed Apr 13, 2016
1 parent ff3dc65 commit 33faaf8
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 40 deletions.
23 changes: 7 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
buildToolsVersion '23.0.3'

defaultConfig {
minSdkVersion 16
minSdkVersion 14
targetSdkVersion 23
versionCode 7
versionName "1.2.1"
versionCode 8
versionName "1.3.0"
}

buildTypes {
Expand All @@ -27,28 +27,19 @@ android {
dataBinding {
enabled true
}

defaultConfig {
// Stops the Gradle’s automatic rasterization of vectors
generatedDensities = []
}
// Flag that tells aapt to keep the attribute ids
aaptOptions {
additionalParameters "--no-version-vectors"
}
}

dependencies {
//Android Support Libraries Version
def supportLibrary = '23.2.0'
def supportLibrary = '23.3.0'

compile 'com.github.andriydruk:rxdnssd:0.6.3'
compile 'com.github.andriydruk:rxdnssd:0.7.0'
compile "com.android.support:support-v4:$supportLibrary"
compile "com.android.support:appcompat-v7:$supportLibrary"
compile "com.android.support:design:$supportLibrary"
compile "com.android.support:recyclerview-v7:$supportLibrary"
compile "com.android.support:cardview-v7:$supportLibrary"
compile "com.android.support:support-annotations:$supportLibrary"
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxjava:1.1.3'
}
25 changes: 22 additions & 3 deletions app/src/main/java/com/druk/bonjour/browser/BonjourApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
package com.druk.bonjour.browser;

import com.github.druk.rxdnssd.RxDnssd;
import com.github.druk.rxdnssd.RxDnssdBindable;
import com.github.druk.rxdnssd.RxDnssdEmbedded;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -34,6 +38,7 @@ public class BonjourApplication extends Application {

private static final String TAG = "BonjourApplication";
private TreeMap<String, String> mServiceNamesTree;
private RxDnssd mRxDnssd;

@Override
public void onCreate() {
Expand All @@ -46,23 +51,37 @@ public void onCreate() {
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.penaltyDeath()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
mRxDnssd = createDnssd();
}

RxDnssd.init(this);
public static RxDnssd getRxDnssd(@NonNull Context context){
return ((BonjourApplication)context.getApplicationContext()).mRxDnssd;
}

public static String getRegTypeDescription(@NonNull Context context, String regType) {
return ((BonjourApplication) context.getApplicationContext()).getRegTypeDescription(regType);
}

private RxDnssd createDnssd(){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
Log.i(TAG, "Using embedded version of dns sd because of API < 16");
return new RxDnssdEmbedded();
}
if (Build.VERSION.RELEASE.contains("4.4.2") && Build.MANUFACTURER.toLowerCase().contains("samsung")){
Log.i(TAG, "Using embedded version of dns sd because of Samsung 4.4.2");
return new RxDnssdEmbedded();
}
Log.i(TAG, "Using systems dns sd daemon");
return new RxDnssdBindable(this);
}

private String getRegTypeDescription(String regType) {
if (mServiceNamesTree == null){
mServiceNamesTree = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.druk.bonjour.browser.Config;
import com.druk.bonjour.browser.ui.adapter.ServiceAdapter;
import com.github.druk.rxdnssd.BonjourService;
import com.github.druk.rxdnssd.RxDnssd;

import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand All @@ -31,6 +30,7 @@
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.druk.bonjour.browser.Config.EMPTY_DOMAIN;
import static com.druk.bonjour.browser.Config.TCP_REG_TYPE_SUFFIX;
Expand Down Expand Up @@ -70,7 +70,8 @@ public void onBindViewHolder(ViewHolder viewHolder, int i) {

@Override
protected void startDiscovery() {
mSubscription = RxDnssd.browse(Config.SERVICES_DOMAIN, "local.")
mSubscription = mRxDnssd.browse(Config.SERVICES_DOMAIN, "local.")
.subscribeOn(Schedulers.io())
.subscribe(reqTypeAction, errorAction);
}

Expand All @@ -86,7 +87,6 @@ protected void stopDiscovery() {

private final Action1<BonjourService> reqTypeAction = service -> {
if ((service.getFlags() & BonjourService.LOST) == BonjourService.LOST){
Log.d("TAG", "Lose reg type: " + service);
//Ignore this call
return;
}
Expand All @@ -96,17 +96,22 @@ protected void stopDiscovery() {
if (TCP_REG_TYPE_SUFFIX.equals(protocolSuffix) || UDP_REG_TYPE_SUFFIX.equals(protocolSuffix)) {
String key = service.getServiceName() + "." + protocolSuffix;
if (!mBrowsers.containsKey(key)) {
mBrowsers.put(key, RxDnssd.browse(key, serviceDomain)
mBrowsers.put(key, mRxDnssd.browse(key, serviceDomain)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(RegTypeBrowserFragment.this.servicesAction, RegTypeBrowserFragment.this.errorAction));
}
mServices.put(createKey(service.getDomain(), service.getRegType(), service.getServiceName()), new BonjourDomain(service));
} else {
Log.e("TAG", "Unknown service protocol " + protocolSuffix);
//Just ignore service with different protocol suffixes
}
};

protected final Action1<Throwable> errorAction = throwable -> Log.e("DNSSD", "Error: ", throwable);
protected final Action1<Throwable> errorAction = throwable -> {
Log.e("DNSSD", "Error: ", throwable);
showError(throwable);
};

private final Action1<BonjourService> servicesAction = service -> {
String[] regTypeParts = service.getRegType().split(Config.REG_TYPE_SEPARATOR);
Expand All @@ -121,10 +126,14 @@ protected void stopDiscovery() {
} else {
domain.serviceCount++;
}
final int itemsCount = mAdapter.getItemCount();
mAdapter.clear();
Observable.from(mServices.values())
.filter(bonjourDomain -> bonjourDomain.serviceCount > 0)
.subscribe(mAdapter::add, throwable -> {/* empty */}, mAdapter::notifyDataSetChanged);
.subscribe(mAdapter::add, throwable -> {/* empty */}, () -> {
showList(itemsCount);
mAdapter.notifyDataSetChanged();
});
} else {
Log.w(TAG, "Service from unknown service type " + key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package com.druk.bonjour.browser.ui.fragment;

import com.druk.bonjour.browser.BonjourApplication;
import com.druk.bonjour.browser.R;
import com.druk.bonjour.browser.ui.adapter.ServiceAdapter;
import com.github.druk.rxdnssd.BonjourService;
import com.github.druk.rxdnssd.RxDnssd;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand All @@ -30,9 +33,14 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ServiceBrowserFragment<T> extends Fragment {

Expand All @@ -45,6 +53,9 @@ public class ServiceBrowserFragment<T> extends Fragment {
protected String mReqType;
protected String mDomain;
protected RecyclerView mRecyclerView;
protected ProgressBar mProgressView;
protected LinearLayout mErrorView;
protected RxDnssd mRxDnssd;

protected View.OnClickListener mListener = new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -78,6 +89,8 @@ public void onAttach(Context context) {
if (!(context instanceof ServiceListener)) {
throw new IllegalArgumentException("Fragment context should implement ServiceListener interface");
}

mRxDnssd = BonjourApplication.getRxDnssd(context);
}

@Override
Expand Down Expand Up @@ -110,14 +123,17 @@ else if (service.getInet6Address() != null) {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_service_browser, container, false);
FrameLayout rootView = (FrameLayout) inflater.inflate(R.layout.fragment_service_browser, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mProgressView = (ProgressBar) rootView.findViewById(R.id.progress);
mErrorView = (LinearLayout) rootView.findViewById(R.id.error_container);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
if (savedInstanceState != null) {
mAdapter.setSelectedItemId(savedInstanceState.getLong(KEY_SELECTED_POSITION, -1L));
}
return mRecyclerView;
return rootView;
}

@Override
Expand All @@ -139,22 +155,75 @@ public void onSaveInstanceState(Bundle outState) {
}

protected void startDiscovery() {
mSubscription = RxDnssd.browse(mReqType, mDomain)
.compose(RxDnssd.resolve())
.compose(RxDnssd.queryRecords())
mSubscription = mRxDnssd.browse(mReqType, mDomain)
.compose(mRxDnssd.resolve())
.compose(mRxDnssd.queryRecords())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bonjourService -> {
int itemsCount = mAdapter.getItemCount();
if ((bonjourService.getFlags() & BonjourService.LOST) != BonjourService.LOST) {
mAdapter.add(bonjourService);
} else {
mAdapter.remove(bonjourService);
}
showList(itemsCount);
mAdapter.notifyDataSetChanged();
}, throwable -> {
Log.e("DNSSD", "Error: ", throwable);
showError(throwable);
});
}

protected boolean showList(int itemsBefore){
if (itemsBefore > 0 && mAdapter.getItemCount() == 0) {
mRecyclerView.animate().alpha(0.0f).setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mRecyclerView.setVisibility(View.GONE);
}
}).start();
mProgressView.setAlpha(0.0f);
mProgressView.setVisibility(View.VISIBLE);
mProgressView.animate().alpha(1.0f).setInterpolator(new AccelerateDecelerateInterpolator()).start();
return true;
}
if (itemsBefore == 0 && mAdapter.getItemCount() > 0) {
mProgressView.animate().alpha(0.0f).setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressView.setVisibility(View.GONE);
}
}).start();
mRecyclerView.setAlpha(0.0f);
mRecyclerView.setVisibility(View.VISIBLE);
mRecyclerView.animate().alpha(1.0f).setInterpolator(new AccelerateDecelerateInterpolator()).start();
return false;
}
return mAdapter.getItemCount() > 0;
}

protected void showError(final Throwable e){
getActivity().runOnUiThread(() -> {
mRecyclerView.animate().alpha(0.0f).setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mRecyclerView.setVisibility(View.GONE);
}
}).start();
mProgressView.animate().alpha(0.0f).setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressView.setVisibility(View.GONE);
}
}).start();
mErrorView.setAlpha(0.0f);
mErrorView.setVisibility(View.VISIBLE);
mErrorView.animate().alpha(1.0f).setInterpolator(new AccelerateDecelerateInterpolator()).start();
mErrorView.findViewById(R.id.send_report).setOnClickListener(v -> Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e));
});
}

protected void stopDiscovery() {
if (mSubscription != null) {
mSubscription.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.druk.bonjour.browser.ui.fragment;

import com.druk.bonjour.browser.BonjourApplication;
import com.druk.bonjour.browser.R;
import com.druk.bonjour.browser.ui.adapter.TxtRecordsAdapter;
import com.github.druk.rxdnssd.BonjourService;
Expand All @@ -21,8 +22,10 @@
import java.util.Map;

import rx.Observable;
import rx.Scheduler;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ServiceDetailFragment extends Fragment implements View.OnClickListener {

Expand Down Expand Up @@ -63,7 +66,7 @@ public void onCreate(final Bundle savedInstanceState) {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_service_browser, container, false);
mRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_service_detail, container, false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
Expand Down Expand Up @@ -103,10 +106,12 @@ private void updateUI(BonjourService service, boolean withSnakeBar) {

@Override
public void onClick(View v) {
RxDnssd mRxDnssd = BonjourApplication.getRxDnssd(getContext());
v.animate().rotationBy(180).start();
mResolveSubscription = Observable.just(mService)
.compose(RxDnssd.resolve())
.compose(RxDnssd.queryRecords())
.compose(mRxDnssd.resolve())
.compose(mRxDnssd.queryRecords())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bonjourService -> {
if ((bonjourService.getFlags() & BonjourService.LOST) == BonjourService.LOST) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_report_problem.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="48dp">
<path android:fillColor="@color/divider" android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z"/>
</vector>
Loading

0 comments on commit 33faaf8

Please sign in to comment.