瀏覽代碼

清除缓存

徐勤民 3 月之前
父節點
當前提交
a5dc26c839

+ 3 - 2
app/src/main/java/com/xuqinmin/android/app/common/CrashHandler.java

@@ -5,6 +5,8 @@ import android.os.Environment;
 import android.os.SystemClock;
 import android.util.Log;
 
+import com.xuqm.base.common.AppManager;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -56,8 +58,7 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler {
         } else {
             SystemClock.sleep(2000);
             // 退出程序
-            android.os.Process.killProcess(android.os.Process.myPid());
-            System.exit(1);
+            AppManager.getInstance().exit();
         }
     }
 

+ 41 - 0
app/src/main/java/com/xuqinmin/android/app/common/FileUtils.java

@@ -0,0 +1,41 @@
+package com.xuqinmin.android.app.common;
+
+import com.xuqm.base.common.FileHelper;
+
+import java.io.File;
+
+public class FileUtils {
+    public static long getFolderSize(File folder) {
+        if (folder.isFile()) return folder.length();
+        else {
+            long length = 0;
+            File[] files = folder.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    if (file.isFile()) {
+                        length += file.length();
+                    } else {
+                        length += getFolderSize(file);
+                    }
+                }
+            }
+            return length;
+        }
+    }
+
+    public static void clearFolder(File folder) {
+        if (folder.isFile()) FileHelper.delete(folder);
+        else {
+            File[] files = folder.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    if (file.isFile()) {
+                        FileHelper.delete(folder);
+                    } else {
+                        clearFolder(file);
+                    }
+                }
+            }
+        }
+    }
+}

+ 83 - 0
app/src/main/java/com/xuqinmin/android/app/common/StorageUtils.java

@@ -0,0 +1,83 @@
+package com.xuqinmin.android.app.common;
+
+import static android.os.Environment.MEDIA_MOUNTED;
+
+import android.content.Context;
+import android.os.Environment;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+
+/**
+ * Provides application storage paths
+ * <p/>
+ * See https://github.com/nostra13/Android-Universal-Image-Loader
+ *
+ * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
+ * @since 1.0.0
+ */
+public class StorageUtils {
+
+    private static final Logger LOG = LoggerFactory.getLogger("StorageUtils");
+    private static final String INDIVIDUAL_DIR_NAME = "video-cache";
+
+    /**
+     * Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
+     * created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
+     * Else - Android defines cache directory on device's file system.
+     *
+     * @param context Application context
+     * @return Cache {@link File directory}
+     */
+    public static File getIndividualCacheDirectory(Context context) {
+        File cacheDir = getCacheDirectory(context, true);
+        return new File(cacheDir, INDIVIDUAL_DIR_NAME);
+    }
+
+    /**
+     * Returns application cache directory. Cache directory will be created on SD card
+     * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
+     * on device's file system depending incoming parameters.
+     *
+     * @param context        Application context
+     * @param preferExternal Whether prefer external location for cache
+     * @return Cache {@link File directory}.<br />
+     * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
+     * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
+     */
+    private static File getCacheDirectory(Context context, boolean preferExternal) {
+        File appCacheDir = null;
+        String externalStorageState;
+        try {
+            externalStorageState = Environment.getExternalStorageState();
+        } catch (NullPointerException e) { // (sh)it happens
+            externalStorageState = "";
+        }
+        if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState)) {
+            appCacheDir = getExternalCacheDir(context);
+        }
+        if (appCacheDir == null) {
+            appCacheDir = context.getCacheDir();
+        }
+        if (appCacheDir == null) {
+            String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
+            LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
+            appCacheDir = new File(cacheDirPath);
+        }
+        return appCacheDir;
+    }
+
+    private static File getExternalCacheDir(Context context) {
+        File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
+        File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
+        if (!appCacheDir.exists()) {
+            if (!appCacheDir.mkdirs()) {
+                LOG.warn("Unable to create external cache directory");
+                return null;
+            }
+        }
+        return appCacheDir;
+    }
+}

+ 2 - 2
app/src/main/java/com/xuqinmin/android/app/ui/FriendsActivity.kt

@@ -69,9 +69,9 @@ class FriendsActivity :
                     else -> Color.parseColor("#FFEA2D2D")
                 }
             )
