diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/profile/ProfileConfigExport.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/profile/ProfileConfigExport.kt index 005031148..037dbbe66 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/profile/ProfileConfigExport.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/profile/ProfileConfigExport.kt @@ -29,4 +29,10 @@ object ProfileConfigExport { val name = if (xrayConfigs.isEmpty()) "${entity.displayName()}.json" else "${entity.displayName()}.txt" return text.toString() to name } + + fun exportAll(entities: List): Pair { + if (entities.size == 1) return export(entities.single()) + val text = entities.joinToString("\n\n") { entity -> "// ${entity.displayName()}\n" + export(entity).first } + return text to "profiles.txt" + } } diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/ProfileItemMenu.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/ProfileItemMenu.kt index c1729cc48..94cade21d 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/ProfileItemMenu.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/ProfileItemMenu.kt @@ -187,10 +187,11 @@ class ProfileItemMenu(private val host: ConfigurationFragment) { // a chain has no link of its own, only the config it starts (bf89f273) if (profile.isChain()) { popup.menu.removeItem(R.id.action_group_qr) - popup.menu.removeItem(R.id.action_group_clipboard) + popup.menu.removeItem(R.id.action_standard_clipboard) + popup.menu.removeItem(R.id.action_universal_clipboard) } else if (profile.exportLink().isEmpty()) { popup.menu.findItem(R.id.action_group_qr).subMenu?.removeItem(R.id.action_standard_qr) - popup.menu.findItem(R.id.action_group_clipboard).subMenu?.removeItem(R.id.action_standard_clipboard) + popup.menu.removeItem(R.id.action_standard_clipboard) } popup.setOnMenuItemClickListener { item -> try { @@ -218,10 +219,14 @@ class ProfileItemMenu(private val host: ConfigurationFragment) { */ private fun exportConfiguration(profile: ProxyEntity, toFile: Boolean) { // A copy parses its own outbound, so the row's cached one is only ever touched on the main thread. - val entity = profile.copy() + exportConfigurations(listOf(profile.copy()), toFile) + } + + internal fun exportConfigurations(entities: List, toFile: Boolean) { + if (entities.isEmpty()) return host.launchIo { val (config, fileName) = try { - ProfileConfigExport.export(entity) + ProfileConfigExport.exportAll(entities) } catch (e: Exception) { Logs.w(e) host.onUi { snackbar(e.readableMessage).show() } diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/SelectionMode.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/SelectionMode.kt index e2f7c7ef8..fbf47c921 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/SelectionMode.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/profiles/SelectionMode.kt @@ -10,11 +10,14 @@ import io.nekohasekai.sagernet.database.DataStore import io.nekohasekai.sagernet.database.GroupRepo import io.nekohasekai.sagernet.database.ProfileManager import io.nekohasekai.sagernet.database.ProxyEntity +import io.nekohasekai.sagernet.group.SubscriptionClient +import io.nekohasekai.sagernet.ktx.Logs import io.nekohasekai.sagernet.ktx.confirmAction import io.nekohasekai.sagernet.ktx.nameList import io.nekohasekai.sagernet.ktx.showAllowingStateLoss import io.nekohasekai.sagernet.ktx.snackbar import io.nekohasekai.sagernet.ui.ConfigurationFragment +import io.nekohasekai.sagernet.ui.MainActivity import io.nekohasekai.sagernet.widget.QRCodeDialog /** @@ -123,6 +126,16 @@ class SelectionMode(private val host: ConfigurationFragment) { refreshPinTitle() } + private fun invertSelection() { + val shown = host.listFor(groupId)?.adapter?.displayedIds ?: return + for (id in shown) { + if (!checked.remove(id)) checked.add(id) + } + if (checked.isEmpty()) return finish() + host.onSelectionChanged(null) + refreshPinTitle() + } + /** Every checked profile is pinned, so the action unpins (refresh_pin_action). */ private fun allPinned(): Boolean { val adapter = host.listFor(groupId)?.adapter ?: return false @@ -155,14 +168,19 @@ class SelectionMode(private val host: ConfigurationFragment) { } R.id.action_selection_all -> selectAll() + R.id.action_selection_invert -> invertSelection() R.id.action_selection_delete -> delete(ids) R.id.action_selection_pin -> pin(ids) R.id.action_selection_clear_results -> clearResults(ids) + R.id.action_selection_resolve_domain -> resolve(ids) + R.id.action_selection_clear_traffic -> clearTraffic(ids) R.id.action_selection_move -> moveToGroup(ids) R.id.action_selection_copy_links -> copyLinks(ids, throne = false) R.id.action_selection_copy_throne -> copyLinks(ids, throne = true) R.id.action_selection_qr_links -> showQr(ids, throne = false) R.id.action_selection_qr_throne -> showQr(ids, throne = true) + R.id.action_selection_config_clipboard -> exportConfigs(ids, toFile = false) + R.id.action_selection_config_file -> exportConfigs(ids, toFile = true) else -> return false } return true @@ -199,6 +217,35 @@ class SelectionMode(private val host: ConfigurationFragment) { } } + private fun resolve(ids: List) { + if (ids.isEmpty()) return + val gid = groupId + finish() + host.launchIo { + val report = SubscriptionClient.resolveProfiles(gid, ids) + if (report.isNotBlank()) host.onUi { snackbar(report).show() } + } + } + + private fun clearTraffic(ids: List) { + val gid = groupId + confirm(ids, host.getString(R.string.confirm_clear_traffic), R.string.confirm_clear) { + val service = (host.activity as? MainActivity)?.connection?.service + val adapter = host.listFor(gid)?.adapter + finish() + ids.forEach { adapter?.clearTraffic(it) } + host.launchIo { + val array = ids.toLongArray() + try { + service?.resetTraffic(array) + } catch (e: Exception) { + Logs.w(e) + } + ProfileManager.resetTraffic(array) + } + } + } + /** [title] above the names of [ids] in list order. */ private fun confirm(ids: List, title: String, @StringRes action: Int, run: () -> Unit) { host.launchIo { @@ -268,6 +315,15 @@ class SelectionMode(private val host: ConfigurationFragment) { } } + private fun exportConfigs(ids: List, toFile: Boolean) { + finish() + host.launchIo { + val position = ids.withIndex().associate { it.value to it.index } + val profiles = ProfileManager.getProfiles(ids).sortedBy { position[it.id] } + host.itemMenu.exportConfigurations(profiles, toFile) + } + } + /** The links of [ids] in list order with their profile names, profiles without one left out. */ private fun links(ids: List, throne: Boolean): List> { val position = ids.withIndex().associate { it.value to it.index } diff --git a/app/src/main/res/menu/profile_selection_menu.xml b/app/src/main/res/menu/profile_selection_menu.xml index 74e99f3f3..99970161f 100644 --- a/app/src/main/res/menu/profile_selection_menu.xml +++ b/app/src/main/res/menu/profile_selection_menu.xml @@ -33,6 +33,10 @@ android:id="@+id/action_selection_all" android:title="@string/profiles_select_all" app:showAsAction="never" /> + + + + android:id="@+id/action_selection_qr_links" + android:title="@string/standard" /> + android:id="@+id/action_selection_qr_throne" + android:title="@string/deep_link" /> + + + android:id="@+id/action_selection_config_clipboard" + android:title="@string/action_export_clipboard" /> + android:id="@+id/action_selection_config_file" + android:title="@string/action_export_file" /> diff --git a/app/src/main/res/menu/profile_share_menu.xml b/app/src/main/res/menu/profile_share_menu.xml index 3b2d4ca0f..79690a4a3 100644 --- a/app/src/main/res/menu/profile_share_menu.xml +++ b/app/src/main/res/menu/profile_share_menu.xml @@ -2,32 +2,25 @@ + android:title="@string/share_qr_link"> + android:title="@string/deep_link" /> - - - - - + android:id="@+id/action_standard_clipboard" + android:title="@string/copy_link" /> + + android:title="@string/export_config"> Разрешить домен Работающий профиль сохранён. Выбрать все + Инвертировать выбор + QR-код и ссылка + Копировать ссылку + Копировать deep link + Deep link + Экспортировать конфиг Показать Внешний IP Скорость diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8b6872e7c..e29576def 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1302,6 +1302,12 @@ %d selected Select all + Invert selection + QR code and link + Copy link + Copy deep link + Deep link + Export config Move to group There is no other basic group to move to.