Skip to content

Commit 5efe1f4

Browse files
authored
Merge pull request element-hq#4841 from vector-im/feature/bma/safe_start_activity
Small cleanup
2 parents 24c7064 + 74af485 commit 5efe1f4

File tree

3 files changed

+30
-155
lines changed

3 files changed

+30
-155
lines changed

vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt

Lines changed: 22 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ fun openUrlInExternalBrowser(context: Context, uri: Uri?) {
8080
putExtra(Browser.EXTRA_CREATE_NEW_TAB, true)
8181
}
8282

83-
try {
84-
context.startActivity(browserIntent)
85-
} catch (activityNotFoundException: ActivityNotFoundException) {
86-
context.toast(R.string.error_no_external_application_found)
87-
}
83+
context.safeStartActivity(browserIntent)
8884
}
8985
}
9086

@@ -123,22 +119,6 @@ fun openUrlInChromeCustomTab(context: Context,
123119
}
124120
}
125121

126-
/**
127-
* Open sound recorder external application
128-
*/
129-
fun openSoundRecorder(activity: Activity, requestCode: Int) {
130-
val recordSoundIntent = Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION)
131-
132-
// Create chooser
133-
val chooserIntent = Intent.createChooser(recordSoundIntent, activity.getString(R.string.go_on_with))
134-
135-
try {
136-
activity.startActivityForResult(chooserIntent, requestCode)
137-
} catch (activityNotFoundException: ActivityNotFoundException) {
138-
activity.toast(R.string.error_no_external_application_found)
139-
}
140-
}
141-
142122
/**
143123
* Open file selection activity
144124
*/
@@ -153,96 +133,14 @@ fun openFileSelection(activity: Activity,
153133
fileIntent.type = MimeTypes.Any
154134

155135
try {
156-
activityResultLauncher
157-
?.launch(fileIntent)
158-
?: run {
159-
activity.startActivityForResult(fileIntent, requestCode)
160-
}
161-
} catch (activityNotFoundException: ActivityNotFoundException) {
162-
activity.toast(R.string.error_no_external_application_found)
163-
}
164-
}
165-
166-
/**
167-
* Open external video recorder
168-
*/
169-
fun openVideoRecorder(activity: Activity, requestCode: Int) {
170-
val captureIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
171-
172-
// lowest quality
173-
captureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0)
174-
175-
try {
176-
activity.startActivityForResult(captureIntent, requestCode)
177-
} catch (activityNotFoundException: ActivityNotFoundException) {
178-
activity.toast(R.string.error_no_external_application_found)
179-
}
180-
}
181-
182-
/**
183-
* Open external camera
184-
* @return the latest taken picture camera uri
185-
*/
186-
fun openCamera(activity: Activity, titlePrefix: String, requestCode: Int): String? {
187-
val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
188-
189-
// the following is a fix for buggy 2.x devices
190-
val date = Date()
191-
val formatter = SimpleDateFormat("yyyyMMddHHmmss", Locale.US)
192-
val values = ContentValues()
193-
values.put(MediaStore.Images.Media.TITLE, titlePrefix + formatter.format(date))
194-
// The Galaxy S not only requires the name of the file to output the image to, but will also not
195-
// set the mime type of the picture it just took (!!!). We assume that the Galaxy S takes image/jpegs
196-
// so the attachment uploader doesn't freak out about there being no mimetype in the content database.
197-
values.put(MediaStore.Images.Media.MIME_TYPE, MimeTypes.Jpeg)
198-
var dummyUri: Uri? = null
199-
try {
200-
dummyUri = activity.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
201-
202-
if (null == dummyUri) {
203-
Timber.e("Cannot use the external storage media to save image")
204-
}
205-
} catch (uoe: UnsupportedOperationException) {
206-
Timber.e(uoe, "Unable to insert camera URI into MediaStore.Images.Media.EXTERNAL_CONTENT_URI.")
207-
Timber.e("no SD card? Attempting to insert into device storage.")
208-
} catch (e: Exception) {
209-
Timber.e(e, "Unable to insert camera URI into MediaStore.Images.Media.EXTERNAL_CONTENT_URI.")
210-
}
211-
212-
if (null == dummyUri) {
213-
try {
214-
dummyUri = activity.contentResolver.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values)
215-
if (null == dummyUri) {
216-
Timber.e("Cannot use the internal storage to save media to save image")
217-
}
218-
} catch (e: Exception) {
219-
Timber.e(e, "Unable to insert camera URI into internal storage. Giving up.")
136+
if (activityResultLauncher != null) {
137+
activityResultLauncher.launch(fileIntent)
138+
} else {
139+
activity.startActivityForResult(fileIntent, requestCode)
220140
}
221-
}
222-
223-
if (dummyUri != null) {
224-
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, dummyUri)
225-
Timber.v("trying to take a photo on $dummyUri")
226-
} else {
227-
Timber.v("trying to take a photo with no predefined uri")
228-
}
229-
230-
// Store the dummy URI which will be set to a placeholder location. When all is lost on Samsung devices,
231-
// this will point to the data we're looking for.
232-
// Because Activities tend to use a single MediaProvider for all their intents, this field will only be the
233-
// *latest* TAKE_PICTURE Uri. This is deemed acceptable as the normal flow is to create the intent then immediately
234-
// fire it, meaning onActivityResult/getUri will be the next thing called, not another createIntentFor.
235-
val result = if (dummyUri == null) null else dummyUri.toString()
236-
237-
try {
238-
activity.startActivityForResult(captureIntent, requestCode)
239-
240-
return result
241141
} catch (activityNotFoundException: ActivityNotFoundException) {
242142
activity.toast(R.string.error_no_external_application_found)
243143
}
244-
245-
return null
246144
}
247145

