diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index ed0f3484d..fd80759b7 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -2,7 +2,6 @@ package io.legado.app.ui.book.source.manage import android.annotation.SuppressLint import android.app.Activity -import android.content.ActivityNotFoundException import android.content.Intent import android.os.Bundle import android.view.Menu @@ -10,7 +9,6 @@ import android.view.MenuItem import android.view.SubMenu import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView -import androidx.core.content.FileProvider import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.LiveData import androidx.recyclerview.widget.DiffUtil @@ -18,7 +16,6 @@ import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar import io.legado.app.App -import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppPattern @@ -99,30 +96,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity when (item.itemId) { R.id.menu_add_book_source -> startActivity() R.id.menu_import_source_qr -> startActivityForResult(qrRequestCode) - R.id.menu_share_source -> { - try { - val json = GSON.toJson(adapter.getSelection()) - val intent = Intent(Intent.ACTION_SEND) - val file = FileUtils.createFileWithReplace("$filesDir/shareBookSource.json") - file.writeText(json) - val fileUri = FileProvider.getUriForFile( - this, - BuildConfig.APPLICATION_ID + ".fileProvider", - file - ) - intent.type = "text/*" - intent.putExtra(Intent.EXTRA_STREAM, fileUri) - intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - startActivity( - Intent.createChooser( - intent, - getString(R.string.share_selected_source) - ) - ) - } catch (e: ActivityNotFoundException) { - e.printStackTrace() - } - } + R.id.menu_share_source -> viewModel.shareSelection(adapter.getSelection()) R.id.menu_group_manage -> GroupManageDialog().show(supportFragmentManager, "groupManage") R.id.menu_import_source_local -> FilePicker diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index e291ac625..126d47dac 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -1,9 +1,13 @@ package io.legado.app.ui.book.source.manage import android.app.Application +import android.content.Intent import android.text.TextUtils +import androidx.core.content.FileProvider import androidx.documentfile.provider.DocumentFile import io.legado.app.App +import io.legado.app.BuildConfig +import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern import io.legado.app.data.entities.BookSource @@ -163,6 +167,28 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } + fun shareSelection(sources: List) { + execute { + val intent = Intent(Intent.ACTION_SEND) + val file = FileUtils.createFileWithReplace("${context.filesDir}/shareBookSource.json") + file.writeText(GSON.toJson(sources)) + val fileUri = FileProvider.getUriForFile( + context, + BuildConfig.APPLICATION_ID + ".fileProvider", + file + ) + intent.type = "text/*" + intent.putExtra(Intent.EXTRA_STREAM, fileUri) + intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent + }.onSuccess { + context.startActivity( + Intent.createChooser(it, context.getString(R.string.share_selected_source)) + ) + } + } + fun addGroup(group: String) { execute { val sources = App.db.bookSourceDao().noGroup diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 7366cb602..cd391a0c3 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -126,7 +126,7 @@ fun Context.shareWithQr(title: String, text: String) { file ) val intent = Intent(Intent.ACTION_SEND) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.putExtra(Intent.EXTRA_STREAM, contentUri) intent.type = "image/png" startActivity(Intent.createChooser(intent, title)) @@ -160,6 +160,7 @@ fun Context.sendMail(mail: String) { try { val intent = Intent(Intent.ACTION_SENDTO) intent.data = Uri.parse("mailto:$mail") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(intent) } catch (e: Exception) { toast(e.localizedMessage ?: "Error") @@ -197,6 +198,7 @@ fun Context.openUrl(url: String) { fun Context.openUrl(uri: Uri) { val intent = Intent(Intent.ACTION_VIEW) intent.data = uri + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) if (intent.resolveActivity(packageManager) != null) { try { startActivity(intent)