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

Commit 1c91990

Browse files
committed
SettingsActivity: Finished
Signed-off-by: Fung <[email protected]>
1 parent f2121b5 commit 1c91990

File tree

10 files changed

+214
-50
lines changed

10 files changed

+214
-50
lines changed

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/build
2+
app-release.apk

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ dependencies {
3232
compile 'org.jetbrains.anko:anko-sdk21:0.9.1'
3333

3434
compile "com.android.support:cardview-v7:$support_lib_version"
35+
compile "com.android.support:customtabs:$support_lib_version"
3536

3637
compile 'com.squareup.okhttp3:okhttp:3.6.0'
3738
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
39+
compile 'com.orhanobut:hawk:2.0.1'
3840

41+
compile 'moe.feng:AlipayZeroSdk:1.1'
3942
compile project(':libraries:StatusBarCompat')
4043
}
4144

app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@
99
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
1010

1111
<application
12-
android:allowBackup="true"
1312
android:name=".Application"
13+
android:allowBackup="true"
1414
android:icon="@mipmap/ic_launcher"
1515
android:label="@string/app_name"
1616
android:supportsRtl="true"
1717
android:theme="@style/BaseAppTheme">
18-
19-
<activity android:name=".MainActivity"
20-
android:theme="@style/AppTheme.Translucent">
18+
<activity
19+
android:name=".MainActivity"
20+
android:theme="@style/AppTheme.Translucent">
2121
<intent-filter>
2222
<action android:name="android.intent.action.MAIN"/>
2323

2424
<category android:name="android.intent.category.LAUNCHER"/>
2525
</intent-filter>
2626
</activity>
27+
28+
<activity android:name=".SettingsActivity"
29+
android:label="@string/title_activity_settings">
30+
<intent-filter>
31+
<action android:name="android.intent.action.MAIN"/>
32+
</intent-filter>
33+
</activity>
2734
</application>
2835

2936
</manifest>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package moe.feng.scut.autowifi
22

3+
import com.orhanobut.hawk.Hawk
34
import moe.feng.scut.autowifi.support.HttpUtils
45

56
class Application : android.app.Application() {
67

78
override fun onCreate() {
89
super.onCreate()
910
HttpUtils.init(this)
11+
Hawk.init(this).build()
1012
}
1113

1214
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import android.content.Intent
77
import android.content.IntentFilter
88
import android.net.ConnectivityManager
99
import android.os.Bundle
10-
import android.widget.EditText
10+
import android.text.TextUtils
11+
import android.widget.ImageButton
1112
import android.widget.TextView
13+
import com.orhanobut.hawk.Hawk
1214

1315
import moe.feng.material.statusbar.StatusBarCompat
1416
import moe.feng.scut.autowifi.api.DormitoryApi
@@ -18,17 +20,17 @@ import org.jetbrains.anko.*
1820

1921
class MainActivity : Activity(), AnkoLogger {
2022

23+
val settingsButton by lazy { find<ImageButton>(R.id.btn_settings) }
2124
val fab by lazy { find<FloatingActionButton>(R.id.fab) }
2225
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) }
2526

