Skip to content

Use app label in shared APK filename #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,31 @@ class AppPackageManagerImpl(private val application: Application) : ApkInstaller
}
}

override fun prepareShareIntent(apkPath: String): Flow<DataState<Intent, Errors>> = flow {
runCatching {
val apkFile = File(apkPath)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "application/vnd.android.package-archive"
val contentUri: Uri = FileProvider.getUriForFile(
application, "${application.packageName}.fileprovider", apkFile
)
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooserIntent = Intent.createChooser(shareIntent, null)
chooserIntent
}.onSuccess { chooserIntent: Intent ->
emit(value = DataState.Success(data = chooserIntent))
}.onFailure { throwable ->
emit(value = DataState.Error(error = throwable.toError(default = Errors.UseCase.FAILED_TO_SHARE_APK)))
override fun prepareShareIntent(apkPath: String, packageName: String): Flow<DataState<Intent, Errors>> =
flow {
runCatching {
val sourceFile = File(apkPath)
val label = getAppName(packageName).replace("[^A-Za-z0-9._-]".toRegex(), "_")
val cachedFile = File(application.cacheDir, "$label.apk")
sourceFile.copyTo(cachedFile, overwrite = true)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "application/vnd.android.package-archive"
val contentUri: Uri = FileProvider.getUriForFile(
application, "${application.packageName}.fileprovider", cachedFile
)
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Intent.createChooser(shareIntent, null)
}.onSuccess { chooserIntent: Intent ->
emit(value = DataState.Success(data = chooserIntent))
}.onFailure { throwable ->
emit(
value = DataState.Error(
error = throwable.toError(default = Errors.UseCase.FAILED_TO_SHARE_APK)
)
)
}
}
}

override fun shareApp(packageName: String): Flow<DataState<Intent, Errors>> = flow {
runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import com.d4rk.cleaner.core.domain.model.network.Errors
import kotlinx.coroutines.flow.Flow

interface ApkSharer {
fun prepareShareIntent(apkPath: String): Flow<DataState<Intent, Errors>>
fun prepareShareIntent(
apkPath: String,
packageName: String,
): Flow<DataState<Intent, Errors>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import com.d4rk.cleaner.core.domain.model.network.Errors
import kotlinx.coroutines.flow.Flow

class ShareApkUseCase(private val apkSharer: ApkSharer) {
operator fun invoke(apkPath: String): Flow<DataState<Intent, Errors>> {
return apkSharer.prepareShareIntent(apkPath = apkPath)
operator fun invoke(apkPath: String, packageName: String): Flow<DataState<Intent, Errors>> {
return apkSharer.prepareShareIntent(
apkPath = apkPath,
packageName = packageName,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.io.File
import java.util.Locale

class AppManagerViewModel(
Expand Down Expand Up @@ -287,7 +288,11 @@ class AppManagerViewModel(
launch(dispatchers.io) {
when (item) {
is AppManagerItem.ApkFile -> {
shareApkUseCase(item.path).collectLatest { result ->
val pkgName =
applicationContext.packageManager
.getPackageArchiveInfo(item.path, 0)?.packageName
?: File(item.path).nameWithoutExtension
shareApkUseCase(item.path, pkgName).collectLatest { result ->
when (result) {
is DataState.Success -> sendAction(
AppManagerAction.LaunchShareIntent(
Expand Down Expand Up @@ -341,7 +346,7 @@ class AppManagerViewModel(
runCatching {
applicationContext.packageManager.getApplicationInfo(packageName, 0).publicSourceDir
}.onSuccess { apkPath ->
shareApkUseCase(apkPath).collectLatest { result ->
shareApkUseCase(apkPath, packageName).collectLatest { result ->
when (result) {
is DataState.Success -> sendAction(
AppManagerAction.LaunchShareIntent(result.data),
Expand Down