Commit b88c7908 authored by fallenstardust's avatar fallenstardust

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

parent 709702ee
...@@ -32,6 +32,7 @@ import cn.garymb.ygomobile.utils.CardUtils; ...@@ -32,6 +32,7 @@ import cn.garymb.ygomobile.utils.CardUtils;
import cn.garymb.ygomobile.utils.DownloadUtil; import cn.garymb.ygomobile.utils.DownloadUtil;
import cn.garymb.ygomobile.utils.FileUtils; import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.YGOUtil; import cn.garymb.ygomobile.utils.YGOUtil;
import ocgcore.CardManager;
import ocgcore.StringManager; import ocgcore.StringManager;
import ocgcore.data.Card; import ocgcore.data.Card;
import ocgcore.enums.CardType; import ocgcore.enums.CardType;
...@@ -311,7 +312,6 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -311,7 +312,6 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
}); });
photoView.setOnLongClickListener(new View.OnLongClickListener() { photoView.setOnLongClickListener(new View.OnLongClickListener() {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
if (!isDownloadCardImage) if (!isDownloadCardImage)
...@@ -356,43 +356,46 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -356,43 +356,46 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
private void downloadCardImage(int code, File file) { private void downloadCardImage(int code, File file) {
isDownloadCardImage = false; isDownloadCardImage = false;
DownloadUtil.get().download(YGOUtil.getCardImageDetailUrl(code), file.getParent(), file.getName(), new DownloadUtil.OnDownloadListener() { CardManager cdb = new CardManager(file.getAbsolutePath(), null);
@Override if (cdb.getCard(code) != null) {
public void onDownloadSuccess(File file) { DownloadUtil.get().download(YGOUtil.getCardImageDetailUrl(code), file.getParent(), file.getName(), new DownloadUtil.OnDownloadListener() {
if (file.length() < 50 * 1024) { @Override
FileUtils.deleteFile(file); public void onDownloadSuccess(File file) {
Message message = new Message(); if (file.length() < 50 * 1024) {
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION; FileUtils.deleteFile(file);
message.obj = context.getString(R.string.download_image_error); Message message = new Message();
handler.sendMessage(message); message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
} else { 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 message = new Message();
message.what = TYPE_DOWNLOAD_CARD_IMAGE_OK; message.what = TYPE_DOWNLOAD_CARD_IMAGE_ING;
message.arg1 = code; message.arg1 = progress;
handler.sendMessage(message); handler.sendMessage(message);
} }
}
@Override @Override
public void onDownloading(int progress) { public void onDownloadFailed(Exception e) {
Message message = new Message(); //下载失败后删除下载的文件
message.what = TYPE_DOWNLOAD_CARD_IMAGE_ING; FileUtils.deleteFile(file);
message.arg1 = progress; downloadCardImage(code, file);
handler.sendMessage(message);
}
@Override Message message = new Message();
public void onDownloadFailed(Exception e) { message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
//下载失败后删除下载的文件 message.obj = e.toString();
FileUtils.deleteFile(file); handler.sendMessage(message);
downloadCardImage(code, file); }
});
Message message = new Message(); }
message.what = TYPE_DOWNLOAD_CARD_IMAGE_EXCEPTION;
message.obj = e.toString();
handler.sendMessage(message);
}
});
} }
public void onPreCard() { public void onPreCard() {
......
...@@ -30,64 +30,15 @@ import ocgcore.data.Card; ...@@ -30,64 +30,15 @@ import ocgcore.data.Card;
public class CardManager { public class CardManager {
private String dbDir, exDbPath; private static int cdbNum = 0;
private final SparseArray<Card> cardDataHashMap = new SparseArray<>(); private final SparseArray<Card> cardDataHashMap = new SparseArray<>();
private static int cdbNum=0; private String dbDir, exDbPath;
public CardManager(String dbDir, String exPath) { public CardManager(String dbDir, String exPath) {
this.dbDir = dbDir; this.dbDir = dbDir;
this.exDbPath = exPath; 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) { private static SQLiteDatabase openDatabase(String file) {
return SQLiteDatabase.openDatabase(file, null, return SQLiteDatabase.openDatabase(file, null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY); SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
...@@ -124,21 +75,21 @@ public class CardManager { ...@@ -124,21 +75,21 @@ public class CardManager {
} }
public static List<File> readZipCdb(String zipPath) throws IOException { public static List<File> readZipCdb(String zipPath) throws IOException {
String savePath= App.get().getExternalCacheDir().getAbsolutePath(); String savePath = App.get().getExternalCacheDir().getAbsolutePath();
List<File> fileList=new ArrayList<>(); List<File> fileList = new ArrayList<>();
ZipFile zf = new ZipFile(zipPath,"GBK"); ZipFile zf = new ZipFile(zipPath, "GBK");
InputStream in = new BufferedInputStream(new FileInputStream(zipPath)); InputStream in = new BufferedInputStream(new FileInputStream(zipPath));
ZipInputStream zin = new ZipInputStream(in); ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze; ZipEntry ze;
Enumeration<ZipEntry> entris = zf.getEntries(); Enumeration<ZipEntry> entris = zf.getEntries();
while (entris.hasMoreElements()) { while (entris.hasMoreElements()) {
ze=entris.nextElement(); ze = entris.nextElement();
if (ze.isDirectory()) { if (ze.isDirectory()) {
//Do nothing //Do nothing
} else { } else {
if (ze.getName().endsWith(".cdb")) { 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); InputStream inputStream = zf.getInputStream(ze);
OutputStream os = new FileOutputStream(file); OutputStream os = new FileOutputStream(file);
int bytesRead = 0; int bytesRead = 0;
...@@ -157,6 +108,55 @@ public class CardManager { ...@@ -157,6 +108,55 @@ public class CardManager {
return fileList; 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 @WorkerThread
protected int readAllCards(File file, SparseArray<Card> cardMap) { protected int readAllCards(File file, SparseArray<Card> cardMap) {
if (!file.exists()) { 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