-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Add quick settings tile placement for FTP server.
Fixes: #671
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/src/main/java/me/zhanghai/android/files/ftpserver/FtpServerAddTilePreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.ftpserver | ||
|
||
import android.app.StatusBarManager | ||
import android.content.Context | ||
import android.os.Build | ||
import android.util.AttributeSet | ||
import androidx.annotation.AttrRes | ||
import androidx.annotation.RequiresApi | ||
import androidx.annotation.StyleRes | ||
import androidx.core.content.res.ResourcesCompat | ||
import androidx.preference.Preference | ||
import me.zhanghai.android.files.R | ||
import me.zhanghai.android.files.compat.getSystemServiceCompat | ||
import me.zhanghai.android.files.compat.mainExecutorCompat | ||
import me.zhanghai.android.files.util.requestAddTileService | ||
import me.zhanghai.android.files.util.showToast | ||
|
||
class FtpServerAddTilePreference : Preference { | ||
constructor(context: Context) : super(context) | ||
|
||
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) | ||
|
||
constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : super( | ||
context, attrs, defStyleAttr | ||
) | ||
|
||
constructor( | ||
context: Context, | ||
attrs: AttributeSet?, | ||
@AttrRes defStyleAttr: Int, | ||
@StyleRes defStyleRes: Int | ||
) : super(context, attrs, defStyleAttr, defStyleRes) | ||
|
||
init { | ||
isPersistent = false | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
override fun onClick() { | ||
val statusBarManager = context.getSystemServiceCompat(StatusBarManager::class.java) | ||
statusBarManager.requestAddTileService( | ||
FtpServerTileService::class.java, context.mainExecutorCompat | ||
) { result -> | ||
val resultRes = when (result) { | ||
StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_NOT_ADDED -> | ||
return@requestAddTileService | ||
StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ALREADY_ADDED -> | ||
R.string.ftp_server_add_tile_result_already_added | ||
StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ADDED -> | ||
R.string.ftp_server_add_tile_result_added | ||
else -> R.string.ftp_server_add_tile_result_error | ||
} | ||
context.showToast(resultRes) | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/me/zhanghai/android/files/util/StatusBarManagerExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.util | ||
|
||
import android.app.StatusBarManager | ||
import android.content.ComponentName | ||
import android.content.pm.PackageManager | ||
import android.graphics.drawable.Icon | ||
import android.os.Build | ||
import android.service.quicksettings.TileService | ||
import androidx.annotation.RequiresApi | ||
import me.zhanghai.android.files.app.application | ||
import me.zhanghai.android.files.app.packageManager | ||
import java.util.concurrent.Executor | ||
|
||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
fun StatusBarManager.requestAddTileService( | ||
serviceClass: Class<out TileService>, | ||
resultExecutor: Executor, | ||
resultCallback: (Int) -> Unit | ||
) { | ||
val application = application | ||
val componentName = ComponentName(application, serviceClass) | ||
val packageManager = packageManager | ||
val serviceInfo = packageManager.getServiceInfo( | ||
componentName, | ||
PackageManager.MATCH_DIRECT_BOOT_AWARE or PackageManager.MATCH_DIRECT_BOOT_UNAWARE | ||
) | ||
val label = serviceInfo.loadLabel(packageManager) | ||
val icon = Icon.createWithResource(application, serviceInfo.iconResource) | ||
requestAddTileService(componentName, label, icon, resultExecutor, resultCallback) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<!-- | ||
~ Copyright (c) 2023 Hai Zhang <dreaming.in.code.zh@gmail.com> | ||
~ All Rights Reserved. | ||
--> | ||
|
||
<resources> | ||
<bool name="pref_visible_ftp_server_add_tile">true</bool> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters