|
| 1 | +package com.netease.im; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.pm.IPackageDataObserver; |
| 5 | +import android.content.pm.IPackageStatsObserver; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.content.pm.PackageStats; |
| 8 | +import android.os.AsyncTask; |
| 9 | +import android.os.Build; |
| 10 | +import android.os.Environment; |
| 11 | +import android.os.RemoteException; |
| 12 | +import android.os.StatFs; |
| 13 | + |
| 14 | +import com.netease.im.uikit.common.util.file.FileUtil; |
| 15 | +import com.netease.im.uikit.common.util.log.LogUtil; |
| 16 | +import com.netease.im.uikit.common.util.storage.StorageType; |
| 17 | +import com.netease.im.uikit.common.util.storage.StorageUtil; |
| 18 | +import com.netease.nimlib.sdk.NIMClient; |
| 19 | +import com.netease.nimlib.sdk.msg.MsgService; |
| 20 | +import com.netease.nimlib.sdk.msg.model.RecentContact; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +/** |
| 29 | + * Created by dowin on 2017/7/6. |
| 30 | + * <p> |
| 31 | + * log: SDK日志 |
| 32 | + * file: 文件消息文件 |
| 33 | + * image: 图片消息文件 |
| 34 | + * audio:语音消息文件 |
| 35 | + * video:视频消息文件 |
| 36 | + * thumb:图片/视频缩略图文件 |
| 37 | + */ |
| 38 | + |
| 39 | +public class FileCacheUtil { |
| 40 | + |
| 41 | + final static String TAG = "FileCacheUtil"; |
| 42 | + |
| 43 | + public static void getCacheSie() { |
| 44 | + |
| 45 | + new AsyncTask<Void, Void, Void>() { |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Void doInBackground(Void... params) { |
| 49 | + Set<String> pathList = getCacheDir(); |
| 50 | + long allLength = 0; |
| 51 | + for (String s : pathList) { |
| 52 | + long t = makeDirSize(new File(s)); |
| 53 | + LogUtil.i(TAG, s + ":" + FileUtil.formatFileSize(t)); |
| 54 | + allLength += t; |
| 55 | + } |
| 56 | + LogUtil.i(TAG, "allFile" + ":" + FileUtil.formatFileSize(allLength)); |
| 57 | + final long finalAllLength = allLength; |
| 58 | + getCacheSize(new IPackageStatsObserver.Stub() { |
| 59 | + @Override |
| 60 | + public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { |
| 61 | + |
| 62 | + LogUtil.i(TAG, "cacheSize" + ":" + FileUtil.formatFileSize(pStats.cacheSize)); |
| 63 | + LogUtil.i(TAG, "externalCacheSize" + ":" + FileUtil.formatFileSize(pStats.externalCacheSize)); |
| 64 | + |
| 65 | +// LogUtil.i(TAG, "codeSize" + ":" + FileUtil.formatFileSize(pStats.codeSize)); |
| 66 | +// LogUtil.i(TAG, "dataSize" + ":" + FileUtil.formatFileSize(pStats.dataSize)); |
| 67 | +// LogUtil.i(TAG, "externalCodeSize" + ":" + FileUtil.formatFileSize(pStats.externalCodeSize)); |
| 68 | +// LogUtil.i(TAG, "externalDataSize" + ":" + FileUtil.formatFileSize(pStats.externalDataSize)); |
| 69 | +// LogUtil.i(TAG, "externalMediaSize" + ":" + FileUtil.formatFileSize(pStats.externalMediaSize)); |
| 70 | +// LogUtil.i(TAG, "externalObbSize" + ":" + FileUtil.formatFileSize(pStats.externalObbSize)); |
| 71 | + long result = finalAllLength; |
| 72 | + result += pStats.cacheSize; |
| 73 | + result += pStats.cacheSize; |
| 74 | + LogUtil.i(TAG, "result" + ":" + FileUtil.formatFileSize(result)); |
| 75 | + } |
| 76 | + }); |
| 77 | + return null; |
| 78 | + } |
| 79 | + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 80 | + } |
| 81 | + |
| 82 | + public static void clearCache() { |
| 83 | + new AsyncTask<Void, Void, Void>() { |
| 84 | + |
| 85 | + @Override |
| 86 | + protected Void doInBackground(Void... params) { |
| 87 | +// IMApplication.getImageLoaderKit().clearCache(); |
| 88 | + Set<String> pathList = getCacheDir(); |
| 89 | + for (String s : pathList) { |
| 90 | +// deleteDir(new File(s)); |
| 91 | + } |
| 92 | + List<RecentContact> recentContacts = NIMClient.getService(MsgService.class).queryRecentContactsBlock(); |
| 93 | + if (recentContacts != null && !recentContacts.isEmpty()) { |
| 94 | +// NIMClient.getService(MsgService.class).clearMsgDatabase(true); |
| 95 | + } |
| 96 | + freeStorageAndNotify(new IPackageDataObserver.Stub() { |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { |
| 100 | + LogUtil.i(TAG, "result" + ":" + packageName); |
| 101 | + LogUtil.i(TAG, "result" + ":" + succeeded); |
| 102 | + } |
| 103 | + }); |
| 104 | + return null; |
| 105 | + } |
| 106 | + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 107 | + } |
| 108 | + |
| 109 | + static void deleteDir(File file) { |
| 110 | + if (file == null || !file.exists()) { |
| 111 | + return; |
| 112 | + } |
| 113 | + if (file.isFile()) { |
| 114 | + file.delete(); |
| 115 | + } |
| 116 | + File[] list = file.listFiles(); |
| 117 | + if (list != null && list.length > 0) { |
| 118 | + for (File f : list) { |
| 119 | + if (f.isDirectory()) { |
| 120 | + deleteDir(f); |
| 121 | + } else { |
| 122 | + f.delete(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + static long makeDirSize(File file) { |
| 129 | + |
| 130 | + if (file == null || !file.exists()) { |
| 131 | + return 0L; |
| 132 | + } |
| 133 | + if (file.isFile()) { |
| 134 | + return file.length(); |
| 135 | + } |
| 136 | + long all = 0L; |
| 137 | + File[] list = file.listFiles(); |
| 138 | + if (list != null && list.length > 0) { |
| 139 | + for (File f : list) { |
| 140 | + if (f.isDirectory()) { |
| 141 | + all += makeDirSize(f); |
| 142 | + } else { |
| 143 | + all += f.length(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return all; |
| 148 | + } |
| 149 | + |
| 150 | + static void getCacheSize(IPackageStatsObserver.Stub observer) { |
| 151 | + Context context = IMApplication.getContext(); |
| 152 | + String pkg = context.getPackageName(); |
| 153 | + PackageManager pm = context.getPackageManager(); |
| 154 | + try { |
| 155 | + LogUtil.i(TAG, "name:" + pm.getClass().getName()); |
| 156 | + Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); |
| 157 | + getPackageSizeInfo.invoke(pm, pkg, observer); |
| 158 | + } catch (Exception ex) { |
| 159 | + LogUtil.e("", "NoSuchMethodException"); |
| 160 | + ex.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static Set<String> getCacheDir() { |
| 165 | + |
| 166 | + StorageType[] storageTypes = StorageType.values(); |
| 167 | + String[] sdkFileName = {"log/", "file/", "image/", "audio/", "video/", "thumb/"}; |
| 168 | + Set<String> path = new HashSet<>(); |
| 169 | + for (StorageType type : storageTypes) { |
| 170 | + path.add(StorageUtil.getDirectoryByDirType(type)); |
| 171 | + } |
| 172 | + for (String sdk : sdkFileName) { |
| 173 | + path.add(IMApplication.getSdkStorageRooPath() + "/" + sdk); |
| 174 | + } |
| 175 | + File imageCacheDir = IMApplication.getImageLoaderKit().getChacheDir(); |
| 176 | + if (imageCacheDir.exists()) { |
| 177 | + path.add(imageCacheDir.getAbsolutePath()); |
| 178 | + } |
| 179 | + |
| 180 | + Context context = IMApplication.getContext(); |
| 181 | + path.add(context.getCacheDir().getAbsolutePath()); |
| 182 | + path.add(context.getExternalCacheDir().getAbsolutePath()); |
| 183 | + |
| 184 | + return path; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + private static long getEnvironmentSize() { |
| 189 | + File localFile = Environment.getDataDirectory(); |
| 190 | + if (localFile == null) |
| 191 | + return 0L; |
| 192 | + |
| 193 | + StatFs statFs = new StatFs(localFile.getPath()); |
| 194 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 195 | + return statFs.getBlockCountLong() * statFs.getBlockSizeLong(); |
| 196 | + } |
| 197 | + return statFs.getBlockCount() * statFs.getBlockSize(); |
| 198 | + } |
| 199 | + |
| 200 | + private static void freeStorageAndNotify(IPackageDataObserver.Stub observer) { |
| 201 | + |
| 202 | + try { |
| 203 | + Context context = IMApplication.getContext(); |
| 204 | + PackageManager pm = context.getPackageManager(); |
| 205 | + LogUtil.i(TAG, "name:" + pm.getClass().getName()); |
| 206 | + Method localMethod = pm.getClass().getMethod("freeStorageAndNotify", Long.TYPE, |
| 207 | + IPackageDataObserver.class); |
| 208 | + long localLong = Long.valueOf(getEnvironmentSize() - 1L); |
| 209 | + |
| 210 | + localMethod.invoke(pm, localLong, observer); |
| 211 | + } catch (Exception e) { |
| 212 | + e.printStackTrace(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | +} |
0 commit comments