Skip to content

Commit ae794e8

Browse files
committed
crude dialog for notification permission bug #1414
1 parent a8a213a commit ae794e8

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public final AlertDialog initiateScan(@Nullable Collection<String> desiredBarcod
321321
// set the desired barcode types
322322
StringBuilder joinedByComma = new StringBuilder();
323323
for (String format : desiredBarcodeFormats) {
324-
if (joinedByComma.length() > 0) {
324+
if (!joinedByComma.isEmpty()) {
325325
joinedByComma.append(',');
326326
}
327327
joinedByComma.append(format);
@@ -393,6 +393,7 @@ private AlertDialog showDownloadDialog() {
393393
packageName = BS_PACKAGE;
394394
} else {
395395
// Otherwise, first option:
396+
//noinspection SequencedCollectionMethodCanBeUsed
396397
packageName = targetApplications.get(0);
397398
}
398399
Uri uri = Uri.parse("market://details?id=" + packageName);

app/src/main/java/org/torproject/android/OrbotActivity.kt

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import android.content.IntentFilter
99
import android.content.pm.PackageManager
1010
import android.os.Build
1111
import android.os.Bundle
12+
import android.util.Log
1213
import android.view.View
1314
import android.view.WindowInsetsController
1415
import androidx.activity.addCallback
1516

1617
import androidx.activity.enableEdgeToEdge
1718
import androidx.activity.result.contract.ActivityResultContracts
1819
import androidx.activity.viewModels
20+
import androidx.annotation.RequiresApi
1921
import androidx.biometric.BiometricPrompt
2022
import androidx.core.content.ContextCompat
2123
import androidx.core.view.ViewCompat
@@ -36,6 +38,7 @@ import org.torproject.android.service.util.Prefs
3638
import org.torproject.android.service.util.showToast
3739
import org.torproject.android.ui.more.LogBottomSheet
3840
import org.torproject.android.ui.connect.ConnectViewModel
41+
import org.torproject.android.ui.connect.RequestPostNotificationPermission
3942
import org.torproject.android.ui.core.DeviceAuthenticationPrompt
4043
import java.util.Locale
4144

@@ -186,25 +189,32 @@ class OrbotActivity : BaseActivity() {
186189
rootDetectionShown = true
187190
}
188191

189-
onBackPressedDispatcher.addCallback(this ) {
192+
onBackPressedDispatcher.addCallback(this) {
190193
if (lastSelectedItemId != R.id.connectFragment) {
191194
bottomNavigationView.selectedItemId = R.id.connectFragment
192-
}
193-
else finish()
195+
} else finish()
194196
}
195197
}
196198

197199
private fun requestNotificationPermission() {
198-
when (PackageManager.PERMISSION_GRANTED) {
199-
ContextCompat.checkSelfPermission(
200-
this, Manifest.permission.POST_NOTIFICATIONS
201-
) -> {
202-
// You can use the API that requires the permission.
200+
// automatically granted on Android 12 and lower
201+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
202+
return
203+
val checkPostNotificationPerm =
204+
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
205+
when (checkPostNotificationPerm) {
206+
PackageManager.PERMISSION_GRANTED -> {
207+
Log.d(
208+
"OrbotActivity",
209+
"Granted Permission ${Manifest.permission.POST_NOTIFICATIONS}"
210+
)
203211
}
204212

205213
else -> {
206-
// You can directly ask for the permission.
207-
// The registered ActivityResultCallback gets the result of this request.
214+
Log.d(
215+
"OrbotActivity",
216+
"Try Prompting For ${Manifest.permission.POST_NOTIFICATIONS}"
217+
)
208218
requestPermissionLauncher.launch(
209219
Manifest.permission.POST_NOTIFICATIONS
210220
)
@@ -213,21 +223,16 @@ class OrbotActivity : BaseActivity() {
213223
}
214224

215225
// Register the permissions callback, which handles the user's response to the
216-
// system permissions dialog. Save the return value, an instance of
217-
// ActivityResultLauncher. You can use either a val, as shown in this snippet,
218-
// or a lateinit var in your onAttach() or onCreate() method.
226+
// system permissions dialog.
227+
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
219228
private val requestPermissionLauncher = registerForActivityResult(
220229
ActivityResultContracts.RequestPermission()
221230
) { isGranted: Boolean ->
222231
if (isGranted) {
223-
// Permission is granted. Continue the action or workflow in your
224-
// app.
232+
Log.d("OrbotActivity", "User just granted ${Manifest.permission.POST_NOTIFICATIONS}")
225233
} else {
226-
// Explain to the user that the feature is unavailable because the
227-
// feature requires a permission that the user has denied. At the
228-
// same time, respect the user's decision. Don't link to system
229-
// settings in an effort to convince the user to change their
230-
// decision.
234+
Log.d("OrbotActivity", "Notification denied")
235+
RequestPostNotificationPermission().show(supportFragmentManager, "RequestNotificationDialog")
231236
}
232237
}
233238

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.torproject.android.ui.connect
2+
3+
import android.app.Dialog
4+
import android.content.DialogInterface
5+
import android.content.Intent
6+
import android.os.Build
7+
import android.os.Bundle
8+
import android.provider.Settings
9+
import androidx.annotation.RequiresApi
10+
import androidx.appcompat.app.AlertDialog
11+
import androidx.fragment.app.DialogFragment
12+
import org.torproject.android.R
13+
14+
class RequestPostNotificationPermission : DialogFragment() {
15+
@RequiresApi(Build.VERSION_CODES.O)
16+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
17+
AlertDialog.Builder(requireActivity())
18+
.setTitle(R.string.request_notification_permission)
19+
.setMessage(R.string.request_notification_permission_msg)
20+
.setCancelable(false)
21+
.setPositiveButton(
22+
R.string.open_settings
23+
) { dialog: DialogInterface?, which: Int ->
24+
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
25+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
26+
.putExtra(Settings.EXTRA_APP_PACKAGE, requireActivity().packageName)
27+
28+
startActivity(intent)
29+
dismiss()
30+
}
31+
.create()
32+
}

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@
239239
<string name="power_user_mode_permission">Alarms &amp; Reminders Permission Needed for Power User Mode</string>
240240
<string name="power_user_mode_permission_msg">Please allow Orbot to set alarms and reminders in order to use power user mode. This won\'t actually set any alarms on your device. This permission is needed to have the system keep OrbotService running when Orbot is not being used as a system-wide VPN.</string>
241241

242+
<string name="request_notification_permission">Please enable notifications for Orbot</string>
243+
<string name="request_notification_permission_msg">Without turning on notifications Orbot can\'t act as a Tor VPN or use Kindness Mode.\n\nWhen enabled, we only show a single notification when Orbot is running.</string>
244+
<string name="open_settings">Open Settings</string>
245+
242246
<string name="pref_require_password">Orbot Authentication</string>
243247
<string name="pref_require_password_description">Prevent access to Orbot until you authenticate by entering your device password or fingerprint. This can be combined with Camouflage Mode to help hide that Orbot is installed on your device</string>
244248
<string name="error_no_password_set">Please secure your device in order to use Orbot Authentication </string>

0 commit comments

Comments
 (0)