1
1
package com.d4rk.cleaner.ui.appmanager
2
2
3
+ import android.app.Application
3
4
import android.content.Intent
4
5
import android.content.pm.ApplicationInfo
5
- import android.content.pm.PackageManager
6
6
import android.graphics.Bitmap
7
7
import android.graphics.Canvas
8
8
import android.graphics.drawable.BitmapDrawable
9
9
import android.net.Uri
10
- import android.provider.MediaStore
11
10
import android.provider.Settings
12
11
import androidx.compose.foundation.Image
13
12
import androidx.compose.foundation.layout.Box
@@ -33,7 +32,7 @@ import androidx.compose.material3.TabRowDefaults
33
32
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
34
33
import androidx.compose.material3.Text
35
34
import androidx.compose.runtime.Composable
36
- import androidx.compose.runtime.LaunchedEffect
35
+ import androidx.compose.runtime.collectAsState
37
36
import androidx.compose.runtime.getValue
38
37
import androidx.compose.runtime.mutableIntStateOf
39
38
import androidx.compose.runtime.mutableStateOf
@@ -49,24 +48,24 @@ import androidx.compose.ui.res.imageResource
49
48
import androidx.compose.ui.res.stringResource
50
49
import androidx.compose.ui.text.style.TextOverflow
51
50
import androidx.compose.ui.unit.dp
51
+ import androidx.lifecycle.viewmodel.compose.viewModel
52
52
import com.d4rk.cleaner.R
53
+ import com.d4rk.cleaner.data.model.ui.ApkInfo
53
54
import java.io.File
54
55
55
56
/* *
56
57
* Composable function for managing and displaying different app categories.
57
- *
58
- * This composable function displays tabs for "Installed Apps", "System Apps", and "App Install Files".
59
- * Each tab shows corresponding app information based on the selected category.
60
58
*/
61
59
@Composable
62
60
fun AppManagerComposable () {
61
+
62
+ val viewModel: AppManagerViewModel = viewModel(
63
+ factory = AppManagerViewModelFactory (LocalContext .current.applicationContext as Application )
64
+ )
63
65
val tabs = listOf (" Installed Apps" , " System Apps" , " App Install Files" )
64
66
var selectedIndex by remember { mutableIntStateOf(0 ) }
65
- var apps by remember { mutableStateOf(listOf<ApplicationInfo >()) }
66
- val context = LocalContext .current
67
- LaunchedEffect (Unit ) {
68
- apps = context.packageManager.getInstalledApplications(PackageManager .GET_META_DATA )
69
- }
67
+ val installedApps by viewModel.installedApps.collectAsState()
68
+ val apkFiles by viewModel.apkFiles.collectAsState()
70
69
71
70
Column {
72
71
TabRow (
@@ -94,9 +93,11 @@ fun AppManagerComposable() {
94
93
}
95
94
}
96
95
when (selectedIndex) {
97
- 0 -> AppsComposable (apps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM == 0 })
98
- 1 -> AppsComposable (apps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM != 0 })
99
- 2 -> ApksComposable ()
96
+ 0 -> AppsComposable (
97
+ apps = installedApps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM == 0 })
98
+ 1 -> AppsComposable (
99
+ apps = installedApps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM != 0 })
100
+ 2 -> ApksComposable (apkFiles = apkFiles)
100
101
}
101
102
}
102
103
}
@@ -181,7 +182,9 @@ fun AppItemComposable(
181
182
}
182
183
183
184
DropdownMenu (expanded = showMenu, onDismissRequest = { showMenu = false }) {
184
- DropdownMenuItem (text = { Text (stringResource(R .string.uninstall)) },
185
+ DropdownMenuItem (text = {
186
+ Text (stringResource(R .string.uninstall))
187
+ },
185
188
onClick = {
186
189
val uri = Uri .fromParts(" package" , app.packageName, null )
187
190
val intent = Intent (Intent .ACTION_DELETE , uri)
@@ -220,33 +223,16 @@ fun AppItemComposable(
220
223
* Composable function for displaying a list of APK files on the device.
221
224
*/
222
225
@Composable
223
- fun ApksComposable () {
224
- val context = LocalContext .current
225
- val uri = MediaStore .Files .getContentUri(" external" )
226
- val cursor = context.contentResolver.query(
227
- uri,
228
- arrayOf(MediaStore .Files .FileColumns .DATA ),
229
- MediaStore .Files .FileColumns .MIME_TYPE + " =?" ,
230
- arrayOf(" application/vnd.android.package-archive" ),
231
- null
232
- )
233
-
234
- var apkPaths = listOf<String >()
235
- cursor?.use {
236
- while (it.moveToNext()) {
237
- val dataColumnIndex = it.getColumnIndex(MediaStore .Files .FileColumns .DATA )
238
- val filePath = it.getString(dataColumnIndex)
239
- apkPaths = apkPaths + filePath
240
- }
241
- }
226
+ fun ApksComposable (apkFiles : List <ApkInfo >) {
242
227
243
228
LazyColumn {
244
- items(apkPaths ) { apkPath ->
245
- ApkItemComposable (apkPath)
229
+ items(apkFiles ) { apkInfo ->
230
+ ApkItemComposable (apkPath = apkInfo.path )
246
231
}
247
232
}
248
233
}
249
234
235
+
250
236
/* *
251
237
* Composable function for displaying detailed information about an APK file.
252
238
*
@@ -321,7 +307,8 @@ fun ApkItemComposable(apkPath: String) {
321
307
DropdownMenuItem (text = { Text (" Install" ) }, onClick = {
322
308
val installIntent = Intent (Intent .ACTION_VIEW )
323
309
installIntent.setDataAndType(
324
- Uri .fromFile(apkFile), " application/vnd.android.package-archive"
310
+ Uri .fromFile(apkFile),
311
+ " application/vnd.android.package-archive"
325
312
)
326
313
installIntent.flags = Intent .FLAG_ACTIVITY_NEW_TASK
327
314
context.startActivity(installIntent)
0 commit comments