@@ -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
267161fun 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
300186fun 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
317203fun 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
465347fun 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)
0 commit comments