package com.bigdo.common;
import java.io.File;
import com.bigdo.app.BaseActivity;
import com.bigdo.util.MD5;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
public final class VideoDownHelper {
public static int down(BaseActivity a, int idtype, int idvideo,
String title, String video) {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
String baseDir = Environment.getExternalStorageDirectory()
+ RConfig.Video_Dir;
if (idtype > 0 && idvideo > 0 && title != null && !title.equals("")
&& video != null && !video.equals("")) {
String _f = cacheFileName(idtype, idvideo, video);
String l_f = _f + ".r.t";
String l_f_f = _f + ".r";
File dir = new File(baseDir);
File dff = new File(dir, l_f_f);
if (dff.exists()) {
a.showInfo("文件已经缓存了哦");
return 2;
}
File df = new File(dir, l_f);
if (df.exists()) {
a.showInfo("文件已经开始下载了哦.");
return 1;
}
BDSQLiteHelper sqlobj = null;
SQLiteDatabase sqlobj_w = null;
Cursor cu = null;
try {
sqlobj = new BDSQLiteHelper(a);
sqlobj_w = sqlobj.getWritableDatabase();
sqlobj_w.delete(
"download",
"idtype <= ? or idvideo <= ? or title = ? or downurl = ?",
new String[] { "0", "0", "", "" });
String sql = "select * from download where idtype = ? and idvideo = ?";
cu = sqlobj_w.rawQuery(sql, new String[] { idtype + "",
idvideo + "" });
int c = cu.getCount();
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
if (c <= 0) {
sql = "select idown from download order by idown asc limit 0,1";
cu = sqlobj_w.rawQuery(sql, null);
int idown = 0;
if (cu.moveToNext()) {
idown = cu.getInt(0);
}
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
ContentValues cv = new ContentValues();
cv.put("idown", ++idown);
cv.put("idvideo", idvideo);
cv.put("idtype", idtype);
cv.put("title", title);
cv.put("downurl", video);
cv.put("isfinish", 0);
if (sqlobj_w.insert("download", "", cv) > 0) {
a.showInfo("亲!已加入下载队列.");
} else {
a.showInfo("亲!加入下载队列失败.");
}
} else {
a.showInfo("亲!已加入下载队列.");
}
sendMag(a);
return 1;
} catch (Exception e) {
}
try {
if (cu != null) {
cu.close();
cu = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj_w != null) {
sqlobj_w.close();
sqlobj_w = null;
}
} catch (Exception ex) {
}
try {
if (sqlobj != null) {
sqlobj.close();
sqlobj = null;
}
} catch (Exception ex) {
}
}
} else {
a.showInfo("亲!没有发现SD卡哦.");
}
return 0;
}
public static int isExist(int idtype, int idvideo, String video,
boolean isAll) {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
String baseDir = Environment.getExternalStorageDirectory()
+ RConfig.Video_Dir;
File fd = new File(baseDir);
File f = new File(fd, cacheFileName(idtype, idvideo, video) + ".r");
if (f.exists()) {
return 2;
}
if (isAll) {
File ft = new File(fd, cacheFileName(idtype, idvideo, video)
+ ".r.t");
if (ft.exists()) {
return 1;
}
}
}
return 0;
}
public static String cacheFileName(int idtype, int idvideo, String video) {
String durl = video.replace("/", "-");
durl = idtype + idvideo + durl.replace("\\", "-");
int ix = durl.lastIndexOf('.');
if (ix >= 0) {
String pf = durl.substring(0, ix);
String lf = durl.substring(ix);
String fn = MD5.encode(pf) + lf;
fn = fn.replace("-", "");
return fn;
}
return durl;
}
public static void sendMag(BaseActivity a) {
Intent i = new Intent();
i.setAction("com.bigdo.service.DownloadService");
a.sendBroadcast(i);
}
}