Compare commits

...

3 Commits

Author SHA1 Message Date
Hai Zhang
7a2b228dfe Refactor: Move TileService startForegroundService() workaround to one
function

Bug: #1238
2024-06-25 23:08:08 -07:00
Hai Zhang
c9fd16ec2c Fix: Fix icon for CBR and CBZ files
Fixes: #1231
2024-06-25 22:00:53 -07:00
Hai Zhang
7c6e3e827a Fix: Workaround FTP tile crash on Android 14
Bug: #1238
2024-06-25 21:36:45 -07:00
3 changed files with 67 additions and 3 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 Hai Zhang <dreaming.in.code.zh@gmail.com>
* All Rights Reserved.
*/
package me.zhanghai.android.files.compat
import android.graphics.PixelFormat
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.TileService
import android.view.View
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.core.view.doOnPreDraw
import me.zhanghai.android.files.hiddenapi.RestrictedHiddenApi
import me.zhanghai.android.files.util.lazyReflectedField
// Work around https://issuetracker.google.com/issues/299506164 on U which is fixed in V.
fun TileService.doWithStartForegroundServiceAllowed(action: () -> Unit) {
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
action()
return
}
val windowManager = getSystemService(WindowManager::class.java)
val view = View(this)
val layoutParams =
WindowManager.LayoutParams().apply {
type = WindowManager_LayoutParams_TYPE_QS_DIALOG
format = PixelFormat.TRANSLUCENT
token = this@doWithStartForegroundServiceAllowed.token
}
windowManager.addView(view, layoutParams)
// We need to wait for WindowState.onSurfaceShownChanged(), basically when the first draw has
// finished and the surface is about to be shown to the user. However there's no good callback
// for that, while waiting for the second pre-draw seems to work.
view.doOnPreDraw {
view.post {
view.invalidate()
view.doOnPreDraw {
try {
action()
} finally {
windowManager.removeView(view)
}
}
}
}
}
private const val WindowManager_LayoutParams_TYPE_QS_DIALOG =
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 35
@delegate:RequiresApi(Build.VERSION_CODES.N)
@get:RequiresApi(Build.VERSION_CODES.N)
@RestrictedHiddenApi
private val tokenField by lazyReflectedField(TileService::class.qualifiedName!!, "mToken")
private val TileService.token: IBinder?
@RequiresApi(Build.VERSION_CODES.N)
get() = tokenField.get(this) as IBinder?

View File

@ -159,6 +159,8 @@ private val mimeTypeToIconMap = mapOf(
"application/epub+zip" to MimeTypeIcon.EBOOK,
"application/vnd.amazon.ebook" to MimeTypeIcon.EBOOK,
"application/vnd.amazon.mobi8-ebook" to MimeTypeIcon.EBOOK,
"application/vnd.comicbook-rar" to MimeTypeIcon.EBOOK,
"application/vnd.comicbook+zip" to MimeTypeIcon.EBOOK,
"application/x-cbr" to MimeTypeIcon.EBOOK,
"application/x-cbz" to MimeTypeIcon.EBOOK,
"application/x-ibooks+zip" to MimeTypeIcon.EBOOK,

View File

@ -10,6 +10,7 @@ import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import me.zhanghai.android.files.compat.doWithStartForegroundServiceAllowed
@RequiresApi(Build.VERSION_CODES.N)
class FtpServerTileService : TileService() {
@ -30,8 +31,8 @@ class FtpServerTileService : TileService() {
private fun onFtpServerStateChanged(state: FtpServerService.State) {
val tile = qsTile
when (state) {
FtpServerService.State.STARTING, FtpServerService.State.RUNNING ->
tile.state = Tile.STATE_ACTIVE
FtpServerService.State.STARTING,
FtpServerService.State.RUNNING -> tile.state = Tile.STATE_ACTIVE
FtpServerService.State.STOPPING -> tile.state = Tile.STATE_UNAVAILABLE
FtpServerService.State.STOPPED -> tile.state = Tile.STATE_INACTIVE
}
@ -49,6 +50,6 @@ class FtpServerTileService : TileService() {
}
private fun toggle() {
FtpServerService.toggle(this)
doWithStartForegroundServiceAllowed { FtpServerService.toggle(this) }
}
}