2627
override fun onCreate(savedInstanceState: Bundle?) {
2728
StatusBarCompat.setUpActivity(this)
2829

2930
super.onCreate(savedInstanceState)
3031
setContentView(R.layout.activity_main2)
3132

33+
settingsButton.onClick { startActivity<SettingsActivity>() }
3234
fab.onClick {
3335
if (WifiUtils.isWifiConnected(this) && !WifiUtils.isSCUTSSID(this)) {
3436
val dialog = AlertDialogBuilder(this)
@@ -69,10 +71,18 @@ class MainActivity : Activity(), AnkoLogger {
6971
}
7072

7173
private fun doConnect() {
74+
if (TextUtils.isEmpty(Hawk.get("username", "")) || TextUtils.isEmpty(Hawk.get("password", ""))) {
75+
val dialog = AlertDialogBuilder(this)
76+
dialog.title(R.string.dialog_no_account)
77+
dialog.message(R.string.dialog_no_account_msg)
78+
dialog.okButton { startActivity<SettingsActivity>() }
79+
dialog.show()
80+
return
81+
}
7282
doAsync {
7383
val result = DormitoryApi
7484
.setCurrentIp(WifiUtils.getCurrentIP(this@MainActivity))
75-
.connect(username = userName.text.toString(), password = userPwd.text.toString())
85+
.connect(username = Hawk.get("username"), password = Hawk.get("password"))
7686
val errCode = DormitoryApi.checkError()
7787
uiThread {
7888
val dialog = AlertDialogBuilder(this@MainActivity)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package moe.feng.scut.autowifi
2+
3+
import android.net.Uri
4+
import android.os.Bundle
5+
import android.preference.EditTextPreference
6+
import android.preference.Preference
7+
import android.preference.PreferenceActivity
8+
import android.view.MenuItem
9+
import com.orhanobut.hawk.Hawk
10+
import android.support.customtabs.CustomTabsIntent
11+
import android.content.pm.PackageManager
12+
import org.jetbrains.anko.email
13+
14+
15+
class SettingsActivity : PreferenceActivity(), Preference.OnPreferenceChangeListener {
16+
17+
lateinit var prefUsername : EditTextPreference
18+
lateinit var prefPassword : EditTextPreference
19+
lateinit var prefVersion : Preference
20+
lateinit var prefGithub : Preference
21+
lateinit var prefTelegram : Preference
22+
lateinit var prefEmail : Preference
23+
24+
override fun onCreate(savedInstanceState: Bundle?) {
25+
super.onCreate(savedInstanceState)
26+
addPreferencesFromResource(R.xml.pref_settings)
27+
28+
actionBar.setDisplayHomeAsUpEnabled(true)
29+
30+
prefUsername = findPreference("username") as EditTextPreference
31+
prefPassword = findPreference("password") as EditTextPreference
32+
prefVersion = findPreference("version")
33+
prefGithub = findPreference("github")
34+
prefTelegram = findPreference("telegram")
35+
prefEmail = findPreference("email")
36+
37+
prefUsername.text = Hawk.get("username", "")
38+
prefPassword.text = Hawk.get("password", "")
39+
if (!prefUsername.text.isNullOrEmpty()) prefUsername.summary = prefUsername.text
40+
if (!prefPassword.text.isNullOrEmpty()) prefPassword.summary = prefPassword.text
41+
42+
var versionName: String? = null
43+
var versionCode = 0
44+
try {
45+
versionName = packageManager.getPackageInfo(packageName, 0).versionName
46+
versionCode = packageManager.getPackageInfo(packageName, 0).versionCode
47+
} catch (e: PackageManager.NameNotFoundException) {
48+
e.printStackTrace()
49+
}
50+
prefVersion.summary = "$versionName ($versionCode)"
51+
52+
prefUsername.onPreferenceChangeListener = this
53+
prefPassword.onPreferenceChangeListener = this
54+
prefGithub.setOnPreferenceClickListener { openWebsite(getString(R.string.pref_about_github_url)); true }
55+
prefTelegram.setOnPreferenceClickListener { openWebsite(getString(R.string.pref_about_author_telegram_url)); true }
56+
prefEmail.setOnPreferenceClickListener { email(getString(R.string.pref_about_author_email_url), "", ""); true }
57+
}
58+
59+
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
60+
when (preference) {
61+
prefUsername -> {
62+
Hawk.put("username", newValue as String)
63+
preference.summary =
64+
if (newValue.isNullOrEmpty()) getString(R.string.pref_account_user_name_hint) else newValue
65+
}
66+
prefPassword -> {
67+
Hawk.put("password", newValue as String)
68+
preference.summary =
69+
if (newValue.isNullOrEmpty()) getString(R.string.pref_account_user_pwd_hint) else newValue
70+
}
71+
}
72+
return !(newValue is Boolean && !newValue)
73+
}
74+
75+
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
76+
if (item?.itemId == android.R.id.home) {
77+
onBackPressed()
78+
return true
79+
}
80+
return super.onOptionsItemSelected(item)
81+
}
82+
83+
private fun openWebsite(url : String) {
84+
val builder = CustomTabsIntent.Builder()
85+
builder.setToolbarColor(resources.getColor(R.color.teal_500))
86+
builder.build().launchUrl(this, Uri.parse(url))
87+
}
88+
89+
}
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="#FF000000"
8+
android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>
9+
</vector>

app/src/main/res/layout/activity_main2.xml

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@
7171

7272
</LinearLayout>
7373

74+
<ImageButton
75+
android:id="@+id/btn_settings"
76+
android:layout_width="wrap_content"
77+
android:layout_height="wrap_content"
78+
android:src="@drawable/ic_settings_black_24dp"
79+
android:tint="@android:color/white"
80+
android:alpha="0.8"
81+
android:background="?android:attr/selectableItemBackgroundBorderless"
82+
android:padding="16dp"
83+
android:layout_marginTop="24dp"
84+
android:layout_gravity="end"/>
85+
7486
<moe.feng.scut.autowifi.view.FloatingActionButton
7587
android:id="@+id/fab"
7688
android:layout_width="wrap_content"
@@ -83,48 +95,6 @@
8395

8496
</FrameLayout>
8597

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-
98-
<LinearLayout
99-
android:layout_width="match_parent"
100-
android:layout_height="wrap_content"
101-
android:orientation="vertical"
102-
android:padding="16dp">
103-
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
113-
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"
119-
android:layout_height="wrap_content"
120-
android:hint="Password"
121-
android:inputType="textPassword"
122-
android:typeface="monospace"/>
123-
124-
</LinearLayout>
125-
126-
</android.support.v7.widget.CardView>
127-
12898
</LinearLayout>
12999

130100
</ScrollView>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,30 @@
1010
<string name="status_text_wifi_connected_scut">Connected (SCUT-Student). Need login</string>
1111
<string name="status_text_wifi_connected_scut_logon">Connected (SCUT-Student).</string>
1212

13+
<string name="dialog_no_account">No Account</string>
14+
<string name="dialog_no_account_msg">Please go to settings and set up your account.</string>
15+
1316
<string name="dialog_connected_wifi_but_not_scut">Switch to SCUT WiFi</string>
1417
<string name="dialog_connected_wifi_but_not_scut_msg">You\'re using other wireless network. This application doesn\'t
1518
support auto-login. Do you want to switch to SCUT wireless network?</string>
1619

20+
<!-- Settings -->
21+
<string name="title_activity_settings">Settings</string>
22+
23+
<string name="pref_account_header">SCUT Network Account</string>
24+
<string name="pref_account_user_name">Username</string>
25+
<string name="pref_account_user_name_hint">Mostly use Student ID as username</string>
26+
<string name="pref_account_user_pwd">Password</string>
27+
<string name="pref_account_user_pwd_hint">Enter your password</string>
28+
29+
<string name="pref_about_header">About</string>
30+
<string name="pref_about_github">GitHub Repository</string>
31+
<string name="pref_about_github_url">https://github.com/fython/SCUTAutoWiFi-Android</string>
32+
<string name="pref_about_author_telegram">Author Telegram</string>
33+
<string name="pref_about_author_telegram_url">https://t.me/fython</string>
34+
<string name="pref_about_author_email">Contact via Email</string>
35+
<string name="pref_about_author_email_url">fythonx\@gmail.com</string>
36+
<string name="pref_about_alipay_donate">Donate me</string>
37+
<string name="pref_about_alipay_donate_url">316643843\@qq.com</string>
38+
1739
</resources>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<PreferenceCategory android:title="@string/pref_account_header">
5+
6+
<EditTextPreference
7+
android:key="username"
8+
android:title="@string/pref_account_user_name"
9+
android:selectAllOnFocus="true"
10+
android:inputType="number"
11+
android:singleLine="true"
12+
android:hint="@string/pref_account_user_name_hint"
13+
android:summary="@string/pref_account_user_name_hint"
14+
android:typeface="monospace"
15+
android:maxLines="1"/>
16+
17+
<EditTextPreference
18+
android:key="password"
19+
android:title="@string/pref_account_user_pwd"
20+
android:selectAllOnFocus="true"
21+
android:inputType="textPassword"
22+
android:singleLine="true"
23+
android:hint="@string/pref_account_user_pwd_hint"
24+
android:summary="@string/pref_account_user_pwd_hint"
25+
android:typeface="monospace"
26+
android:maxLines="1"/>
27+
28+
</PreferenceCategory>
29+
30+
<PreferenceCategory android:title="@string/pref_about_header">
31+
32+
<Preference android:key="version" android:title="@string/app_name"/>
33+
34+
<Preference
35+
android:key="github"
36+
android:title="@string/pref_about_github"
37+
android:summary="@string/pref_about_github_url"/>
38+
39+
<Preference
40+
android:key="telegram"
41+
android:title="@string/pref_about_author_telegram"
42+
android:summary="@string/pref_about_author_telegram_url"/>
43+
44+
<Preference
45+
android:key="email"
46+
android:title="@string/pref_about_author_email"
47+
android:summary="@string/pref_about_author_email_url"/>
48+
49+
</PreferenceCategory>
50+
51+
</PreferenceScreen>

0 commit comments

Comments
 (0)