From fbe6a6cbc54678ba4b2e36e86a0c74c7e811e62e Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 10 Jan 2020 19:55:56 +0800 Subject: [PATCH] up --- .../source/edit/BookSourceEditActivity.kt | 10 ++--- .../rss/source/edit/RssSourceEditActivity.kt | 10 ++--- .../io/legado/app/utils/ContextExtensions.kt | 40 +++++++++++++++++++ app/src/main/res/menu/source_edit.xml | 2 +- app/src/main/res/values/strings.xml | 3 ++ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 4bab3e780..cc63c93d4 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -26,10 +26,7 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.widget.KeyboardToolPop -import io.legado.app.utils.GSON -import io.legado.app.utils.applyTint -import io.legado.app.utils.getViewModel -import io.legado.app.utils.shareText +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_source_edit.* import org.jetbrains.anko.displayMetrics import org.jetbrains.anko.startActivity @@ -89,7 +86,10 @@ class BookSourceEditActivity : } R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } R.id.menu_share_str -> GSON.toJson(getSource())?.let { sourceStr -> - shareText("分享书源", sourceStr) + shareText(getString(R.string.share_book_source), sourceStr) + } + R.id.menu_share_qr -> GSON.toJson(getSource())?.let { sourceStr -> + shareWithQr(getString(R.string.share_book_source), sourceStr) } R.id.menu_rule_summary -> { try { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index 2b5d88b7a..1ae70df5d 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -22,10 +22,7 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity import io.legado.app.ui.widget.KeyboardToolPop -import io.legado.app.utils.GSON -import io.legado.app.utils.applyTint -import io.legado.app.utils.getViewModel -import io.legado.app.utils.shareText +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_rss_source_edit.* import org.jetbrains.anko.displayMetrics import org.jetbrains.anko.startActivity @@ -105,7 +102,10 @@ class RssSourceEditActivity : } R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } R.id.menu_share_str -> GSON.toJson(getRssSource())?.let { sourceStr -> - shareText("分享RSS源", sourceStr) + shareText(getString(R.string.share_rss_source), sourceStr) + } + R.id.menu_share_qr -> GSON.toJson(getRssSource())?.let { sourceStr -> + shareWithQr(getString(R.string.share_rss_source), sourceStr) } } return super.onCompatOptionsItemSelected(item) 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 ff917b09f..2ab88b481 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -1,16 +1,25 @@ package io.legado.app.utils +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.res.ColorStateList +import android.graphics.Bitmap import android.graphics.drawable.Drawable import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.core.content.ContextCompat +import androidx.core.content.FileProvider import androidx.core.content.edit +import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder +import com.google.zxing.EncodeHintType +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel +import io.legado.app.BuildConfig import io.legado.app.R import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.toast +import java.io.File +import java.io.FileOutputStream fun Context.getPrefBoolean(key: String, defValue: Boolean = false) = defaultSharedPreferences.getBoolean(key, defValue) @@ -75,6 +84,37 @@ fun Context.shareText(title: String, text: String) { } } +@SuppressLint("SetWorldReadable") +fun Context.shareWithQr(title: String, text: String) { + QRCodeEncoder.HINTS[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L + val bitmap = QRCodeEncoder.syncEncodeQRCode(text, 600) + QRCodeEncoder.HINTS[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H + if (bitmap == null) { + toast(R.string.text_too_long_qr_error) + } else { + try { + val file = File(externalCacheDir, "qr.png") + val fOut = FileOutputStream(file) + bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut) + fOut.flush() + fOut.close() + file.setReadable(true, false) + val contentUri = FileProvider.getUriForFile( + this, + "${BuildConfig.APPLICATION_ID}.fileProvider", + file + ) + val intent = Intent(Intent.ACTION_SEND) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + intent.putExtra(Intent.EXTRA_STREAM, contentUri) + intent.type = "image/png" + startActivity(Intent.createChooser(intent, title)) + } catch (e: Exception) { + toast(e.localizedMessage ?: "ERROR") + } + } +} + val Context.isNightTheme: Boolean get() = getPrefBoolean("isNightTheme") diff --git a/app/src/main/res/menu/source_edit.xml b/app/src/main/res/menu/source_edit.xml index ec8739c11..2fd9678c4 100644 --- a/app/src/main/res/menu/source_edit.xml +++ b/app/src/main/res/menu/source_edit.xml @@ -35,7 +35,7 @@ app:showAsAction="never" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5036723df..3ceda2af3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -579,5 +579,8 @@ 上一句 下一句 其它目录 + 文字太多,生成二维码失败 + 分享RSS源 + 分享书源