Skip to content

Commit c4472e4

Browse files
committed
Add dark theme support
Using the system theme is semi-supported, but not stable or complete. That's why the "System" option is commented out in the array, but it's checked for in the code.
1 parent b1df07a commit c4472e4

14 files changed

+116
-32
lines changed

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ android {
2424
dependencies {
2525
implementation fileTree(dir: 'libs', include: ['*.jar'])
2626
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
27+
implementation 'com.android.support:appcompat-v7:27.1.0'
2728
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
2829
implementation 'com.github.kenglxn.QRGen:android:2.4.0'
2930
}

app/src/main/java/tk/superl2/xwifi/MainActivity.kt

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
package tk.superl2.xwifi
22

3-
import android.app.Activity
43
import android.app.AlertDialog
54
import android.content.Intent
65
import android.content.SharedPreferences
76
import android.os.AsyncTask
87
import android.os.Build
98
import android.os.Bundle
109
import android.preference.PreferenceManager
10+
import android.support.v7.app.AppCompatActivity
11+
import android.support.v7.app.AppCompatDelegate
1112
import android.text.Html
1213
import android.util.Log
1314
import android.view.Menu
1415
import android.view.MenuItem
1516
import android.widget.ArrayAdapter
1617
import android.widget.ImageView
1718
import android.widget.ProgressBar
19+
import android.widget.Toast
1820
import kotlinx.android.synthetic.main.activity_main.*
1921
import net.glxn.qrgen.android.QRCode
2022
import net.glxn.qrgen.core.scheme.Wifi
2123

2224
private const val TAG = "MainActivity"
2325
private const val DEFAULT_QR_GENERATION_RESOLUTION = "300"
2426

25-
class MainActivity : Activity() {
27+
class MainActivity: AppCompatActivity() {
2628
// This variable holds an ArrayList of WifiEntry objects that each contain a saved wifi SSID and
2729
// password. It is updated whenever focus returns to the app (onResume).
2830
private lateinit var wifiEntries: ArrayList<WifiEntry>
@@ -32,12 +34,13 @@ class MainActivity : Activity() {
3234

3335
private lateinit var qrDialog: AlertDialog
3436
override fun onCreate(savedInstanceState: Bundle?) {
35-
super.onCreate(savedInstanceState)
36-
setContentView(R.layout.activity_main)
37-
3837
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
3938
prefs = PreferenceManager.getDefaultSharedPreferences(this)
4039

40+
setThemeFromSharedPrefs(prefs)
41+
super.onCreate(savedInstanceState)
42+
setContentView(R.layout.activity_main)
43+
4144
wifi_ListView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, wifiEntrySSIDs)
4245
wifi_ListView.setOnItemClickListener { _, _, position, _ ->
4346
val qrCodeView = ImageView(this)
@@ -48,6 +51,7 @@ class MainActivity : Activity() {
4851
.withSsid(wifiEntrySSIDs[position])
4952
.withPsk(wifiEntries[position].getPassword(true))
5053
.withAuthentication(wifiEntries[position].type.asQRCodeAuth()))
54+
.withColor((if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO) 0xFF000000 else 0xFFE0E0E0).toInt(), 0x00000000) //TODO Better colour handling - atm, the colours may be wrong if the theme is set to system or auto.
5155
.withSize(prefs.getString("qr_code_resolution", DEFAULT_QR_GENERATION_RESOLUTION).toInt(), prefs.getString("qr_code_resolution", DEFAULT_QR_GENERATION_RESOLUTION).toInt())
5256
.bitmap())
5357

@@ -81,6 +85,11 @@ class MainActivity : Activity() {
8185
}
8286
}
8387

88+
override fun onRestart() {
89+
super.onRestart()
90+
recreate()
91+
}
92+
8493
override fun onResume() {
8594
super.onResume()
8695
loadWifiEntriesInBackgroundTask = LoadWifiEntriesInBackground()

app/src/main/java/tk/superl2/xwifi/SettingsActivity.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package tk.superl2.xwifi
22

3+
import android.content.SharedPreferences
4+
import android.os.Build
35
import android.os.Bundle
6+
import android.preference.Preference
47
import android.preference.PreferenceActivity
58
import android.preference.PreferenceFragment
9+
import android.support.v7.app.AppCompatActivity
10+
import android.support.v7.app.AppCompatDelegate
11+
import android.widget.Toast
612

713

814
/**
@@ -15,7 +21,7 @@ import android.preference.PreferenceFragment
1521
* for design guidelines and the [Settings API Guide](http://developer.android.com/guide/topics/ui/settings.html)
1622
* for more information on developing a Settings UI.
1723
*/
18-
class SettingsActivity : PreferenceActivity() {
24+
class SettingsActivity: AppCompatActivity() {
1925
override fun onCreate(savedInstanceState: Bundle?) {
2026
super.onCreate(savedInstanceState)
2127

@@ -24,11 +30,18 @@ class SettingsActivity : PreferenceActivity() {
2430
}
2531
}
2632

27-
class SettingsFragment : PreferenceFragment() {
33+
class SettingsFragment: PreferenceFragment() {
2834
override fun onCreate(savedInstanceState: Bundle?) {
2935
super.onCreate(savedInstanceState)
3036

3137
// Load the preferences from an XML resource
3238
addPreferencesFromResource(R.xml.preferences)
39+
40+
findPreference("theme").setOnPreferenceChangeListener { _, newValue ->
41+
AppCompatDelegate.setDefaultNightMode(getThemeFromPreferenceString(newValue as String))
42+
Toast.makeText(activity.applicationContext, "You may need to restart for the new theme to apply properly.", Toast.LENGTH_SHORT).show()
43+
activity.recreate()
44+
true
45+
}
3346
}
3447
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tk.superl2.xwifi
2+
3+
import android.content.SharedPreferences
4+
import android.graphics.Color
5+
import android.os.Build
6+
import android.support.v7.app.AppCompatActivity
7+
import android.support.v7.app.AppCompatDelegate
8+
import android.util.Log
9+
import android.view.WindowManager
10+
11+
private const val TAG = "Utils"
12+
13+
fun setThemeFromSharedPrefs(prefs: SharedPreferences) {
14+
AppCompatDelegate.setDefaultNightMode(getThemeFromPreferenceString(prefs.getString("theme", "Light")))
15+
}
16+
17+
fun getThemeFromPreferenceString(value: String): Int {
18+
return when (value) {
19+
"Light" -> AppCompatDelegate.MODE_NIGHT_NO
20+
"Dark" -> AppCompatDelegate.MODE_NIGHT_YES
21+
"System" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
22+
else -> {
23+
Log.w(TAG, "Invalid theme set in shared preferences! Using system theme.")
24+
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
25+
}
26+
}
27+
}

app/src/main/res/drawable/ic_aspect_ratio_black_24dp.xml renamed to app/src/main/res/drawable/ic_aspect_ratio_24dp.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
android:viewportWidth="24.0"
55
android:viewportHeight="24.0">
66
<path
7-
android:fillColor="#FF000000"
7+
android:fillColor="?attr/textColorAlertDialogListItem"
88
android:pathData="M19,12h-2v3h-3v2h5v-5zM7,9h3L10,7L5,7v5h2L7,9zM21,3L3,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2L23,5c0,-1.1 -0.9,-2 -2,-2zM21,19.01L3,19.01L3,4.99h18v14.02z"/>
99
</vector>
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="?attr/textColorAlertDialogListItem"
8+
android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
9+
</vector>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
<vector android:alpha="0.95" android:height="24dp"
2-
android:tint="#FFFFFF" android:viewportHeight="24.0"
3-
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4-
<path android:fillColor="#FF000000" android:pathData="M12,3C6.95,3 3.15,4.85 0,7.23L12,22 24,7.25C20.85,4.87 17.05,3 12,3zM13,16h-2v-6h2v6zM11,8L11,6h2v2h-2z"/>
1+
<vector android:alpha="0.95"
2+
android:height="24dp"
3+
android:tint="#FFFFFF"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0"
6+
android:width="24dp"
7+
xmlns:android="http://schemas.android.com/apk/res/android">
8+
<path
9+
android:fillColor="#FF000000"
10+
android:pathData="M12,3C6.95,3 3.15,4.85 0,7.23L12,22 24,7.25C20.85,4.87 17.05,3 12,3zM13,16h-2v-6h2v6zM11,8L11,6h2v2h-2z" />
511
</vector>

app/src/main/res/drawable/ic_settings_black_24dp.xml renamed to app/src/main/res/drawable/ic_settings_24dp.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
android:viewportWidth="24.0"
55
android:viewportHeight="24.0">
66
<path
7-
android:fillColor="#FF000000"
7+
android:fillColor="?attr/textColorAlertDialogListItem"
88
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"/>
99
</vector>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
2+
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
34

4-
<item android:title="@string/item_settings_menu_activity_main" android:checkable="false"
5-
android:showAsAction="ifRoom"
6-
android:id="@+id/settingsItem" android:icon="@drawable/ic_settings_black_24dp"/>
5+
<item
6+
android:id="@+id/settingsItem"
7+
android:checkable="false"
8+
android:icon="@drawable/ic_settings_24dp"
9+
android:showAsAction="ifRoom"
10+
android:title="@string/item_settings_menu_activity_main"
11+
app:showAsAction="ifRoom" />
712
</menu>
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<bool name="is_theme_light">false</bool>
4+
</resources>

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string-array name="qr_code_resolutions_entries" >
3+
<string-array name="qr_code_resolutions_entries">
44
<item>100x100</item>
55
<item>150x150</item>
66
<item>200x200</item>
@@ -22,7 +22,7 @@
2222
<item>1000x1000</item>
2323
<item>1050x1050</item>
2424
</string-array>
25-
<string-array name="qr_code_resolutions_values" >
25+
<string-array name="qr_code_resolutions_values">
2626
<item>100</item>
2727
<item>150</item>
2828
<item>200</item>
@@ -44,4 +44,10 @@
4444
<item>1000</item>
4545
<item>1050</item>
4646
</string-array>
47+
48+
<string-array name="themes">
49+
<item>Light</item>
50+
<item>Dark</item>
51+
<!--<item>System</item> Disabled for now. TODO Get system theme working. -->
52+
</string-array>
4753
</resources>

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

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<bool name="is_theme_light">true</bool>
4+
</resources>

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
<resources>
2-
1+
<resources xmlns:tools="http://schemas.android.com/tools">
32
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="android:Theme.Material.Light">
5-
<!-- Customize your theme here. -->
3+
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
4+
<!--Use the light statusbar only when in day/light mode-->
5+
<!--<item tools:targetApi="23" name="android:windowLightStatusBar">@bool/is_theme_light</item>-->
66
</style>
7-
87
</resources>

app/src/main/res/xml/preferences.xml

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3-
<!--<ListPreference-->
4-
<!--android:defaultValue="Light"-->
5-
<!--android:entries="@array/themes"-->
6-
<!--android:entryValues="@array/themes"-->
7-
<!--android:key="theme"-->
8-
<!--android:summary="%s"-->
9-
<!--android:title="App theme" />-->
3+
<ListPreference
4+
android:defaultValue="Light"
5+
android:entries="@array/themes"
6+
android:entryValues="@array/themes"
7+
android:icon="@drawable/ic_palette_24dp"
8+
android:key="theme"
9+
android:summary="%s"
10+
android:title="App theme" />
1011
<ListPreference
1112
android:defaultValue="300"
1213
android:entries="@array/qr_code_resolutions_entries"
1314
android:entryValues="@array/qr_code_resolutions_values"
14-
android:icon="@drawable/ic_aspect_ratio_black_24dp"
15+
android:icon="@drawable/ic_aspect_ratio_24dp"
1516
android:key="qr_code_resolution"
1617
android:summary="%s"
1718
android:title="Resolution of generated QR codes" />

0 commit comments

Comments
 (0)