Async XML loading, Move XML methods to parser class
This commit is contained in:
parent
5b94408a63
commit
809a1afa28
|
@ -1,14 +1,11 @@
|
||||||
package com.reicast.emulator;
|
package com.reicast.emulator;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -18,18 +15,11 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.graphics.drawable.BitmapDrawable;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
@ -328,7 +318,12 @@ public class FileBrowser extends Fragment {
|
||||||
} else {
|
} else {
|
||||||
title = name.substring(0, name.lastIndexOf("."));
|
title = name.substring(0, name.lastIndexOf("."));
|
||||||
}
|
}
|
||||||
displayDetails(game_index + title.replace(" ", "+"), game);
|
title = title.replace(" ", "+").replace("_", "+");
|
||||||
|
if (title.endsWith("+")) {
|
||||||
|
title = title.substring(0, title.length() - 1);
|
||||||
|
}
|
||||||
|
XMLParser xmlParser = new XMLParser(parentActivity, game, mCallback, vib);
|
||||||
|
xmlParser.execute(game_index + title);
|
||||||
} else {
|
} else {
|
||||||
mCallback.onGameSelected(game != null ? Uri
|
mCallback.onGameSelected(game != null ? Uri
|
||||||
.fromFile(game) : Uri.EMPTY);
|
.fromFile(game) : Uri.EMPTY);
|
||||||
|
@ -353,90 +348,6 @@ public class FileBrowser extends Fragment {
|
||||||
});
|
});
|
||||||
list.addView(childview);
|
list.addView(childview);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayDetails(String url, final File game) {
|
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
|
|
||||||
builder.setCancelable(true);
|
|
||||||
XMLParser xmlParser = new XMLParser(parentActivity);
|
|
||||||
String gameData = xmlParser.getXmlFromUrl(url);
|
|
||||||
if (gameData != null) {
|
|
||||||
Document doc = xmlParser.getDomElement(gameData);
|
|
||||||
if (doc != null && doc.getElementsByTagName("Game") != null) {
|
|
||||||
Element root = (Element) doc.getElementsByTagName("Game").item(
|
|
||||||
0);
|
|
||||||
String title = xmlParser.getValue(root, "GameTitle");
|
|
||||||
builder.setTitle(getString(R.string.game_details, title));
|
|
||||||
String details = xmlParser.getValue(root, "Overview");
|
|
||||||
builder.setMessage(details);
|
|
||||||
Element boxart = (Element) root.getElementsByTagName("Images")
|
|
||||||
.item(0);
|
|
||||||
String image = "http://thegamesdb.net/banners/"
|
|
||||||
+ xmlParser.getValue(boxart, "boxart");
|
|
||||||
try {
|
|
||||||
builder.setIcon(new BitmapDrawable(decodeBitmapIcon(image)));
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
builder.setTitle(getString(R.string.info_unavailable));
|
|
||||||
}
|
|
||||||
builder.setPositiveButton("Close",
|
|
||||||
new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
dialog.dismiss();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
builder.setPositiveButton("Launch",
|
|
||||||
new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
dialog.dismiss();
|
|
||||||
mCallback.onGameSelected(game != null ? Uri
|
|
||||||
.fromFile(game) : Uri.EMPTY);
|
|
||||||
vib.vibrate(250);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
builder.create().show();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bitmap decodeBitmapIcon(String filename) throws IOException {
|
|
||||||
URL updateURL = new URL(filename);
|
|
||||||
URLConnection conn1 = updateURL.openConnection();
|
|
||||||
InputStream im = conn1.getInputStream();
|
|
||||||
BufferedInputStream bis = new BufferedInputStream(im, 512);
|
|
||||||
|
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
||||||
options.inJustDecodeBounds = true;
|
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(bis, null, options);
|
|
||||||
|
|
||||||
int heightRatio = (int) Math.ceil(options.outHeight / (float) 72);
|
|
||||||
int widthRatio = (int) Math.ceil(options.outWidth / (float) 72);
|
|
||||||
|
|
||||||
if (heightRatio > 1 || widthRatio > 1) {
|
|
||||||
if (heightRatio > widthRatio) {
|
|
||||||
options.inSampleSize = heightRatio;
|
|
||||||
} else {
|
|
||||||
options.inSampleSize = widthRatio;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
options.inJustDecodeBounds = false;
|
|
||||||
bis.close();
|
|
||||||
im.close();
|
|
||||||
conn1 = updateURL.openConnection();
|
|
||||||
im = conn1.getInputStream();
|
|
||||||
bis = new BufferedInputStream(im, 512);
|
|
||||||
bitmap = BitmapFactory.decodeStream(bis, null, options);
|
|
||||||
|
|
||||||
bis.close();
|
|
||||||
im.close();
|
|
||||||
bis = null;
|
|
||||||
im = null;
|
|
||||||
return bitmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
void navigate(final File root_sd) {
|
void navigate(final File root_sd) {
|
||||||
LinearLayout v = (LinearLayout) parentActivity
|
LinearLayout v = (LinearLayout) parentActivity
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package com.reicast.emulator;
|
package com.reicast.emulator;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
@ -21,15 +26,36 @@ import org.w3c.dom.NodeList;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.StrictMode;
|
import android.os.StrictMode;
|
||||||
|
import android.os.Vibrator;
|
||||||
|
|
||||||
public class XMLParser {
|
import com.reicast.emulator.FileBrowser.OnItemSelectedListener;
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
public class XMLParser extends AsyncTask<String, Integer, String> {
|
||||||
public XMLParser(Context mContext) {
|
|
||||||
|
private Context mContext;
|
||||||
|
private String url;
|
||||||
|
private File game;
|
||||||
|
private OnItemSelectedListener mCallback;
|
||||||
|
private Vibrator vib;
|
||||||
|
|
||||||
|
public XMLParser(Context mContext, File game, OnItemSelectedListener mCallback, Vibrator vib) {
|
||||||
|
this.mContext = mContext;
|
||||||
|
this.game = game;
|
||||||
|
this.mCallback = mCallback;
|
||||||
|
this.vib = vib;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onPreExecute() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
|
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
|
||||||
.permitAll().build();
|
.permitAll().build();
|
||||||
|
@ -37,9 +63,46 @@ public class XMLParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getXmlFromUrl(String url) {
|
public Bitmap decodeBitmapIcon(String filename) throws IOException {
|
||||||
String xml = null;
|
URL updateURL = new URL(filename);
|
||||||
|
URLConnection conn1 = updateURL.openConnection();
|
||||||
|
InputStream im = conn1.getInputStream();
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(im, 512);
|
||||||
|
|
||||||
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
|
options.inJustDecodeBounds = true;
|
||||||
|
Bitmap bitmap = BitmapFactory.decodeStream(bis, null, options);
|
||||||
|
|
||||||
|
int heightRatio = (int) Math.ceil(options.outHeight / (float) 72);
|
||||||
|
int widthRatio = (int) Math.ceil(options.outWidth / (float) 72);
|
||||||
|
|
||||||
|
if (heightRatio > 1 || widthRatio > 1) {
|
||||||
|
if (heightRatio > widthRatio) {
|
||||||
|
options.inSampleSize = heightRatio;
|
||||||
|
} else {
|
||||||
|
options.inSampleSize = widthRatio;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
options.inJustDecodeBounds = false;
|
||||||
|
bis.close();
|
||||||
|
im.close();
|
||||||
|
conn1 = updateURL.openConnection();
|
||||||
|
im = conn1.getInputStream();
|
||||||
|
bis = new BufferedInputStream(im, 512);
|
||||||
|
bitmap = BitmapFactory.decodeStream(bis, null, options);
|
||||||
|
|
||||||
|
bis.close();
|
||||||
|
im.close();
|
||||||
|
bis = null;
|
||||||
|
im = null;
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... params) {
|
||||||
|
url = params[0];
|
||||||
|
String xml = null;
|
||||||
try {
|
try {
|
||||||
DefaultHttpClient httpClient = new DefaultHttpClient();
|
DefaultHttpClient httpClient = new DefaultHttpClient();
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
@ -49,16 +112,65 @@ public class XMLParser {
|
||||||
xml = EntityUtils.toString(httpEntity);
|
xml = EntityUtils.toString(httpEntity);
|
||||||
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
|
||||||
} catch (ClientProtocolException e) {
|
} catch (ClientProtocolException e) {
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
// return XML
|
// return XML
|
||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String gameData) {
|
||||||
|
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
|
||||||
|
builder.setCancelable(true);
|
||||||
|
if (gameData != null) {
|
||||||
|
Document doc = getDomElement(gameData);
|
||||||
|
if (doc != null && doc.getElementsByTagName("Game") != null) {
|
||||||
|
Element root = (Element) doc.getElementsByTagName("Game").item(
|
||||||
|
0);
|
||||||
|
String title = getValue(root, "GameTitle");
|
||||||
|
builder.setTitle(mContext.getString(R.string.game_details,
|
||||||
|
title));
|
||||||
|
String details = getValue(root, "Overview");
|
||||||
|
builder.setMessage(details);
|
||||||
|
Element boxart = (Element) root.getElementsByTagName("Images")
|
||||||
|
.item(0);
|
||||||
|
String image = "http://thegamesdb.net/banners/"
|
||||||
|
+ getValue(boxart, "boxart");
|
||||||
|
try {
|
||||||
|
builder.setIcon(new BitmapDrawable(decodeBitmapIcon(image)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
builder.setTitle(mContext.getString(R.string.info_unavailable));
|
||||||
|
}
|
||||||
|
builder.setPositiveButton("Close",
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
builder.setPositiveButton("Launch",
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
mCallback.onGameSelected(game != null ? Uri
|
||||||
|
.fromFile(game) : Uri.EMPTY);
|
||||||
|
vib.vibrate(250);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
builder.create().show();
|
||||||
|
}
|
||||||
|
|
||||||
public Document getDomElement(String xml) {
|
public Document getDomElement(String xml) {
|
||||||
Document doc = null;
|
Document doc = null;
|
||||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
|
@ -71,13 +183,13 @@ public class XMLParser {
|
||||||
doc = db.parse(is);
|
doc = db.parse(is);
|
||||||
|
|
||||||
} catch (ParserConfigurationException e) {
|
} catch (ParserConfigurationException e) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue