mirror of
https://github.com/zhanghai/MaterialFiles
synced 2024-06-29 05:04:19 +00:00
Compare commits
3 Commits
5127250961
...
7a2b228dfe
Author | SHA1 | Date | |
---|---|---|---|
|
7a2b228dfe | ||
|
c9fd16ec2c | ||
|
7c6e3e827a |
|
@ -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?
|
|
@ -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,
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user