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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mannodermaus committed May 7, 2015
1 parent 216ba09 commit ea0ead0
Show file tree
Hide file tree
Showing 37 changed files with 1,839 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
*.iml
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Start a network service discovery using `RxBonjour.startDiscovery(Context, Strin
RxBonjour.startDiscovery(this, "_http._tcp")
.subscribe(new Action1<BonjourEvent>() {
@Override public void call(BonjourEvent bonjourEvent) {
// Depending on the type of event and the availability of the item, adjust the adapter
BonjourService item = bonjourEvent.getService();
switch (bonjourEvent.getType()) {
case ADDED:
Expand Down
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}
27 changes: 27 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion COMPILE_SDK_VERSION
buildToolsVersion BUILD_TOOLS_VERSION

defaultConfig {
applicationId "rxbonjour.example"
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
versionCode 1
versionName VERSION_NAME
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile project(':lib')
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:22.1.1'
}
17 changes: 17 additions & 0 deletions example/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/marcel/Documents/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
21 changes: 21 additions & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rxbonjour.example">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>

</manifest>
37 changes: 37 additions & 0 deletions example/src/main/java/rxbonjour/example/BonjourVH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package rxbonjour.example;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rxbonjour.example.rv.RvBaseHolder;
import rxbonjour.model.BonjourService;

/**
* @author marcel
*/
public class BonjourVH extends RvBaseHolder<BonjourService> {

@InjectView(R.id.tv_name) TextView tvName;
@InjectView(R.id.tv_type) TextView tvType;
@InjectView(R.id.tv_host_port) TextView tvHostPort;

/**
* Constructor
*
* @param inflater Layout inflater used to inflate the ViewHolder's layout
* @param parent Parent of the View to inflate
*/
protected BonjourVH(LayoutInflater inflater, ViewGroup parent) {
super(inflater, parent, R.layout.item_bonjourservice);
ButterKnife.inject(this, itemView);
}

@Override protected void onBindItem(BonjourService item) {
tvName.setText(item.getName());
tvType.setText(item.getType());
tvHostPort.setText(item.getHost() + ":" + item.getPort());
}
}
116 changes: 116 additions & 0 deletions example/src/main/java/rxbonjour/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package rxbonjour.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;
import rxbonjour.RxBonjour;
import rxbonjour.example.rv.RvBaseAdapter;
import rxbonjour.example.rv.RvBaseHolder;
import rxbonjour.model.BonjourEvent;
import rxbonjour.model.BonjourService;

/**
* @author marcel
*/
public class MainActivity extends AppCompatActivity {

@InjectView(R.id.rv) rxbonjour.example.rv.Rv rvItems;
@InjectView(R.id.et_type) EditText etInput;
private RvBaseAdapter<BonjourService> adapter;

private Subscription nsdSubscription;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);

// Setup RecyclerView
rvItems.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapter = new RvBaseAdapter<BonjourService>() {
@Override protected RvBaseHolder<BonjourService> createViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
return new BonjourVH(inflater, parent);
}
};
rvItems.setEmptyView(ButterKnife.findById(this, R.id.tv_empty));
rvItems.setAdapter(adapter);
}

@Override protected void onResume() {
super.onResume();

// Start a Bonjour lookup
restartDiscovery();
}

@Override protected void onPause() {
super.onPause();

// Unsubscribe from the network service discovery Observable
unsubscribe();
}

@OnClick(R.id.button_apply) void onApplyClicked() {
CharSequence input = etInput.getText();
if (input != null && input.length() > 0) {
// For non-empty input, restart the discovery with the new input
restartDiscovery();
}
}

/* Begin private */

private void unsubscribe() {
if (nsdSubscription != null) {
nsdSubscription.unsubscribe();
nsdSubscription = null;
}
}

private void restartDiscovery() {
// Check the current input, only proceed if valid
String input = etInput.getText().toString();
if (!RxBonjour.isBonjourType(input)) {
Toast.makeText(this, getString(R.string.toast_invalidtype, input), Toast.LENGTH_SHORT).show();
return;
}

// Cancel any previous subscription
unsubscribe();

// Clear the adapter's items, then start a new discovery
adapter.clearItems();
nsdSubscription = RxBonjour.startDiscovery(this, input)
.subscribe(new Action1<BonjourEvent>() {
@Override public void call(BonjourEvent bonjourEvent) {
// Depending on the type of event and the availability of the item, adjust the adapter
BonjourService item = bonjourEvent.getService();
switch (bonjourEvent.getType()) {
case ADDED:
if (!adapter.containsItem(item)) adapter.addItem(item);
break;

case REMOVED:
if (adapter.containsItem(item)) adapter.removeItem(item);
break;
}
}
}, new Action1<Throwable>() {
@Override public void call(Throwable throwable) {
Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

}
Loading

0 comments on commit ea0ead0

Please sign in to comment.