Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
feat: launch and install from apk intent (#279)
Browse files Browse the repository at this point in the history
close #90
  • Loading branch information
JasonKhew96 authored Dec 11, 2023
1 parent 5bf4944 commit 4bf1371
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
8 changes: 8 additions & 0 deletions manager/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="application/vnd.android.package-archive" />
</intent-filter>
</activity>

<service
Expand Down
31 changes: 30 additions & 1 deletion manager/src/main/java/org/lsposed/lspatch/ui/page/HomeScreen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.lsposed.lspatch.ui.page

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.compose.foundation.clickable
Expand All @@ -16,7 +18,11 @@ import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -27,10 +33,13 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.launch
import org.lsposed.lspatch.R
import org.lsposed.lspatch.share.LSPConfig
import org.lsposed.lspatch.ui.component.CenterTopBar
import org.lsposed.lspatch.ui.page.destinations.ManageScreenDestination
import org.lsposed.lspatch.ui.page.destinations.NewPatchScreenDestination
import org.lsposed.lspatch.ui.util.HtmlText
import org.lsposed.lspatch.ui.util.LocalSnackbarHost
import org.lsposed.lspatch.util.ShizukuApi
Expand All @@ -40,7 +49,27 @@ import rikka.shizuku.Shizuku
@RootNavGraph(start = true)
@Destination
@Composable
fun HomeScreen() {
fun HomeScreen(navigator: DestinationsNavigator) {
// Install from intent
var isIntentLaunched by rememberSaveable { mutableStateOf(false) }
val activity = LocalContext.current as Activity
val intent = activity.intent
LaunchedEffect(Unit) {
if (!isIntentLaunched && intent.action == Intent.ACTION_VIEW && intent.hasCategory(Intent.CATEGORY_DEFAULT) && intent.type == "application/vnd.android.package-archive") {
isIntentLaunched = true
val uri = intent.data
if (uri != null) {
navigator.navigate(ManageScreenDestination)
navigator.navigate(
NewPatchScreenDestination(
id = ACTION_INTENT_INSTALL,
data = uri
)
)
}
}
}

Scaffold(
topBar = { CenterTopBar(stringResource(R.string.app_name)) }
) { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageInstaller
import android.net.Uri
import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
Expand Down Expand Up @@ -58,13 +59,18 @@ import org.lsposed.lspatch.util.ShizukuApi

private const val TAG = "NewPatchPage"

const val ACTION_STORAGE = 0
const val ACTION_APPLIST = 1
const val ACTION_INTENT_INSTALL = 2

@OptIn(ExperimentalMaterial3Api::class)
@Destination
@Composable
fun NewPatchScreen(
navigator: DestinationsNavigator,
resultRecipient: ResultRecipient<SelectAppsScreenDestination, SelectAppsResult>,
from: String
id: Int,
data: Uri? = null
) {
val viewModel = viewModel<NewPatchViewModel>()
val snackbarHost = LocalSnackbarHost.current
Expand Down Expand Up @@ -116,11 +122,34 @@ fun NewPatchScreen(
PatchState.INIT -> {
LaunchedEffect(Unit) {
LSPPackageManager.cleanTmpApkDir()
when (from) {
"storage" -> storageLauncher.launch(arrayOf("application/vnd.android.package-archive"))
"applist" -> navigator.navigate(SelectAppsScreenDestination(false))
when (id) {
ACTION_STORAGE -> {
storageLauncher.launch(arrayOf("application/vnd.android.package-archive"))
viewModel.dispatch(ViewAction.DoneInit)
}

ACTION_APPLIST -> {
navigator.navigate(SelectAppsScreenDestination(false))
viewModel.dispatch(ViewAction.DoneInit)
}

ACTION_INTENT_INSTALL -> {
runBlocking {
data?.let { uri ->
LSPPackageManager.getAppInfoFromApks(listOf(uri)).onSuccess {
viewModel.dispatch(ViewAction.ConfigurePatch(it.first()))
}.onFailure {
lspApp.globalScope.launch {
snackbarHost.showSnackbar(
it.message ?: errorUnknown
)
}
navigator.navigateUp()
}
}
}
}
}
viewModel.dispatch(ViewAction.DoneInit)
}
}
PatchState.SELECTING -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import org.lsposed.lspatch.share.LSPConfig
import org.lsposed.lspatch.ui.component.AnywhereDropdown
import org.lsposed.lspatch.ui.component.AppItem
import org.lsposed.lspatch.ui.component.LoadingDialog
import org.lsposed.lspatch.ui.page.ACTION_APPLIST
import org.lsposed.lspatch.ui.page.ACTION_STORAGE
import org.lsposed.lspatch.ui.page.SelectAppsResult
import org.lsposed.lspatch.ui.page.destinations.NewPatchScreenDestination
import org.lsposed.lspatch.ui.page.destinations.SelectAppsScreenDestination
Expand Down Expand Up @@ -329,7 +331,7 @@ fun AppManageFab(navigator: DestinationsNavigator) {
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.secondary),
onClick = {
navigator.navigate(NewPatchScreenDestination("storage"))
navigator.navigate(NewPatchScreenDestination(id = ACTION_STORAGE))
showNewPatchDialog = false
}
) {
Expand All @@ -343,7 +345,7 @@ fun AppManageFab(navigator: DestinationsNavigator) {
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.secondary),
onClick = {
navigator.navigate(NewPatchScreenDestination("applist"))
navigator.navigate(NewPatchScreenDestination(id = ACTION_APPLIST))
showNewPatchDialog = false
}
) {
Expand Down

0 comments on commit 4bf1371

Please sign in to comment.