Merge pull request #279 from vector-im/feature/clear_glide_cache

Clear media cache from the settings and clear cache when signing out
This commit is contained in:
Benoit Marty 2019-07-02 21:56:31 +02:00 committed by GitHub
commit d90698fe92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 68 deletions

View File

@ -28,12 +28,9 @@ typealias ActionOnFile = (file: File) -> Boolean
* Delete * Delete
* ========================================================================================== */ * ========================================================================================== */
fun deleteAllFiles(context: Context) { fun deleteAllFiles(root: File) {
Timber.v("Delete cache dir:") Timber.v("Delete ${root.absolutePath}")
recursiveActionOnFile(context.cacheDir, ::deleteAction) recursiveActionOnFile(root, ::deleteAction)
Timber.v("Delete files dir:")
recursiveActionOnFile(context.filesDir, ::deleteAction)
} }
private fun deleteAction(file: File): Boolean { private fun deleteAction(file: File): Boolean {
@ -130,4 +127,21 @@ fun getFileExtension(fileUri: String): String? {
} }
return null return null
} }
/* ==========================================================================================
* Size
* ========================================================================================== */
fun getSizeOfFiles(context: Context, root: File): Int {
Timber.v("Get size of " + root.absolutePath)
return if (root.isDirectory) {
root.list()
.map {
getSizeOfFiles(context, File(root, it))
}
.fold(0, { acc, other -> acc + other })
} else {
root.length().toInt()
}
}

View File

