[ad_1]
I’m trying to delete the cache, not my application, but when I look in the settings, everything remains the same. As far as I understand, you need to find the /cache folder in the application package and delete it, are there any other ways?
Now I get a list of applications installed on the phone:
private fun getListOfAppsInfo(activity: Activity, isAll: Int): MutableList<ApplicationInfo> {
val appForReturnedList = mutableListOf<ApplicationInfo>()
val appsInfoList: MutableList<ApplicationInfo> = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
activity.packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
} else {
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val arrayAppsNew = mutableListOf<ApplicationInfo>()
val listAppsReturned = activity.packageManager.queryIntentActivities(intent, 0)
listAppsReturned.forEach {
arrayAppsNew.add(it.activityInfo.applicationInfo)
}
arrayAppsNew
}
val appsInstalled: MutableList<ApplicationInfo> = mutableListOf()
val appsSystem: MutableList<ApplicationInfo> = mutableListOf()
(appsInfoList.indices).forEach { i ->
// if (appsInfoList[i].packageName != activity.packageName) {
if (appsInfoList[i].flags and ApplicationInfo.FLAG_SYSTEM == 1) {
appsSystem.add(appsInfoList[i])
} else {
appsInstalled.add(appsInfoList[i])
getPackagePathByPackageName(this, appsInfoList[i].packageName)
}
// }
}
when (isAll) {
ALL_APPS -> {
appForReturnedList.addAll(appsInstalled)
appForReturnedList.addAll(appsSystem)
}
USER_APPS -> appForReturnedList.addAll(appsInstalled)
SYSTEM_APPS -> appForReturnedList.addAll(appsSystem)
else -> appForReturnedList.addAll(appsInstalled)
}
return appForReturnedList
}
private fun getAppsListInfo(activity: Activity, isAll: Int): ArrayList<String> {
val appsArrayList: ArrayList<String> = ArrayList()
for (app in getListOfAppsInfo(activity, isAll)) {
appsArrayList.add(app.packageName)
}
return appsArrayList
}
Then, by the package name, I find the path to the desired folder, if there is one:
private fun getPackagePathByPackageName(activity: Activity, packageName: String): String {
val packageManager = activity.packageManager
var packagePath = packageName
val packageInfo = packageManager.getPackageInfo(packagePath, 0)
var appCacheDir = ""
packagePath = packageInfo.applicationInfo.dataDir
val dir = File("$packagePath/cache")
if (dir.exists()) {
appCacheDir = dir.absolutePath
}
return appCacheDir
}
And delete it:
private fun deleteCache(context: Context, packagePathByPackageName: String) {
try {
// val dir: File = context.cacheDir
val dir = File(packagePathByPackageName)
deleteDir(dir)
} catch (e: Exception) {
}
}
private fun deleteDir(dir: File?): Boolean {
return if (dir != null && dir.isDirectory) {
dir.deleteRecursively()
val children: Array<String> = dir.list()
for (i in children.indices) {
val success = deleteDir(File(dir, children[i]))
if (!success) {
return false
}
}
dir.deleteRecursively()
} else if (dir != null && dir.isFile) {
dir.deleteRecursively()
} else {
false
}
}
But nothing happens, even if I substitute the path to my application.
Here is the list of permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_MANAGE_WRITE_SETTINGS " />
<uses-permission
android:name="android.permission.CLEAR_APP_CACHE"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.GET_PACKAGE_SIZE"
tools:ignore="ProtectedPermissions" />
<queries>
<package android:name="QUERY_ALL_PACKAGES" />
<package android:name="com.android.settings" />
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
<intent>
<action android:name="android.intent.action.POWER_USAGE_SUMMARY" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
</intent>
</queries>
And I request the permissions READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, maybe some are missing? Or this cannot be done at all, it seems like based on this https://developer.android.com/training/data-storage/app-specific after api 29+ you cannot get such access. I tested it on a 30+ phone and the folders with the cache are really not visible, on 21+ they are visible but do not delete anything, it’s the same on emulators. Can you please tell me if this can be done at all, if so, what am I doing wrong?
[ad_2]