-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
681e6ce
commit d34aa30
Showing
10 changed files
with
260 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<shortcut | ||
android:shortcutId="switch_mode" | ||
android:enabled="true" | ||
android:icon="@drawable/ic_outline_next_24dp" | ||
android:shortcutShortLabel="@string/shortcut_switch_mode"> | ||
<intent | ||
android:action="android.intent.action.VIEW" | ||
android:targetPackage="ru.karasevm.privatednstoggle.dev" | ||
android:targetClass="ru.karasevm.privatednstoggle.ui.SettingsDialogActivity" | ||
android:data="myapp://switch_mode"/> | ||
</shortcut> | ||
<shortcut | ||
android:shortcutId="toggle_mode" | ||
android:enabled="true" | ||
android:icon="@drawable/ic_play_pause_24px" | ||
android:shortcutShortLabel="@string/shortcut_toggle_on_off"> | ||
<intent | ||
android:action="android.intent.action.VIEW" | ||
android:targetPackage="ru.karasevm.privatednstoggle.dev" | ||
android:targetClass="ru.karasevm.privatednstoggle.ui.SettingsDialogActivity" | ||
android:data="myapp://toggle_mode"/> | ||
</shortcut> | ||
</shortcuts> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/src/main/java/ru/karasevm/privatednstoggle/service/ShortcutService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ru.karasevm.privatednstoggle.service | ||
|
||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.IBinder | ||
import android.util.Log | ||
import android.widget.Toast | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
import ru.karasevm.privatednstoggle.PrivateDNSApp | ||
import ru.karasevm.privatednstoggle.data.DnsServerRepository | ||
import ru.karasevm.privatednstoggle.util.PreferenceHelper | ||
import ru.karasevm.privatednstoggle.util.PrivateDNSUtils | ||
import ru.karasevm.privatednstoggle.util.PrivateDNSUtils.DNS_MODE_AUTO | ||
import ru.karasevm.privatednstoggle.util.PrivateDNSUtils.DNS_MODE_OFF | ||
import ru.karasevm.privatednstoggle.util.PrivateDNSUtils.DNS_MODE_PRIVATE | ||
import ru.karasevm.privatednstoggle.util.PrivateDNSUtils.checkForPermission | ||
|
||
class ShortcutService : Service() { | ||
|
||
private val repository: DnsServerRepository by lazy { (application as PrivateDNSApp).repository } | ||
private val job = SupervisorJob() | ||
private val scope = CoroutineScope(Dispatchers.IO + job) | ||
|
||
companion object { | ||
private const val ACTION_SWITCH_MODE = "myapp://switch_mode" | ||
private const val ACTION_TOGGLE_MODE = "myapp://toggle_mode" | ||
} | ||
|
||
private fun setDnsModeAndShowToast(dnsMode: String) { | ||
PrivateDNSUtils.setPrivateMode(contentResolver, dnsMode) | ||
val text = when (dnsMode) { | ||
DNS_MODE_OFF -> "DNS set to Off" | ||
DNS_MODE_AUTO -> "DNS set to Auto" | ||
DNS_MODE_PRIVATE -> "DNS set to Private Provider" | ||
else -> "Unknown" | ||
} | ||
scope.launch { | ||
launch(Dispatchers.Main) { | ||
Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
val data = intent?.data.toString() | ||
val sharedPrefs = PreferenceHelper.defaultPreference(this) | ||
if(checkForPermission(this)) { | ||
if (data == ACTION_SWITCH_MODE) { | ||
PrivateDNSUtils.getNextProvider( | ||
sharedPrefs, | ||
scope, | ||
repository, | ||
contentResolver, | ||
skipProvider = true, | ||
onNext = { dnsMode, _ -> | ||
setDnsModeAndShowToast(dnsMode) | ||
}) | ||
} else if(data == ACTION_TOGGLE_MODE) { | ||
val dnsMode = PrivateDNSUtils.getPrivateMode(contentResolver) | ||
when (dnsMode) { | ||
DNS_MODE_OFF -> { | ||
PrivateDNSUtils.getNextProvider( | ||
sharedPrefs, | ||
scope, | ||
repository, | ||
contentResolver, | ||
skipProvider = true, | ||
onNext = { dnsMode, _ -> | ||
setDnsModeAndShowToast(dnsMode) | ||
}) | ||
} | ||
else -> { | ||
PrivateDNSUtils.setPrivateMode(contentResolver, DNS_MODE_OFF) | ||
} | ||
} | ||
} | ||
} | ||
return START_NOT_STICKY | ||
} | ||
|
||
override fun onBind(intent: Intent): IBinder? { | ||
return null | ||
} | ||
} |
15 changes: 13 additions & 2 deletions
15
app/src/main/java/ru/karasevm/privatednstoggle/ui/SettingsDialogActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
package ru.karasevm.privatednstoggle.ui | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import ru.karasevm.privatednstoggle.service.ShortcutService | ||
|
||
class SettingsDialogActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val newFragment = DNSServerDialogFragment() | ||
newFragment.show(supportFragmentManager, DNSServerDialogFragment.TAG) | ||
|
||
if (intent != null && intent.data != null) { | ||
// Start the service when the shortcut is clicked | ||
val serviceIntent = Intent(this, ShortcutService::class.java) | ||
serviceIntent.data = intent.data | ||
startService(serviceIntent) | ||
finish() | ||
} else { | ||
val newFragment = DNSServerDialogFragment() | ||
newFragment.show(supportFragmentManager, DNSServerDialogFragment.TAG) | ||
} | ||
} | ||
} |
Oops, something went wrong.