|
|
|
|
@ -4,8 +4,6 @@ import com.fongmi.android.tv.bean.DanmakuData; |
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.regex.Matcher; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
@ -23,46 +21,44 @@ public class Parser extends BaseDanmakuParser { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Danmakus parse() { |
|
|
|
|
if (mDataSource == null) return null; |
|
|
|
|
String line; |
|
|
|
|
int index = 0; |
|
|
|
|
Pattern pattern = null; |
|
|
|
|
if (mDataSource == null) return null; |
|
|
|
|
List<DanmakuData> items = new ArrayList<>(); |
|
|
|
|
Danmakus result = new Danmakus(IDanmakus.ST_BY_TIME); |
|
|
|
|
AndroidFileSource source = (AndroidFileSource) mDataSource; |
|
|
|
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(source.data()))) { |
|
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
|
if (pattern == null) pattern = line.startsWith("<") ? XML : TXT; |
|
|
|
|
Matcher matcher = pattern.matcher(line); |
|
|
|
|
while (matcher.find() && matcher.groupCount() == 2) { |
|
|
|
|
try { |
|
|
|
|
items.add(new DanmakuData(matcher, mDispDensity)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
DanmakuData data = new DanmakuData(matcher, mDispDensity); |
|
|
|
|
BaseDanmaku item = createDanmakuItem(data, index++); |
|
|
|
|
if (item != null) synchronized (result.obtainSynchronizer()) { |
|
|
|
|
result.addItem(item); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Danmakus result = new Danmakus(IDanmakus.ST_BY_TIME); |
|
|
|
|
for (int i = 0; i < items.size(); i++) { |
|
|
|
|
DanmakuData data = items.get(i); |
|
|
|
|
int type = data.getType(); |
|
|
|
|
if (type == 2 || type == 3) type = 1; |
|
|
|
|
BaseDanmaku item = mContext.mDanmakuFactory.createDanmaku(type, mContext); |
|
|
|
|
if (item == null || item.getType() == BaseDanmaku.TYPE_SPECIAL) continue; |
|
|
|
|
DanmakuUtils.fillText(item, data.getText()); |
|
|
|
|
item.textShadowColor = data.getShadow(); |
|
|
|
|
item.textColor = data.getColor(); |
|
|
|
|
item.flags = mContext.mGlobalFlagValues; |
|
|
|
|
item.textSize = data.getSize(); |
|
|
|
|
item.setTime(data.getTime()); |
|
|
|
|
item.setTimer(mTimer); |
|
|
|
|
item.index = i; |
|
|
|
|
synchronized (result.obtainSynchronizer()) { |
|
|
|
|
result.addItem(item); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private BaseDanmaku createDanmakuItem(DanmakuData data, int index) { |
|
|
|
|
int type = data.getType(); |
|
|
|
|
if (type == 2 || type == 3) type = BaseDanmaku.TYPE_SCROLL_RL; |
|
|
|
|
BaseDanmaku item = mContext.mDanmakuFactory.createDanmaku(type, mContext); |
|
|
|
|
if (item == null || item.getType() == BaseDanmaku.TYPE_SPECIAL) return null; |
|
|
|
|
DanmakuUtils.fillText(item, data.getText()); |
|
|
|
|
item.textShadowColor = data.getShadow(); |
|
|
|
|
item.textColor = data.getColor(); |
|
|
|
|
item.flags = mContext.mGlobalFlagValues; |
|
|
|
|
item.textSize = data.getSize(); |
|
|
|
|
item.setTime(data.getTime()); |
|
|
|
|
item.setTimer(mTimer); |
|
|
|
|
item.index = index; |
|
|
|
|
return item; |
|
|
|
|
} |
|
|
|
|
} |