Commit 445e34ac authored by fallenstardust's avatar fallenstardust

fix loadDeckFromFile

parent 63852a9c
......@@ -9,8 +9,7 @@ import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.io.FileInputStream;
import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.bean.Deck;
......@@ -68,35 +67,86 @@ public class GameUriManager {
return activity;
}
private String getDeckName(Uri uri) {
String path = uri.getPath();
Log.i("kk", "path="+path);
if(path != null) {
int index = path.lastIndexOf("/");
if (index > 0) {
String name = path.substring(index + 1);
index = name.lastIndexOf(".");
if (index > 0) {
//1.ydk
name = name.substring(0, index);
}
return name;
}
}
return "tmp_" + System.currentTimeMillis();
}
private File getDeckFile(File dir, String name) {
File file = new File(dir, name + ".ydk");
if (file.exists()) {
for (int i = 2; i < 10; i++) {
file = new File(dir, name + "(" + i + ").ydk");
if (!file.exists()) {
return file;
}
}
return new File(dir, "tmp_" + System.currentTimeMillis() + ".ydk");
} else {
if (!dir.exists()) {
dir.mkdirs();
}
}
return file;
}
private boolean isDeckDir(File file) {
if (!Constants.COPY_YDK_FILE) {
return true;
}
String deck = new File(AppsSettings.get().getDeckDir()).getAbsolutePath();
return TextUtils.equals(deck, file.getParentFile().getAbsolutePath());
}
private void doUri(Uri uri) {
if ("file".equals(uri.getScheme())) {
File file = new File(uri.getPath());
Intent startdeck = new Intent(getActivity(), DeckManagerActivity.getDeckManager());
startdeck.putExtra(Intent.EXTRA_TEXT, file.getAbsolutePath());
if (isDeckDir(file)) {
//deck目录
startdeck.putExtra(Intent.EXTRA_TEXT, file.getAbsolutePath());
} else {
//非deck目录
File ydk = getDeckFile(new File(AppsSettings.get().getDeckDir()), getDeckName(uri));
FileUtils.copyFile(file, ydk);
startdeck.putExtra(Intent.EXTRA_TEXT, ydk.getAbsolutePath());
}
activity.startActivity(startdeck);
} else if("content".equals(uri.getScheme())) {
} else if ("content".equals(uri.getScheme())) {
try {
List<String> paths = uri.getPathSegments();
String name = "tmp_" + System.currentTimeMillis() + ".ydk";
if (paths.size() > 0) {
String tmp = paths.get(paths.size() - 1);
if (tmp.endsWith(".ydk")) {
name = tmp;
}
}
File file = new File(AppsSettings.get().getDeckDir(), name);
File dir = Constants.COPY_YDK_FILE?new File(AppsSettings.get().getDeckDir()):new File(getActivity().getApplicationInfo().dataDir, "cache");
File ydk = getDeckFile(dir, getDeckName(uri));
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r");
if (pfd == null) {
return;
} else {
FileUtils.copyFile(file.getAbsolutePath(), new FileOutputStream(pfd.getFileDescriptor()));
pfd.close();
try {
FileUtils.copyFile(new FileInputStream(pfd.getFileDescriptor()), ydk);
} catch (Throwable e) {
e.printStackTrace();
} finally {
pfd.close();
}
}
Intent startdeck = new Intent(getActivity(), DeckManagerActivity.getDeckManager());
startdeck.putExtra(Intent.EXTRA_TEXT, file.getAbsolutePath());
startdeck.putExtra(Intent.EXTRA_TEXT, ydk.getAbsolutePath());
activity.startActivity(startdeck);
} catch (Throwable e) {
throw new RuntimeException(e);
e.printStackTrace();
}
} else {
String host = uri.getHost();
......
......@@ -2,6 +2,8 @@ package cn.garymb.ygomobile.ui.cards.deck;
import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
......@@ -65,6 +67,7 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement
private ImageLoader imageLoader;
private boolean showHead = false;
private String mDeckMd5;
private DeckInfo mDeckInfo;
public DeckAdapater(Context context, RecyclerView recyclerView, ImageLoader imageLoader) {
this.context = context;
......@@ -378,7 +381,23 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement
return mSideCount;
}
public DeckInfo getDeckInfo() {
return mDeckInfo;
}
public LimitList getLimitList() {
return mLimitList;
}
public File getYdkFile(){
if(mDeckInfo != null){
return mDeckInfo.source;
}
return null;
}
public void setDeck(DeckInfo deck) {
mDeckInfo = deck;
if (deck != null) {
loadData(deck);
}
......@@ -386,7 +405,9 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement
}
public DeckInfo read(CardLoader cardLoader, File file, LimitList limitList) {
setLimitList(limitList);
if(limitList != null) {
setLimitList(limitList);
}
return DeckLoader.readDeck(cardLoader, file, limitList);
}
......
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