Commit b88c7908 authored by fallenstardust's avatar fallenstardust

不是自带数据库卡片不做下载

parent 709702ee
......@@ -32,6 +32,7 @@ import cn.garymb.ygomobile.utils.CardUtils;
import cn.garymb.ygomobile.utils.DownloadUtil;
import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.YGOUtil;
import ocgcore.CardManager;
import ocgcore.StringManager;
import ocgcore.data.Card;
import ocgcore.enums.CardType;
......@@ -311,7 +312,6 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
});
photoView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!isDownloadCardImage)
......@@ -356,43 +356,46 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
private void downloadCardImage(int code, File file) {
isDownloadCardImage = false;
DownloadUtil.get().download(YGOUtil.getCardImageDetailUrl(code), file.getParent(), file.getName(), new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
if (file.length() < 50 * 1024) {
FileUtils.deleteFile(file);
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
message.obj = context.getString(R.string.download_image_error);
handler.sendMessage(message);
} else {
CardManager cdb = new CardManager(file.getAbsolutePath(), null);
if (cdb.getCard(code) != null) {
DownloadUtil.get().download(YGOUtil.getCardImageDetailUrl(code), file.getParent(), file.getName(), new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(File file) {
if (file.length() < 50 * 1024) {
FileUtils.deleteFile(file);
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
message.obj = context.getString(R.string.download_image_error);
handler.sendMessage(message);
} else {
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_OK;
message.arg1 = code;
handler.sendMessage(message);
}
}
@Override
public void onDownloading(int progress) {
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_OK;
message.arg1 = code;
message.what = TYPE_DOWNLOAD_CARD_IMAGE_ING;
message.arg1 = progress;
handler.sendMessage(message);
}
}
@Override
public void onDownloading(int progress) {
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_ING;
message.arg1 = progress;
handler.sendMessage(message);
}
@Override
public void onDownloadFailed(Exception e) {
//下载失败后删除下载的文件
FileUtils.deleteFile(file);
downloadCardImage(code, file);
@Override
public void onDownloadFailed(Exception e) {
//下载失败后删除下载的文件
FileUtils.deleteFile(file);
downloadCardImage(code, file);
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
message.obj = e.toString();
handler.sendMessage(message);
}
});
Message message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
message.obj = e.toString();
handler.sendMessage(message);
}
});
}
}
public void onPreCard() {
......
......@@ -30,64 +30,15 @@ import ocgcore.data.Card;
public class CardManager {
private String dbDir, exDbPath;
private static int cdbNum = 0;
private final SparseArray<Card> cardDataHashMap = new SparseArray<>();
private static int cdbNum=0;
private String dbDir, exDbPath;
public CardManager(String dbDir, String exPath) {
this.dbDir = dbDir;
this.exDbPath = exPath;
}
public Card getCard(int code) {
return cardDataHashMap.get(Integer.valueOf(code));
}
public int getCount() {
return cardDataHashMap.size();
}
public SparseArray<Card> getAllCards() {
return cardDataHashMap;
}
@WorkerThread
public void loadCards() {
cardDataHashMap.clear();
int count = readAllCards(AppsSettings.get().getDataBaseFile(), cardDataHashMap);
Log.i("Irrlicht", "load defualt cdb:" + count);
if (AppsSettings.get().isReadExpansions()) {
File dir = new File(exDbPath);
if (dir.exists()) {
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
return file.isFile() && (name.endsWith(".cdb")||name.endsWith(".zip"));
}
});
//读取全部卡片
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".cdb")){
count = readAllCards(file, cardDataHashMap);
}else if(file.getName().endsWith(".zip")){
Log.e("CardManager","读取压缩包");
try {
for(File file1:readZipCdb(file.getAbsolutePath())){
count = readAllCards(file1, cardDataHashMap);
}
} catch (IOException e) {
Log.e("CardManager","读取压缩包错误"+e);
}
}
Log.i("Irrlicht", "load " + count + " cdb:" + file);
}
}
}
}
}
private static SQLiteDatabase openDatabase(String file) {
return SQLiteDatabase.openDatabase(file, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
......@@ -124,21 +75,21 @@ public class CardManager {
}
public static List<File> readZipCdb(String zipPath) throws IOException {
String savePath= App.get().getExternalCacheDir().getAbsolutePath();
List<File> fileList=new ArrayList<>();
String savePath = App.get().getExternalCacheDir().getAbsolutePath();
List<File> fileList = new ArrayList<>();
ZipFile zf = new ZipFile(zipPath,"GBK");
ZipFile zf = new ZipFile(zipPath, "GBK");
InputStream in = new BufferedInputStream(new FileInputStream(zipPath));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
Enumeration<ZipEntry> entris = zf.getEntries();
while (entris.hasMoreElements()) {
ze=entris.nextElement();
ze = entris.nextElement();
if (ze.isDirectory()) {
//Do nothing
} else {
if (ze.getName().endsWith(".cdb")) {
File file=new File(savePath,"cards"+cdbNum+".cdb");
File file = new File(savePath, "cards" + cdbNum + ".cdb");
InputStream inputStream = zf.getInputStream(ze);
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
......@@ -157,6 +108,55 @@ public class CardManager {
return fileList;
}
public Card getCard(int code) {
return cardDataHashMap.get(Integer.valueOf(code));
}
public int getCount() {
return cardDataHashMap.size();
}
public SparseArray<Card> getAllCards() {
return cardDataHashMap;
}
@WorkerThread
public void loadCards() {
cardDataHashMap.clear();
int count = readAllCards(AppsSettings.get().getDataBaseFile(), cardDataHashMap);
Log.i("Irrlicht", "load defualt cdb:" + count);
if (AppsSettings.get().isReadExpansions()) {
File dir = new File(exDbPath);
if (dir.exists()) {
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
return file.isFile() && (name.endsWith(".cdb") || name.endsWith(".zip"));
}
});
//读取全部卡片
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".cdb")) {
count = readAllCards(file, cardDataHashMap);
} else if (file.getName().endsWith(".zip")) {
Log.e("CardManager", "读取压缩包");
try {
for (File file1 : readZipCdb(file.getAbsolutePath())) {
count = readAllCards(file1, cardDataHashMap);
}
} catch (IOException e) {
Log.e("CardManager", "读取压缩包错误" + e);
}
}
Log.i("Irrlicht", "load " + count + " cdb:" + file);
}
}
}
}
}
@WorkerThread
protected int readAllCards(File file, SparseArray<Card> cardMap) {
if (!file.exists()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment