
package master.flame.danmaku.danmaku.parser.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;

import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.parser.DanmakuFactory;

public class AcFunDanmakuParser extends BaseDanmakuParser {

    @Override
    public Danmakus parse() {
        if (mDataSource != null && mDataSource instanceof JSONSource) {
            JSONSource jsonSource = (JSONSource) mDataSource;
            return _parse(jsonSource.data());
        }
        return new Danmakus();
    }

    private Danmakus _parse(JSONArray jsonArray) {
        Danmakus danmakus = new Danmakus();
        if (jsonArray == null || jsonArray.length() == 0) {
            return danmakus;
        }
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject obj = jsonArray.getJSONObject(i);
                String c = obj.getString("c");
                String[] values = c.split(",");
                if (values.length > 0) {
                    int type = Integer.parseInt(values[2]); // 弹幕类型
                    if (type == 7)
                        // FIXME : hard code
                        // TODO : parse advance danmaku json
                        continue;
                    long time = (long) (Float.parseFloat(values[0]) * 1000); // 出现时间
                    int color = Integer.parseInt(values[1]) | 0xFF000000; // 颜色
                    float textSize = Float.parseFloat(values[3]); // 字体大小
                    BaseDanmaku item = DanmakuFactory.createDanmaku(type, mDispWidth / (mDispDensity - 0.6f));
                    if (item != null) {
                        item.time = time;
                        item.textSize = textSize * (mDispDensity - 0.6f);
                        item.textColor = color;
                        item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK;
                        DanmakuFactory.fillText(item, obj.optString("m", "...."));
                        item.index = i;
                        item.setTimer(mTimer);
                        danmakus.addItem(item);
                    }
                }

            } catch (JSONException e) {
            }
        }

        return danmakus;
    }
}

@@ -682,18 +682,19 @@
  return 
-_p
+doP
 arse(jso
@@ -759,24 +759,766 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * @param danmakuListData %E5%BC%B9%E5%B9%95%E6%95%B0%E6%8D%AE%0D%0A     *                        %E4%BC%A0%E5%85%A5%E7%9A%84%E6%95%B0%E7%BB%84%E5%86%85%E5%8C%85%E5%90%AB%E6%99%AE%E9%80%9A%E5%BC%B9%E5%B9%95%EF%BC%8C%E4%BC%9A%E5%91%98%E5%BC%B9%E5%B9%95%EF%BC%8C%E9%94%81%E5%AE%9A%E5%BC%B9%E5%B9%95%E3%80%82%0D%0A     * @return %E8%BD%AC%E6%8D%A2%E5%90%8E%E7%9A%84Danmakus%0D%0A     */%0D%0A    private Danmakus doParse(JSONArray danmakuListData) %7B%0D%0A        Danmakus danmakus = new Danmakus();%0D%0A        if (danmakuListData == null %7C%7C danmakuListData.length() == 0) %7B%0D%0A            return danmakus;%0D%0A        %7D%0D%0A        for (int i = 0; i %3C danmakuListData.length(); i++) %7B%0D%0A            try %7B%0D%0A                JSONArray danmakuArray = danmakuListData.getJSONArray(i);%0D%0A                if (danmakuArray != null) %7B%0D%0A                    danmakus = _parse(danmakuArray, danmakus);%0D%0A                %7D%0D%0A            %7D catch (JSONException e) %7B%0D%0A            %7D%0D%0A        %7D%0D%0A        return danmakus;%0D%0A    %7D%0D%0A%0D%0A
     private 
@@ -1552,16 +1552,35 @@
 sonArray
+, Danmakus danmakus
 ) %7B%0D%0A   
@@ -1580,33 +1580,37 @@
  %7B%0D%0A        
-D
+if (d
 anmakus 
 danmakus = n
@@ -1589,32 +1589,56 @@
    if (danmakus 
+== null) %7B%0D%0A            
 danmakus = new D
@@ -1641,32 +1641,43 @@
 ew Danmakus();%0D%0A
+        %7D%0D%0A
         if (json
@@ -3202,34 +3202,32 @@
              %7D%0D%0A
-%0D%0A
             %7D ca
@@ -3256,32 +3256,66 @@
 %7B%0D%0A            %7D
+ catch (NumberFormatException e) %7B
 %0D%0A        %7D%0D%0A%0D%0A 
@@ -3304,27 +3304,40 @@
  %7B%0D%0A        
+ 
-%7D%0D%0A
+   %7D%0D%0A        %7D
 %0D%0A        re

Support new AcFun danmaku format