Add a proper info API, but retain the requirements for now

This commit is contained in:
TwistedUmbrella 2014-04-14 20:05:57 -04:00 committed by ptitSeb
parent 4743d8d31f
commit 554c86522e
4 changed files with 178 additions and 71 deletions

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<WebView
android:id="@+id/webframe"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</LinearLayout>

View File

@ -42,7 +42,8 @@
<string name="games_listing">Game List</string> <string name="games_listing">Game List</string>
<string name="game_details">Game Information</string> <string name="game_details">Game Info - %1$s</string>
<string name="info_unavailable">Game Info Unavailable</string>
<string name="report_issue">Previous Crash Detected</string> <string name="report_issue">Previous Crash Detected</string>
<string name="bios_config">Configuration failed!</string> <string name="bios_config">Configuration failed!</string>

View File

@ -1,11 +1,14 @@
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;
@ -15,22 +18,24 @@ 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.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; 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;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -40,12 +45,6 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnTouchListener; import android.view.View.OnTouchListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ScrollView; import android.widget.ScrollView;
@ -65,6 +64,8 @@ public class FileBrowser extends Fragment {
private boolean games; private boolean games;
private OnItemSelectedListener mCallback; private OnItemSelectedListener mCallback;
private static final String game_index = "http://thegamesdb.net/api/GetGame.php?platform=sega+dreamcast&name=";
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
private File sdcard = Environment.getExternalStorageDirectory(); private File sdcard = Environment.getExternalStorageDirectory();
private String home_directory = sdcard + "/dc"; private String home_directory = sdcard + "/dc";
@ -326,9 +327,7 @@ public class FileBrowser extends Fragment {
} else { } else {
title = name.substring(0, name.lastIndexOf(".")); title = name.substring(0, name.lastIndexOf("."));
} }
String url = "http://www.retrocollect.com/videogamedatabase/results.php?gamename=" displayDetails(game_index + title.replace(" ", "+"), game);
+ title.replace(" ", "+") + "&mode=releases&platform=21";
displayDetails(title, url, game);
} }
}); });
@ -349,16 +348,34 @@ public class FileBrowser extends Fragment {
list.addView(childview); list.addView(childview);
} }
private void displayDetails(String message, String url, final File game) { private void displayDetails(String url, final File game) {
final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity); final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
builder.setCancelable(true); builder.setCancelable(true);
builder.setTitle(R.string.game_details); XMLParser xmlParser = new XMLParser(parentActivity);
builder.setMessage(message); String gameData = xmlParser.getXmlFromUrl(url);
LayoutInflater infalter = LayoutInflater.from(parentActivity); if (gameData != null) {
final View popWebView = infalter.inflate(R.layout.webview, null); Document doc = xmlParser.getDomElement(gameData);
WebView mWebView = (WebView) popWebView.findViewById(R.id.webframe); if (doc != null && doc.getElementsByTagName("Game") != null) {
mWebView = configureWebview(url, parentActivity, mWebView); Element root = (Element) doc.getElementsByTagName("Game").item(
builder.setView(popWebView); 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", builder.setPositiveButton("Close",
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@ -379,42 +396,40 @@ public class FileBrowser extends Fragment {
builder.create().show(); builder.create().show();
} }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public Bitmap decodeBitmapIcon(String filename) throws IOException {
@SuppressLint("SetJavaScriptEnabled") URL updateURL = new URL(filename);
@SuppressWarnings("deprecation") URLConnection conn1 = updateURL.openConnection();
public static WebView configureWebview(String url, Context context, InputStream im = conn1.getInputStream();
WebView mWebView) { BufferedInputStream bis = new BufferedInputStream(im, 512);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true); BitmapFactory.Options options = new BitmapFactory.Options();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { options.inJustDecodeBounds = true;
mWebView.getSettings().setDisplayZoomControls(false); 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;
} }
mWebView.setInitialScale(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
mWebView.getSettings().setUseWideViewPort(true);
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
mWebView.getSettings().setLoadWithOverviewMode(true); options.inJustDecodeBounds = false;
} bis.close();
mWebView.getSettings().setJavaScriptEnabled(true); im.close();
mWebView.getSettings().setPluginState(PluginState.ON); conn1 = updateURL.openConnection();
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); im = conn1.getInputStream();
mWebView.clearHistory(); bis = new BufferedInputStream(im, 512);
mWebView.clearFormData(); bitmap = BitmapFactory.decodeStream(bis, null, options);
mWebView.clearCache(true);
CookieSyncManager.createInstance(context); bis.close();
CookieManager cookieManager = CookieManager.getInstance(); im.close();
cookieManager.removeAllCookie(); bis = null;
CookieSyncManager.getInstance().stopSync(); im = null;
mWebView.setWebViewClient(new WebViewClient() { return bitmap;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
return mWebView;
} }
void navigate(final File root_sd) { void navigate(final File root_sd) {

View File

@ -0,0 +1,106 @@
package com.reicast.emulator;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
public class XMLParser {
@SuppressLint("NewApi")
public XMLParser(Context mContext) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
// return XML
return xml;
}
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
return null;
} catch (SAXException e) {
return null;
} catch (IOException e) {
return null;
}
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}