@ -19,14 +19,20 @@ package im.vector.riotx.features
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import com.bumptech.glide.Glide
import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.Authenticator import im.vector.matrix.android.api.auth.Authenticator
import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.di.ScreenComponent
import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.core.platform.VectorBaseActivity
import im.vector.riotx.core.utils.deleteAllFiles
import im.vector.riotx.features.home.HomeActivity import im.vector.riotx.features.home.HomeActivity
import im.vector.riotx.features.login.LoginActivity import im.vector.riotx.features.login.LoginActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -60,7 +66,22 @@ class MainActivity : VectorBaseActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val clearCache = intent.getBooleanExtra(EXTRA_CLEAR_CACHE, false) val clearCache = intent.getBooleanExtra(EXTRA_CLEAR_CACHE, false)
val clearCredentials = intent.getBooleanExtra(EXTRA_CLEAR_CREDENTIALS, false) val clearCredentials = intent.getBooleanExtra(EXTRA_CLEAR_CREDENTIALS, false)
// Handle some wanted cleanup // Handle some wanted cleanup
if (clearCache || clearCredentials) {
GlobalScope.launch(Dispatchers.Main) {
// On UI Thread
Glide.get(this@MainActivity).clearMemory()
withContext(Dispatchers.IO) {
// On BG thread
Glide.get(this@MainActivity).clearDiskCache()
// Also clear cache (Logs, etc...)
deleteAllFiles(this@MainActivity.cacheDir)
}
}
}
when { when {
clearCredentials -> sessionHolder.getActiveSession().signOut(object : MatrixCallback<Unit> { clearCredentials -> sessionHolder.getActiveSession().signOut(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {

View File

@ -19,7 +19,6 @@ package im.vector.riotx.features.settings
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.AsyncTask
import android.text.Editable import android.text.Editable
import android.text.TextUtils import android.text.TextUtils
import android.view.View import android.view.View
@ -33,6 +32,7 @@ import androidx.preference.EditTextPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceCategory import androidx.preference.PreferenceCategory
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.cache.DiskCache
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import im.vector.riotx.R import im.vector.riotx.R
@ -40,10 +40,7 @@ import im.vector.riotx.core.extensions.showPassword
import im.vector.riotx.core.platform.SimpleTextWatcher import im.vector.riotx.core.platform.SimpleTextWatcher
import im.vector.riotx.core.preference.UserAvatarPreference import im.vector.riotx.core.preference.UserAvatarPreference
import im.vector.riotx.core.preference.VectorPreference import im.vector.riotx.core.preference.VectorPreference
import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA import im.vector.riotx.core.utils.*
import im.vector.riotx.core.utils.allGranted
import im.vector.riotx.core.utils.copyToClipboard
import im.vector.riotx.core.utils.toast
import im.vector.riotx.features.MainActivity import im.vector.riotx.features.MainActivity
import im.vector.riotx.features.themes.ThemeUtils import im.vector.riotx.features.themes.ThemeUtils
import im.vector.riotx.features.workers.signout.SignOutUiWorker import im.vector.riotx.features.workers.signout.SignOutUiWorker
@ -51,7 +48,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference import java.io.File
import java.util.* import java.util.*
class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
@ -189,58 +186,32 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
// clear medias cache // clear medias cache
findPreference(PreferencesManager.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY).let { findPreference(PreferencesManager.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY).let {
/* val size = getSizeOfFiles(requireContext(),
TODO File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR))
MXMediaCache.getCachesSize(activity, object : SimpleApiCallback<Long>() {
override fun onSuccess(size: Long) { it.summary = android.text.format.Formatter.formatFileSize(activity, size.toLong())
if (null != activity) {
it.summary = android.text.format.Formatter.formatFileSize(activity, size)
}
}
})
*/
it.onPreferenceClickListener = Preference.OnPreferenceClickListener { it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
notImplemented()
// TODO DECRYPT_FILE Quick implementation of clear cache, finish this
GlobalScope.launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
// On UI Thread // On UI Thread
displayLoadingView()
Glide.get(requireContext()).clearMemory() Glide.get(requireContext()).clearMemory()
var newSize = 0
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
// On BG thread // On BG thread
Glide.get(requireContext()).clearDiskCache() Glide.get(requireContext()).clearDiskCache()
newSize = getSizeOfFiles(requireContext(),
File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR))
} }
}
/* TODO it.summary = android.text.format.Formatter.formatFileSize(activity, newSize.toLong())
displayLoadingView()
val task = ClearMediaCacheAsyncTask(
backgroundTask = {
session.mediaCache.clear()
activity?.let { it -> Glide.get(it).clearDiskCache() }
},
onCompleteTask = {
hideLoadingView()
MXMediaCache.getCachesSize(activity, object : SimpleApiCallback<Long>() {
override fun onSuccess(size: Long) {
it.summary = Formatter.formatFileSize(activity, size)
}
})
}
)
try {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
} catch (e: Exception) {
Timber.e(e, "## session.getMediaCache().clear() failed " + e.message)
task.cancel(true)
hideLoadingView() hideLoadingView()
} }
*/
false false
} }
@ -894,23 +865,6 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
*/ */
} }
private class ClearMediaCacheAsyncTask internal constructor(
backgroundTask: () -> Unit,
onCompleteTask: () -> Unit
) : AsyncTask<Unit, Unit, Unit>() {
private val backgroundTaskReference = WeakReference(backgroundTask)
private val onCompleteTaskReference = WeakReference(onCompleteTask)
override fun doInBackground(vararg params: Unit?) {
backgroundTaskReference.get()?.invoke()
}
override fun onPostExecute(result: Unit?) {
super.onPostExecute(result)
onCompleteTaskReference.get()?.invoke()
}
}
companion object { companion object {
private const val ADD_EMAIL_PREFERENCE_KEY = "ADD_EMAIL_PREFERENCE_KEY" private const val ADD_EMAIL_PREFERENCE_KEY = "ADD_EMAIL_PREFERENCE_KEY"
private const val ADD_PHONE_NUMBER_PREFERENCE_KEY = "ADD_PHONE_NUMBER_PREFERENCE_KEY" private const val ADD_PHONE_NUMBER_PREFERENCE_KEY = "ADD_PHONE_NUMBER_PREFERENCE_KEY"