1package com.peachpasswords.qs23import android.content.ClipData4import android.content.ClipboardManager5import android.content.Context6import android.content.Intent7import android.graphics.drawable.Icon8import android.os.Build9import android.service.quicksettings.Tile10import android.service.quicksettings.TileService11import android.widget.Toast12import com.peachpasswords.MainActivity13import com.peachpasswords.R14import com.peachpasswords.vault.VaultRepository15import dagger.hilt.android.AndroidEntryPoint16import javax.inject.Inject1718192021222324@AndroidEntryPoint25class PeachTileService : TileService() {2627 @Inject lateinit var vault: VaultRepository2829 override fun onStartListening() {30 super.onStartListening()31 updateTile()32 }3334 override fun onClick() {35 super.onClick()36 if (vault.isUnlocked.value) {37 val recent = vault.entries.value38 .filter { !it.password.isNullOrEmpty() }39 .maxByOrNull { it.lastUsedAt ?: it.modifiedAt }40 if (recent?.password != null) {41 copyToClipboard(recent.title, recent.password!!)42 Toast.makeText(this, "Copied ${recent.title} password", Toast.LENGTH_SHORT).show()43 } else {44 Toast.makeText(this, "No entries to copy", Toast.LENGTH_SHORT).show()45 }46 updateTile()47 } else {48 launchApp()49 }50 }5152 private fun launchApp() {53 val intent = Intent(this, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK }54 if (Build.VERSION.SDK_INT >= 34) {55 startActivityAndCollapse(56 android.app.PendingIntent.getActivity(57 this, 0, intent,58 android.app.PendingIntent.FLAG_IMMUTABLE or android.app.PendingIntent.FLAG_UPDATE_CURRENT,59 ),60 )61 } else {62 @Suppress("DEPRECATION", "StartActivityAndCollapseDeprecated")63 startActivityAndCollapse(intent)64 }65 }6667 private fun copyToClipboard(label: String, value: String) {68 val clip = ClipData.newPlainText(label, value).apply {69 description.extras = android.os.PersistableBundle().apply {70 71 putBoolean("android.content.extra.IS_SENSITIVE", true)72 }73 }74 (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(clip)75 }7677 private fun updateTile() {78 val tile = qsTile ?: return79 val unlocked = vault.isUnlocked.value80 tile.state = if (unlocked) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE81 tile.label = "Peach"82 tile.contentDescription = if (unlocked) "Copy recent password" else "Unlock Peach"83 if (Build.VERSION.SDK_INT >= 29) {84 tile.subtitle = if (unlocked) "tap to copy" else "locked"85 }86 tile.icon = Icon.createWithResource(this, R.drawable.ic_peach_mark)87 tile.updateTile()88 }89}