Skip to content

Commit 32664bb

Browse files
author
Piotr Wittchen
committed
adding sample app written in Kotlin
1 parent 174230b commit 32664bb

File tree

19 files changed

+259
-13
lines changed

19 files changed

+259
-13
lines changed

app-kotlin/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app-kotlin/build.gradle

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
4+
android {
5+
compileSdkVersion 23
6+
buildToolsVersion "23.0.1"
7+
8+
defaultConfig {
9+
applicationId "com.github.pwittchen.reactivenetwork.kotlinapp"
10+
minSdkVersion 15
11+
targetSdkVersion 23
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
sourceSets {
24+
main.java.srcDirs += 'src/main/kotlin'
25+
}
26+
}
27+
28+
dependencies {
29+
compile project(':library')
30+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
31+
compile 'com.android.support:appcompat-v7:23.1.0'
32+
}
33+
34+
repositories {
35+
mavenCentral()
36+
}
37+
38+
buildscript {
39+
ext.kotlin_version = '1.0.0-beta-1103'
40+
41+
repositories {
42+
mavenCentral()
43+
}
44+
45+
dependencies {
46+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
47+
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
48+
}
49+
}

app-kotlin/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/piotr/Development/android/android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.github.pwittchen.reactivenetwork.kotlinapp"
4+
>
5+
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme"
12+
>
13+
<activity android:name="com.github.pwittchen.reactivenetwork.kotlinapp.MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.github.pwittchen.reactivenetwork.kotlinapp
2+
3+
import android.net.wifi.ScanResult
4+
import android.os.Bundle
5+
import android.support.v7.app.AppCompatActivity
6+
import android.util.Log
7+
import android.widget.ArrayAdapter
8+
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork
9+
import kotlinx.android.synthetic.activity_main.access_points
10+
import kotlinx.android.synthetic.activity_main.connectivity_status
11+
import rx.Subscription
12+
import rx.android.schedulers.AndroidSchedulers
13+
import rx.schedulers.Schedulers
14+
import java.util.*
15+
16+
class MainActivity : AppCompatActivity() {
17+
private var wifiSubscription: Subscription? = null
18+
private var connectivitySubscription: Subscription? = null
19+
20+
companion object {
21+
private val TAG = "ReactiveNetwork"
22+
}
23+
24+
override fun onCreate(savedInstanceState: Bundle?) {
25+
super.onCreate(savedInstanceState)
26+
setContentView(R.layout.activity_main)
27+
}
28+
29+
override fun onResume() {
30+
super.onResume()
31+
val reactiveNetwork: ReactiveNetwork = ReactiveNetwork()
32+
33+
connectivitySubscription = reactiveNetwork.observeConnectivity(this)
34+
.subscribeOn(Schedulers.io())
35+
.observeOn(AndroidSchedulers.mainThread())
36+
.subscribe { connectivityStatus ->
37+
connectivity_status.text = connectivityStatus.toString();
38+
Log.d(TAG, connectivityStatus.toString())
39+
}
40+
41+
wifiSubscription = reactiveNetwork.observeWifiAccessPoints(this)
42+
.subscribeOn(Schedulers.io())
43+
.observeOn(AndroidSchedulers.mainThread())
44+
.subscribe { scanResults -> displayAccessPoints(scanResults) }
45+
}
46+
47+
private fun displayAccessPoints(scanResults: List<ScanResult>) {
48+
val ssids = ArrayList<String>()
49+
50+
for (scanResult in scanResults) {
51+
ssids.add(scanResult.SSID)
52+
}
53+
54+
access_points.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ssids)
55+
}
56+
57+
override fun onPause() {
58+
super.onPause()
59+
safelyUnsubscribe(connectivitySubscription)
60+
safelyUnsubscribe(wifiSubscription)
61+
}
62+
63+
private fun safelyUnsubscribe(subscription: Subscription?) {
64+
if (subscription != null && !subscription.isUnsubscribed) {
65+
subscription.unsubscribe()
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingLeft="@dimen/activity_horizontal_margin"
6+
android:paddingRight="@dimen/activity_horizontal_margin"
7+
android:paddingTop="@dimen/activity_vertical_margin"
8+
android:paddingBottom="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity"
10+
>
11+
12+
<TextView
13+
android:id="@+id/connectivity_status_label"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:paddingBottom="@dimen/text_view_padding"
17+
android:paddingTop="@dimen/text_view_padding"
18+
android:text="@string/connectivity_status"
19+
android:textAppearance="@android:style/TextAppearance.Medium"
20+
android:textStyle="bold"
21+
/>
22+
23+
<TextView
24+
android:id="@+id/connectivity_status"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_below="@+id/connectivity_status_label"
28+
android:paddingBottom="@dimen/text_view_padding"
29+
android:paddingTop="@dimen/text_view_padding"
30+
android:text="@string/waiting_for_connectivity_status"
31+
android:textAppearance="@android:style/TextAppearance.Medium"
32+
/>
33+
34+
35+
<TextView
36+
android:id="@+id/access_points_label"
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:layout_below="@id/connectivity_status"
40+
android:paddingBottom="@dimen/text_view_padding"
41+
android:paddingTop="@dimen/text_view_padding"
42+
android:text="@string/available_access_points"
43+
android:textAppearance="@android:style/TextAppearance.Medium"
44+
android:textStyle="bold"
45+
/>
46+
47+
<ListView
48+
android:id="@+id/access_points"
49+
android:layout_width="match_parent"
50+
android:layout_height="wrap_content"
51+
android:layout_below="@+id/access_points_label"
52+
/>
53+
54+
</RelativeLayout>
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<dimen name="activity_horizontal_margin">16dp</dimen>
3+
<dimen name="activity_vertical_margin">16dp</dimen>
4+
<dimen name="text_view_padding">10dp</dimen>
5+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
<string name="app_name">ReactiveNetwork</string>
3+
<string name="waiting_for_connectivity_status">Waiting for connectivity status&#8230;</string>
4+
<string name="connectivity_status">Connectivity status:</string>
5+
<string name="available_access_points">Available Access Points:</string>
6+
<string name="waiting_for_signal_strength_change">Waiting for signal strength change&#8230;</string>
7+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

build.gradle

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
repositories {
5-
jcenter()
6-
}
7-
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.3.1'
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
99

10-
// NOTE: Do not place your application dependencies here; they belong
11-
// in the individual module build.gradle files
12-
}
10+
// NOTE: Do not place your application dependencies here; they belong
11+
// in the individual module build.gradle files
12+
}
1313
}
1414

1515
allprojects {
16-
repositories {
17-
jcenter()
18-
}
16+
repositories {
17+
jcenter()
18+
}
1919
}

gradle.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ POM_LICENCE_NAME=The Apache Software License, Version 2.0
1111
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
1212
POM_LICENCE_DIST=repo
1313
POM_DEVELOPER_ID=pwittchen
14-
POM_DEVELOPER_NAME=Piotr Wittchen
14+
POM_DEVELOPER_NAME=Piotr Wittchen
15+
16+
org.gradle.daemon=true
17+
org.gradle.jvmargs=-XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Xmx2048m

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app', ':library'
1+
include ':app', ':library', ':app-kotlin'

0 commit comments

Comments
 (0)