Fix "Copied to clipboard" message not showing in API 32 and lower
This commit is contained in:
1 parent
cc94bed772
commit
187c35fe88
1 file changed
+17
-2
@@ -17,10 +17,12 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ClipData
|
||||
import android.content.ClipDescription
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.os.Build
|
||||
import android.view.textclassifier.TextClassifier
|
||||
import androidx.core.os.persistableBundleOf
|
||||
@@ -49,8 +51,9 @@ fun Context.addTextToClipboard(text: CharSequence?, isSensitive: Boolean, useToa
|
||||
}
|
||||
// Avoid double notify user since system have done that on Tiramisu above
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
||||
if (this is MainActivity) {
|
||||
showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT, useToast)
|
||||
val activity = findActivity()
|
||||
if (activity is MainActivity) {
|
||||
activity.showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT, useToast)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,3 +66,15 @@ fun ClipboardManager.getUrlFromClipboard(context: Context): String? {
|
||||
val string = item?.coerceToText(context).toString()
|
||||
return string.ifEmpty { null }
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the closest Activity in a given Context.
|
||||
*/
|
||||
internal fun Context.findActivity(): Activity {
|
||||
var context = this
|
||||
while (context is ContextWrapper) {
|
||||
if (context is Activity) return context
|
||||
context = context.baseContext
|
||||
}
|
||||
throw IllegalStateException("findActivity() should be called in the context of an Activity")
|
||||
}
|
||||
Reference in new issue
Block a user