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
parent 9de771f30e
commit 4c965b883c
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="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="bios_config">Configuration failed!</string>

View File

@ -1,11 +1,14 @@
package com.reicast.emulator;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -15,22 +18,24 @@ import java.util.List;
import java.util.Locale;
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.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
@ -40,12 +45,6 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
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.LinearLayout;
import android.widget.ScrollView;
@ -64,6 +63,8 @@ public class FileBrowser extends Fragment {
private boolean ImgBrowse;
private boolean games;
private OnItemSelectedListener mCallback;
private static final String game_index = "http://thegamesdb.net/api/GetGame.php?platform=sega+dreamcast&name=";
private SharedPreferences mPrefs;
private File sdcard = Environment.getExternalStorageDirectory();
@ -326,9 +327,7 @@ public class FileBrowser extends Fragment {
} else {
title = name.substring(0, name.lastIndexOf("."));
}
String url = "http://www.retrocollect.com/videogamedatabase/results.php?gamename="
+ title.replace(" ", "+") + "&mode=releases&platform=21";
displayDetails(title, url, game);
displayDetails(game_index + title.replace(" ", "+"), game);
}
});
@ -349,16 +348,34 @@ public class FileBrowser extends Fragment {
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);
builder.setCancelable(true);
builder.setTitle(R.string.game_details);
builder.setMessage(message);
LayoutInflater infalter = LayoutInflater.from(parentActivity);
final View popWebView = infalter.inflate(R.layout.webview, null);
WebView mWebView = (WebView) popWebView.findViewById(R.id.webframe);
mWebView = configureWebview(url, parentActivity, mWebView);
builder.setView(popWebView);
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) {
@ -379,42 +396,40 @@ public class FileBrowser extends Fragment {
builder.create().show();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("SetJavaScriptEnabled")
@SuppressWarnings("deprecation")
public static WebView configureWebview(String url, Context context,
WebView mWebView) {
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mWebView.getSettings().setDisplayZoomControls(false);
}
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);
}
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.clearHistory();
mWebView.clearFormData();
mWebView.clearCache(true);
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
CookieSyncManager.getInstance().stopSync();
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
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;
}
});
mWebView.loadUrl(url);
return mWebView;
}
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) {

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 "";
}
}