Rename
This commit is contained in:
1 parent
b86d90021c
commit
6c0757c91b
972 files changed
+6885
-7321
No files matched your search
@@ -0,0 +1,121 @@
|
||||
package io.legato.kazusa.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.view.KeyEvent
|
||||
import io.legato.kazusa.constant.EventBus
|
||||
import io.legato.kazusa.data.appDb
|
||||
import io.legato.kazusa.help.LifecycleHelp
|
||||
import io.legato.kazusa.help.config.AppConfig
|
||||
import io.legato.kazusa.model.AudioPlay
|
||||
import io.legato.kazusa.model.ReadAloud
|
||||
import io.legato.kazusa.model.ReadBook
|
||||
import io.legato.kazusa.service.AudioPlayService
|
||||
import io.legato.kazusa.service.BaseReadAloudService
|
||||
import io.legato.kazusa.ui.book.audio.AudioPlayActivity
|
||||
import io.legato.kazusa.ui.book.read.ReadBookActivity
|
||||
import io.legato.kazusa.utils.LogUtils
|
||||
import io.legato.kazusa.utils.getPrefBoolean
|
||||
import io.legato.kazusa.utils.postEvent
|
||||
|
||||
|
||||
/**
|
||||
* Created by GKF on 2018/1/6.
|
||||
* 监听耳机键
|
||||
*/
|
||||
class MediaButtonReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (handleIntent(context, intent) && isOrderedBroadcast) {
|
||||
abortBroadcast()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val TAG = "MediaButtonReceiver"
|
||||
|
||||
fun handleIntent(context: Context, intent: Intent): Boolean {
|
||||
val intentAction = intent.action
|
||||
if (Intent.ACTION_MEDIA_BUTTON == intentAction) {
|
||||
@Suppress("DEPRECATION")
|
||||
val keyEvent = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
|
||||
?: return false
|
||||
val keycode: Int = keyEvent.keyCode
|
||||
val action: Int = keyEvent.action
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
LogUtils.d(TAG, "Receive mediaButton event, keycode:$keycode")
|
||||
when (keycode) {
|
||||
KeyEvent.KEYCODE_MEDIA_PREVIOUS -> {
|
||||
if (context.getPrefBoolean("mediaButtonPerNext", false)) {
|
||||
ReadBook.moveToPrevChapter(true)
|
||||
} else {
|
||||
ReadAloud.prevParagraph(context)
|
||||
}
|
||||
}
|
||||
|
||||
KeyEvent.KEYCODE_MEDIA_NEXT -> {
|
||||
if (context.getPrefBoolean("mediaButtonPerNext", false)) {
|
||||
ReadBook.moveToNextChapter(true)
|
||||
} else {
|
||||
ReadAloud.nextParagraph(context)
|
||||
}
|
||||
}
|
||||
|
||||
else -> readAloud(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun readAloud(context: Context, isMediaKey: Boolean = true) {
|
||||
when {
|
||||
BaseReadAloudService.isRun -> {
|
||||
if (BaseReadAloudService.isPlay()) {
|
||||
ReadAloud.pause(context)
|
||||
AudioPlay.pause(context)
|
||||
} else {
|
||||
ReadAloud.resume(context)
|
||||
AudioPlay.resume(context)
|
||||
}
|
||||
}
|
||||
|
||||
AudioPlayService.isRun -> {
|
||||
if (AudioPlayService.pause) {
|
||||
AudioPlay.resume(context)
|
||||
} else {
|
||||
AudioPlay.pause(context)
|
||||
}
|
||||
}
|
||||
|
||||
isMediaKey && !AppConfig.readAloudByMediaButton -> {
|
||||
// break
|
||||
}
|
||||
|
||||
LifecycleHelp.isExistActivity(ReadBookActivity::class.java) ->
|
||||
postEvent(EventBus.MEDIA_BUTTON, true)
|
||||
|
||||
LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) ->
|
||||
postEvent(EventBus.MEDIA_BUTTON, true)
|
||||
|
||||
else -> if (AppConfig.mediaButtonOnExit || LifecycleHelp.activitySize() > 0 || !isMediaKey) {
|
||||
ReadAloud.upReadAloudClass()
|
||||
if (ReadBook.book != null) {
|
||||
ReadBook.readAloud()
|
||||
} else {
|
||||
appDb.bookDao.lastReadBook?.let {
|
||||
ReadBook.resetData(it)
|
||||
ReadBook.clearTextChapter()
|
||||
ReadBook.loadContent(false) {
|
||||
ReadBook.readAloud()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package io.legato.kazusa.receiver
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.os.Build
|
||||
import splitties.systemservices.connectivityManager
|
||||
|
||||
/**
|
||||
* 监测网络变化
|
||||
*/
|
||||
@SuppressLint("ObsoleteSdkInt")
|
||||
class NetworkChangedListener(private val context: Context) {
|
||||
|
||||
var onNetworkChanged: (() -> Unit)? = null
|
||||
|
||||
private val receiver: NetworkChangedReceiver? by lazy {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
NetworkChangedReceiver()
|
||||
}
|
||||
return@lazy null
|
||||
}
|
||||
|
||||
private val networkCallback: ConnectivityManager.NetworkCallback? by lazy {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
return@lazy object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
onNetworkChanged?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
return@lazy null
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission", "UnspecifiedRegisterReceiverFlag")
|
||||
fun register() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
networkCallback?.let {
|
||||
connectivityManager.registerDefaultNetworkCallback(it)
|
||||
}
|
||||
} else {
|
||||
receiver?.let {
|
||||
context.registerReceiver(it, it.filter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun unRegister() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
networkCallback?.let {
|
||||
connectivityManager.unregisterNetworkCallback(it)
|
||||
}
|
||||
} else {
|
||||
receiver?.let {
|
||||
context.unregisterReceiver(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class NetworkChangedReceiver : BroadcastReceiver() {
|
||||
|
||||
val filter = IntentFilter().apply {
|
||||
@Suppress("DEPRECATION")
|
||||
addAction(ConnectivityManager.CONNECTIVITY_ACTION)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
onNetworkChanged?.invoke()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.legato.kazusa.receiver
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import io.legato.kazusa.ui.book.search.SearchActivity
|
||||
import io.legato.kazusa.ui.main.MainActivity
|
||||
import io.legato.kazusa.utils.startActivity
|
||||
import splitties.init.appCtx
|
||||
|
||||
class SharedReceiverActivity : AppCompatActivity() {
|
||||
|
||||
private val receivingType = "text/plain"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
initIntent()
|
||||
finish()
|
||||
}
|
||||
|
||||
@SuppressLint("ObsoleteSdkInt")
|
||||
private fun initIntent() {
|
||||
when {
|
||||
intent.action == Intent.ACTION_SEND && intent.type == receivingType -> {
|
||||
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
|
||||
dispose(it)
|
||||
}
|
||||
}
|
||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
||||
&& intent.action == Intent.ACTION_PROCESS_TEXT
|
||||
&& intent.type == receivingType -> {
|
||||
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)?.let {
|
||||
dispose(it)
|
||||
}
|
||||
}
|
||||
intent.getStringExtra("action") == "readAloud" -> {
|
||||
MediaButtonReceiver.readAloud(appCtx, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun dispose(text: String) {
|
||||
if (text.isBlank()) {
|
||||
return
|
||||
}
|
||||
val urls = text.split("\\s".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||
val result = StringBuilder()
|
||||
for (url in urls) {
|
||||
if (url.matches("http.+".toRegex()))
|
||||
result.append("\n").append(url.trim { it <= ' ' })
|
||||
}
|
||||
if (result.length > 1) {
|
||||
startActivity<MainActivity>()
|
||||
} else {
|
||||
SearchActivity.start(this, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.legato.kazusa.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.BatteryManager
|
||||
import io.legato.kazusa.constant.EventBus
|
||||
import io.legato.kazusa.utils.postEvent
|
||||
|
||||
|
||||
class TimeBatteryReceiver : BroadcastReceiver() {
|
||||
|
||||
val filter = IntentFilter().apply {
|
||||
addAction(Intent.ACTION_TIME_TICK)
|
||||
addAction(Intent.ACTION_BATTERY_CHANGED)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
when (intent.action) {
|
||||
Intent.ACTION_TIME_TICK -> {
|
||||
postEvent(EventBus.TIME_CHANGED, "")
|
||||
}
|
||||
Intent.ACTION_BATTERY_CHANGED -> {
|
||||
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
||||
postEvent(EventBus.BATTERY_CHANGED, level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user