Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Revert to standard broadcast receiver for Tasker integration
Browse files Browse the repository at this point in the history
Some devices are finicky with services and end up breaking the feature altogether.

Fixes #148

Revert "Application: Restart tasker service from background below Oreo"

This reverts commit 209e87b.

Revert "Application: Don't try to start service from background"

This reverts commit d533be7.

Revert "Persist service state across application restarts"

This reverts commit 3a41fd3.

Revert "Convert Tasker receiver to a service"

This reverts commit f159fad.

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
  • Loading branch information
msfjarvis committed Jul 21, 2019
1 parent a5e4e72 commit bc297d7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 131 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@
</intent-filter>
</receiver>

<service android:name=".services.TaskerIntegrationService" />
<receiver android:name=".services.TaskerIntegrationReceiver">
<intent-filter>
<action android:name="${applicationId}.SET_TUNNEL_UP" />
<action android:name="${applicationId}.SET_TUNNEL_DOWN" />
</intent-filter>
</receiver>

<service
android:name=".backend.GoBackend$VpnService"
Expand Down
22 changes: 2 additions & 20 deletions app/src/main/java/com/wireguard/android/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
*/
package com.wireguard.android

import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.os.Build
import android.os.StrictMode
import androidx.annotation.RequiresApi
Expand All @@ -19,10 +15,9 @@ import com.wireguard.android.di.backendAsyncModule
import com.wireguard.android.di.backendModule
import com.wireguard.android.di.configStoreModule
import com.wireguard.android.di.earlyInitModules
import com.wireguard.android.di.ext.injectPrefs
import com.wireguard.android.di.ext.getPrefs
import com.wireguard.android.di.toolsInstallerModule
import com.wireguard.android.model.TunnelManager
import com.wireguard.android.services.TaskerIntegrationService
import com.wireguard.android.util.updateAppTheme
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
Expand All @@ -31,8 +26,6 @@ import java.lang.ref.WeakReference

class Application : android.app.Application() {

val prefs by injectPrefs()

init {
weakSelf = WeakReference(this)
}
Expand Down Expand Up @@ -80,21 +73,10 @@ class Application : android.app.Application() {
)
}

updateAppTheme(prefs.useDarkTheme)
updateAppTheme(getPrefs().useDarkTheme)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createNotificationChannel()

if (prefs.allowTaskerIntegration && canStartService()) {
startService(Intent(this, TaskerIntegrationService::class.java))
}
}

private fun canStartService(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return true
val appProcessInfo = ActivityManager.RunningAppProcessInfo()
ActivityManager.getMyMemoryState(appProcessInfo)
return (appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.wireguard.android.di.ext.getBackendAsync
import com.wireguard.android.di.ext.getPrefs
import com.wireguard.android.di.ext.getTunnelManager
import com.wireguard.android.fragment.AppListDialogFragment
import com.wireguard.android.services.TaskerIntegrationService
import com.wireguard.android.util.ExceptionLoggers
import com.wireguard.android.util.ZipExporter
import com.wireguard.android.util.asString
Expand Down Expand Up @@ -68,7 +67,6 @@ class SettingsActivity : AppCompatActivity() {
}

class SettingsFragment : PreferenceFragmentCompat(), AppListDialogFragment.AppExclusionListener {

private val prefs = getPrefs()

override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) {
Expand Down Expand Up @@ -127,10 +125,7 @@ class SettingsActivity : AppCompatActivity() {
}

taskerPref?.onPreferenceChangeListener = ChangeListener { _, newValue ->
val isEnabled = newValue as Boolean
integrationSecretPref?.isVisible = isEnabled
val intent = Intent(ctx, TaskerIntegrationService::class.java)
ctx.apply { if (isEnabled) startService(intent) else stopService(intent) }
integrationSecretPref?.isVisible = (newValue as Boolean)
true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright © 2017-2019 WireGuard LLC.
* Copyright © 2018-2019 Harsh Shandilya <msfjarvis@gmail.com>. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.services

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.wireguard.android.BuildConfig
import com.wireguard.android.di.ext.getPrefs
import com.wireguard.android.di.ext.getTunnelManager
import com.wireguard.android.model.Tunnel
import com.wireguard.android.model.TunnelManager
import org.koin.core.KoinComponent
import timber.log.Timber

class TaskerIntegrationReceiver : BroadcastReceiver(), KoinComponent {
val manager = getTunnelManager()
val prefs = getPrefs()

override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null || intent.action == null)
return

val isSelfPackage = intent.`package` == BuildConfig.APPLICATION_ID
val taskerEnabled = !prefs.allowTaskerIntegration || prefs.taskerIntegrationSecret.isEmpty()
val tunnelName: String? = intent.getStringExtra(TunnelManager.TUNNEL_NAME_INTENT_EXTRA)
val integrationSecret: String? = intent.getStringExtra(TunnelManager.INTENT_INTEGRATION_SECRET_EXTRA)

var state: Tunnel.State? = null
Timber.tag("IntentReceiver")
when (intent.action) {
"${BuildConfig.APPLICATION_ID}.SET_TUNNEL_UP" -> {
state = Tunnel.State.UP
}
"${BuildConfig.APPLICATION_ID}.SET_TUNNEL_DOWN" -> {
state = Tunnel.State.DOWN
}
else -> Timber.d("Invalid intent action: ${intent.action}")
}

if (taskerEnabled && !isSelfPackage) {
Timber.e("Tasker integration is disabled! Not allowing tunnel state change to pass through.")
return
}

if (tunnelName != null && state != null) {
if (isSelfPackage) {
toggleTunnelState(tunnelName, state, manager)
return
}
when (integrationSecret) {
prefs.taskerIntegrationSecret -> toggleTunnelState(tunnelName, state, manager)
else -> Timber.e("Intent integration secret mis-match! Exiting...")
}
} else if (tunnelName == null) {
Timber.d("Intent parameter ${TunnelManager.TUNNEL_NAME_INTENT_EXTRA} not set!")
}
}

private fun toggleTunnelState(tunnelName: String, state: Tunnel.State, manager: TunnelManager) {
Timber.d("Setting $tunnelName's state to $state")
manager.getTunnels().thenAccept { tunnels ->
val tunnel = tunnels[tunnelName]
tunnel?.let {
manager.setTunnelState(it, state)
}
}
}
}

This file was deleted.

0 comments on commit bc297d7

Please sign in to comment.