This source file includes following definitions.
- listing
- loadAllLibraries
- getHandle
- checkCurrentThread
- disconnect
- connect
- eventKey
- render
- resize
- motionEvent
- finalize
- gpacconnect
- createInstance
- gpacdisconnect
- gpacrender
- gpacresize
- gpacfree
- gpaceventkeypress
- gpaceventmousedown
- gpaceventmouseup
- gpaceventmousemove
- gpaceventorientationchange
- onOrientationChange
- destroy
- setGpacPreference
- setGpacLogs
package com.gpac.Osmo4;
import java.io.File;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class GPACInstance implements GPACInstanceInterface {
private final static String LOG_LIB = "LibrariesLoader";
private final Thread uniqueThread;
private static void listing(StringBuilder sb, File root, int inc) {
StringBuilder increment = new StringBuilder();
for (int i = 0; i < inc; i++)
increment.append(' ');
String incr = increment.toString();
for (File f : root.listFiles()) {
sb.append(incr).append(f.getName());
if (f.isDirectory()) {
sb.append(" [Directory]\n");
listing(sb, f, inc + 2);
} else {
sb.append(" [").append(f.length() + " bytes]\n");
}
}
}
synchronized static Map<String, Throwable> loadAllLibraries(GpacConfig config) {
if (errors != null)
return errors;
StringBuilder sb = new StringBuilder();
final String[] toLoad = { "GLESv2", "log",
"jpegdroid", "javaenv",
"mad", "editline", "ft2",
"js_osmo", "openjpeg", "png", "z",
"stlport_shared", "stdc++", "faad", "gpac",
"gm_droid_cam", "gm_droid_mpegv","gm_mediacodec_dec",
"avutil", "swscale", "swresample", "avcodec", "avformat", "avfilter",
"gpacWrapper" };
HashMap<String, Throwable> exceptions = new HashMap<String, Throwable>();
for (String s : toLoad) {
try {
String msg = "Loading library " + s + "...";
sb.append(msg);
Log.i(LOG_LIB, msg);
System.loadLibrary(s);
} catch (UnsatisfiedLinkError e) {
sb.append("Failed to load " + s + ", error=" + e.getLocalizedMessage() + " :: "
+ e.getClass().getSimpleName() + "\n");
exceptions.put(s, e);
Log.e(LOG_LIB, "Failed to load library : " + s + " due to link error " + e.getLocalizedMessage(), e);
} catch (SecurityException e) {
exceptions.put(s, e);
Log.e(LOG_LIB, "Failed to load library : " + s + " due to security error " + e.getLocalizedMessage(), e);
} catch (Throwable e) {
exceptions.put(s, e);
Log.e(LOG_LIB, "Failed to load library : " + s + " due to Runtime error " + e.getLocalizedMessage(), e);
}
}
if (!exceptions.isEmpty()) {
try {
PrintStream out = new PrintStream(config.getGpacLogDirectory() + "debug_libs.txt", "UTF-8");
out.println("$Revision$");
out.println(new Date());
out.println("\n*** Configuration\n");
out.println(config.getConfigAsText());
sb.append("*** Libs listing: ");
sb.append(config.getGpacLibsDirectory());
sb.append('\n');
listing(sb, new File(config.getGpacLibsDirectory()), 2);
sb.append("*** Modules listing: ");
sb.append(config.getGpacModulesDirectory());
sb.append('\n');
listing(sb, new File(config.getGpacModulesDirectory()), 2);
sb.append("*** Fonts listing: \n");
sb.append(config.getGpacFontDirectory());
sb.append('\n');
listing(sb, new File(config.getGpacFontDirectory()), 2);
sb.append("*** Exceptions:\n");
for (Map.Entry<String, Throwable> ex : exceptions.entrySet()) {
sb.append(ex.getKey()).append(": ")
.append(ex.getValue().getLocalizedMessage())
.append('(')
.append(ex.getValue().getClass())
.append(")\n");
}
out.println(sb.toString());
out.flush();
out.close();
} catch (Exception e) {
Log.e(LOG_LIB, "Failed to output debug info to debug file", e);
}
}
errors = Collections.unmodifiableMap(exceptions);
return errors;
}
private static Map<String, Throwable> errors = null;
private boolean hasToBeFreed = true;
public GPACInstance(GpacCallback callback, int width, int height, GpacConfig config, String urlToOpen)
throws GpacInstanceException {
StringBuilder sb = new StringBuilder();
Map<String, Throwable> errors = loadAllLibraries(config);
if (!errors.isEmpty()) {
sb.append("Exceptions while loading libraries:");
for (Map.Entry<String, Throwable> x : errors.entrySet()) {
sb.append('\n')
.append(x.getKey())
.append('[')
.append(x.getValue().getClass().getSimpleName())
.append("]: ")
.append(x.getValue().getLocalizedMessage());
}
Log.e(LOG_LIB, sb.toString());
}
try {
handle = createInstance(callback,
width,
height,
config.getGpacConfigDirectory(),
config.getGpacModulesDirectory(),
config.getGpacCacheDirectory(),
config.getGpacFontDirectory(),
config.getGpacGuiDirectory(),
urlToOpen);
} catch (Throwable e) {
throw new GpacInstanceException("Error while creating instance\n" + sb.toString());
}
if (handle == 0) {
throw new GpacInstanceException("Error while creating instance, no handle created!\n" + sb.toString());
}
synchronized (this) {
hasToBeFreed = true;
}
uniqueThread = Thread.currentThread();
}
private final long handle;
public synchronized long getHandle() {
return handle;
}
private void checkCurrentThread() throws RuntimeException {
if (Thread.currentThread() != uniqueThread)
throw new RuntimeException("Method called outside allowed Thread scope !");
}
@Override
public void disconnect() {
checkCurrentThread();
gpacrender();
gpacdisconnect();
}
@Override
public void connect(String url) {
Log.i(LOG_LIB, "connect(" + url + ")");
checkCurrentThread();
gpacrender();
gpacconnect(url);
}
public void eventKey(int keyCode, KeyEvent event, boolean pressed, int unicode) {
checkCurrentThread();
gpaceventkeypress(keyCode, event.getScanCode(), pressed ? 1 : 0, event.getFlags(), unicode);
}
public void render() {
checkCurrentThread();
gpacrender();
}
public void resize(int width, int height) {
Log.i(LOG_LIB, "Resizing to " + width + "x" + height);
gpacresize(width, height);
}
private boolean touched = false;
public void motionEvent(MotionEvent event) {
checkCurrentThread();
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
gpaceventmousemove(x, y);
gpaceventmousedown(x, y);
touched = true;
break;
case MotionEvent.ACTION_UP:
if ( !touched )
break;
gpaceventmouseup(x, y);
touched = false;
break;
case MotionEvent.ACTION_MOVE:
gpaceventmousemove(x, y);
if ( !touched ) {
touched = true;
gpaceventmousedown(x, y);
}
break;
case MotionEvent.ACTION_CANCEL:
if ( !touched )
break;
gpaceventmouseup(x, y);
touched = false;
break;
}
}
@Override
public void finalize() throws Throwable {
super.finalize();
}
private native void gpacconnect(String url);
private native long createInstance(GpacCallback callback, int width, int height, String cfg_dir,
String modules_dir, String cache_dir, String font_dir, String gui_dir, String url_to_open);
private native void gpacdisconnect();
private native void gpacrender();
private native void gpacresize(int width, int height);
private native void gpacfree();
private native void gpaceventkeypress(int keycode, int rawkeycode, int up, int flag, int unicode);
private native void gpaceventmousedown(float x, float y);
private native void gpaceventmouseup(float x, float y);
private native void gpaceventmousemove(float x, float y);
private native void gpaceventorientationchange(float x, float y, float z);
public void onOrientationChange(float yaw, float pitch, float roll){
gpaceventorientationchange(yaw, pitch, roll);
}
@Override
public void destroy() {
boolean freeIt;
synchronized (this) {
freeIt = hasToBeFreed;
hasToBeFreed = false;
}
if (freeIt) {
disconnect();
gpacfree();
}
}
@Override
public native void setGpacPreference(String category, String name, String value);
@Override
public native void setGpacLogs(String tools_at_levels);
}