@@ -80,11 +80,7 @@ fun openUrlInExternalBrowser(context: Context, uri: Uri?) {
80
80
putExtra(Browser .EXTRA_CREATE_NEW_TAB , true )
81
81
}
82
82
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)
88
84
}
89
85
}
90
86
@@ -123,22 +119,6 @@ fun openUrlInChromeCustomTab(context: Context,
123
119
}
124
120
}
125
121
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
-
142
122
/* *
143
123
* Open file selection activity
144
124
*/
@@ -153,96 +133,14 @@ fun openFileSelection(activity: Activity,
153
133
fileIntent.type = MimeTypes .Any
154
134
155
135
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)
220
140
}
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
241
141
} catch (activityNotFoundException: ActivityNotFoundException ) {
242
142
activity.toast(R .string.error_no_external_application_found)
243
143
}
244
-
245
- return null
246
144
}
247
145
248
146
/* *
@@ -254,11 +152,7 @@ fun sendMailTo(address: String, subject: String? = null, message: String? = null
254
152
intent.putExtra(Intent .EXTRA_SUBJECT , subject)
255
153
intent.putExtra(Intent .EXTRA_TEXT , message)
256
154
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)
262
156
}
263
157
264
158
/* *
@@ -267,11 +161,7 @@ fun sendMailTo(address: String, subject: String? = null, message: String? = null
267
161
fun openUri (activity : Activity , uri : String ) {
268
162
val intent = Intent (Intent .ACTION_VIEW , Uri .parse(uri))
269
163
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)
275
165
}
276
166
277
167
/* *
@@ -290,11 +180,7 @@ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) {
290
180
addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
291
181
}
292
182
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)
298
184
}
299
185
300
186
fun shareMedia (context : Context , file : File , mediaMimeType : String? ) {
@@ -311,7 +197,7 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
311
197
.setChooserTitle(R .string.action_share)
312
198
.createChooserIntent()
313
199
314
- createChooser( context, chooserIntent)
200
+ context.safeStartActivity( chooserIntent)
315
201
}
316
202
317
203
fun shareText (context : Context , text : String ) {
@@ -321,14 +207,14 @@ fun shareText(context: Context, text: String) {
321
207
.setChooserTitle(R .string.action_share)
322
208
.createChooserIntent()
323
209
324
- createChooser( context, chooserIntent)
210
+ context.safeStartActivity( chooserIntent)
325
211
}
326
212
327
- private fun createChooser ( context : Context , intent : Intent ) {
213
+ fun Context. safeStartActivity ( intent : Intent ) {
328
214
try {
329
- context. startActivity(intent)
215
+ startActivity(intent)
330
216
} catch (activityNotFoundException: ActivityNotFoundException ) {
331
- context. toast(R .string.error_no_external_application_found)
217
+ toast(R .string.error_no_external_application_found)
332
218
}
333
219
}
334
220
@@ -454,25 +340,18 @@ fun openPlayStore(activity: Activity, appId: String = BuildConfig.APPLICATION_ID
454
340
try {
455
341
activity.startActivity(Intent (Intent .ACTION_VIEW , Uri .parse(" market://details?id=$appId " )))
456
342
} 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 " )))
462
344
}
463
345
}
464
346
465
347
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
+ )
476
355
}
477
356
478
357
/* *
@@ -488,9 +367,8 @@ fun selectTxtFileToWrite(
488
367
intent.addCategory(Intent .CATEGORY_OPENABLE )
489
368
intent.type = " text/plain"
490
369
intent.putExtra(Intent .EXTRA_TITLE , defaultFileName)
491
-
370
+ val chooserIntent = Intent .createChooser(intent, chooserHint)
492
371
try {
493
- val chooserIntent = Intent .createChooser(intent, chooserHint)
494
372
activityResultLauncher.launch(chooserIntent)
495
373
} catch (activityNotFoundException: ActivityNotFoundException ) {
496
374
activity.toast(R .string.error_no_external_application_found)
0 commit comments