Skip to content

fix: make implicit intents safe #159

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

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RiskDetailFragment : Fragment() {
// Intent.ACTION_VIEW,
// Uri.parse("https://support.apple.com/en-us/HT212227")
// )
// startActivity(intent)
// startActivitySafe(intent)
// }

view.findViewById<MaterialCardView>(R.id.card_trackers_found).setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.fragment.app.Fragment
import dagger.hilt.android.AndroidEntryPoint
import de.seemoo.at_tracking_detection.R
import de.seemoo.at_tracking_detection.databinding.FragmentIgnoreBatteryOptimizationBinding
import de.seemoo.at_tracking_detection.util.startActivitySafe

@AndroidEntryPoint
class IgnoreBatteryOptimizationFragment : Fragment() {
Expand Down Expand Up @@ -64,7 +65,7 @@ class IgnoreBatteryOptimizationFragment : Fragment() {
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packageName")
}
requireContext().startActivity(intent)
startActivitySafe(intent)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.seemoo.at_tracking_detection.R
import de.seemoo.at_tracking_detection.util.startActivitySafe

class InformationFragment : Fragment() {

Expand Down Expand Up @@ -84,7 +85,7 @@ class InformationFragment : Fragment() {
Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/AirGuardAndroid")
)
startActivity(intent)
startActivitySafe(intent)
}
}

Expand All @@ -102,7 +103,7 @@ class InformationFragment : Fragment() {

private fun openAttributionLink(link: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
startActivity(intent)
startActivitySafe(intent)
}

private fun getAttributions(): List<AttributionItem> {
Expand All @@ -119,8 +120,7 @@ class InformationFragment : Fragment() {
putExtra(Intent.EXTRA_SUBJECT, "Subject of the email")
putExtra(Intent.EXTRA_TEXT, "Body of the email")
}

startActivity(emailIntent)
startActivitySafe(emailIntent)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import de.seemoo.at_tracking_detection.ATTrackingDetectionApplication
import de.seemoo.at_tracking_detection.R
import de.seemoo.at_tracking_detection.util.SharedPrefs
import de.seemoo.at_tracking_detection.util.Utility
import de.seemoo.at_tracking_detection.util.startActivitySafe
import de.seemoo.at_tracking_detection.worker.BackgroundWorkScheduler
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -52,7 +53,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
Intent.ACTION_VIEW,
Uri.parse("https://tpe.seemoo.tu-darmstadt.de/privacy-policy.html")
)
startActivity(intent)
startActivitySafe(intent)
return@OnPreferenceClickListener true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.seemoo.at_tracking_detection.util

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.fragment.app.Fragment
import de.seemoo.at_tracking_detection.R

inline fun Context.startActivitySafe(intent: Intent, onError: () -> Unit = { showNotAppFound() }) {
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
onError()
}
}

fun Context.showNotAppFound() {
val message = getString(R.string.app_handler_not_found)
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

inline fun Fragment.startActivitySafe(
intent: Intent,
onError: () -> Unit = { context?.showNotAppFound() }
) {
context?.startActivitySafe(intent, onError)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import de.seemoo.at_tracking_detection.ATTrackingDetectionApplication
import de.seemoo.at_tracking_detection.database.models.device.DeviceManager
import de.seemoo.at_tracking_detection.detection.LocationRequester
import de.seemoo.at_tracking_detection.util.Utility
import de.seemoo.at_tracking_detection.util.startActivitySafe
import timber.log.Timber


Expand Down Expand Up @@ -159,6 +160,6 @@ object BLEScanner {
fun openBluetoothSettings(context: Context) {
val intentOpenBluetoothSettings = Intent()
intentOpenBluetoothSettings.action = Settings.ACTION_BLUETOOTH_SETTINGS
context.startActivity(intentOpenBluetoothSettings)
context.startActivitySafe(intentOpenBluetoothSettings)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

<string name="settings_information_contact_title">Information &amp; Contact</string>
<string name="settings_information_contact_summary">Get information about us</string>
<string name="app_handler_not_found">App for opening this content isn\'t found</string>

<string name="settings_delete_study_data_title">Delete study data</string>
<string name="settings_delete_study_data_summary">Request deletion of study data</string>
Expand Down