LibreTube/app/src/main/java/com/github/libretube/RouterActivity.kt

46 lines
1.5 KiB
Kotlin
Raw Normal View History

2022-05-29 16:31:04 +05:30
package com.github.libretube
import android.content.Intent
2022-05-29 18:52:35 +05:30
import android.content.pm.PackageManager
import android.net.Uri
2022-05-29 16:31:04 +05:30
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class RouterActivity : AppCompatActivity() {
2022-05-29 18:52:35 +05:30
val TAG = "RouterActivity"
2022-05-29 16:31:04 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
when (intent?.action) {
Intent.ACTION_SEND -> {
2022-05-30 17:54:50 +05:30
if (intent.type == "text/plain" && checkHost(intent)) {
2022-05-29 19:27:01 +05:30
handleSendText(intent)
} else {
// start app as normal if wrong intent type
restartMainActivity(this)
2022-05-29 16:31:04 +05:30
}
}
}
}
2022-05-30 17:54:50 +05:30
private fun checkHost(intent: Intent): Boolean {
val hostsList = resources.getStringArray(R.array.shareHostsList)
val intentDataUri: Uri = Uri.parse(intent.getStringExtra(Intent.EXTRA_TEXT))
val intentDataHost = intentDataUri.host
Log.d(TAG, "$intentDataHost")
return hostsList.contains(intentDataHost)
}
2022-05-29 16:31:04 +05:30
private fun handleSendText(intent: Intent) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
2022-05-29 18:52:35 +05:30
Log.i(TAG, it)
val pm: PackageManager = this.packageManager
val intent = pm.getLaunchIntentForPackage(this.packageName)
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
intent?.data = Uri.parse(it)
this.startActivity(intent)
2022-05-29 21:41:05 +05:30
this.finishAndRemoveTask()
2022-05-29 16:31:04 +05:30
}
}
2022-05-29 18:52:35 +05:30
}