Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use alternate method for quick-tile #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions ui/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission
android:name="android.permission.SYSTEM_ALERT_WINDOW"
android:minSdkVersion="34" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
Expand Down
27 changes: 18 additions & 9 deletions ui/src/main/java/com/wireguard/android/QuickTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.wireguard.android

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
Expand All @@ -21,6 +22,7 @@ import androidx.databinding.Observable
import androidx.databinding.Observable.OnPropertyChangedCallback
import com.wireguard.android.activity.MainActivity
import com.wireguard.android.activity.TunnelToggleActivity
import com.wireguard.android.activity.TunnelToggleActivity.Companion.SHOW_PROGRESS
import com.wireguard.android.backend.Tunnel
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.util.applicationScope
Expand Down Expand Up @@ -52,6 +54,7 @@ class QuickTileService : TileService() {
}

override fun onClick() {
updateTile()
when (val tunnel = tunnel) {
null -> {
val intent = Intent(this, MainActivity::class.java)
Expand All @@ -70,15 +73,15 @@ class QuickTileService : TileService() {
tunnel.setStateAsync(Tunnel.State.TOGGLE)
updateTile()
} catch (_: Throwable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && !Settings.canDrawOverlays(this@QuickTileService)) {
val permissionIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
permissionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, permissionIntent, PendingIntent.FLAG_IMMUTABLE))
return@launch
val intent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(SHOW_PROGRESS, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(PendingIntent.getActivity(this@QuickTileService, 0, intent, PendingIntent.FLAG_IMMUTABLE))
} else {
@Suppress("DEPRECATION")
startActivity(intent)
}
val toggleIntent = Intent(this@QuickTileService, TunnelToggleActivity::class.java)
toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(toggleIntent)
}
}
}
Expand Down Expand Up @@ -133,7 +136,7 @@ class QuickTileService : TileService() {
isAdded = false
}

private fun updateTile() {
private fun updateTile(isConnecting: Boolean = false) {
// Update the tunnel.
val newTunnel = Application.getTunnelManager().lastUsedTunnel
if (newTunnel != tunnel) {
Expand All @@ -148,6 +151,12 @@ class QuickTileService : TileService() {
null -> {
tile.label = getString(R.string.app_name)
tile.state = Tile.STATE_INACTIVE
if(isConnecting) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
tile.subtitle = getString(R.string.quick_settings_tile_connecting)
tile.state = Tile.STATE_ACTIVE
}
}
tile.icon = iconOff
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.wireguard.android.activity
import android.content.ComponentName
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.service.quicksettings.TileService
import android.util.Log
import android.widget.Toast
Expand All @@ -24,6 +25,9 @@ import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.N)
class TunnelToggleActivity : AppCompatActivity() {

private var mIsVisible = false

private val permissionActivityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { toggleTunnelWithPermissionsResult() }

Expand All @@ -38,16 +42,23 @@ class TunnelToggleActivity : AppCompatActivity() {
val message = getString(R.string.toggle_error, error)
Log.e(TAG, message, e)
Toast.makeText(this@TunnelToggleActivity, message, Toast.LENGTH_LONG).show()
finishAffinity()
exitActivity()
return@launch
}
TileService.requestListeningState(this@TunnelToggleActivity, ComponentName(this@TunnelToggleActivity, QuickTileService::class.java))
finishAffinity()
exitActivity()
}
}

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

if(intent.getBooleanExtra(SHOW_PROGRESS, false)) {
mIsVisible = true
title = "" // Otherwise the apptitle will be shown above the spinner.
setContentView(R.layout.loading_activity)
}

lifecycleScope.launch {
if (Application.getBackend() is GoBackend) {
val intent = GoBackend.VpnService.prepare(this@TunnelToggleActivity)
Expand All @@ -58,9 +69,29 @@ class TunnelToggleActivity : AppCompatActivity() {
}
toggleTunnelWithPermissionsResult()
}
exitActivity()
}

private fun exitActivity() {
/*
We add this delay, so that the user gets the impression we are actually doing something.
This is to make the transition of the closing quicktile-menu more palatable,
because startActivityAndCollapse() will immediately close that menu.
This can be jarring, so we show this placeholder spinner and close it after a second.
*/

if(mIsVisible) {
Handler().postDelayed({
finishAffinity()
}, 1000L)
} else {
finishAffinity()
}

}

companion object {
private const val TAG = "WireGuard/TunnelToggleActivity"
const val SHOW_PROGRESS = "ShowLoadingbar"
}
}
44 changes: 44 additions & 0 deletions ui/src/main/res/layout/loading_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:focusable="true"
app:contentPadding="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Preparing Tunnel..." />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1 change: 1 addition & 0 deletions ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,5 @@
<string name="biometric_prompt_private_key_title">Authenticate to view private key</string>
<string name="biometric_auth_error">Authentication failure</string>
<string name="biometric_auth_error_reason">Authentication failure: %s</string>
<string name="quick_settings_tile_connecting">Connecting…</string>
</resources>
5 changes: 4 additions & 1 deletion ui/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
<item name="colorPrimaryDark">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
<item name="android:windowIsFloating">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>

</style>

<style name="TvTheme" parent="AppTheme">
Expand Down