Merge pull request #704 from AbandonedCart/loungekatt

XMLParser and GLES 3 cleanup
This commit is contained in:
TwistedUmbrella 2015-07-28 20:20:53 -04:00
commit a3f66be3ce
4 changed files with 161 additions and 118 deletions

View File

@ -28,7 +28,6 @@ import android.graphics.Typeface;
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.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Vibrator; import android.os.Vibrator;
@ -38,7 +37,6 @@ import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener; import android.view.View.OnTouchListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
@ -370,8 +368,8 @@ public class FileBrowser extends Fragment {
final View childview = parentActivity.getLayoutInflater().inflate( final View childview = parentActivity.getLayoutInflater().inflate(
R.layout.app_list_item, null, false); R.layout.app_list_item, null, false);
final XMLParser xmlParser = new XMLParser(game, index, mPrefs); XMLParser xmlParser = new XMLParser(game, index, mPrefs);
xmlParser.setViewParent(parentActivity, childview); xmlParser.setViewParent(parentActivity, childview, mCallback);
orig_bg = childview.getBackground(); orig_bg = childview.getBackground();
childview.findViewById(R.id.childview).setOnClickListener( childview.findViewById(R.id.childview).setOnClickListener(
@ -398,40 +396,6 @@ public class FileBrowser extends Fragment {
} }
} }
}); });
if (mPrefs.getBoolean(Config.pref_gamedetails, false) && isGame) {
childview.findViewById(R.id.childview).setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
vib.vibrate(50);
final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
builder.setCancelable(true);
builder.setTitle(getString(R.string.game_details,
xmlParser.getGameTitle()));
builder.setMessage(xmlParser.getGameDetails());
builder.setIcon(xmlParser.getGameIcon());
builder.setNegativeButton("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();
return true;
}
});
}
childview.findViewById(R.id.childview).setOnTouchListener( childview.findViewById(R.id.childview).setOnTouchListener(
new OnTouchListener() { new OnTouchListener() {

View File

@ -2,12 +2,15 @@ package com.reicast.emulator;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader; import java.io.StringReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Locale; import java.util.Locale;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -27,9 +30,9 @@ 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 com.reicast.emulator.config.Config; 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.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -37,27 +40,35 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.SparseArray; import android.os.Vibrator;
import android.view.View; import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.reicast.emulator.FileBrowser.OnItemSelectedListener;
import com.reicast.emulator.config.Config;
public class XMLParser extends AsyncTask<String, Integer, String> { public class XMLParser extends AsyncTask<String, Integer, String> {
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
private File game; private File game;
private int index; private int index;
private View childview; private View childview;
private OnItemSelectedListener mCallback;
private Context mContext; private Context mContext;
private String game_name; private String game_name;
private Bitmap coverart;
private Drawable game_icon; private Drawable game_icon;
private String gameId;
private String game_details;
private static final String game_index = "http://thegamesdb.net/api/GetGame.php?platform=sega+dreamcast&name="; private static final String game_index = "http://thegamesdb.net/api/GetGamesList.php?platform=sega+dreamcast&name=";
private SparseArray<String> game_details = new SparseArray<String>(); private static final String game_id = "http://thegamesdb.net/api/GetGame.php?platform=sega+dreamcast&id=";
private SparseArray<Bitmap> game_preview = new SparseArray<Bitmap>();
public XMLParser(File game, int index, SharedPreferences mPrefs) { public XMLParser(File game, int index, SharedPreferences mPrefs) {
this.mPrefs = mPrefs; this.mPrefs = mPrefs;
@ -65,9 +76,15 @@ public class XMLParser extends AsyncTask<String, Integer, String> {
this.index = index; this.index = index;
} }
public void setViewParent(Context mContext, View childview) { public void setViewParent(Context mContext, View childview, OnItemSelectedListener mCallback) {
this.mContext = mContext; this.mContext = mContext;
this.childview = childview; this.childview = childview;
this.mCallback = mCallback;
}
public void setGameID(String id) {
this.gameId = id;
initializeDefaults();
} }
protected void onPreExecute() { protected void onPreExecute() {
@ -79,63 +96,72 @@ public class XMLParser extends AsyncTask<String, Integer, String> {
} }
public Bitmap decodeBitmapIcon(String filename) throws IOException { public Bitmap decodeBitmapIcon(String filename) throws IOException {
URL updateURL = new URL(filename); String index = filename.substring(filename.lastIndexOf("/") + 1, filename.lastIndexOf("."));
URLConnection conn1 = updateURL.openConnection(); File file = new File(mContext.getExternalFilesDir(null) + "/images", index + ".png");
InputStream im = conn1.getInputStream(); if (file.exists()) {
BufferedInputStream bis = new BufferedInputStream(im, 512); return BitmapFactory.decodeFile(file.getAbsolutePath());
} else {
BitmapFactory.Options options = new BitmapFactory.Options(); URL updateURL = new URL(filename);
options.inJustDecodeBounds = true; URLConnection conn1 = updateURL.openConnection();
Bitmap bitmap = BitmapFactory.decodeStream(bis, null, options); InputStream im = conn1.getInputStream();
BufferedInputStream bis = new BufferedInputStream(im, 512);
int heightRatio = (int) Math.ceil(options.outHeight / (float) 72); BitmapFactory.Options options = new BitmapFactory.Options();
int widthRatio = (int) Math.ceil(options.outWidth / (float) 72); options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(bis, null, options);
if (heightRatio > 1 || widthRatio > 1) { int heightRatio = (int) Math.ceil(options.outHeight / (float) 72);
if (heightRatio > widthRatio) { int widthRatio = (int) Math.ceil(options.outWidth / (float) 72);
options.inSampleSize = heightRatio; if (heightRatio > 1 || widthRatio > 1) {
} else { if (heightRatio > widthRatio) {
options.inSampleSize = 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;
OutputStream fOut = null;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
try {
fOut = new FileOutputStream(file, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception ex) {
}
return bitmap;
} }
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 @Override
protected String doInBackground(String... params) { protected String doInBackground(String... params) {
String filename = game_name = params[0]; String filename = game_name = params[0];
if (isNetworkAvailable() && mPrefs.getBoolean(Config.pref_gamedetails, false)) { if (isNetworkAvailable() && mPrefs.getBoolean(Config.pref_gamedetails, false)) {
if (filename.startsWith("[")) { DefaultHttpClient httpClient = new DefaultHttpClient();
filename = filename.substring(filename.indexOf("]") + 1, filename.length()); HttpPost httpPost;
} if (gameId != null) {
if (filename.contains("[")) { httpPost = new HttpPost(game_id + gameId);
filename = filename.substring(0, filename.indexOf("["));
} else { } else {
filename = filename.substring(0, filename.lastIndexOf(".")); filename = filename.substring(0, filename.lastIndexOf("."));
} try {
filename = filename.replace("_", " ").replace(":", " "); filename = URLEncoder.encode(filename, "UTF-8");
filename = filename.replaceAll("[^\\p{Alpha}\\p{Digit}]+"," "); } catch (UnsupportedEncodingException e) {
filename = filename.replace(" ", " ").replace(" ", "+"); filename = filename.replace(" ", "+");
if (filename.endsWith("+")) { }
filename = filename.substring(0, filename.length() - 1); httpPost = new HttpPost(game_index + filename);
} }
try { try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(game_index + filename);
HttpResponse httpResponse = httpClient.execute(httpPost); HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity(); HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity); return EntityUtils.toString(httpEntity);
@ -157,37 +183,76 @@ public class XMLParser extends AsyncTask<String, Integer, String> {
Document doc = getDomElement(gameData); Document doc = getDomElement(gameData);
if (doc.getElementsByTagName("Game") != null) { if (doc.getElementsByTagName("Game") != null) {
Element root = (Element) doc.getElementsByTagName("Game").item(0); Element root = (Element) doc.getElementsByTagName("Game").item(0);
game_name = getValue(root, "GameTitle"); if (gameId == null) {
String details = getValue(root, "Overview"); XMLParser xmlParser = new XMLParser(game, index, mPrefs);
game_details.put(index, details); xmlParser.setViewParent(mContext, childview, mCallback);
Element images = (Element) root.getElementsByTagName("Images").item(0); xmlParser.setGameID(getValue(root, "id"));
Element boxart = (Element) images.getElementsByTagName("boxart").item(1); xmlParser.execute(game_name);
String image = "http://thegamesdb.net/banners/" + getElementValue(boxart); } else {
game_preview.put(index, decodeBitmapIcon(image)); game_name = getValue(root, "GameTitle");
game_icon = new BitmapDrawable(decodeBitmapIcon(image)); game_details = getValue(root, "Overview");
} else { Element images = (Element) root.getElementsByTagName("Images").item(0);
initializeDefaults(); Element boxart = null;
if (images.getElementsByTagName("boxart").getLength() > 1) {
boxart = (Element) images.getElementsByTagName("boxart").item(1);
} else if (images.getElementsByTagName("boxart").getLength() == 1) {
boxart = (Element) images.getElementsByTagName("boxart").item(0);
}
if (boxart != null) {
coverart = decodeBitmapIcon("http://thegamesdb.net/banners/" + getElementValue(boxart));
game_icon = new BitmapDrawable(coverart);
}
}
} }
} catch (Exception e) { } catch (Exception e) {
initializeDefaults();
} }
} else {
initializeDefaults();
} }
((TextView) childview.findViewById(R.id.item_name)).setText(game_name); ((TextView) childview.findViewById(R.id.item_name)).setText(game_name);
if (Build.VERSION.SDK_INT < 21) { if (Build.VERSION.SDK_INT < 21) {
((ImageView) childview.findViewById(R.id.item_icon)) ((ImageView) childview.findViewById(R.id.item_icon)).setImageDrawable(game_icon);
.setImageDrawable(game_icon); } else {
((ImageView) childview.findViewById(R.id.item_icon)).setImageBitmap(coverart);
}
if (mPrefs.getBoolean(Config.pref_gamedetails, false)) {
childview.findViewById(R.id.childview).setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setCancelable(true);
builder.setTitle(mContext.getString(R.string.game_details, game_name));
builder.setMessage(game_details);
builder.setIcon(game_icon);
builder.setNegativeButton("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);
((Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(250);
return;
}
});
builder.create().show();
return true;
}
});
} }
childview.setTag(game_name); childview.setTag(game_name);
} }
private void initializeDefaults() { private void initializeDefaults() {
game_details.put(index, game_details = mContext.getString(R.string.info_unavailable);
mContext.getString(R.string.info_unavailable));
final String nameLower = game.getName().toLowerCase( final String nameLower = game.getName().toLowerCase(
Locale.getDefault()); Locale.getDefault());
if (Build.VERSION.SDK_INT < 21) { if (Build.VERSION.SDK_INT < 21) {
@ -216,17 +281,17 @@ public class XMLParser extends AsyncTask<String, Integer, String> {
public Drawable getGameIcon() { public Drawable getGameIcon() {
return game_icon; return game_icon;
} }
public Bitmap getGameCover() {
return coverart;
}
public String getGameTitle() { public String getGameTitle() {
return game_name; return game_name;
} }
public String getGameDetails() { public String getGameDetails() {
return game_details.get(index); return game_details;
}
public Bitmap getGamePreview() {
return game_preview.get(index);
} }
public Document getDomElement(String xml) { public Document getDomElement(String xml) {

View File

@ -170,6 +170,14 @@ public class OptionsFragment extends Fragment {
public void onCheckedChanged(CompoundButton buttonView, public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) { boolean isChecked) {
mPrefs.edit().putBoolean(Config.pref_gamedetails, isChecked).commit(); mPrefs.edit().putBoolean(Config.pref_gamedetails, isChecked).commit();
if (!isChecked) {
File dir = new File(getActivity().getExternalFilesDir(null), "images");
for (File file : dir.listFiles()) {
if (!file.isDirectory()) {
file.delete();
}
}
}
} }
}; };
Switch details_opt = (Switch) getView().findViewById( Switch details_opt = (Switch) getView().findViewById(

View File

@ -26,14 +26,20 @@ public class GLCFactory6 {
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
public EGLContext createContext(EGL10 egl,EGLDisplay display,EGLConfig eglConfig) public EGLContext createContext(EGL10 egl,EGLDisplay display,EGLConfig eglConfig)
{ {
int[] attrList = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE }; EGLContext context = EGL10.EGL_NO_CONTEXT;
for ( int clientVersion = 3; clientVersion >= 2; clientVersion-- ) {
int[] attrList = { EGL_CONTEXT_CLIENT_VERSION, clientVersion, EGL14.EGL_NONE };
LOGI("Creating OpenGL ES X context"); LOGI("Creating OpenGL ES " + clientVersion + " context");
checkEglError("Before eglCreateContext",egl); checkEglError("Before eglCreateContext",egl);
EGLContext context = egl.eglCreateContext(display,eglConfig,EGL10.EGL_NO_CONTEXT,attrList); context = egl.eglCreateContext(display,eglConfig,EGL10.EGL_NO_CONTEXT,attrList);
checkEglError("After eglCreateContext",egl); checkEglError("After eglCreateContext",egl);
if (context != EGL10.EGL_NO_CONTEXT) {
break;
}
}
return(context); return(context);
} }