Skip to content

Commit

Permalink
add shortcut toggle and refactoring quicktile
Browse files Browse the repository at this point in the history
  • Loading branch information
romanvht committed Feb 7, 2025
1 parent 94aa59a commit ccc20bd
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
//noinspection OldTargetApi
targetSdk = 34
versionCode = 10
versionName = "1.4.5"
versionName = "1.4.6"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.ByeDPI"
tools:targetApi="34">

<activity
android:name=".activities.MainActivity"
android:exported="true">
Expand All @@ -37,6 +38,19 @@
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>

<activity
android:name=".activities.ToggleActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true">
<intent-filter>
<action android:name="io.github.romanvht.byedpi.TOGGLE_SERVICE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.dovecoteescapee.byedpi.activities

import android.net.VpnService
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import io.github.dovecoteescapee.byedpi.data.AppStatus
import io.github.dovecoteescapee.byedpi.data.Mode
import io.github.dovecoteescapee.byedpi.services.ServiceManager
import io.github.dovecoteescapee.byedpi.services.appStatus
import io.github.dovecoteescapee.byedpi.utility.getPreferences
import io.github.dovecoteescapee.byedpi.utility.mode

class ToggleActivity : AppCompatActivity() {

companion object {
private const val TAG = "ToggleServiceActivity"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
toggleService()
finish()
}

private fun toggleService() {
val (status) = appStatus
when (status) {
AppStatus.Halted -> {
val mode = getPreferences().mode()

if (mode == Mode.VPN && VpnService.prepare(this) != null) {
return
}

ServiceManager.start(this, mode)
Log.i(TAG, "Toggle start")
}
AppStatus.Running -> {
ServiceManager.stop(this)
Log.i(TAG, "Toggle stop")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package io.github.dovecoteescapee.byedpi.services

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.VpnService
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import io.github.dovecoteescapee.byedpi.R
import io.github.dovecoteescapee.byedpi.data.*
import io.github.dovecoteescapee.byedpi.utility.getPreferences
import io.github.dovecoteescapee.byedpi.utility.mode
Expand All @@ -21,78 +16,25 @@ import io.github.dovecoteescapee.byedpi.utility.mode
class QuickTileService : TileService() {

companion object {
private val TAG: String = QuickTileService::class.java.simpleName
}

private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val senderOrd = intent.getIntExtra(SENDER, -1)
val sender = Sender.entries.getOrNull(senderOrd)
if (sender == null) {
Log.w(TAG, "Received intent with unknown sender: $senderOrd")
return
}
private const val TAG = "QuickTileService"

when (val action = intent.action) {
STARTED_BROADCAST,
STOPPED_BROADCAST -> updateStatus()

FAILED_BROADCAST -> {
Toast.makeText(
context,
getString(R.string.failed_to_start, sender.name),
Toast.LENGTH_SHORT,
).show()
updateStatus()
}

else -> Log.w(TAG, "Unknown action: $action")
}
fun updateTile(context: Context) {
requestListeningState(context, ComponentName(context, QuickTileService::class.java))
}
}

override fun onStartListening() {
super.onStartListening()
updateStatus()
ContextCompat.registerReceiver(
this,
receiver,
IntentFilter().apply {
addAction(STARTED_BROADCAST)
addAction(STOPPED_BROADCAST)
addAction(FAILED_BROADCAST)
},
ContextCompat.RECEIVER_EXPORTED,
)
}

override fun onStopListening() {
unregisterReceiver(receiver)
}

override fun onClick() {
if (qsTile.state == Tile.STATE_UNAVAILABLE) {
return
}

unlockAndRun(this::handleClick)
}

private fun setState(newState: Int) {
qsTile.apply {
state = newState
updateTile()
}
}
if (qsTile.state == Tile.STATE_UNAVAILABLE) return

private fun updateStatus() {
val (status) = appStatus
setState(if (status == AppStatus.Halted) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE)
unlockAndRun { handleClick() }
}

private fun handleClick() {
setState(Tile.STATE_ACTIVE)
setState(Tile.STATE_UNAVAILABLE)

val (status) = appStatus
when (status) {
AppStatus.Halted -> {
Expand All @@ -103,9 +45,32 @@ class QuickTileService : TileService() {
}

ServiceManager.start(this, mode)
setState(Tile.STATE_ACTIVE)
}
AppStatus.Running -> {
ServiceManager.stop(this)
setState(Tile.STATE_INACTIVE)
}
}

Log.i(TAG, "Toggle tile")
updateTile(this)
}

private fun updateStatus() {
val (status) = appStatus

if (status == AppStatus.Running) {
setState(Tile.STATE_ACTIVE)
} else {
setState(Tile.STATE_INACTIVE)
}
}

AppStatus.Running -> ServiceManager.stop(this)
private fun setState(newState: Int) {
qsTile.apply {
state = newState
updateTile()
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_toggle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>

</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<string name="byedpi_docs">https://github.com/hufrea/byedpi/blob/main/README.md</string>
<string name="app_name">ByeByeDPI</string>
<string name="cmd_documentation">Documentation</string>
<string name="toggle_connect">On/Off</string>
<string name="vpn_connect">Connect</string>
<string name="vpn_disconnect">Disconnect</string>
<string name="vpn_connected">Connected (VPN)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<string name="byedpi_docs">https://github.com/hufrea/byedpi/blob/main/README.md</string>
<string name="app_name">ByeByeDPI</string>
<string name="cmd_documentation">Документация ByeDPI</string>
<string name="toggle_connect">Вкл/Выкл</string>
<string name="vpn_connect">Подключить</string>
<string name="vpn_disconnect">Отключить</string>
<string name="vpn_connected">Подключено (VPN)</string>
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/xml/shortcuts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="toggle_connect"
android:enabled="true"
android:icon="@drawable/ic_toggle"
android:shortcutShortLabel="@string/toggle_connect"
android:shortcutLongLabel="@string/toggle_connect"
tools:ignore="UnusedAttribute">
<intent
android:action="io.github.romanvht.byedpi.TOGGLE_SERVICE" />
</shortcut>
</shortcuts>

0 comments on commit ccc20bd

Please sign in to comment.