Skip to content

Commit c459cda

Browse files
committed
improving some intent handling for Android 11+
1 parent b5ed794 commit c459cda

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ android {
5656
}
5757

5858
dependencies {
59-
implementation 'com.github.SimpleMobileTools:Simple-Commons:5cec51606a'
59+
implementation 'com.github.SimpleMobileTools:Simple-Commons:8084f88f20'
6060
implementation 'joda-time:joda-time:2.10.3'
6161
implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5'
6262
implementation 'com.github.tibbi:IndicatorFastScroll:c3de1d040a'

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.simplemobiletools.contacts.pro.activities
22

33
import android.app.Activity
4+
import android.content.ActivityNotFoundException
45
import android.content.ClipData
56
import android.content.ContentValues
67
import android.content.Intent
@@ -275,11 +276,7 @@ class EditContactActivity : ContactActivity() {
275276
Intent().apply {
276277
action = Intent.ACTION_EDIT
277278
data = getContactPublicUri(contact!!)
278-
if (resolveActivity(packageManager) != null) {
279-
startActivity(this)
280-
} else {
281-
toast(R.string.no_app_found)
282-
}
279+
launchActivityIntent(this)
283280
}
284281
}
285282

@@ -320,10 +317,13 @@ class EditContactActivity : ContactActivity() {
320317
putExtra("scaleUpIfNeeded", "true")
321318
clipData = ClipData("Attachment", arrayOf("text/primaryUri-list"), ClipData.Item(lastPhotoIntentUri))
322319
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
323-
if (resolveActivity(packageManager) != null) {
320+
321+
try {
324322
startActivityForResult(this, INTENT_CROP_PHOTO)
325-
} else {
323+
} catch (e: ActivityNotFoundException) {
326324
toast(R.string.no_app_found)
325+
} catch (e: Exception) {
326+
showErrorToast(e)
327327
}
328328
}
329329
}
@@ -494,9 +494,9 @@ class EditContactActivity : ContactActivity() {
494494
private fun setupRingtone() {
495495
contact_ringtone.setOnClickListener {
496496
val ringtonePickerIntent = getRingtonePickerIntent()
497-
if (ringtonePickerIntent.resolveActivity(packageManager) != null) {
497+
try {
498498
startActivityForResult(ringtonePickerIntent, INTENT_SELECT_RINGTONE)
499-
} else {
499+
} catch (e: Exception) {
500500
val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri
501501
SelectAlarmSoundDialog(this, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true,
502502
onAlarmPicked = {
@@ -1227,10 +1227,13 @@ class EditContactActivity : ContactActivity() {
12271227
lastPhotoIntentUri = uri
12281228
Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
12291229
putExtra(MediaStore.EXTRA_OUTPUT, uri)
1230-
if (resolveActivity(packageManager) != null) {
1230+
1231+
try {
12311232
startActivityForResult(this, INTENT_TAKE_PHOTO)
1232-
} else {
1233+
} catch (e: ActivityNotFoundException) {
12331234
toast(R.string.no_app_found)
1235+
} catch (e: Exception) {
1236+
showErrorToast(e)
12341237
}
12351238
}
12361239
}
@@ -1243,10 +1246,13 @@ class EditContactActivity : ContactActivity() {
12431246
clipData = ClipData("Attachment", arrayOf("text/uri-list"), ClipData.Item(uri))
12441247
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
12451248
putExtra(MediaStore.EXTRA_OUTPUT, uri)
1246-
if (resolveActivity(packageManager) != null) {
1249+
1250+
try {
12471251
startActivityForResult(this, INTENT_CHOOSE_PHOTO)
1248-
} else {
1252+
} catch (e: ActivityNotFoundException) {
12491253
toast(R.string.no_app_found)
1254+
} catch (e: Exception) {
1255+
showErrorToast(e)
12501256
}
12511257
}
12521258
}

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.pro.activities
22

33
import android.app.Activity
44
import android.app.SearchManager
5+
import android.content.ActivityNotFoundException
56
import android.content.Context
67
import android.content.Intent
78
import android.graphics.drawable.ColorDrawable
@@ -269,10 +270,12 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
269270
putExtra(KEY_EMAIL, email)
270271
}
271272

272-
if (resolveActivity(packageManager) != null) {
273+
try {
273274
startActivityForResult(this, START_INSERT_ACTIVITY)
274-
} else {
275+
} catch (e: ActivityNotFoundException) {
275276
toast(R.string.no_app_found)
277+
} catch (e: Exception) {
278+
showErrorToast(e)
276279
}
277280
}
278281
}

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,9 @@ class ViewContactActivity : ContactActivity() {
595595

596596
contact_ringtone.setOnClickListener {
597597
val ringtonePickerIntent = getRingtonePickerIntent()
598-
if (ringtonePickerIntent.resolveActivity(packageManager) != null) {
598+
try {
599599
startActivityForResult(ringtonePickerIntent, INTENT_SELECT_RINGTONE)
600-
} else {
600+
} catch (e: Exception) {
601601
val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri
602602
SelectAlarmSoundDialog(this@ViewContactActivity, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true,
603603
onAlarmPicked = {

app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Activity.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ fun SimpleActivity.startCallIntent(recipient: String) {
2121
val action = if (it) Intent.ACTION_CALL else Intent.ACTION_DIAL
2222
Intent(action).apply {
2323
data = Uri.fromParts("tel", recipient, null)
24-
25-
if (resolveActivity(packageManager) != null) {
26-
startActivity(this)
27-
} else {
28-
toast(R.string.no_app_found)
29-
}
24+
launchActivityIntent(this)
3025
}
3126
}
3227
}

app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ fun Context.editContact(contact: Contact) {
5858
fun Context.sendEmailIntent(recipient: String) {
5959
Intent(Intent.ACTION_SENDTO).apply {
6060
data = Uri.fromParts(KEY_MAILTO, recipient, null)
61-
if (resolveActivity(packageManager) != null) {
62-
startActivity(this)
63-
} else {
64-
toast(R.string.no_app_found)
65-
}
61+
launchActivityIntent(this)
6662
}
6763
}
6864

@@ -71,11 +67,7 @@ fun Context.sendAddressIntent(address: String) {
7167
val uri = Uri.parse("geo:0,0?q=$location")
7268

7369
Intent(Intent.ACTION_VIEW, uri).apply {
74-
if (resolveActivity(packageManager) != null) {
75-
startActivity(this)
76-
} else {
77-
toast(R.string.no_app_found)
78-
}
70+
launchActivityIntent(this)
7971
}
8072
}
8173

@@ -88,11 +80,7 @@ fun Context.openWebsiteIntent(url: String) {
8880

8981
Intent(Intent.ACTION_VIEW).apply {
9082
data = Uri.parse(website)
91-
if (resolveActivity(packageManager) != null) {
92-
startActivity(this)
93-
} else {
94-
toast(R.string.no_app_found)
95-
}
83+
launchActivityIntent(this)
9684
}
9785
}
9886

@@ -235,11 +223,7 @@ fun Context.sendSMSToContacts(contacts: ArrayList<Contact>) {
235223

236224
val uriString = "smsto:${numbers.toString().trimEnd(';')}"
237225
Intent(Intent.ACTION_SENDTO, Uri.parse(uriString)).apply {
238-
if (resolveActivity(packageManager) != null) {
239-
startActivity(this)
240-
} else {
241-
toast(R.string.no_app_found)
242-
}
226+
launchActivityIntent(this)
243227
}
244228
}
245229

@@ -256,11 +240,7 @@ fun Context.sendEmailToContacts(contacts: ArrayList<Contact>) {
256240
Intent(Intent.ACTION_SEND_MULTIPLE).apply {
257241
type = "message/rfc822"
258242
putExtra(Intent.EXTRA_EMAIL, emails.toTypedArray())
259-
if (resolveActivity(packageManager) != null) {
260-
startActivity(this)
261-
} else {
262-
toast(R.string.no_app_found)
263-
}
243+
launchActivityIntent(this)
264244
}
265245
}
266246

0 commit comments

Comments
 (0)