Skip to content

Commit

Permalink
refactor: support multiple instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiao committed Oct 24, 2015
1 parent 709319f commit 4181e8c
Show file tree
Hide file tree
Showing 23 changed files with 573 additions and 490 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.GlobalFlagValues;
import master.flame.danmaku.danmaku.model.IDanmakuIterator;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.DanmakuFactory;

public class DanmakuFilters {

Expand All @@ -32,18 +31,18 @@ public class DanmakuFilters {
public static final int FILTER_TYPE_OVERLAPPING = 512;


public static interface IDanmakuFilter<T> {
public interface IDanmakuFilter<T> {
/*
* 是否过滤
*/
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask);
boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config);

public void setData(T data);
void setData(T data);

public void reset();
void reset();

public void clear();
void clear();

}

Expand Down Expand Up @@ -77,7 +76,7 @@ public void disableType(Integer type) {

@Override
public boolean filter(BaseDanmaku danmaku, int orderInScreen, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = danmaku != null && mFilterTypes.contains(danmaku.getType());
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_TYPE;
Expand Down Expand Up @@ -114,14 +113,14 @@ public static class QuantityDanmakuFilter extends BaseDanmakuFilter<Integer> {
protected BaseDanmaku mLastSkipped = null;

private boolean needFilter(BaseDanmaku danmaku, int orderInScreen,
int totalSizeInScreen, DanmakuTimer timer, boolean fromCachingTask) {
int totalSizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext context) {

if (mMaximumSize <= 0 || danmaku.getType() != BaseDanmaku.TYPE_SCROLL_RL) {
return false;
}

if (totalSizeInScreen < mMaximumSize || danmaku.isShown()
|| (mLastSkipped != null && (danmaku.time - mLastSkipped.time > DanmakuFactory.MAX_DANMAKU_DURATION / 20))) {
|| (mLastSkipped != null && (danmaku.time - mLastSkipped.time > context.mDanmakuFactory.MAX_DANMAKU_DURATION / 20))) {
mLastSkipped = danmaku;
return false;
}
Expand All @@ -135,8 +134,8 @@ private boolean needFilter(BaseDanmaku danmaku, int orderInScreen,

@Override
public synchronized boolean filter(BaseDanmaku danmaku, int orderInScreen,
int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask) {
boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask);
int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask, config);
if (filtered) {
danmaku.mFilterParam |= FILYER_TYPE_QUANTITY;
}
Expand Down Expand Up @@ -188,7 +187,7 @@ private synchronized boolean needFilter(BaseDanmaku danmaku, int orderInScreen,

@Override
public boolean filter(BaseDanmaku danmaku, int orderInScreen,
int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask) {
int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_ELAPSED_TIME;
Expand Down Expand Up @@ -230,7 +229,7 @@ private void addToWhiteList(Integer color) {

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = danmaku != null && !mWhiteList.contains(danmaku.textColor);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_TEXTCOLOR;
Expand Down Expand Up @@ -272,7 +271,7 @@ private void addToBlackList(T id) {

@Override
public abstract boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask);
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config);

@Override
public void setData(List<T> data) {
Expand Down Expand Up @@ -300,7 +299,7 @@ public static class UserIdFilter extends UserFilter<Integer> {

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = danmaku != null && mBlackList.contains(danmaku.userId);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_USER_ID;
Expand All @@ -319,7 +318,7 @@ public static class UserHashFilter extends UserFilter<String> {

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = danmaku != null && mBlackList.contains(danmaku.userHash);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_USER_HASH;
Expand All @@ -340,7 +339,7 @@ public static class GuestFilter extends BaseDanmakuFilter<Boolean> {

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = mBlock && danmaku.isGuest;
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_USER_GUEST;
Expand Down Expand Up @@ -434,7 +433,7 @@ public synchronized boolean needFilter(BaseDanmaku danmaku, int index, int total

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) {
boolean filtered = needFilter(danmaku, index, totalsizeInScreen, timer, fromCachingTask);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_DUPLICATE_MERGE;
Expand Down Expand Up @@ -466,7 +465,7 @@ public static class MaximumLinesFilter extends BaseDanmakuFilter<Map<Integer, In
private Map<Integer, Integer> mMaximumLinesPairs;

@Override
public boolean filter(BaseDanmaku danmaku, int lines, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) {
public boolean filter(BaseDanmaku danmaku, int lines, int totalsizeInScreen, DanmakuTimer timer, boolean willHit, DanmakuContext config) {
boolean filtered = false;
if (mMaximumLinesPairs != null) {
Integer maxLines = mMaximumLinesPairs.get(danmaku.getType());
Expand Down Expand Up @@ -494,7 +493,7 @@ public static class OverlappingFilter extends BaseDanmakuFilter<Map<Integer, Boo
private Map<Integer, Boolean> mEnabledPairs;

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) {
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, DanmakuTimer timer, boolean willHit, DanmakuContext config) {
boolean filtered = false;
if (mEnabledPairs != null) {
Boolean enabledValue = mEnabledPairs.get(danmaku.getType());
Expand Down Expand Up @@ -529,24 +528,22 @@ public void reset() {

public final static String TAG_USER_HASH_FILTER = "1015_Filter";

public static final String TAG_GUEST_FILTER = "1016_Filter";

public static final String TAG_DUPLICATE_FILTER = "1017_Filter";
public final static String TAG_GUEST_FILTER = "1016_Filter";

public static final String TAG_MAXIMUN_LINES_FILTER = "1018_Filter";
public final static String TAG_DUPLICATE_FILTER = "1017_Filter";

public static final String TAG_OVERLAPPING_FILTER = "1019_Filter";
public final static String TAG_MAXIMUN_LINES_FILTER = "1018_Filter";

private static DanmakuFilters instance = null;
public final static String TAG_OVERLAPPING_FILTER = "1019_Filter";

public final Exception filterException = new Exception("not suuport this filter tag");

public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask, DanmakuContext context) {
for (IDanmakuFilter<?> f : mFilterArray) {
if (f != null) {
boolean filtered = f.filter(danmaku, index, totalsizeInScreen, timer, fromCachingTask);
danmaku.filterResetFlag = GlobalFlagValues.FILTER_RESET_FLAG;
boolean filtered = f.filter(danmaku, index, totalsizeInScreen, timer, fromCachingTask, context);
danmaku.filterResetFlag = context.mGlobalFlagValues.FILTER_RESET_FLAG;
if (filtered) {
break;
}
Expand All @@ -555,11 +552,11 @@ public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
}

public boolean filterSecondary(BaseDanmaku danmaku, int lines, int totalsizeInScreen,
DanmakuTimer timer, boolean willHit) {
DanmakuTimer timer, boolean willHit, DanmakuContext context) {
for (IDanmakuFilter<?> f : mFilterArraySecondary) {
if (f != null) {
boolean filtered = f.filter(danmaku, lines, totalsizeInScreen, timer, willHit);
danmaku.filterResetFlag = GlobalFlagValues.FILTER_RESET_FLAG;
boolean filtered = f.filter(danmaku, lines, totalsizeInScreen, timer, willHit, context);
danmaku.filterResetFlag = context.mGlobalFlagValues.FILTER_RESET_FLAG;
if (filtered) {
return true;
}
Expand All @@ -568,9 +565,9 @@ public boolean filterSecondary(BaseDanmaku danmaku, int lines, int totalsizeInSc
return false;
}

private final static Map<String, IDanmakuFilter<?>> filters = Collections
private final Map<String, IDanmakuFilter<?>> filters = Collections
.synchronizedSortedMap(new TreeMap<String, IDanmakuFilter<?>>());
private final static Map<String, IDanmakuFilter<?>> filtersSecondary = Collections
private final Map<String, IDanmakuFilter<?>> filtersSecondary = Collections
.synchronizedSortedMap(new TreeMap<String, IDanmakuFilter<?>>());
IDanmakuFilter<?>[] mFilterArray = new IDanmakuFilter[0];
IDanmakuFilter<?>[] mFilterArraySecondary = new IDanmakuFilter[0];
Expand Down Expand Up @@ -688,11 +685,4 @@ private void throwFilterException() {
}
}

public static DanmakuFilters getDefault() {
if (instance == null) {
instance = new DanmakuFilters();
}
return instance;
}

}
Loading

0 comments on commit 4181e8c

Please sign in to comment.