Skip to content

Commit

Permalink
🔨 Refactor request key format
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyge committed Nov 4, 2017
1 parent 481d194 commit 055f103
Show file tree
Hide file tree
Showing 53 changed files with 433 additions and 285 deletions.
2 changes: 2 additions & 0 deletions docs/logs/log_2.6.0_p1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* :hammer: HttpStack.ImageHttpResponse.getResponseHeader() 重命名为 HttpStack.Response.getHeader()
* :hammer: ImageAttrs 从 drawable 中移动到 decode 包中
* :bug: 修复开启解码 gif 后内存缓存失效的 bug
* :bug: 修复在生成 WrappedImageProcessor 的 key 时崩溃的错误,并且优化 key 的格式
* :hammer: 重构 WrappedImageProcessor 的 getKey() 和 toString() 方法,所有子类都需要重新适配

sample app:
* :bug: 修复 UNSPLASH 页面没有加载完数据就切换到别的页面时崩溃的 BUG
Expand Down
1 change: 1 addition & 0 deletions docs/wiki/image_processor.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Sketch.with(context).display(uri, sketchImageView)
2. 通过 [ImageProcessor] 的 process() 方法传进去的 Bitmap 在处理完之后无需回收它,Sketch 会去回收
3. 创建新的 bitmap 之前,先从 BitmapPool 中查找可复用 bitmap,实在没有再创建新的 bitmap
4. 在处理的过程中产生的过渡 Bitmap 在用完之后一定要调用 BitmapPoolUtils.freeBitmapToPool(Bitmap, BitmapPool) 回收掉
4. 实现 onToString() 和 onGetKey() 方法

自定义的 [ImageProcessor] 写好后通过 [LoadOptions]/[DisplayOptions] 的 setProcessor(ImageProcessor) 方法或 [LoadHelper]/[DisplayHelper] 的 processor(ImageProcessor) 方法使用即可

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class SampleErrorTracker(context: Context) : ErrorTracker(context) {
this.context = context.applicationContext
}

