Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
R3.15.2
Browse files Browse the repository at this point in the history
在Android P上支持暗黑主题
normal版也记录Timber日志到logcat
  • Loading branch information
ryuunoakaihitomi committed Jan 16, 2022
1 parent fcced94 commit 462886c
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 29 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ android {
//targetSdkVersion compileSdkVersion
targetSdkVersion 31
// 版本号:发布日期
versionCode 20211224
versionCode 20220116
// 版本名说明: R3.x.y.z (R3:Refactoring 第三次重构,z:Quick Fix序号)
// 不要包含非ASCII可打印字符,会导致Github release文件名异常
versionName 'R3.15.1'
versionName 'R3.15.2'
resConfigs "en", "zh-rCN"

buildConfigField 'String', 'BUILD_TIME', '\"' + new Date() + '\"'
Expand Down Expand Up @@ -124,7 +124,7 @@ dependencies {

// https://github.com/androidx/androidx
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.multidex:multidex:2.0.1'
def lifecycle_version = "2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
Expand Down Expand Up @@ -165,7 +165,7 @@ dependencies {
implementation 'com.github.GrenderG:Toasty:1.5.2'

// https://github.com/topjohnwu/libsu
implementation 'com.github.topjohnwu.libsu:core:3.2.0'
implementation 'com.github.topjohnwu.libsu:core:3.2.1'

// https://github.com/apache/commons-io
// 固定在此版本,理由同下段注释 (使用的一些java8 API是脱糖库无法完成转换的,即使脱糖库的文档说明支持)
Expand Down Expand Up @@ -194,7 +194,7 @@ dependencies {
implementation 'org.cyanogenmod:platform.sdk:6.0'

// https://github.com/LSPosed/AndroidHiddenApiBypass
implementation 'org.lsposed.hiddenapibypass:hiddenapibypass:3.0'
implementation 'org.lsposed.hiddenapibypass:hiddenapibypass:4.2'

// https://github.com/eclipse/org.aspectj
implementation 'org.aspectj:aspectjrt:1.9.7'
Expand All @@ -203,7 +203,7 @@ dependencies {
implementation 'dev.rikka.rikkax.compatibility:compatibility:2.0.0'

// https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'

// https://github.com/whataa/pandora
// 最新版是v2.1.0,仅支持android-support
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/assets/dependency_list/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"type": "Apache Software License 2.0",
"url": "https://github.com/wurensen/gradle_plugin_android_aspectjx"
},
{
"author": "Ben Manes",
"name": "Gradle Versions Plugin",
"type": "Apache Software License 2.0",
"url": "https://github.com/ben-manes/gradle-versions-plugin"
},
{
"author": "Leon Lambert",
"name": "AndResGuard",
"type": "Apache Software License 2.0",
"url": "https://github.com/Leon406/AndResGuard"
},
{
"author": "Ben Manes",
"name": "Gradle Versions Plugin",
"type": "Apache Software License 2.0",
"url": "https://github.com/ben-manes/gradle-versions-plugin"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package github.ryuunoakaihitomi.powerpanel

import android.app.Application
import android.app.UiModeManager
import android.app.WallpaperColors
import android.app.WallpaperManager
import android.database.ContentObserver
import android.os.Build
import android.os.Handler
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.getSystemService
import github.ryuunoakaihitomi.powerpanel.util.nox
import timber.log.Timber

/**
* 在Android P中根据实验性的“设备主题”设置选项应用暗色主题,**这是为了与系统的电源菜单颜色主题保持一致**
*
* TODO: 暂时未进行除了HMD真机设备和AVD以外环境的更多实际测试,如在其他环境下出现兼容性问题,可能需要在这环境下禁用这项功能
*/
@RequiresApi(Build.VERSION_CODES.P)
object DarkThemeForP {

/**
* The current device UI theme mode effect SystemUI and Launcher.<br/>
* <b>Values:</b><br/>
* 0 - The mode that theme will controlled by wallpaper color.<br/>
* 1 - The mode that will always light theme.<br/>
* 2 - The mode that will always dark theme.<br/>
*
* @see Settings.Secure
* @hide
*/
private const val key = "theme_mode"

/**
* 实测只会根据主屏幕变色
*/
private const val whichPaper = WallpaperManager.FLAG_SYSTEM

fun main(app: Application) = app.run {
// 实测AVD中的黑暗主题不会随着设置的“设备主题”选项变更,反而随着省电模式变更了,而且应用也自动更改了主题,无需做进一步处理
if (getSystemService<UiModeManager>()?.nightMode == UiModeManager.MODE_NIGHT_YES) return
contentResolver.registerContentObserver(Settings.Secure.getUriFor(key), false,
object : ContentObserver(Handler(mainLooper)) {
override fun onChange(selfChange: Boolean) {
Timber.d("DT4P: config changed")
changeTheme()
super.onChange(selfChange)
}
})
changeTheme()
}

private fun Application.changeTheme() {
when (Settings.Secure.getInt(contentResolver, key, -1)) {
0 -> {
getSystemService<WallpaperManager>()?.run {
if (isWallpaperSupported) {
if (getWallpaperColors(whichPaper)?.supportDarkTheme() == true) nox()
addOnColorsChangedListener({ colors, which ->
Timber.d("DT4P: colors changed. c=$colors w=$which")
if (which == whichPaper && colors.supportDarkTheme()) nox()
}, Handler(mainLooper))
}
}
}
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
2 -> nox()
}
}

/**
* 系统判断变色的机制
*/
@Suppress("NewApi")
private fun WallpaperColors.supportDarkTheme() =
colorHints and WallpaperColors.HINT_SUPPORTS_DARK_THEME != 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import android.content.Context
import android.os.Build
import android.os.StrictMode
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDexApplication
import github.ryuunoakaihitomi.poweract.ExternalUtils
import github.ryuunoakaihitomi.powerpanel.receiver.ShutdownReceiver
import github.ryuunoakaihitomi.powerpanel.stat.Statistics
import github.ryuunoakaihitomi.powerpanel.util.MyLogTree
import github.ryuunoakaihitomi.powerpanel.util.isWatch
import github.ryuunoakaihitomi.powerpanel.util.nox
import org.lsposed.hiddenapibypass.HiddenApiBypass
import rikka.compatibility.DeviceCompatibility
import timber.log.Timber
Expand Down Expand Up @@ -62,8 +62,9 @@ class MyApplication : MultiDexApplication() {
*
* @see [android.app.UiModeManager.setNightMode](https://developer.android.google.cn/reference/android/app/UiModeManager#setNightMode(int))
*/
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || isWatch())
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || isWatch()) nox()
// 为Android P启用暗色主题支持
else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) DarkThemeForP.main(this)

// 后门:在后台应用被杀检测
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface InternalDoer {
fun initialize(app: Application)
fun setCustomKey(k: String, v: Any)
fun logEvent(tag: String, bundle: Bundle)
fun log(level: String, tag: String, msg: String) {}
fun log(level: Int, tag: String, msg: String) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.view.accessibility.AccessibilityNodeInfo
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.getSystemService
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.set
Expand Down Expand Up @@ -99,4 +100,6 @@ fun View.emptyAccessibilityDelegate() = run {
override fun sendAccessibilityEvent(host: View?, eventType: Int) {}
override fun sendAccessibilityEventUnchecked(host: View?, event: AccessibilityEvent?) {}
}
}
}

fun nox() = AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package github.ryuunoakaihitomi.powerpanel.stat

import android.app.Application
import android.os.Bundle
import android.util.Log
import android.util.LogPrinter
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
Expand Down Expand Up @@ -38,8 +40,17 @@ object InternalDoerImpl : InternalDoer {
AppCenterCompat.trackEvent(tag, bundle)
}

override fun log(level: String, tag: String, msg: String) {
val logLine = listOf(level, tag, msg).toString()
override fun log(level: Int, tag: String, msg: String) {
LogPrinter(level, "NPP#$tag").println(msg)
val level2Str = when (level) {
Log.VERBOSE -> "V"
Log.DEBUG -> "D"
Log.INFO -> "I"
Log.WARN -> "W"
Log.ERROR -> "E"
else -> level.toString()
}
val logLine = listOf(level2Str, tag, msg).toString()
Firebase.crashlytics.log(logLine)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ import timber.log.Timber
class MyLogTree : Timber.DebugTree() {

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
InternalDoerImpl.log(levelToString(priority), tag ?: "empty tag", message)
InternalDoerImpl.log(priority, tag ?: "empty tag", message)
if (BuildConfig.DEBUG) super.log(priority, tag, message, t)
}

private fun levelToString(level: Int) = when (level) {
Log.VERBOSE -> "V"
Log.DEBUG -> "D"
Log.INFO -> "I"
Log.WARN -> "W"
Log.ERROR -> "E"
else -> level.toString()
}
}

0 comments on commit 462886c

Please sign in to comment.