ui: add shortcut for adding quick settings tile

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-10 20:48:11 +02:00
parent e53a8d76f9
commit 70f8e5ab3a
5 changed files with 81 additions and 1 deletions

View File

@ -75,6 +75,7 @@ class QuickTileService : TileService() {
} }
override fun onCreate() { override fun onCreate() {
isAdded = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
iconOn = Icon.createWithResource(this, R.drawable.ic_tile) iconOn = Icon.createWithResource(this, R.drawable.ic_tile)
iconOff = iconOn iconOff = iconOn
@ -98,6 +99,11 @@ class QuickTileService : TileService() {
iconOff = Icon.createWithBitmap(b) iconOff = Icon.createWithBitmap(b)
} }
override fun onDestroy() {
super.onDestroy()
isAdded = false
}
override fun onStartListening() { override fun onStartListening() {
Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback) Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback)
if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback) if (tunnel != null) tunnel!!.addOnPropertyChangedCallback(onStateChangedCallback)
@ -109,6 +115,14 @@ class QuickTileService : TileService() {
Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback) Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback)
} }
override fun onTileAdded() {
isAdded = true
}
override fun onTileRemoved() {
isAdded = false
}
private fun updateTile() { private fun updateTile() {
// Update the tunnel. // Update the tunnel.
val newTunnel = Application.getTunnelManager().lastUsedTunnel val newTunnel = Application.getTunnelManager().lastUsedTunnel
@ -157,5 +171,7 @@ class QuickTileService : TileService() {
companion object { companion object {
private const val TAG = "WireGuard/QuickTileService" private const val TAG = "WireGuard/QuickTileService"
var isAdded: Boolean = false
private set
} }
} }

View File

@ -4,9 +4,11 @@
*/ */
package com.wireguard.android.activity package com.wireguard.android.activity
import android.content.ComponentName
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.service.quicksettings.TileService
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit import androidx.fragment.app.commit
@ -14,6 +16,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.wireguard.android.Application import com.wireguard.android.Application
import com.wireguard.android.QuickTileService
import com.wireguard.android.R import com.wireguard.android.R
import com.wireguard.android.backend.WgQuickBackend import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.preference.PreferencesPreferenceDataStore import com.wireguard.android.preference.PreferencesPreferenceDataStore
@ -47,7 +50,13 @@ class SettingsActivity : AppCompatActivity() {
override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) {
preferenceManager.preferenceDataStore = PreferencesPreferenceDataStore(lifecycleScope, Application.getPreferencesDataStore()) preferenceManager.preferenceDataStore = PreferencesPreferenceDataStore(lifecycleScope, Application.getPreferencesDataStore())
addPreferencesFromResource(R.xml.preferences) addPreferencesFromResource(R.xml.preferences)
preferenceScreen.initialExpandedChildrenCount = 4 preferenceScreen.initialExpandedChildrenCount = 5
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || QuickTileService.isAdded) {
val quickTile = preferenceManager.findPreference<Preference>("quick_tile")
quickTile?.parent?.removePreference(quickTile)
--preferenceScreen.initialExpandedChildrenCount
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val darkTheme = preferenceManager.findPreference<Preference>("dark_theme") val darkTheme = preferenceManager.findPreference<Preference>("dark_theme")
darkTheme?.parent?.removePreference(darkTheme) darkTheme?.parent?.removePreference(darkTheme)

View File

@ -0,0 +1,50 @@
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.preference
import android.app.StatusBarManager
import android.content.ComponentName
import android.content.Context
import android.graphics.drawable.Icon
import android.os.Build
import android.util.AttributeSet
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.preference.Preference
import com.wireguard.android.QuickTileService
import com.wireguard.android.R
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
class QuickTilePreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
override fun getSummary() = context.getString(R.string.quick_settings_tile_add_summary)
override fun getTitle() = context.getString(R.string.quick_settings_tile_add_title)
override fun onClick() {
val statusBarManager = context.getSystemService(StatusBarManager::class.java)
statusBarManager.requestAddTileService(
ComponentName(context, QuickTileService::class.java),
context.getString(R.string.quick_settings_tile_action),
Icon.createWithResource(context, R.drawable.ic_tile),
context.mainExecutor
) {
when (it) {
StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ALREADY_ADDED,
StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ADDED -> {
parent?.removePreference(this)
--preferenceManager.preferenceScreen.initialExpandedChildrenCount
}
StatusBarManager.TILE_ADD_REQUEST_ERROR_MISMATCHED_PACKAGE,
StatusBarManager.TILE_ADD_REQUEST_ERROR_REQUEST_IN_PROGRESS,
StatusBarManager.TILE_ADD_REQUEST_ERROR_BAD_COMPONENT,
StatusBarManager.TILE_ADD_REQUEST_ERROR_NOT_CURRENT_USER,
StatusBarManager.TILE_ADD_REQUEST_ERROR_APP_NOT_IN_FOREGROUND,
StatusBarManager.TILE_ADD_REQUEST_ERROR_NO_STATUS_BAR_SERVICE ->
Toast.makeText(context, context.getString(R.string.quick_settings_tile_add_failure, it), Toast.LENGTH_SHORT).show()
}
}
}
}

View File

@ -185,6 +185,10 @@
<string name="private_key">Private key</string> <string name="private_key">Private key</string>
<string name="public_key">Public key</string> <string name="public_key">Public key</string>
<string name="qr_code_hint">Tip: generate with `qrencode -t ansiutf8 &lt; tunnel.conf`.</string> <string name="qr_code_hint">Tip: generate with `qrencode -t ansiutf8 &lt; tunnel.conf`.</string>
<string name="quick_settings_tile_add_title">Add tile to quick settings panel</string>
<string name="quick_settings_tile_add_summary">The shortcut tile toggles the most recent tunnel</string>
<string name="quick_settings_tile_add_failure">Unable to add shortcut tile: error %d</string>
<string name="quick_settings_tile_action">Toggle tunnel</string>
<string name="restore_on_boot_summary_off">Will not bring up enabled tunnels at boot</string> <string name="restore_on_boot_summary_off">Will not bring up enabled tunnels at boot</string>
<string name="restore_on_boot_summary_on">Will bring up enabled tunnels at boot</string> <string name="restore_on_boot_summary_on">Will bring up enabled tunnels at boot</string>
<string name="restore_on_boot_title">Restore on boot</string> <string name="restore_on_boot_title">Restore on boot</string>

View File

@ -12,6 +12,7 @@
android:summaryOn="@string/restore_on_boot_summary_on" android:summaryOn="@string/restore_on_boot_summary_on"
android:title="@string/restore_on_boot_title" /> android:title="@string/restore_on_boot_title" />
<com.wireguard.android.preference.ZipExporterPreference android:key="zip_exporter" /> <com.wireguard.android.preference.ZipExporterPreference android:key="zip_exporter" />
<com.wireguard.android.preference.QuickTilePreference android:key="quick_tile" />
<Preference <Preference
android:key="log_viewer" android:key="log_viewer"
android:singleLineTitle="false" android:singleLineTitle="false"