Skip to content

Commit

Permalink
Add instruction to allow opening links
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Oct 17, 2024
1 parent 2e4b9ea commit 8c27329
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.widget.Toast
import androidx.activity.viewModels
import androidx.core.net.toFile
import androidx.core.net.toUri
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dev.brahmkshatriya.echo.extensions.getPackageName
import dev.brahmkshatriya.echo.ui.extension.ExtensionInstallerBottomSheet
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -62,14 +65,36 @@ class ExtensionOpenerActivity : Activity() {
val file = b.getString("file")?.toUri()?.toFile()
val install = b.getBoolean("install")
val installAsApk = b.getBoolean("installAsApk")
val links = b.getStringArrayList("links").orEmpty()
val context = this
if (install && file != null) {
val extensionViewModel by viewModels<ExtensionViewModel>()
lifecycleScope.launch {
extensionViewModel.install(context, file, installAsApk)
val result = extensionViewModel.install(context, file, installAsApk)
if (result && installAsApk) {
context.createLinksDialog(file, links)
}
}
}
}
}

private fun Context.createLinksDialog(
file: File, links: List<String>
) = MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.allow_opening_links))
.setMessage(
links.joinToString("\n") + "\n" +
getString(R.string.open_links_instruction)
)
.setPositiveButton(getString(R.string.ok)) { dialog, _ ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val packageName = getPackageName(file.path)
intent.setData(Uri.parse("package:$packageName"))
startActivity(intent)
dialog.dismiss()
}
.setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.dismiss() }
.show()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.brahmkshatriya.echo.extensions

import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -75,10 +76,7 @@ suspend fun uninstallExtension(context: FragmentActivity, extension: Extension<*
}

ImportType.App -> {
val packageInfo = context.packageManager.getPackageArchiveInfo(
extension.metadata.path, ApkPluginSource.PACKAGE_FLAGS
)!!
val packageName = packageInfo.packageName
val packageName = context.getPackageName(extension.metadata.path)
val intent = Intent(Intent.ACTION_DELETE).apply {
data = "package:$packageName".toUri()
putExtra(Intent.EXTRA_RETURN_RESULT, true)
Expand All @@ -88,6 +86,10 @@ suspend fun uninstallExtension(context: FragmentActivity, extension: Extension<*
}
}

fun Context.getPackageName(path: String) = packageManager.getPackageArchiveInfo(
path, ApkPluginSource.PACKAGE_FLAGS
)!!.packageName

private suspend fun FragmentActivity.waitForResult(intent: Intent) = suspendCoroutine { cont ->
val contract = ActivityResultContracts.StartActivityForResult()
val activityResultLauncher = registerActivityResultLauncher(contract) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class ApkPluginSource(

private fun Context.getStaticPackages(featureName: String): List<ApplicationInfo> {
return packageManager.getInstalledPackages(PACKAGE_FLAGS).filter {
it.reqFeatures.orEmpty().any { featureInfo ->
featureInfo.name == featureName
}
}.mapNotNull { it.applicationInfo }
it.reqFeatures.orEmpty().any { featureInfo ->
featureInfo.name == featureName
}
}.mapNotNull { it.applicationInfo }
}

companion object {
Expand All @@ -40,6 +40,6 @@ class ApkPluginSource(
}

override fun onPackageChanged() {
context.getStaticPackages(featureName)
loadedPlugins.value = context.getStaticPackages(featureName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ class ExtensionViewModel @Inject constructor(
}
}

suspend fun install(context: FragmentActivity, file: File, installAsApk: Boolean) {
suspend fun install(context: FragmentActivity, file: File, installAsApk: Boolean): Boolean {
val result = installExtension(context, file, installAsApk).getOrElse {
throwableFlow.emit(it)
false
}
if (result) messageFlow.emit(SnackBar.Message(app.getString(R.string.extension_installed_successfully)))
return result
}

suspend fun uninstall(
Expand All @@ -114,7 +115,7 @@ class ExtensionViewModel @Inject constructor(
fun moveExtensionItem(type: ExtensionType, toPos: Int, fromPos: Int) {
settings.edit {
val flow = extensionLoader.priorityMap[type]!!
val list = flow.value.toMutableList()
val list = getExtensionListFlow(type).value.orEmpty().map { it.id }.toMutableList()
list.add(toPos, list.removeAt(fromPos))
flow.value = list
putString(type.priorityKey(), list.joinToString(","))
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Selectable"
android:background="?colorSurface"
android:background="?echoBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@
<string name="invalid_extension">Invalid Extension</string>
<string name="extension_installed_successfully">Extension installed successfully.</string>
<string name="extension_uninstalled_successfully">Extension uninstalled successfully.</string>
<string name="allow_opening_links">Allow opening links</string>
<string name="open_links_instruction">To allow opening these links, you need to:\n- Scroll down and click on \"Open by Default\"\n- Click on \"+ Add Link\"</string>
<string-array name="stream_qualities">
<item>Highest</item>
<item>Medium</item>
Expand Down

0 comments on commit 8c27329

Please sign in to comment.