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

Request POST_NOTIFICATIONS permission #51

Open
wants to merge 3 commits into
base: main
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
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "eu.neilalexander.yggdrasil"
minSdkVersion 21
targetSdkVersion 33
versionCode 15
versionName "0.1-015"
versionCode 16
versionName "0.1-016"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'com.guolindev.permissionx:permissionx:1.7.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/eu/neilalexander/yggdrasil/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package eu.neilalexander.yggdrasil

import android.Manifest
import android.app.Activity
import android.content.*
import android.graphics.Color
import android.net.VpnService
import android.os.Build
import android.os.Bundle
import android.widget.Switch
import android.widget.TextView
Expand All @@ -14,6 +16,7 @@ import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.edit
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import com.permissionx.guolindev.PermissionX
import eu.neilalexander.yggdrasil.PacketTunnelProvider.Companion.STATE_INTENT
import mobile.Mobile
import org.json.JSONArray
Expand All @@ -37,6 +40,38 @@ class MainActivity : AppCompatActivity() {
startService(intent)
}

private fun checkNotificationPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return
}

PermissionX.init(this)
.permissions(
Manifest.permission.POST_NOTIFICATIONS,
)
.onExplainRequestReason { scope, deniedList ->
scope.showRequestReasonDialog(
deniedList,
getString(R.string.explain_ntfn_perms),
getString(R.string.ok),
getString(R.string.cancel),
)
}
.onForwardToSettings { scope, deniedList ->
scope.showForwardToSettingsDialog(
deniedList,
getString(R.string.manual_ntfn_perms),
getString(R.string.ok),
getString(R.string.cancel),
)
}
.request { allGranted, _, _ ->
if (!allGranted) {
Toast.makeText(this, R.string.ntfn_denied, Toast.LENGTH_LONG).show()
}
}
}

private var startVpnActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
start()
Expand Down Expand Up @@ -65,6 +100,7 @@ class MainActivity : AppCompatActivity() {
enabledSwitch.setOnCheckedChangeListener { _, isChecked ->
when (isChecked) {
true -> {
checkNotificationPermission()
val vpnIntent = VpnService.prepare(this)
if (vpnIntent != null) {
startVpnActivity.launch(vpnIntent)
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<string name="settings_reset">Сброс</string>
<string name="main_status">Состояние</string>
<string name="main_enable_yggdrasil">Включить Yggdrasil</string>
<string name="explain_ntfn_perms">Пожалуйста включите нотификации чтобы видеть статус подключения Yggdrasil</string>
<string name="manual_ntfn_perms">Чтобы видеть статус подключения Yggdrasil, включите нотификации в настройках</string>
<string name="ntfn_denied">Нотификация статуса подключения Yggdrasil не будет показана</string>
<string name="main_statistics">Статистика</string>
<string name="main_not_available">Н/Д</string>
<string name="main_ip">Адрес</string>
Expand Down Expand Up @@ -83,4 +86,4 @@
<string name="private_key_label">Приватный ключ:</string>
<string name="set_keys">Установить свой ключ</string>
<string name="save">Сохранить</string>
</resources>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<string name="settings_reset">Reset</string>
<string name="main_status">Status</string>
<string name="main_enable_yggdrasil">Enable Yggdrasil</string>
<string name="explain_ntfn_perms">Please enable notifications to easily see Yggdrasil connection status</string>
<string name="manual_ntfn_perms">To see Yggdrasil connection status, enable notifications in app settings</string>
<string name="ntfn_denied">Yggdrasil connection status notification will not be shown</string>
<string name="main_statistics">Statistics</string>
<string name="main_not_available">N/A</string>
<string name="main_ip">IP</string>
Expand Down Expand Up @@ -83,4 +86,4 @@
<string name="private_key_label">Private key:</string>
<string name="set_keys">Set your own key</string>
<string name="save">Save</string>
</resources>
</resources>