-            holder.setBackgroundColor(
+            holder.setBackgroundResource(
                 R.id.btn,
-                if (item.status == 0) Color.parseColor("#FFCFD4E0") else Color.parseColor("#FFFF02A7")
+                if (item.status == 0) R.drawable.bg_cfd4e0_25 else R.drawable.bg_ff02a7_25
             )
             holder.setText(
                 R.id.btn, if (viewModel.type == 0) "Unfollow" else {

+ 29 - 8
app/src/main/java/com/xuqinmin/android/app/ui/SettingActivity.kt

@@ -4,15 +4,15 @@ import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
 import com.bigkoo.alertview.AlertView
+import com.bumptech.glide.Glide
 import com.xuqinmin.android.app.R
+import com.xuqinmin.android.app.common.FileUtils
+import com.xuqinmin.android.app.common.StorageUtils
 import com.xuqinmin.android.app.databinding.ActivitySettingBinding
 import com.xuqinmin.android.app.db.XuqmDbHelper
 import com.xuqinmin.android.app.ui.dialog.DialogDelete
 import com.xuqm.base.common.AppManager
-import com.xuqm.base.common.GsonImplHelp
-import com.xuqm.base.common.SHARE_LOGIN_OBJ
 import com.xuqm.base.extensions.clearForPreferences
-import com.xuqm.base.extensions.putString
 import com.xuqm.base.ui.BaseActivity
 
 class SettingActivity : BaseActivity<ActivitySettingBinding>() {
@@ -48,9 +48,12 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>() {
             ) { _, position ->
                 when (position) {
                     0 -> {
-                        clearForPreferences()
-                        startActivity(Intent(mContext, WelcomeActivity::class.java))
-                        AppManager.getInstance().exitWithOutLast()
+                        Glide.get(mContext).clearMemory()
+                        Thread {
+                            Glide.get(mContext).clearDiskCache()
+                        }.start()
+                        FileUtils.clearFolder(StorageUtils.getIndividualCacheDirectory(mContext))
+                        setLength()
                     }
                 }
             }.show()
@@ -79,8 +82,7 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>() {
             try {
                 startActivity(
                     Intent(
-                        Intent.ACTION_VIEW,
-                        Uri.parse("market://details?id=$packageName")
+                        Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")
                     )
                 )
             } catch (e: android.content.ActivityNotFoundException) {
@@ -92,5 +94,24 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>() {
                 )
             }
         }
+        setLength()
+    }
+
+    private var l = 0L
+    private fun setLength() {
+        val length =
+            FileUtils.getFolderSize(StorageUtils.getIndividualCacheDirectory(mContext)) + FileUtils.getFolderSize(
+                Glide.getPhotoCacheDir(mContext)
+            )
+
+        binding.length.text = if (length < 1024 * 1024 * 1024) {
+            "${length / 1024 / 1024} M"
+        } else {
+            "${length / 1024 / 1024 / 1024} G"
+        }
+        if (l != length){
+            l = length
+            setLength()
+        }
     }
 }

+ 9 - 0
app/src/main/res/drawable/bg_ff02a7_25.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item>
+        <shape android:shape="rectangle">
+            <solid android:color="#ffff02a7" />
+            <corners android:radius="25dp" />
+        </shape>
+    </item>
+</selector>

+ 13 - 0
app/src/main/res/layout/activity_setting.xml

@@ -209,8 +209,21 @@
                         app:layout_constraintBottom_toBottomOf="parent"
                         app:layout_constraintStart_toEndOf="@+id/img3"
                         app:layout_constraintTop_toTopOf="parent" />
+                    <TextView
+                        android:id="@+id/length"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:layout_marginStart="10dp"
+                        android:text="0"
+                        android:textColor="#999999"
+                        android:textSize="18sp"
+                        android:textStyle="bold"
+                        app:layout_constraintBottom_toBottomOf="parent"
+                        app:layout_constraintEnd_toStartOf="@+id/go"
+                        app:layout_constraintTop_toTopOf="parent" />
 
                     <ImageView
+                        android:id="@+id/go"
                         android:layout_width="30dp"
                         android:layout_height="30dp"
                         android:src="@mipmap/go"