override fun getKey(): String {
override fun toString(): String {
return "SampleErrorTracker"
}

Expand Down Expand Up @@ -197,7 +197,7 @@ internal class SampleErrorTracker(context: Context) : ErrorTracker(context) {
"Sketch - %s - " +
"%s" +
"\n%s",
processor.key,
processor.toString(),
decodeUri(context, imageUri),
outOfMemoryInfo
), throwable))
Expand Down
82 changes: 41 additions & 41 deletions sketch/src/main/java/me/xiaopan/sketch/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Configuration setDiskCache(@NonNull DiskCache newDiskCache) {
if (oldDiskCache != null) {
oldDiskCache.close();
}
SLog.w(NAME, "diskCache=%s", diskCache.getKey());
SLog.w(NAME, "diskCache=%s", diskCache.toString());
}
return this;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public Configuration setBitmapPool(@NonNull BitmapPool newBitmapPool) {
if (oldBitmapPool != null) {
oldBitmapPool.close();
}
SLog.w(NAME, "bitmapPool = %s", bitmapPool.getKey());
SLog.w(NAME, "bitmapPool=%s", bitmapPool.toString());
}
return this;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ public Configuration setMemoryCache(@NonNull MemoryCache memoryCache) {
if (oldMemoryCache != null) {
oldMemoryCache.close();
}
SLog.w(NAME, "memoryCache=", memoryCache.getKey());
SLog.w(NAME, "memoryCache=", memoryCache.toString());
}
return this;
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public Configuration setProcessedImageCache(@NonNull ProcessedImageCache process
//noinspection ConstantConditions
if (processedImageCache != null) {
this.processedImageCache = processedImageCache;
SLog.w(NAME, "processedCache=", processedImageCache.getKey());
SLog.w(NAME, "processedCache=", processedImageCache.toString());
}
return this;
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public Configuration setHttpStack(@NonNull HttpStack httpStack) {
//noinspection ConstantConditions
if (httpStack != null) {
this.httpStack = httpStack;
SLog.w(NAME, "httpStack=", httpStack.getKey());
SLog.w(NAME, "httpStack=", httpStack.toString());
}
return this;
}
Expand All @@ -321,7 +321,7 @@ public Configuration setDecoder(@NonNull ImageDecoder decoder) {
//noinspection ConstantConditions
if (decoder != null) {
this.decoder = decoder;
SLog.w(NAME, "decoder=%s", decoder.getKey());
SLog.w(NAME, "decoder=%s", decoder.toString());
}
return this;
}
Expand All @@ -348,7 +348,7 @@ public Configuration setDownloader(@NonNull ImageDownloader downloader) {
//noinspection ConstantConditions
if (downloader != null) {
this.downloader = downloader;
SLog.w(NAME, "downloader=%s", downloader.getKey());
SLog.w(NAME, "downloader=%s", downloader.toString());
}
return this;
}
Expand All @@ -375,7 +375,7 @@ public Configuration setOrientationCorrector(@NonNull ImageOrientationCorrector
//noinspection ConstantConditions
if (orientationCorrector != null) {
this.orientationCorrector = orientationCorrector;
SLog.w(NAME, "orientationCorrector=%s", orientationCorrector.getKey());
SLog.w(NAME, "orientationCorrector=%s", orientationCorrector.toString());
}
return this;
}
Expand Down Expand Up @@ -403,7 +403,7 @@ public Configuration setDefaultDisplayer(@NonNull ImageDisplayer defaultDisplaye
//noinspection ConstantConditions
if (defaultDisplayer != null) {
this.defaultDisplayer = defaultDisplayer;
SLog.w(NAME, "defaultDisplayer=%s", defaultDisplayer.getKey());
SLog.w(NAME, "defaultDisplayer=%s", defaultDisplayer.toString());
}
return this;
}
Expand All @@ -430,7 +430,7 @@ public Configuration setResizeProcessor(@NonNull ImageProcessor resizeProcessor)
//noinspection ConstantConditions
if (resizeProcessor != null) {
this.resizeProcessor = resizeProcessor;
SLog.w(NAME, "resizeProcessor=%s", resizeProcessor.getKey());
SLog.w(NAME, "resizeProcessor=%s", resizeProcessor.toString());
}
return this;
}
Expand All @@ -457,7 +457,7 @@ public Configuration setResizeCalculator(@NonNull ResizeCalculator resizeCalcula
//noinspection ConstantConditions
if (resizeCalculator != null) {
this.resizeCalculator = resizeCalculator;
SLog.w(NAME, "resizeCalculator=%s", resizeCalculator.getKey());
SLog.w(NAME, "resizeCalculator=%s", resizeCalculator.toString());
}
return this;
}
Expand All @@ -484,7 +484,7 @@ public Configuration setSizeCalculator(@NonNull ImageSizeCalculator sizeCalculat
//noinspection ConstantConditions
if (sizeCalculator != null) {
this.sizeCalculator = sizeCalculator;
SLog.w(NAME, "sizeCalculator=%s", sizeCalculator.getKey());
SLog.w(NAME, "sizeCalculator=%s", sizeCalculator.toString());
}
return this;
}
Expand Down Expand Up @@ -512,7 +512,7 @@ public Configuration setFreeRideManager(@NonNull FreeRideManager freeRideManager
//noinspection ConstantConditions
if (freeRideManager != null) {
this.freeRideManager = freeRideManager;
SLog.w(NAME, "freeRideManager=%s", freeRideManager.getKey());
SLog.w(NAME, "freeRideManager=%s", freeRideManager.toString());
}
return this;
}
Expand Down Expand Up @@ -543,7 +543,7 @@ public Configuration setExecutor(@NonNull RequestExecutor newRequestExecutor) {
if (oldRequestExecutor != null) {
oldRequestExecutor.shutdown();
}
SLog.w(NAME, "executor=%s", executor.getKey());
SLog.w(NAME, "executor=%s", executor.toString());
}
return this;
}
Expand All @@ -570,7 +570,7 @@ public Configuration setHelperFactory(@NonNull HelperFactory helperFactory) {
//noinspection ConstantConditions
if (helperFactory != null) {
this.helperFactory = helperFactory;
SLog.w(NAME, "helperFactory=%s", helperFactory.getKey());
SLog.w(NAME, "helperFactory=%s", helperFactory.toString());
}
return this;
}
Expand All @@ -597,7 +597,7 @@ public Configuration setRequestFactory(@NonNull RequestFactory requestFactory) {
//noinspection ConstantConditions
if (requestFactory != null) {
this.requestFactory = requestFactory;
SLog.w(NAME, "requestFactory=%s", requestFactory.getKey());
SLog.w(NAME, "requestFactory=%s", requestFactory.toString());
}
return this;
}
Expand Down Expand Up @@ -625,7 +625,7 @@ public Configuration setErrorTracker(@NonNull ErrorTracker errorTracker) {
//noinspection ConstantConditions
if (errorTracker != null) {
this.errorTracker = errorTracker;
SLog.w(NAME, "errorTracker=%s", errorTracker.getKey());
SLog.w(NAME, "errorTracker=%s", errorTracker.toString());
}
return this;
}
Expand Down Expand Up @@ -749,31 +749,31 @@ public Configuration setMobileDataPauseDownloadEnabled(boolean mobileDataPauseDo
}

