|
5 | 5 |
|
6 | 6 | package me.zhanghai.android.files.ftpserver
|
7 | 7 |
|
8 |
| -import android.graphics.PixelFormat |
9 | 8 | import android.os.Build
|
10 | 9 | import android.service.quicksettings.Tile
|
11 | 10 | import android.service.quicksettings.TileService
|
12 |
| -import android.view.View |
13 |
| -import android.view.WindowManager |
14 | 11 | import androidx.annotation.RequiresApi
|
15 |
| -import androidx.core.view.doOnPreDraw |
16 | 12 | import androidx.lifecycle.Observer
|
17 |
| -import me.zhanghai.android.files.compat.WindowManagerLayoutParamsCompat |
18 |
| -import me.zhanghai.android.files.compat.token |
| 13 | +import me.zhanghai.android.files.compat.doWithStartForegroundServiceAllowed |
19 | 14 |
|
20 | 15 | @RequiresApi(Build.VERSION_CODES.N)
|
21 | 16 | class FtpServerTileService : TileService() {
|
@@ -55,39 +50,6 @@ class FtpServerTileService : TileService() {
|
55 | 50 | }
|
56 | 51 |
|
57 | 52 | private fun toggle() {
|
58 |
| - if (Build.VERSION.SDK_INT == Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { |
59 |
| - runWithForegroundWindow { FtpServerService.toggle(this) } |
60 |
| - } else { |
61 |
| - FtpServerService.toggle(this) |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - // Work around https://issuetracker.google.com/issues/299506164 on U which is fixed in V. |
66 |
| - @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
67 |
| - private fun runWithForegroundWindow(block: () -> Unit) { |
68 |
| - val windowManager = getSystemService(WindowManager::class.java) |
69 |
| - val view = View(this) |
70 |
| - val layoutParams = |
71 |
| - WindowManager.LayoutParams().apply { |
72 |
| - type = WindowManagerLayoutParamsCompat.TYPE_QS_DIALOG |
73 |
| - format = PixelFormat.TRANSLUCENT |
74 |
| - token = this@FtpServerTileService.token |
75 |
| - } |
76 |
| - windowManager.addView(view, layoutParams) |
77 |
| - // We need to wait for WindowState.onSurfaceShownChanged(), basically when the first draw |
78 |
| - // has finished and the surface is about to be shown to the user. However there's no good |
79 |
| - // callback for that, while waiting for the second pre-draw seems to work. |
80 |
| - view.doOnPreDraw { |
81 |
| - view.post { |
82 |
| - view.invalidate() |
83 |
| - view.doOnPreDraw { |
84 |
| - try { |
85 |
| - block() |
86 |
| - } finally { |
87 |
| - windowManager.removeView(view) |
88 |
| - } |
89 |
| - } |
90 |
| - } |
91 |
| - } |
| 53 | + doWithStartForegroundServiceAllowed { FtpServerService.toggle(this) } |
92 | 54 | }
|
93 | 55 | }
|
0 commit comments