Skip to content

Commit

Permalink
Merge branch 'release/0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
chihiro hashimoto committed Nov 5, 2017
2 parents 8e230a9 + 4a8dc37 commit b5141a0
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "0.5.7"
versionName "0.6.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Enabling multidex support.
multiDexEnabled true
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/chrhsmt/sisheng/FirstScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.content.Intent
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log
import com.chrhsmt.sisheng.R.drawable.shape_rounded_corners_30dp
import com.chrhsmt.sisheng.R.drawable.shape_rounded_corners_30dp_selected
import com.chrhsmt.sisheng.font.FontUtils
import com.chrhsmt.sisheng.network.MDnsResolver
import com.chrhsmt.sisheng.ui.ScreenUtils
// 画面用にこれをimport しておく
import kotlinx.android.synthetic.main.activity_first_screen.*
import java.util.*

class FirstScreen : AppCompatActivity() {

var mMDnsResolver: MDnsResolver? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first_screen)

// mDNS
mMDnsResolver = MDnsResolver(this)
mMDnsResolver!!.discoverServices()

// フルスクリーンにする
ScreenUtils.setFullScreen(this.window)

Expand Down Expand Up @@ -98,4 +108,25 @@ class FirstScreen : AppCompatActivity() {
btnRandom.setEnabled(enable)
btnNini.setEnabled(enable)
}

override fun onPause() {
super.onPause()
this.mMDnsResolver?.let { it ->
it.tearDown()
}
}

override fun onResume() {
super.onResume()
this.mMDnsResolver?.let { it ->
it.discoverServices()
}
}

override fun onDestroy() {
super.onDestroy()
this.mMDnsResolver?.let { it ->
it.tearDown()
}
}
}
13 changes: 10 additions & 3 deletions app/src/main/java/com/chrhsmt/sisheng/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)

Settings.raspberrypiHost = this.getString(R.string.default_pi_host)
// Settings.raspberrypiHost = this.getString(R.string.default_pi_host) + ":" + context.getString(R.string.default_pi_port)
Settings.raspberrypiPath = this.getString(R.string.default_pi_path)

this.chart = Chart()
Expand Down Expand Up @@ -322,8 +322,15 @@ class MainActivity : AppCompatActivity() {
}

override fun onResponse(call: Call?, response: Response?) {
runOnUiThread {
Toast.makeText(this@MainActivity, response!!.body()!!.string(), Toast.LENGTH_LONG).show()
response?.let {
if (response.isSuccessful) {
response.body()?.let { it ->
val text = it.string()
runOnUiThread {
Toast.makeText(this@MainActivity, text, Toast.LENGTH_LONG).show()
}
}
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/chrhsmt/sisheng/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object Settings {
sex = context.resources.getStringArray(R.array.sexes)[1]
}
if (force || Settings.raspberrypiHost == "") {
raspberrypiHost = context.getString(R.string.default_pi_host)
raspberrypiHost = context.getString(R.string.default_pi_host) + ":" + context.getString(R.string.default_pi_port)
}
if (force || Settings.raspberrypiPath == "") {
raspberrypiPath = context.getString(R.string.default_pi_path)
Expand Down
118 changes: 118 additions & 0 deletions app/src/main/java/com/chrhsmt/sisheng/network/MDnsResolver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.chrhsmt.sisheng.network

import android.app.Activity
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log
import android.widget.Toast
import com.chrhsmt.sisheng.Settings
import com.chrhsmt.sisheng.R

/**
* Created by chihiro on 2017/11/05.
*/
class MDnsResolver {

companion object {
val TAG = "MDnsResolver"
val SERVICE_TYPE = "_workstation._tcp."
}

val activity: Activity
val manager: NsdManager
var mServiceName: String? = null
var mAddress: String? = null
val piDefaultHostName: String
var started: Boolean = false

val discoveryListener: NsdManager.DiscoveryListener
val resolveListener: NsdManager.ResolveListener

constructor(activity: Activity) {
this.activity = activity
manager = activity.getSystemService(android.content.Context.NSD_SERVICE) as NsdManager
this.piDefaultHostName = this.activity.getString(R.string.default_pi_hostname)

resolveListener = object : NsdManager.ResolveListener {

override fun onResolveFailed(serviceInfo: NsdServiceInfo?, errorCode: Int) {
Log.e(TAG, "Resolve failed" + errorCode);
}

override fun onServiceResolved(serviceInfo: NsdServiceInfo?) {
serviceInfo?.let {
val host = serviceInfo.host
val hostAddress = host.hostAddress

if (this@MDnsResolver.mAddress.isNullOrBlank() || !this@MDnsResolver.mAddress!!.equals(hostAddress)) {
this@MDnsResolver.mAddress = hostAddress
Settings.raspberrypiHost = hostAddress + ":" + this@MDnsResolver.activity.getString(R.string.default_pi_port)
this@MDnsResolver.activity.runOnUiThread {
Toast.makeText(this@MDnsResolver.activity, String.format("pi address is changed: %s", this@MDnsResolver.mAddress), Toast.LENGTH_LONG).show()
}
Log.d(TAG, "onServiceResolved host:" + host + ", address: " + hostAddress)
}
}
}
}

discoveryListener = object : NsdManager.DiscoveryListener {

override fun onServiceFound(serviceInfo: NsdServiceInfo?) {

Log.d(TAG, "Service discovery success:" + serviceInfo);

serviceInfo?.let {
if (!serviceInfo.getServiceType().equals(SERVICE_TYPE)) {
Log.d(TAG, "Unknown Service Type: " + serviceInfo.getServiceType());
} else if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same machine: " + mServiceName);
} else if (serviceInfo.serviceType.equals(SERVICE_TYPE) && serviceInfo.serviceName.contains(this@MDnsResolver.piDefaultHostName)){
this@MDnsResolver.mServiceName = serviceInfo.serviceName
this@MDnsResolver.manager.resolveService(serviceInfo, resolveListener);
} else {

}
}
}

override fun onStopDiscoveryFailed(serviceType: String?, errorCode: Int) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
this@MDnsResolver.manager.stopServiceDiscovery(this);
}

override fun onStartDiscoveryFailed(serviceType: String?, errorCode: Int) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
this@MDnsResolver.manager.stopServiceDiscovery(this);
}

override fun onDiscoveryStarted(regType: String?) {
Log.d(TAG, "Service discovery started");
started = true
}

override fun onDiscoveryStopped(serviceType: String?) {
Log.i(TAG, "Discovery stopped: " + serviceType);
started = false
}

override fun onServiceLost(serviceInfo: NsdServiceInfo?) {
Log.e(TAG, "service lost: " + serviceInfo);
}
}
}

fun discoverServices() {

if (!started) {
manager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener)
}
}

fun tearDown() {
if (started) {
// manager.unregisterService(this)
manager.stopServiceDiscovery(discoveryListener)
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/values/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<item>歌呢</item>
<item>我今天真是</item>
</string-array>
<string name="default_pi_host">192.168.137.52:8080</string>
<string name="default_pi_hostname">ras55</string>
<string name="default_pi_host">192.168.137.52</string>
<string name="default_pi_port">8080</string>
<string name="default_pi_path">/ame</string>
</resources>

0 comments on commit b5141a0

Please sign in to comment.