@NonNull
public String getInfo() {
public String toString() {
return NAME + ": " +
"\n" + "uriModelManager:" + uriModelManager.getKey() +
"\n" + "optionsFilterManager:" + optionsFilterManager.getKey() +

"\n" + "diskCache:" + diskCache.getKey() +
"\n" + "bitmapPool:" + bitmapPool.getKey() +
"\n" + "memoryCache:" + memoryCache.getKey() +
"\n" + "processedImageCache:" + processedImageCache.getKey() +

"\n" + "httpStack:" + httpStack.getKey() +
"\n" + "decoder:" + decoder.getKey() +
"\n" + "downloader:" + downloader.getKey() +
"\n" + "orientationCorrector:" + orientationCorrector.getKey() +

"\n" + "defaultDisplayer:" + defaultDisplayer.getKey() +
"\n" + "resizeProcessor:" + resizeProcessor.getKey() +
"\n" + "resizeCalculator:" + resizeCalculator.getKey() +
"\n" + "sizeCalculator:" + sizeCalculator.getKey() +

"\n" + "freeRideManager:" + freeRideManager.getKey() +
"\n" + "executor:" + executor.getKey() +
"\n" + "helperFactory:" + helperFactory.getKey() +
"\n" + "requestFactory:" + requestFactory.getKey() +
"\n" + "errorTracker:" + errorTracker.getKey() +
"\n" + "uriModelManager:" + uriModelManager.toString() +
"\n" + "optionsFilterManager:" + optionsFilterManager.toString() +

"\n" + "diskCache:" + diskCache.toString() +
"\n" + "bitmapPool:" + bitmapPool.toString() +
"\n" + "memoryCache:" + memoryCache.toString() +
"\n" + "processedImageCache:" + processedImageCache.toString() +

"\n" + "httpStack:" + httpStack.toString() +
"\n" + "decoder:" + decoder.toString() +
"\n" + "downloader:" + downloader.toString() +
"\n" + "orientationCorrector:" + orientationCorrector.toString() +

"\n" + "defaultDisplayer:" + defaultDisplayer.toString() +
"\n" + "resizeProcessor:" + resizeProcessor.toString() +
"\n" + "resizeCalculator:" + resizeCalculator.toString() +
"\n" + "sizeCalculator:" + sizeCalculator.toString() +

"\n" + "freeRideManager:" + freeRideManager.toString() +
"\n" + "executor:" + executor.toString() +
"\n" + "helperFactory:" + helperFactory.toString() +
"\n" + "requestFactory:" + requestFactory.toString() +
"\n" + "errorTracker:" + errorTracker.toString() +

"\n" + "pauseDownload:" + optionsFilterManager.isPauseDownloadEnabled() +
"\n" + "pauseLoad:" + optionsFilterManager.isPauseLoadEnabled() +
Expand Down
6 changes: 3 additions & 3 deletions sketch/src/main/java/me/xiaopan/sketch/ErrorTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* 负责输出错误信息,你可借此记录错误日志
*/
public class ErrorTracker implements Identifier {
public class ErrorTracker {
private static final String NAME = "ErrorTracker";

private Context context;
Expand Down Expand Up @@ -125,7 +125,7 @@ public void onProcessImageError(@NonNull Throwable e, @NonNull String imageUri,
}

SLog.e(NAME, "onProcessImageError. imageUri: %s. processor: %s",
imageUri, processor.getKey());
imageUri, processor.toString());
}

/**
Expand Down Expand Up @@ -196,7 +196,7 @@ public void onDecodeRegionError(@NonNull String imageUri, int imageWidth, int im

@NonNull
@Override
public String getKey() {
public String toString() {
return NAME;
}
}
13 changes: 0 additions & 13 deletions sketch/src/main/java/me/xiaopan/sketch/Identifier.java

This file was deleted.

25 changes: 25 additions & 0 deletions sketch/src/main/java/me/xiaopan/sketch/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Peng fei Pan <sky@xiaopan.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.xiaopan.sketch;

import android.support.annotation.Nullable;

public interface Key {

@Nullable
String getKey();
}
2 changes: 1 addition & 1 deletion sketch/src/main/java/me/xiaopan/sketch/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static Sketch with(@NonNull Context context) {
if (instance == null) {
Sketch newInstance = new Sketch(context);
SLog.i(null, "Version %s %s(%d) -> %s",
BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, newInstance.configuration.getInfo());
BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, newInstance.configuration.toString());

Initializer initializer = SketchUtils.findInitializer(context);
if (initializer != null) {
Expand Down
4 changes: 1 addition & 3 deletions sketch/src/main/java/me/xiaopan/sketch/cache/BitmapPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import me.xiaopan.sketch.Identifier;

/**
* {@link Bitmap} 复用缓存池,用于缓存并复用 {@link Bitmap},便于解码时直接使用,减少内存分配
*/
public interface BitmapPool extends Identifier {
public interface BitmapPool {

/**
* 获取最大容量
Expand Down
3 changes: 1 addition & 2 deletions sketch/src/main/java/me/xiaopan/sketch/cache/DiskCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
import java.io.OutputStream;
import java.util.concurrent.locks.ReentrantLock;

import me.xiaopan.sketch.Identifier;
import me.xiaopan.sketch.util.DiskLruCache;

/**
* 磁盘缓存管理器
*/
public interface DiskCache extends Identifier {
public interface DiskCache {
String DISK_CACHE_DIR_NAME = "sketch";
int DISK_CACHE_MAX_SIZE = 100 * 1024 * 1024;
int DISK_CACHE_RESERVED_SPACE_SIZE = 200 * 1024 * 1024;
Expand Down
14 changes: 8 additions & 6 deletions sketch/src/main/java/me/xiaopan/sketch/cache/LruBitmapPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class LruBitmapPool implements BitmapPool {
private static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.ARGB_8888;
private static final String NAME = "LruBitmapPool";

@NonNull
private final LruPoolStrategy strategy;
@NonNull
private final Set<Bitmap.Config> allowedConfigs;
private final int initialMaxSize;
private final BitmapTracker tracker;
Expand All @@ -43,9 +45,8 @@ public class LruBitmapPool implements BitmapPool {
private boolean closed;
private boolean disabled;

LruBitmapPool(Context context, int maxSize, LruPoolStrategy strategy, Set<Bitmap.Config> allowedConfigs) {
context = context.getApplicationContext();
this.context = context;
LruBitmapPool(Context context, int maxSize, @NonNull LruPoolStrategy strategy, @NonNull Set<Bitmap.Config> allowedConfigs) {
this.context = context.getApplicationContext();
this.initialMaxSize = maxSize;
this.maxSize = maxSize;
this.strategy = strategy;
Expand All @@ -69,7 +70,7 @@ public LruBitmapPool(Context context, int maxSize) {
* @param allowedConfigs {@link Bitmap.Config} 白名单
*/
@SuppressWarnings("unused")
public LruBitmapPool(Context context, int maxSize, Set<Bitmap.Config> allowedConfigs) {
public LruBitmapPool(Context context, int maxSize, @NonNull Set<Bitmap.Config> allowedConfigs) {
this(context, maxSize, getDefaultStrategy(), allowedConfigs);
}

Expand Down Expand Up @@ -319,8 +320,9 @@ private void dumpUnchecked() {

@NonNull
@Override
public String getKey() {
return String.format("%s(maxSize=%s)", NAME, Formatter.formatFileSize(context, getMaxSize()));
public String toString() {
return String.format("%s(maxSize=%s,strategy=%s,allowedConfigs=%s)",
NAME, Formatter.formatFileSize(context, getMaxSize()), strategy.getKey(), allowedConfigs.toString());
}

private interface BitmapTracker {
Expand Down
Loading

0 comments on commit 055f103

Please sign in to comment.