Skip to content

Commit

Permalink
add OpenWith action chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Feb 3, 2024
1 parent 31ddca1 commit caff102
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package ru.yourok.torrserve.ui.fragments.main.torrents

import android.content.ClipData
import android.content.ClipboardManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.view.ActionMode
import android.view.Menu
import android.view.MenuInflater
Expand All @@ -13,11 +16,11 @@ import android.widget.AbsListView.MultiChoiceModeListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ru.yourok.torrserve.BuildConfig
import ru.yourok.torrserve.R
import ru.yourok.torrserve.app.App
import ru.yourok.torrserve.server.api.Api
import ru.yourok.torrserve.server.models.torrent.Torrent
import ru.yourok.torrserve.ui.activities.play.addTorrent
import ru.yourok.torrserve.utils.TorrentHelper
import ru.yourok.torrserve.utils.TorrentHelper.getTorrentMagnet
import kotlin.concurrent.thread
Expand Down Expand Up @@ -45,16 +48,62 @@ class TorrentsActionBar(private val listView: AbsListView) : MultiChoiceModeList
override fun onActionItemClicked(actionMode: ActionMode, item: MenuItem): Boolean {
val selected = selectedItems
when (item.itemId) {
R.id.itemOpenWith -> {
val magnetUris = mutableListOf<Uri>()
selected.forEach {
val magnet = getTorrentMagnet(it)
if (magnet.isNotEmpty()) {
magnetUris.add(Uri.parse(magnet))
}
}
if (magnetUris.isNotEmpty()) {
val magnetIntent = Intent()
// if (magnetUris.size == 1) {
val uri = magnetUris.first()
magnetIntent.apply {
action = Intent.ACTION_VIEW
data = uri
addCategory(Intent.CATEGORY_DEFAULT)
addCategory(Intent.CATEGORY_BROWSABLE)
}
// } else {
// val arrayList = arrayListOf<Uri>()
// arrayList.addAll(magnetUris)
// magnetIntent.apply {
// action = Intent.ACTION_SEND_MULTIPLE
// putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayList)
// addCategory(Intent.CATEGORY_BROWSABLE)
// }
// }
val chooser = Intent.createChooser(magnetIntent, App.context.getString(R.string.open_with)).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val excludedComponentNames = arrayOf(ComponentName(BuildConfig.APPLICATION_ID, "${BuildConfig.APPLICATION_ID}.ui.activities.play.PlayActivity"))
putExtra(Intent.EXTRA_EXCLUDE_COMPONENTS, excludedComponentNames)
}
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}

if (magnetIntent.resolveActivity(App.context.packageManager) != null) {
App.context.startActivity(chooser)
} else { // Handle the case where no activity can handle the intent
App.toast(R.string.error_app_not_found, true)
}

}
}

R.id.itemShareMagnet -> {
val msg = selected.joinToString("\n\n") { getTorrentMagnet(it) }
if (msg.isNotEmpty()) {
val share = Intent(Intent.ACTION_SEND)
share.type = "text/plain"
share.putExtra(Intent.EXTRA_TEXT, msg)
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val intent = Intent.createChooser(share, "")
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
App.context.startActivity(intent)
val share = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, msg)
}

val shareIntent = Intent.createChooser(share, null)
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
App.context.startActivity(shareIntent)
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/menu/torrents_action_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@
android:title="@string/copy"
app:showAsAction="ifRoom" />

<item
android:id="@+id/itemOpenWith"
android:icon="@drawable/ic_outline_exit_to_app"
android:title="@string/open_with"
app:showAsAction="ifRoom" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="remove_all_warn">Искате ли да изтриете всички торенти?</string>
<string name="share">Сподели</string>
<string name="open_torrserve">Отвори TorrServe</string>
<string name="open_with">Отваряне с</string>
<string name="show_menu">Покажи Главно меню</string>
<string name="torrents">Торенти</string>
<string name="apply">Приложи</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="remove_all_warn">Вы хотите удалить все торренты?</string>
<string name="share">Отправить</string>
<string name="open_torrserve">Запустить TorrServe</string>
<string name="open_with">Открыть с помощью</string>
<string name="show_menu">Показать главное меню</string>
<string name="torrents">Торренты</string>
<string name="apply">Применить</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="remove_all_warn">Ви хочете видалити всі торренти?</string>
<string name="share">Поділитись</string>
<string name="open_torrserve">Відкрити TorrServe</string>
<string name="open_with">Відкрити за допомогою</string>
<string name="show_menu">Показати головне меню</string>
<string name="torrents">Торренти</string>
<string name="apply">Застосувати</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<string name="share">分享</string>

<string name="open_torrserve">打开 TorrServe</string>
<string name="open_with">打开方式</string>
<string name="show_menu">显示主菜单</string>
<string name="torrents">种子</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="share">Share</string>

<string name="open_torrserve">Open TorrServe</string>
<string name="open_with">Open With</string>
<string name="show_menu">Show Main Menu</string>
<string name="torrents">Torrents</string>

Expand Down

0 comments on commit caff102

Please sign in to comment.