mirror of https://github.com/FongMi/TV.git
parent
2610e4a45d
commit
2a131a0ecd
@ -0,0 +1,48 @@ |
||||
package com.fongmi.android.tv.bean; |
||||
|
||||
import android.text.TextUtils; |
||||
|
||||
import org.simpleframework.xml.Attribute; |
||||
import org.simpleframework.xml.ElementList; |
||||
import org.simpleframework.xml.Root; |
||||
import org.simpleframework.xml.Text; |
||||
import org.simpleframework.xml.core.Persister; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
@Root(name = "i", strict = false) |
||||
public class Danmu { |
||||
|
||||
@ElementList(entry = "d", required = false, inline = true) |
||||
private List<D> d; |
||||
|
||||
public static Danmu fromXml(String str) { |
||||
try { |
||||
return new Persister().read(Danmu.class, str); |
||||
} catch (Exception e) { |
||||
return new Danmu(); |
||||
} |
||||
} |
||||
|
||||
public List<D> getD() { |
||||
return d == null ? Collections.emptyList() : d; |
||||
} |
||||
|
||||
public static class D { |
||||
|
||||
@Attribute(name = "p", required = false) |
||||
public String p; |
||||
|
||||
@Text |
||||
public String t; |
||||
|
||||
public String getP() { |
||||
return TextUtils.isEmpty(p) ? "" : p; |
||||
} |
||||
|
||||
public String getT() { |
||||
return TextUtils.isEmpty(t) ? "" : t; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,44 @@ |
||||
package com.fongmi.android.tv.player.danmu; |
||||
|
||||
import android.graphics.Color; |
||||
|
||||
import com.fongmi.android.tv.bean.Danmu; |
||||
|
||||
import master.flame.danmaku.danmaku.model.BaseDanmaku; |
||||
import master.flame.danmaku.danmaku.model.IDanmakus; |
||||
import master.flame.danmaku.danmaku.model.android.Danmakus; |
||||
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; |
||||
|
||||
public class Parser extends BaseDanmakuParser { |
||||
|
||||
private final Danmu danmu; |
||||
|
||||
public Parser(Danmu danmu) { |
||||
this.danmu = danmu; |
||||
} |
||||
|
||||
@Override |
||||
protected Danmakus parse() { |
||||
Danmakus result = new Danmakus(IDanmakus.ST_BY_TIME); |
||||
for (Danmu.D d : danmu.getD()) { |
||||
String[] values = d.getP().split(","); |
||||
if (values.length < 8) continue; |
||||
int type = Integer.parseInt(values[1]); |
||||
long time = (long) (Float.parseFloat(values[0]) * 1000); |
||||
float size = Float.parseFloat(values[2]) * (mDispDensity - 0.6f); |
||||
int color = (int) ((0x00000000ff000000L | Long.parseLong(values[3])) & 0x00000000ffffffffL); |
||||
BaseDanmaku item = mContext.mDanmakuFactory.createDanmaku(type, mContext); |
||||
item.setTime(time); |
||||
item.setTimer(mTimer); |
||||
item.setTextSize(size); |
||||
item.setTextColor(color); |
||||
item.setTextShadowColor(color <= Color.BLACK ? Color.WHITE : Color.BLACK); |
||||
item.setFlags(mContext.mGlobalFlagValues); |
||||
Object lock = result.obtainSynchronizer(); |
||||
synchronized (lock) { |
||||
result.addItem(item); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
} |
||||
@ -0,0 +1,50 @@ |
||||
package com.github.catvod.net; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
|
||||
import com.google.common.net.HttpHeaders; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.zip.Inflater; |
||||
import java.util.zip.InflaterInputStream; |
||||
|
||||
import okhttp3.Interceptor; |
||||
import okhttp3.MediaType; |
||||
import okhttp3.Response; |
||||
import okhttp3.ResponseBody; |
||||
import okio.BufferedSource; |
||||
import okio.Okio; |
||||
|
||||
public class DeflateInterceptor implements Interceptor { |
||||
|
||||
@NonNull |
||||
@Override |
||||
public Response intercept(@NonNull Chain chain) throws IOException { |
||||
return deflate(chain.proceed(chain.request())); |
||||
} |
||||
|
||||
private Response deflate(Response response) { |
||||
String encoding = response.header(HttpHeaders.CONTENT_ENCODING); |
||||
if (response.body() == null || encoding == null || !encoding.equals("deflate")) return response; |
||||
InflaterInputStream is = new InflaterInputStream(response.body().byteStream(), new Inflater(true)); |
||||
return response.newBuilder().headers(response.headers()).body(new ResponseBody() { |
||||
@Nullable |
||||
@Override |
||||
public MediaType contentType() { |
||||
return response.body().contentType(); |
||||
} |
||||
|
||||
@Override |
||||
public long contentLength() { |
||||
return response.body().contentLength(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public BufferedSource source() { |
||||
return Okio.buffer(Okio.source(is)); |
||||
} |
||||
}).build(); |
||||
} |
||||
} |
||||
@ -1,38 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2013 Chen Hui <calmer91@gmail.com> |
||||
* |
||||
* 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 master.flame.danmaku.danmaku.loader; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
import master.flame.danmaku.danmaku.parser.IDataSource; |
||||
|
||||
public interface ILoader { |
||||
/** |
||||
* @return data source |
||||
*/ |
||||
IDataSource<?> getDataSource(); |
||||
|
||||
/** |
||||
* @param uri 弹幕文件地址(http:// file://)
|
||||
*/ |
||||
void load(String uri) throws IllegalDataException; |
||||
|
||||
/** |
||||
* @param in stream from Internet or local file |
||||
*/ |
||||
void load(InputStream in) throws IllegalDataException; |
||||
} |
||||
@ -1,28 +0,0 @@ |
||||
package master.flame.danmaku.danmaku.loader; |
||||
|
||||
/** |
||||
* Thrown when data is loading which can not be reasonably deal with. |
||||
* |
||||
* @author yrom |
||||
*/ |
||||
public class IllegalDataException extends Exception { |
||||
|
||||
private static final long serialVersionUID = 10441759254L; |
||||
|
||||
public IllegalDataException() { |
||||
super(); |
||||
} |
||||
|
||||
public IllegalDataException(String detailMessage, Throwable throwable) { |
||||
super(detailMessage, throwable); |
||||
} |
||||
|
||||
public IllegalDataException(String detailMessage) { |
||||
super(detailMessage); |
||||
} |
||||
|
||||
public IllegalDataException(Throwable throwable) { |
||||
super(throwable); |
||||
} |
||||
|
||||
} |
||||
@ -1,56 +0,0 @@ |
||||
package master.flame.danmaku.danmaku.loader.android; |
||||
|
||||
import android.net.Uri; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
import master.flame.danmaku.danmaku.loader.ILoader; |
||||
import master.flame.danmaku.danmaku.loader.IllegalDataException; |
||||
import master.flame.danmaku.danmaku.parser.android.JSONSource; |
||||
|
||||
/** |
||||
* Ac danmaku loader |
||||
* |
||||
* @author yrom |
||||
*/ |
||||
public class AcFunDanmakuLoader implements ILoader { |
||||
private static volatile AcFunDanmakuLoader instance; |
||||
private JSONSource dataSource; |
||||
private AcFunDanmakuLoader() { |
||||
} |
||||
|
||||
public static ILoader instance() { |
||||
if (instance == null) { |
||||
synchronized (AcFunDanmakuLoader.class) { |
||||
if (instance == null) |
||||
instance = new AcFunDanmakuLoader(); |
||||
} |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
@Override |
||||
public JSONSource getDataSource() { |
||||
return dataSource; |
||||
} |
||||
|
||||
@Override |
||||
public void load(String uri) throws IllegalDataException { |
||||
try { |
||||
dataSource = new JSONSource(Uri.parse(uri)); |
||||
} catch (Exception e) { |
||||
throw new IllegalDataException(e); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void load(InputStream in) throws IllegalDataException { |
||||
try { |
||||
dataSource = new JSONSource(in); |
||||
} catch (Exception e) { |
||||
throw new IllegalDataException(e); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -1,58 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2013 Chen Hui <calmer91@gmail.com> |
||||
* |
||||
* 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 master.flame.danmaku.danmaku.loader.android; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
import master.flame.danmaku.danmaku.loader.ILoader; |
||||
import master.flame.danmaku.danmaku.loader.IllegalDataException; |
||||
import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; |
||||
|
||||
public class BiliDanmakuLoader implements ILoader { |
||||
|
||||
private static BiliDanmakuLoader _instance; |
||||
|
||||
private AndroidFileSource dataSource; |
||||
|
||||
private BiliDanmakuLoader() { |
||||
|
||||
} |
||||
|
||||
public static BiliDanmakuLoader instance() { |
||||
if (_instance == null) { |
||||
_instance = new BiliDanmakuLoader(); |
||||
} |
||||
return _instance; |
||||
} |
||||
|
||||
public void load(String uri) throws IllegalDataException { |
||||
try { |
||||
dataSource = new AndroidFileSource(uri); |
||||
} catch (Exception e) { |
||||
throw new IllegalDataException(e); |
||||
} |
||||
} |
||||
|
||||
public void load(InputStream stream) { |
||||
dataSource = new AndroidFileSource(stream); |
||||
} |
||||
|
||||
@Override |
||||
public AndroidFileSource getDataSource() { |
||||
return dataSource; |
||||
} |
||||
} |
||||
@ -1,34 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2013 Chen Hui <calmer91@gmail.com> |
||||
* |
||||
* 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 master.flame.danmaku.danmaku.loader.android; |
||||
|
||||
import master.flame.danmaku.danmaku.loader.ILoader; |
||||
|
||||
public class DanmakuLoaderFactory { |
||||
|
||||
public static String TAG_BILI = "bili"; |
||||
public static String TAG_ACFUN = "acfun"; |
||||
|
||||
public static ILoader create(String tag) { |
||||
if (TAG_BILI.equalsIgnoreCase(tag)) { |
||||
return BiliDanmakuLoader.instance(); |
||||
} else if (TAG_ACFUN.equalsIgnoreCase(tag)) |
||||
return AcFunDanmakuLoader.instance(); |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -1,28 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2013 Chen Hui <calmer91@gmail.com> |
||||
* |
||||
* 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 master.flame.danmaku.danmaku.parser; |
||||
|
||||
public interface IDataSource<T> { |
||||
String SCHEME_HTTP_TAG = "http"; |
||||
String SCHEME_HTTPS_TAG = "https"; |
||||
String SCHEME_FILE_TAG = "file"; |
||||
|
||||
public T data(); |
||||
|
||||
public void release(); |
||||
|
||||
} |
||||
@ -1,95 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2013 Chen Hui <calmer91@gmail.com> |
||||
* |
||||
* 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 master.flame.danmaku.danmaku.parser.android; |
||||
|
||||
import android.net.Uri; |
||||
|
||||
import java.io.BufferedInputStream; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
|
||||
import master.flame.danmaku.danmaku.parser.IDataSource; |
||||
import master.flame.danmaku.danmaku.util.IOUtils; |
||||
|
||||
public class AndroidFileSource implements IDataSource<InputStream> { |
||||
|
||||
private InputStream inStream; |
||||
|
||||
public AndroidFileSource(String filepath) { |
||||
fillStreamFromFile(new File(filepath)); |
||||
} |
||||
|
||||
public AndroidFileSource(Uri uri) { |
||||
fillStreamFromUri(uri); |
||||
} |
||||
|
||||
public AndroidFileSource(File file) { |
||||
fillStreamFromFile(file); |
||||
} |
||||
|
||||
public AndroidFileSource(InputStream stream) { |
||||
this.inStream = stream; |
||||
} |
||||
|
||||
public void fillStreamFromFile(File file) { |
||||
try { |
||||
inStream = new BufferedInputStream(new FileInputStream(file)); |
||||
} catch (FileNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
public void fillStreamFromUri(Uri uri) { |
||||
String scheme = uri.getScheme(); |
||||
if (SCHEME_HTTP_TAG.equalsIgnoreCase(scheme) || SCHEME_HTTPS_TAG.equalsIgnoreCase(scheme)) { |
||||
fillStreamFromHttpFile(uri); |
||||
} else if (SCHEME_FILE_TAG.equalsIgnoreCase(scheme)) { |
||||
fillStreamFromFile(new File(uri.getPath())); |
||||
} |
||||
} |
||||
|
||||
public void fillStreamFromHttpFile(Uri uri) { |
||||
try { |
||||
URL url = new URL(uri.getPath()); |
||||
url.openConnection(); |
||||
inStream = new BufferedInputStream(url.openStream()); |
||||
|
||||
} catch (MalformedURLException e) { |
||||
e.printStackTrace(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void release() { |
||||
IOUtils.closeQuietly(inStream); |
||||
inStream = null; |
||||
} |
||||
|
||||
@Override |
||||
public InputStream data() { |
||||
return inStream; |
||||
} |
||||
|
||||
} |
||||
@ -1,78 +0,0 @@ |
||||
package master.flame.danmaku.danmaku.parser.android; |
||||
|
||||
import android.net.Uri; |
||||
import android.text.TextUtils; |
||||
|
||||
import org.json.JSONArray; |
||||
import org.json.JSONException; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.URL; |
||||
|
||||
import master.flame.danmaku.danmaku.parser.IDataSource; |
||||
import master.flame.danmaku.danmaku.util.IOUtils; |
||||
|
||||
/** |
||||
* a json file source |
||||
* |
||||
* @author yrom |
||||
*/ |
||||
public class JSONSource implements IDataSource<JSONArray> { |
||||
private JSONArray mJSONArray; |
||||
private InputStream mInput; |
||||
|
||||
public JSONSource(String json) throws JSONException { |
||||
init(json); |
||||
} |
||||
|
||||
public JSONSource(InputStream in) throws JSONException { |
||||
init(in); |
||||
} |
||||
|
||||
public JSONSource(URL url) throws JSONException, IOException { |
||||
this(url.openStream()); |
||||
} |
||||
|
||||
public JSONSource(File file) throws FileNotFoundException, JSONException { |
||||
init(new FileInputStream(file)); |
||||
} |
||||
|
||||
public JSONSource(Uri uri) throws IOException, JSONException { |
||||
String scheme = uri.getScheme(); |
||||
if (SCHEME_HTTP_TAG.equalsIgnoreCase(scheme) || SCHEME_HTTPS_TAG.equalsIgnoreCase(scheme)) { |
||||
init(new URL(uri.getPath()).openStream()); |
||||
} else if (SCHEME_FILE_TAG.equalsIgnoreCase(scheme)) { |
||||
init(new FileInputStream(uri.getPath())); |
||||
} |
||||
} |
||||
|
||||
private void init(InputStream in) throws JSONException { |
||||
if (in == null) |
||||
throw new NullPointerException("input stream cannot be null!"); |
||||
mInput = in; |
||||
String json = IOUtils.getString(mInput); |
||||
init(json); |
||||
} |
||||
|
||||
private void init(String json) throws JSONException { |
||||
if (!TextUtils.isEmpty(json)) { |
||||
mJSONArray = new JSONArray(json); |
||||
} |
||||
} |
||||
|
||||
public JSONArray data() { |
||||
return mJSONArray; |
||||
} |
||||
|
||||
@Override |
||||
public void release() { |
||||
IOUtils.closeQuietly(mInput); |
||||
mInput = null; |
||||
mJSONArray = null; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue