Skip to content

Commit 03b1067

Browse files
committed
Add an option to manually toggle dark theme
1 parent 8226b28 commit 03b1067

File tree

12 files changed

+260
-3
lines changed

12 files changed

+260
-3
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ android {
1414
targetSdkVersion 29
1515
versionCode 4
1616
versionName "1.20"
17+
vectorDrawables.useSupportLibrary = true
1718
}
1819
compileOptions {
1920
sourceCompatibility JavaVersion.VERSION_1_8
@@ -35,6 +36,7 @@ dependencies {
3536
implementation 'androidx.appcompat:appcompat:1.1.0'
3637
implementation 'androidx.core:core-ktx:1.1.0'
3738
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
39+
implementation 'androidx.preference:preference:1.1.0'
3840

3941
implementation 'com.google.android.material:material:1.2.0-alpha02'
4042

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="app.akilesh.qacc">
44

5-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
5+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66

77
<application
88
android:allowBackup="true"
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
11+
android:name=".App"
1112
android:roundIcon="@mipmap/ic_launcher_round"
1213
android:supportsRtl="true"
1314
android:theme="@style/AppTheme">
14-
<activity android:name=".SplashActivity" android:theme="@style/SplashTheme">
15+
16+
<activity
17+
android:name=".SplashActivity"
18+
android:theme="@style/SplashTheme">
1519
<intent-filter>
1620
<action android:name="android.intent.action.MAIN" />
21+
1722
<category android:name="android.intent.category.LAUNCHER" />
1823
</intent-filter>
1924
</activity>
2025

21-
<activity android:name=".MainActivity"/>
26+
<activity android:name=".MainActivity" />
27+
28+
<activity
29+
android:name=".SettingsActivity"
30+
android:theme="@style/Theme.MaterialComponents.DayNight"
31+
android:label="@string/title_activity_settings" />
32+
2233
</application>
2334

2435
</manifest>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package app.akilesh.qacc
2+
3+
import android.app.Application
4+
import androidx.preference.PreferenceManager
5+
import app.akilesh.qacc.utils.ThemeUtil
6+
import com.topjohnwu.superuser.Shell
7+
8+
class App: Application() {
9+
10+
init {
11+
Shell.Config.setFlags(Shell.FLAG_REDIRECT_STDERR)
12+
Shell.Config.setFlags(Shell.FLAG_VERBOSE_LOGGING)
13+
Shell.Config.verboseLogging(true)
14+
Shell.Config.setTimeout(10)
15+
}
16+
17+
override fun onCreate() {
18+
super.onCreate()
19+
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
20+
val theme = sharedPreferences.getString("themePref", ThemeUtil().default)
21+
ThemeUtil().applyTheme(theme)
22+
23+
}
24+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package app.akilesh.qacc
2+
3+
import android.content.res.Configuration
4+
import android.os.Bundle
5+
import android.view.View
6+
import android.view.WindowManager
7+
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.core.content.ContextCompat
9+
import androidx.preference.ListPreference
10+
import androidx.preference.Preference
11+
import androidx.preference.PreferenceFragmentCompat
12+
import app.akilesh.qacc.databinding.SettingsActivityBinding
13+
import app.akilesh.qacc.utils.ThemeUtil
14+
15+
class SettingsActivity : AppCompatActivity() {
16+
17+
private lateinit var settingsActivityBinding: SettingsActivityBinding
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
settingsActivityBinding = SettingsActivityBinding.inflate(layoutInflater)
22+
setContentView(settingsActivityBinding.root)
23+
24+
val decorView = window.decorView
25+
decorView.systemUiVisibility = WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
26+
27+
when(resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
28+
Configuration.UI_MODE_NIGHT_NO -> {
29+
val colorWhite = ContextCompat.getColor(this, R.color.white)
30+
window.navigationBarColor = colorWhite
31+
window.statusBarColor = colorWhite
32+
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
33+
supportActionBar?.setBackgroundDrawable(getDrawable(R.color.white))
34+
35+
}
36+
Configuration.UI_MODE_NIGHT_YES -> {
37+
val colorDefaultDark = ContextCompat.getColor(this, R.color.colorDefaultDark)
38+
window.statusBarColor = colorDefaultDark
39+
window.navigationBarColor = colorDefaultDark
40+
supportActionBar?.setBackgroundDrawable(getDrawable(R.color.colorDefaultDark))
41+
}
42+
}
43+
44+
supportFragmentManager
45+
.beginTransaction()
46+
.replace(settingsActivityBinding.settings.id, SettingsFragment())
47+
.commit()
48+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
49+
supportActionBar?.setDisplayShowHomeEnabled(true)
50+
}
51+
52+
override fun onSupportNavigateUp(): Boolean {
53+
onBackPressed()
54+
return true
55+
}
56+
57+
class SettingsFragment : PreferenceFragmentCompat() {
58+
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
59+
setPreferencesFromResource(R.xml.root_preferences, rootKey)
60+
61+
val themePreference = findPreference<ListPreference>("themePref")
62+
if (themePreference != null) {
63+
themePreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
64+
val theme = newValue as String
65+
ThemeUtil().applyTheme(theme)
66+
true
67+
}
68+
}
69+
}
70+
}
71+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app.akilesh.qacc.utils
2+
3+
import androidx.appcompat.app.AppCompatDelegate
4+
5+
class ThemeUtil {
6+
private val lightMode = "light"
7+
private val darkMode = "dark"
8+
private val batterySaverMode = "battery"
9+
val default = "default"
10+
11+
fun applyTheme(theme: String?) {
12+
when (theme) {
13+
lightMode -> {
14+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
15+
}
16+
17+
darkMode -> {
18+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
19+
}
20+
21+
batterySaverMode -> {
22+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
23+
}
24+
25+
default -> {
26+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
27+
}
28+
}
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
7+
<path
8+
android:fillColor="?colorOnBackground"
9+
android:pathData="M13.85 22.25h-3.7c-0.74 0-1.36-0.54-1.45-1.27l-0.27-1.89c-0.27-0.14-0.53-0.29-0.79-0.46l-1.8 0.72 c-0.7 0.26 -1.47-0.03-1.81-0.65L2.2 15.53c-0.35-0.66-0.2-1.44 0.36 -1.88l1.53-1.19c-0.01-0.15-0.02-0.3-0.02-0.46 0-0.15 0.01 -0.31 0.02 -0.46l-1.52-1.19c-0.59-0.45-0.74-1.26-0.37-1.88l1.85-3.19c0.34-0.62 1.11-0.9 1.79-0.63l1.81 0.73 c0.26-0.17 0.52 -0.32 0.78 -0.46l0.27-1.91c0.09-0.7 0.71 -1.25 1.44-1.25h3.7c0.74 0 1.36 0.54 1.45 1.27l0.27 1.89c0.27 0.14 0.53 0.29 0.79 0.46 l1.8-0.72c0.71-0.26 1.48 0.03 1.82 0.65 l1.84 3.18c0.36 0.66 0.2 1.44-0.36 1.88l-1.52 1.19c0.01 0.15 0.02 0.3 0.02 0.46 s-0.01 0.31 -0.02 0.46 l1.52 1.19c0.56 0.45 0.72 1.23 0.37 1.86l-1.86 3.22c-0.34 0.62 -1.11 0.9 -1.8 0.63 l-1.8-0.72c-0.26 0.17 -0.52 0.32 -0.78 0.46 l-0.27 1.91c-0.1 0.68 -0.72 1.22-1.46 1.22zm-3.23-2h2.76l0.37-2.55 0.53 -0.22c0.44-0.18 0.88 -0.44 1.34-0.78l0.45-0.34 2.38 0.96 1.38-2.4-2.03-1.58 0.07 -0.56c0.03-0.26 0.06 -0.51 0.06 -0.78s-0.03-0.53-0.06-0.78l-0.07-0.56 2.03-1.58-1.39-2.4-2.39 0.96 -0.45-0.35c-0.42-0.32-0.87-0.58-1.33-0.77l-0.52-0.22-0.37-2.55h-2.76l-0.37 2.55-0.53 0.21 c-0.44 0.19 -0.88 0.44 -1.34 0.79 l-0.45 0.33 -2.38-0.95-1.39 2.39 2.03 1.58-0.07 0.56 a7 7 0 0 0-0.06 0.79 c0 0.26 0.02 0.53 0.06 0.78l0.07 0.56 -2.03 1.58 1.38 2.4 2.39-0.96 0.45 0.35c0.43 0.33 0.86 0.58 1.33 0.77 l0.53 0.22 0.38 2.55z" />
10+
<path
11+
android:fillColor="?colorOnBackground"
12+
android:pathData="M 12 8.5 C 13.9329966244 8.5 15.5 10.0670033756 15.5 12 C 15.5 13.9329966244 13.9329966244 15.5 12 15.5 C 10.0670033756 15.5 8.5 13.9329966244 8.5 12 C 8.5 10.0670033756 10.0670033756 8.5 12 8.5 Z" />
13+
</vector>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!--
2+
~ This file is a part of Lawnchair Launcher, modified to match the accent color of NEX app.
3+
~ <https://github.com/LawnchairLauncher/Lawnchair/blob/alpha/res/drawable/ic_theme.xml/>
4+
5+
~ Lawnchair Launcher is free software: you can redistribute it and/or modify
6+
~ it under the terms of the GNU General Public License as published by
7+
~ the Free Software Foundation, either version 3 of the License, or
8+
~ (at your option) any later version.
9+
~
10+
-->
11+
12+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
13+
android:width="38dp"
14+
android:height="38dp"
15+
android:viewportWidth="32"
16+
android:viewportHeight="32">
17+
<path
18+
android:pathData="M16,16m-16,0a16,16 0,1 1,32 0a16,16 0,1 1,-32 0"
19+
android:strokeAlpha="0.18"
20+
android:fillColor="@color/colorAccent"
21+
android:fillAlpha="0.18"/>
22+
<path
23+
android:pathData="M16,26C10.49,26 6,21.51 6,16C6,10.49 10.49,6 16,6C21.51,6 26,10.04 26,15C26,18.31 23.31,21 20,21H18.23C17.95,21 17.73,21.22 17.73,21.5C17.73,21.62 17.78,21.73 17.86,21.83C18.27,22.3 18.5,22.89 18.5,23.5C18.5,24.88 17.38,26 16,26ZM16,8C11.59,8 8,11.59 8,16C8,20.41 11.59,24 16,24C16.28,24 16.5,23.78 16.5,23.5C16.5,23.34 16.42,23.22 16.36,23.15C15.95,22.69 15.73,22.1 15.73,21.5C15.73,20.12 16.85,19 18.23,19H20C22.21,19 24,17.21 24,15C24,11.14 20.41,8 16,8Z"
24+
android:fillColor="@color/colorAccent"/>
25+
<path
26+
android:pathData="M10.5,17C11.3284,17 12,16.3284 12,15.5C12,14.6716 11.3284,14 10.5,14C9.6716,14 9,14.6716 9,15.5C9,16.3284 9.6716,17 10.5,17Z"
27+
android:fillColor="@color/colorAccent"/>
28+
<path
29+
android:pathData="M13.5,13C14.3284,13 15,12.3284 15,11.5C15,10.6716 14.3284,10 13.5,10C12.6716,10 12,10.6716 12,11.5C12,12.3284 12.6716,13 13.5,13Z"
30+
android:fillColor="@color/colorAccent"/>
31+
<path
32+
android:pathData="M18.5,13C19.3284,13 20,12.3284 20,11.5C20,10.6716 19.3284,10 18.5,10C17.6716,10 17,10.6716 17,11.5C17,12.3284 17.6716,13 18.5,13Z"
33+
android:fillColor="@color/colorAccent"/>
34+
<path
35+
android:pathData="M21.5,17C22.3284,17 23,16.3284 23,15.5C23,14.6716 22.3284,14 21.5,14C20.6716,14 20,14.6716 20,15.5C20,16.3284 20.6716,17 21.5,17Z"
36+
android:fillColor="@color/colorAccent"/>
37+
</vector>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
android:layout_height="wrap_content"
2929
android:layout_width="wrap_content" />
3030

31+
<com.google.android.material.floatingactionbutton.FloatingActionButton
32+
android:id="@+id/fab"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:layout_gravity="end"
36+
app:fabSize="mini"
37+
app:srcCompat="@drawable/ic_settings"
38+
app:tint="?colorOnPrimary"
39+
android:layout_marginEnd="8dp"/>
40+
3141
</FrameLayout>
3242

3343
<FrameLayout
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:orientation="vertical">
5+
6+
<FrameLayout
7+
android:id="@+id/settings"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
10+
</LinearLayout>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string-array name="themeListArray">
4+
<item>Off</item>
5+
<item>On</item>
6+
<item>Set by Battery Saver</item>
7+
<item>System default</item>
8+
</string-array>
9+
10+
<string-array name="themeEntryArray">
11+
<item>light</item>
12+
<item>dark</item>
13+
<item>battery</item>
14+
<item>default</item>
15+
</string-array>
16+
<!-- Reply Preference -->
17+
<string-array name="reply_entries">
18+
<item>Reply</item>
19+
<item>Reply to all</item>
20+
</string-array>
21+
22+
<string-array name="reply_values">
23+
<item>reply</item>
24+
<item>reply_all</item>
25+
</string-array>
26+
</resources>

0 commit comments

Comments
 (0)