Skip to content
This repository was archived by the owner on Oct 7, 2023. It is now read-only.

Commit f2121b5

Browse files
committed
MainActivity: Monitor network changes & Account card
Signed-off-by: Fung <[email protected]>
1 parent cc533ca commit f2121b5

File tree

6 files changed

+193
-50
lines changed

6 files changed

+193
-50
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ dependencies {
3131
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3232
compile 'org.jetbrains.anko:anko-sdk21:0.9.1'
3333

34+
compile "com.android.support:cardview-v7:$support_lib_version"
35+
3436
compile 'com.squareup.okhttp3:okhttp:3.6.0'
3537
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
3638

app/src/main/kotlin/moe/feng/scut/autowifi/MainActivity.kt

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package moe.feng.scut.autowifi
22

33
import android.app.Activity
4+
import android.content.BroadcastReceiver
5+
import android.content.Context
6+
import android.content.Intent
7+
import android.content.IntentFilter
8+
import android.net.ConnectivityManager
49
import android.os.Bundle
10+
import android.widget.EditText
11+
import android.widget.TextView
512

613
import moe.feng.material.statusbar.StatusBarCompat
714
import moe.feng.scut.autowifi.api.DormitoryApi
@@ -12,6 +19,9 @@ import org.jetbrains.anko.*
1219
class MainActivity : Activity(), AnkoLogger {
1320

1421
val fab by lazy { find<FloatingActionButton>(R.id.fab) }
22+
val statusText by lazy { find<TextView>(R.id.status_text) }
23+
val userName by lazy { find<EditText>(R.id.user_name) }
24+
val userPwd by lazy { find<EditText>(R.id.user_pwd) }
1525

1626
override fun onCreate(savedInstanceState: Bundle?) {
1727
StatusBarCompat.setUpActivity(this)
@@ -37,13 +47,32 @@ class MainActivity : Activity(), AnkoLogger {
3747
}
3848
doConnect()
3949
}
50+
51+
/** Check manually after launching */
52+
networkStateReceiver.checkManually(this)
53+
}
54+
55+
override fun onResume() {
56+
val filter = IntentFilter()
57+
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
58+
registerReceiver(networkStateReceiver, filter)
59+
super.onResume()
60+
}
61+
62+
override fun onPause() {
63+
unregisterReceiver(networkStateReceiver)
64+
super.onPause()
65+
}
66+
67+
private fun setStatusText(any : Any) {
68+
statusText.text = getString(R.string.status_text_format, any)
4069
}
4170

4271
private fun doConnect() {
4372
doAsync {
4473
val result = DormitoryApi
4574
.setCurrentIp(WifiUtils.getCurrentIP(this@MainActivity))
46-
.connect(username = "", password = "")
75+
.connect(username = userName.text.toString(), password = userPwd.text.toString())
4776
val errCode = DormitoryApi.checkError()
4877
uiThread {
4978
val dialog = AlertDialogBuilder(this@MainActivity)
@@ -55,4 +84,45 @@ class MainActivity : Activity(), AnkoLogger {
5584
}
5685
}
5786

87+
private class NetworkStateReceiver(
88+
val disconnected: () -> Unit, val connectedMobile: () -> Unit, val connectedWifi: (Boolean) -> Unit
89+
) : BroadcastReceiver() {
90+
91+
override fun onReceive(context: Context?, intent: Intent?) {
92+
context?.let { checkManually(it) }
93+
}
94+
95+
fun checkManually(context : Context) {
96+
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
97+
98+
val wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
99+
val dataNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
100+
101+
if (!dataNetworkInfo.isConnected && !wifiNetworkInfo.isConnected) {
102+
disconnected.invoke()
103+
} else if (dataNetworkInfo.isConnected && !wifiNetworkInfo.isConnected) {
104+
connectedMobile.invoke()
105+
} else if (!dataNetworkInfo.isConnected && wifiNetworkInfo.isConnected) {
106+
connectedWifi.invoke(WifiUtils.isSCUTSSID(context))
107+
}
108+
}
109+
110+
}
111+
112+
private val networkStateReceiver = NetworkStateReceiver(
113+
disconnected = {
114+
setStatusText(getString(R.string.status_text_disconnected))
115+
},
116+
connectedMobile = {
117+
setStatusText(getString(R.string.status_text_mobile_connected))
118+
},
119+
connectedWifi = {
120+
if (it) {
121+
setStatusText(getString(R.string.status_text_wifi_connected_scut))
122+
} else {
123+
setStatusText(getString(R.string.status_text_wifi_connected_other))
124+
}
125+
}
126+
)
127+
58128
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FFFFFFFF"
8+
android:pathData="M18.99,11.5c0.34,0 0.67,0.03 1,0.07L20,0 0,20h11.56c-0.04,-0.33 -0.07,-0.66 -0.07,-1 0,-4.14 3.36,-7.5 7.5,-7.5zM22.7,19.49c0.02,-0.16 0.04,-0.32 0.04,-0.49 0,-0.17 -0.01,-0.33 -0.04,-0.49l1.06,-0.83c0.09,-0.08 0.12,-0.21 0.06,-0.32l-1,-1.73c-0.06,-0.11 -0.19,-0.15 -0.31,-0.11l-1.24,0.5c-0.26,-0.2 -0.54,-0.37 -0.85,-0.49l-0.19,-1.32c-0.01,-0.12 -0.12,-0.21 -0.24,-0.21h-2c-0.12,0 -0.23,0.09 -0.25,0.21l-0.19,1.32c-0.3,0.13 -0.59,0.29 -0.85,0.49l-1.24,-0.5c-0.11,-0.04 -0.24,0 -0.31,0.11l-1,1.73c-0.06,0.11 -0.04,0.24 0.06,0.32l1.06,0.83c-0.02,0.16 -0.03,0.32 -0.03,0.49 0,0.17 0.01,0.33 0.03,0.49l-1.06,0.83c-0.09,0.08 -0.12,0.21 -0.06,0.32l1,1.73c0.06,0.11 0.19,0.15 0.31,0.11l1.24,-0.5c0.26,0.2 0.54,0.37 0.85,0.49l0.19,1.32c0.02,0.12 0.12,0.21 0.25,0.21h2c0.12,0 0.23,-0.09 0.25,-0.21l0.19,-1.32c0.3,-0.13 0.59,-0.29 0.84,-0.49l1.25,0.5c0.11,0.04 0.24,0 0.31,-0.11l1,-1.73c0.06,-0.11 0.03,-0.24 -0.06,-0.32l-1.07,-0.83zM18.99,20.5c-0.83,0 -1.5,-0.67 -1.5,-1.5s0.67,-1.5 1.5,-1.5 1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
9+
</vector>
Lines changed: 100 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,130 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
2+
<ScrollView
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools"
5-
xmlns:app="http://schemas.android.com/apk/res-auto"
65
android:layout_width="match_parent"
76
android:layout_height="match_parent"
8-
android:orientation="vertical"
7+
android:overScrollMode="never"
98
tools:context="moe.feng.scut.autowifi.MainActivity">
109

11-
<FrameLayout
12-
android:id="@+id/app_bar_container"
10+
<LinearLayout
1311
android:layout_width="match_parent"
14-
android:layout_height="wrap_content">
12+
android:layout_height="wrap_content"
13+
android:orientation="vertical">
1514

16-
<LinearLayout
17-
android:id="@+id/app_bar"
15+
<FrameLayout
16+
android:id="@+id/app_bar_container"
1817
android:layout_width="match_parent"
19-
android:layout_height="wrap_content"
20-
android:orientation="vertical">
21-
22-
<ImageView
23-
android:id="@+id/header_image"
24-
android:layout_width="match_parent"
25-
android:layout_height="240dp"
26-
android:scaleType="centerCrop"
27-
android:src="@drawable/header_background"/>
18+
android:layout_height="wrap_content">
2819

2920
<LinearLayout
21+
android:id="@+id/app_bar"
3022
android:layout_width="match_parent"
3123
android:layout_height="wrap_content"
32-
android:background="@color/teal_800"
33-
android:paddingTop="16dp"
34-
android:paddingBottom="16dp"
35-
android:paddingStart="24dp"
36-
android:paddingEnd="24dp">
24+
android:orientation="vertical">
3725

38-
<TextView
39-
android:layout_width="wrap_content"
26+
<ImageView
27+
android:id="@+id/header_image"
28+
android:layout_width="match_parent"
29+
android:layout_height="240dp"
30+
android:scaleType="centerCrop"
31+
android:src="@drawable/header_background"/>
32+
33+
<LinearLayout
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:background="@color/teal_800"
37+
android:paddingTop="16dp"
38+
android:paddingBottom="16dp"
39+
android:paddingStart="24dp"
40+
android:paddingEnd="24dp">
41+
42+
<TextView
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:text="@string/app_name"
46+
android:textAppearance="@android:style/TextAppearance.Material.Display1"
47+
android:textColor="@android:color/primary_text_dark"/>
48+
49+
</LinearLayout>
50+
51+
<LinearLayout
52+
android:layout_width="match_parent"
4053
android:layout_height="wrap_content"
41-
android:text="@string/app_name"
42-
android:textAppearance="@android:style/TextAppearance.Material.Display1"
43-
android:textColor="@android:color/primary_text_dark"/>
54+
android:background="@color/teal_700"
55+
android:paddingTop="24dp"
56+
android:paddingBottom="24dp"
57+
android:paddingStart="24dp"
58+
android:paddingEnd="24dp">
59+
60+
<TextView
61+
android:id="@+id/status_text"
62+
android:layout_width="wrap_content"
63+
android:layout_height="wrap_content"
64+
android:drawableStart="@drawable/ic_perm_data_setting_white_24dp"
65+
android:drawablePadding="16dp"
66+
android:text="@string/status_text_format"
67+
android:textAppearance="@android:style/TextAppearance.Material.Medium"
68+
android:textColor="@android:color/primary_text_dark"/>
69+
70+
</LinearLayout>
4471

4572
</LinearLayout>
4673

74+
<moe.feng.scut.autowifi.view.FloatingActionButton
75+
android:id="@+id/fab"
76+
android:layout_width="wrap_content"
77+
android:layout_height="wrap_content"
78+
android:layout_marginTop="216dp"
79+
android:layout_marginEnd="16dp"
80+
android:layout_gravity="top|end"
81+
android:src="@drawable/ic_settings_input_component_black_24dp"
82+
android:tint="@android:color/white"/>
83+
84+
</FrameLayout>
85+
86+
<!-- This card is for test. Account should be set up in settings activity. -->
87+
<android.support.v7.widget.CardView
88+
android:id="@+id/account_card"
89+
android:layout_width="match_parent"
90+
android:layout_height="wrap_content"
91+
android:layout_marginTop="16dp"
92+
android:layout_marginStart="16dp"
93+
android:layout_marginEnd="16dp"
94+
android:layout_marginBottom="16dp"
95+
android:focusable="true"
96+
android:focusableInTouchMode="true">
97+
4798
<LinearLayout
4899
android:layout_width="match_parent"
49100
android:layout_height="wrap_content"
50-
android:background="@color/teal_700"
51-
android:paddingTop="24dp"
52-
android:paddingBottom="24dp"
53-
android:paddingStart="24dp"
54-
android:paddingEnd="24dp">
101+
android:orientation="vertical"
102+
android:padding="16dp">
55103

56-
<TextView
104+
<EditText
105+
android:id="@+id/user_name"
106+
android:layout_width="match_parent"
107+
android:layout_height="wrap_content"
108+
android:hint="User Name"
109+
android:inputType="number"
110+
android:typeface="monospace"/>
111+
112+
<Space
57113
android:layout_width="wrap_content"
114+
android:layout_height="16dp"/>
115+
116+
<EditText
117+
android:id="@+id/user_pwd"
118+
android:layout_width="match_parent"
58119
android:layout_height="wrap_content"
59-
android:text="Status: Connected"
60-
android:textAppearance="@android:style/TextAppearance.Material.Medium"
61-
android:textColor="@android:color/primary_text_dark"/>
120+
android:hint="Password"
121+
android:inputType="textPassword"
122+
android:typeface="monospace"/>
62123

63124
</LinearLayout>
64125

65-
</LinearLayout>
66-
67-
<moe.feng.scut.autowifi.view.FloatingActionButton
68-
android:id="@+id/fab"
69-
android:layout_width="wrap_content"
70-
android:layout_height="wrap_content"
71-
android:layout_marginTop="216dp"
72-
android:layout_marginEnd="16dp"
73-
android:layout_gravity="top|end"
74-
android:src="@drawable/ic_settings_input_component_black_24dp"
75-
android:tint="@android:color/white"/>
126+
</android.support.v7.widget.CardView>
76127

77-
</FrameLayout>
128+
</LinearLayout>
78129

79-
</LinearLayout>
130+
</ScrollView>

app/src/main/res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
<string name="app_name">SCUTAutoWiFi</string>
44

5+
<string name="status_text_format">Status: %s</string>
6+
<string name="status_text_mobile_connected">Using mobile network.</string>
7+
<string name="status_text_disconnected">Disconnected.</string>
8+
<string name="status_text_wifi_connected_other">Connected (Other: %s).</string>
9+
<string name="status_text_wifi_connecting">Connecting&#8230;</string>
10+
<string name="status_text_wifi_connected_scut">Connected (SCUT-Student). Need login</string>
11+
<string name="status_text_wifi_connected_scut_logon">Connected (SCUT-Student).</string>
12+
513
<string name="dialog_connected_wifi_but_not_scut">Switch to SCUT WiFi</string>
614
<string name="dialog_connected_wifi_but_not_scut_msg">You\'re using other wireless network. This application doesn\'t
715
support auto-login. Do you want to switch to SCUT wireless network?</string>

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
buildscript {
2+
23
ext.kotlin_version = '1.1.1'
4+
ext.support_lib_version = '25.3.0'
5+
36
repositories {
47
jcenter()
58
}

0 commit comments

Comments
 (0)