-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added view model for the app manager
- Loading branch information
1 parent
10c7945
commit c4d1b98
Showing
5 changed files
with
127 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/src/main/kotlin/com/d4rk/cleaner/data/model/ui/ApkInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.d4rk.cleaner.data.model.ui | ||
|
||
data class ApkInfo( | ||
val id: Long, | ||
val path: String, | ||
val size: Long | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/main/kotlin/com/d4rk/cleaner/ui/appmanager/AppManagerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.d4rk.cleaner.ui.appmanager | ||
|
||
import android.app.Application | ||
import android.content.pm.ApplicationInfo | ||
import android.content.pm.PackageManager | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import android.provider.MediaStore | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.d4rk.cleaner.data.model.ui.ApkInfo | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
class AppManagerViewModel(private val application: Application) : ViewModel() { | ||
private val _installedApps = MutableStateFlow<List<ApplicationInfo>>(emptyList()) | ||
val installedApps: StateFlow<List<ApplicationInfo>> = _installedApps.asStateFlow() | ||
|
||
private val _apkFiles = MutableStateFlow<List<ApkInfo>>(emptyList()) | ||
val apkFiles: StateFlow<List<ApkInfo>> = _apkFiles.asStateFlow() | ||
|
||
|
||
init { | ||
loadInstalledApps() | ||
loadApkFiles() | ||
} | ||
|
||
private fun loadInstalledApps() { | ||
viewModelScope.launch { | ||
_installedApps.value = getInstalledApps() | ||
} | ||
} | ||
|
||
private suspend fun getInstalledApps(): List<ApplicationInfo> { | ||
return withContext(Dispatchers.IO) { | ||
application.packageManager.getInstalledApplications(PackageManager.GET_META_DATA) | ||
} | ||
} | ||
|
||
private fun loadApkFiles() { | ||
viewModelScope.launch { | ||
_apkFiles.value = getApkFilesFromStorage() | ||
} | ||
} | ||
|
||
private suspend fun getApkFilesFromStorage(): List<ApkInfo> { | ||
return withContext(Dispatchers.IO) { | ||
val apkFiles = mutableListOf<ApkInfo>() | ||
val uri: Uri = MediaStore.Files.getContentUri("external") | ||
val projection = arrayOf( | ||
MediaStore.Files.FileColumns._ID, | ||
MediaStore.Files.FileColumns.DATA, | ||
MediaStore.Files.FileColumns.SIZE | ||
) | ||
val selection = "${MediaStore.Files.FileColumns.MIME_TYPE} = ?" | ||
val selectionArgs = arrayOf("application/vnd.android.package-archive") | ||
val cursor: Cursor? = application.contentResolver.query( | ||
uri, projection, selection, selectionArgs, null | ||
) | ||
|
||
cursor?.use { | ||
val idColumn = it.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID) | ||
val dataColumn = it.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA) | ||
val sizeColumn = it.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE) | ||
|
||
while (it.moveToNext()) { | ||
val id = it.getLong(idColumn) | ||
val path = it.getString(dataColumn) | ||
val size = it.getLong(sizeColumn) | ||
apkFiles.add(ApkInfo(id, path, size)) | ||
} | ||
} | ||
apkFiles | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/kotlin/com/d4rk/cleaner/ui/appmanager/AppManagerViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.d4rk.cleaner.ui.appmanager | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
|
||
class AppManagerViewModelFactory(private val application: Application) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(AppManagerViewModel::class.java)) { | ||
@Suppress("UNCHECKED_CAST") | ||
return AppManagerViewModel(application) as T | ||
} | ||
throw IllegalArgumentException("Unknown ViewModel class") | ||
} | ||
} |