|
| 1 | +package com.appcontrolx.data.repository |
| 2 | + |
| 3 | +import com.appcontrolx.model.AppInfo |
| 4 | +import com.appcontrolx.service.AppFetcher |
| 5 | +import com.appcontrolx.service.BatteryPolicyManager |
| 6 | +import kotlinx.coroutines.flow.Flow |
| 7 | +import kotlinx.coroutines.flow.flow |
| 8 | +import javax.inject.Inject |
| 9 | +import javax.inject.Singleton |
| 10 | + |
| 11 | +interface AppRepository { |
| 12 | + fun getAllApps(): Flow<Result<List<AppInfo>>> |
| 13 | + fun getUserApps(): Flow<Result<List<AppInfo>>> |
| 14 | + fun getSystemApps(): Flow<Result<List<AppInfo>>> |
| 15 | + suspend fun freezeApp(packageName: String): Result<Unit> |
| 16 | + suspend fun unfreezeApp(packageName: String): Result<Unit> |
| 17 | + suspend fun uninstallApp(packageName: String): Result<Unit> |
| 18 | + suspend fun forceStopApp(packageName: String): Result<Unit> |
| 19 | + suspend fun restrictBackground(packageName: String): Result<Unit> |
| 20 | + suspend fun allowBackground(packageName: String): Result<Unit> |
| 21 | + suspend fun clearCache(packageName: String): Result<Unit> |
| 22 | + suspend fun clearData(packageName: String): Result<Unit> |
| 23 | +} |
| 24 | + |
| 25 | +@Singleton |
| 26 | +class AppRepositoryImpl @Inject constructor( |
| 27 | + private val appFetcher: AppFetcher, |
| 28 | + private val policyManager: BatteryPolicyManager? |
| 29 | +) : AppRepository { |
| 30 | + |
| 31 | + override fun getAllApps(): Flow<Result<List<AppInfo>>> = flow { |
| 32 | + try { |
| 33 | + val apps = appFetcher.getAllApps() |
| 34 | + emit(Result.success(apps)) |
| 35 | + } catch (e: Exception) { |
| 36 | + emit(Result.failure(e)) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + override fun getUserApps(): Flow<Result<List<AppInfo>>> = flow { |
| 41 | + try { |
| 42 | + val apps = appFetcher.getUserApps() |
| 43 | + emit(Result.success(apps)) |
| 44 | + } catch (e: Exception) { |
| 45 | + emit(Result.failure(e)) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + override fun getSystemApps(): Flow<Result<List<AppInfo>>> = flow { |
| 50 | + try { |
| 51 | + val apps = appFetcher.getSystemApps() |
| 52 | + emit(Result.success(apps)) |
| 53 | + } catch (e: Exception) { |
| 54 | + emit(Result.failure(e)) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override suspend fun freezeApp(packageName: String): Result<Unit> { |
| 59 | + return policyManager?.freezeApp(packageName) |
| 60 | + ?: Result.failure(Exception("Policy manager not available")) |
| 61 | + } |
| 62 | + |
| 63 | + override suspend fun unfreezeApp(packageName: String): Result<Unit> { |
| 64 | + return policyManager?.unfreezeApp(packageName) |
| 65 | + ?: Result.failure(Exception("Policy manager not available")) |
| 66 | + } |
| 67 | + |
| 68 | + override suspend fun uninstallApp(packageName: String): Result<Unit> { |
| 69 | + return policyManager?.uninstallApp(packageName) |
| 70 | + ?: Result.failure(Exception("Policy manager not available")) |
| 71 | + } |
| 72 | + |
| 73 | + override suspend fun forceStopApp(packageName: String): Result<Unit> { |
| 74 | + return policyManager?.forceStop(packageName) |
| 75 | + ?: Result.failure(Exception("Policy manager not available")) |
| 76 | + } |
| 77 | + |
| 78 | + override suspend fun restrictBackground(packageName: String): Result<Unit> { |
| 79 | + return policyManager?.restrictBackground(packageName) |
| 80 | + ?: Result.failure(Exception("Policy manager not available")) |
| 81 | + } |
| 82 | + |
| 83 | + override suspend fun allowBackground(packageName: String): Result<Unit> { |
| 84 | + return policyManager?.allowBackground(packageName) |
| 85 | + ?: Result.failure(Exception("Policy manager not available")) |
| 86 | + } |
| 87 | + |
| 88 | + override suspend fun clearCache(packageName: String): Result<Unit> { |
| 89 | + return policyManager?.clearCache(packageName) |
| 90 | + ?: Result.failure(Exception("Policy manager not available")) |
| 91 | + } |
| 92 | + |
| 93 | + override suspend fun clearData(packageName: String): Result<Unit> { |
| 94 | + return policyManager?.clearData(packageName) |
| 95 | + ?: Result.failure(Exception("Policy manager not available")) |
| 96 | + } |
| 97 | +} |
0 commit comments