update the code style at some activity extensions

This commit is contained in:
tibbi 2017-03-05 23:34:23 +01:00
parent d699293c11
commit 80bfc9183f

View file

@ -48,10 +48,10 @@ fun Activity.shareMedium(medium: Medium) {
fun Activity.shareMedia(media: List<Medium>) { fun Activity.shareMedia(media: List<Medium>) {
val shareTitle = resources.getString(R.string.share_via) val shareTitle = resources.getString(R.string.share_via)
val uris = ArrayList<Uri>(media.size)
Intent().apply { Intent().apply {
action = Intent.ACTION_SEND_MULTIPLE action = Intent.ACTION_SEND_MULTIPLE
type = "image/* video/*" type = "image/* video/*"
val uris = ArrayList<Uri>(media.size)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
media.map { File(it.path) } media.map { File(it.path) }
.mapTo(uris) { Uri.fromFile(it) } .mapTo(uris) { Uri.fromFile(it) }
@ -62,44 +62,50 @@ fun Activity.shareMedia(media: List<Medium>) {
} }
fun Activity.setAsWallpaper(file: File) { fun Activity.setAsWallpaper(file: File) {
val intent = Intent(Intent.ACTION_ATTACH_DATA)
val uri = Uri.fromFile(file) val uri = Uri.fromFile(file)
intent.setDataAndType(uri, file.getMimeType("image/*")) Intent().apply {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) action = Intent.ACTION_ATTACH_DATA
val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with)) setDataAndType(uri, file.getMimeType("image/*"))
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(this, getString(R.string.set_as_wallpaper_with))
if (intent.resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {
startActivityForResult(chooser, REQUEST_SET_WALLPAPER) startActivityForResult(chooser, REQUEST_SET_WALLPAPER)
} else { } else {
toast(R.string.no_wallpaper_setter_found) toast(R.string.no_wallpaper_setter_found)
} }
} }
}
fun Activity.openWith(file: File, forceChooser: Boolean = true) { fun Activity.openWith(file: File, forceChooser: Boolean = true) {
val intent = Intent(Intent.ACTION_VIEW)
val uri = Uri.fromFile(file) val uri = Uri.fromFile(file)
intent.setDataAndType(uri, file.getMimeType("image/jpeg")) Intent().apply {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) action = Intent.ACTION_VIEW
val chooser = Intent.createChooser(intent, getString(R.string.open_with)) setDataAndType(uri, file.getMimeType("image/jpeg"))
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(this, getString(R.string.open_with))
if (intent.resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {
startActivity(if (forceChooser) chooser else intent) startActivity(if (forceChooser) chooser else this)
} else { } else {
toast(R.string.no_app_found) toast(R.string.no_app_found)
} }
} }
}
fun Activity.openEditor(file: File) { fun Activity.openEditor(file: File) {
val intent = Intent(Intent.ACTION_EDIT) Intent().apply {
intent.setDataAndType(Uri.fromFile(file), "image/*") action = Intent.ACTION_EDIT
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) setDataAndType(Uri.fromFile(file), "image/*")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (intent.resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {
startActivityForResult(intent, REQUEST_EDIT_IMAGE) startActivityForResult(this, REQUEST_EDIT_IMAGE)
} else { } else {
toast(R.string.no_editor_found) toast(R.string.no_editor_found)
} }
} }
}
fun Activity.hasNavBar(): Boolean { fun Activity.hasNavBar(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {