Skip to content

Commit

Permalink
Fixed app manager APK sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Aug 13, 2024
1 parent c5f9fd9 commit d40552f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
applicationId = "com.d4rk.cleaner"
minSdk = 26
targetSdk = 34
versionCode = 101
versionCode = 102
versionName = "2.0.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.core.content.FileProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d4rk.cleaner.R
import com.d4rk.cleaner.data.model.ui.appmanager.ui.ApkInfo
Expand Down Expand Up @@ -380,18 +381,19 @@ fun ApkItemComposable(apkPath : String) {
DropdownMenuItem(text = { Text(stringResource(R.string.share)) } , onClick = {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "application/vnd.android.package-archive"
shareIntent.putExtra(Intent.EXTRA_STREAM , Uri.fromFile(apkFile))
context.startActivity(Intent.createChooser(shareIntent , "Share APK"))
val contentUri : Uri = FileProvider.getUriForFile(context , "${context.packageName}.fileprovider" , apkFile)
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_apk)))
})

DropdownMenuItem(text = { Text(stringResource(id = R.string.installed)) } ,
DropdownMenuItem(text = { Text(stringResource(id = R.string.install)) } ,
onClick = {
val installIntent = Intent(Intent.ACTION_VIEW)
installIntent.setDataAndType(
Uri.fromFile(apkFile) ,
"application/vnd.android.package-archive"
)
val contentUri : Uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", apkFile)
installIntent.setDataAndType(contentUri, "application/vnd.android.package-archive")
installIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(installIntent)
})
}
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/kotlin/com/d4rk/cleaner/utils/PermissionsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Activity
import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
Expand All @@ -26,7 +27,7 @@ object PermissionsUtils {
* @return True if all required permissions are granted, false otherwise.
*/
fun hasStoragePermissions(context : Context) : Boolean {
val hasStoragePermissions = when {
val hasStoragePermissions : Boolean = when {
Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q -> ContextCompat.checkSelfPermission(
context , Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
Expand All @@ -38,21 +39,21 @@ object PermissionsUtils {
else -> true
}

val hasManageStoragePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val hasManageStoragePermission : Boolean = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Environment.isExternalStorageManager()
}
else {
true
}

val hasUsageStatsPermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val hasUsageStatsPermission : Boolean = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
isAccessGranted(context)
}
else {
true
}

val hasMediaPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val hasMediaPermissions : Boolean = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ContextCompat.checkSelfPermission(
context , Manifest.permission.READ_MEDIA_AUDIO
) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
Expand All @@ -75,14 +76,14 @@ object PermissionsUtils {
* @param activity The Activity instance required to request permissions.
*/
fun requestStoragePermissions(activity : Activity) {
val requiredPermissions = mutableListOf(
val requiredPermissions : MutableList<String> = mutableListOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE , Manifest.permission.READ_EXTERNAL_STORAGE
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (! Environment.isExternalStorageManager()) {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
val uri = Uri.fromParts("package" , activity.packageName , null)
val uri : Uri = Uri.fromParts("package" , activity.packageName , null)
intent.data = uri
activity.startActivity(intent)
}
Expand Down Expand Up @@ -117,9 +118,9 @@ object PermissionsUtils {
* @return True if access is granted, false otherwise.
*/
private fun isAccessGranted(context : Context) : Boolean = try {
val packageManager = context.packageManager
val applicationInfo = packageManager.getApplicationInfo(context.packageName , 0)
val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
val packageManager : PackageManager = context.packageManager
val applicationInfo : ApplicationInfo = packageManager.getApplicationInfo(context.packageName , 0)
val appOpsManager : AppOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
@Suppress("DEPRECATION") val mode : Int = appOpsManager.checkOpNoThrow(
AppOpsManager.OPSTR_GET_USAGE_STATS , applicationInfo.uid , applicationInfo.packageName
)
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 @@ -30,6 +30,8 @@
<string name="share">Share</string>
<string name="app_info">App info</string>
<string name="installed">Installed"</string>
<string name="install">Install"</string>
<string name="share_apk">Share APK</string>

<string name="memory_manager">Memory Manager</string>
<string name="storage_information">Storage Information</string>
Expand Down

0 comments on commit d40552f

Please sign in to comment.