248146
/**
@@ -254,11 +152,7 @@ fun sendMailTo(address: String, subject: String? = null, message: String? = null
254152
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
255153
intent.putExtra(Intent.EXTRA_TEXT, message)
256154

257-
try {
258-
activity.startActivity(intent)
259-
} catch (activityNotFoundException: ActivityNotFoundException) {
260-
activity.toast(R.string.error_no_external_application_found)
261-
}
155+
activity.safeStartActivity(intent)
262156
}
263157

264158
/**
@@ -267,11 +161,7 @@ fun sendMailTo(address: String, subject: String? = null, message: String? = null
267161
fun openUri(activity: Activity, uri: String) {
268162
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
269163

270-
try {
271-
activity.startActivity(intent)
272-
} catch (activityNotFoundException: ActivityNotFoundException) {
273-
activity.toast(R.string.error_no_external_application_found)
274-
}
164+
activity.safeStartActivity(intent)
275165
}
276166

277167
/**
@@ -290,11 +180,7 @@ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) {
290180
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
291181
}
292182

293-
try {
294-
activity.startActivity(intent)
295-
} catch (activityNotFoundException: ActivityNotFoundException) {
296-
activity.toast(R.string.error_no_external_application_found)
297-
}
183+
activity.safeStartActivity(intent)
298184
}
299185

300186
fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
@@ -311,7 +197,7 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
311197
.setChooserTitle(R.string.action_share)
312198
.createChooserIntent()
313199

314-
createChooser(context, chooserIntent)
200+
context.safeStartActivity(chooserIntent)
315201
}
316202

317203
fun shareText(context: Context, text: String) {
@@ -321,14 +207,14 @@ fun shareText(context: Context, text: String) {
321207
.setChooserTitle(R.string.action_share)
322208
.createChooserIntent()
323209

324-
createChooser(context, chooserIntent)
210+
context.safeStartActivity(chooserIntent)
325211
}
326212

327-
private fun createChooser(context: Context, intent: Intent) {
213+
fun Context.safeStartActivity(intent: Intent) {
328214
try {
329-
context.startActivity(intent)
215+
startActivity(intent)
330216
} catch (activityNotFoundException: ActivityNotFoundException) {
331-
context.toast(R.string.error_no_external_application_found)
217+
toast(R.string.error_no_external_application_found)
332218
}
333219
}
334220

@@ -454,25 +340,18 @@ fun openPlayStore(activity: Activity, appId: String = BuildConfig.APPLICATION_ID
454340
try {
455341
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
456342
} catch (activityNotFoundException: ActivityNotFoundException) {
457-
try {
458-
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appId")))
459-
} catch (activityNotFoundException: ActivityNotFoundException) {
460-
activity.toast(R.string.error_no_external_application_found)
461-
}
343+
activity.safeStartActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appId")))
462344
}
463345
}
464346

465347
fun openAppSettingsPage(activity: Activity) {
466-
try {
467-
activity.startActivity(
468-
Intent().apply {
469-
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
470-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
471-
data = Uri.fromParts("package", activity.packageName, null)
472-
})
473-
} catch (activityNotFoundException: ActivityNotFoundException) {
474-
activity.toast(R.string.error_no_external_application_found)
475-
}
348+
activity.safeStartActivity(
349+
Intent().apply {
350+
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
351+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
352+
data = Uri.fromParts("package", activity.packageName, null)
353+
}
354+
)
476355
}
477356

478357
/**
@@ -488,9 +367,8 @@ fun selectTxtFileToWrite(
488367
intent.addCategory(Intent.CATEGORY_OPENABLE)
489368
intent.type = "text/plain"
490369
intent.putExtra(Intent.EXTRA_TITLE, defaultFileName)
491-
370+
val chooserIntent = Intent.createChooser(intent, chooserHint)
492371
try {
493-
val chooserIntent = Intent.createChooser(intent, chooserHint)
494372
activityResultLauncher.launch(chooserIntent)
495373
} catch (activityNotFoundException: ActivityNotFoundException) {
496374
activity.toast(R.string.error_no_external_application_found)

vector/src/main/java/im/vector/app/core/utils/SystemUtils.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ fun startNotificationChannelSettingsIntent(fragment: Fragment, channelID: String
124124
}
125125

126126
fun startAddGoogleAccountIntent(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>) {
127+
val intent = Intent(Settings.ACTION_ADD_ACCOUNT)
128+
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google"))
127129
try {
128-
val intent = Intent(Settings.ACTION_ADD_ACCOUNT)
129-
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google"))
130130
activityResultLauncher.launch(intent)
131131
} catch (activityNotFoundException: ActivityNotFoundException) {
132132
context.toast(R.string.error_no_external_application_found)
@@ -135,9 +135,9 @@ fun startAddGoogleAccountIntent(context: Context, activityResultLauncher: Activi
135135

136136
@RequiresApi(Build.VERSION_CODES.O)
137137
fun startInstallFromSourceIntent(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>) {
138+
val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
139+
.setData(Uri.parse(String.format("package:%s", context.packageName)))
138140
try {
139-
val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
140-
.setData(Uri.parse(String.format("package:%s", context.packageName)))
141141
activityResultLauncher.launch(intent)
142142
} catch (activityNotFoundException: ActivityNotFoundException) {
143143
context.toast(R.string.error_no_external_application_found)
@@ -177,9 +177,9 @@ fun startImportTextFromFileIntent(context: Context, activityResultLauncher: Acti
177177
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
178178
type = "text/plain"
179179
}
180-
if (intent.resolveActivity(context.packageManager) != null) {
180+
try {
181181
activityResultLauncher.launch(intent)
182-
} else {
182+
} catch (activityNotFoundException: ActivityNotFoundException) {
183183
context.toast(R.string.error_no_external_application_found)
184184
}
185185
}

vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import im.vector.app.core.utils.onPermissionDeniedDialog
108108
import im.vector.app.core.utils.onPermissionDeniedSnackbar
109109
import im.vector.app.core.utils.openUrlInExternalBrowser
110110
import im.vector.app.core.utils.registerForPermissionsResult
111+
import im.vector.app.core.utils.safeStartActivity
111112
import im.vector.app.core.utils.saveMedia
112113
import im.vector.app.core.utils.shareMedia
113114
import im.vector.app.core.utils.shareText
@@ -784,11 +785,7 @@ class RoomDetailFragment @Inject constructor(
784785
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
785786
}
786787

787-
if (intent.resolveActivity(requireActivity().packageManager) != null) {
788-
requireActivity().startActivity(intent)
789-
} else {
790-
requireActivity().toast(R.string.error_no_external_application_found)
791-
}
788+
requireActivity().safeStartActivity(intent)
792789
}
793790

794791
private fun installApk(action: RoomDetailViewEvents.OpenFile) {

0 commit comments

Comments
 (0)