@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { /*boolean res = super.onFling(e1, e2, velocityX, velocityY); Log.i("OF","onFLing" + res); return res;*/ final float xDistance = Math.abs(e1.getX() - e2.getX()); final float yDistance = Math.abs(e1.getY() - e2.getY()); if (xDistance > OFGestureListener.swipe_Max_Distance || yDistance > OFGestureListener.swipe_Max_Distance) return false; velocityX = Math.abs(velocityX); velocityY = Math.abs(velocityY); boolean result = false; if (velocityX > OFGestureListener.swipe_Min_Velocity && xDistance > OFGestureListener.swipe_Min_Distance) { if (e1.getX() > e2.getX()) // right to left OFAndroid.onSwipe(e1.getPointerId(0), SWIPE_LEFT); else OFAndroid.onSwipe(e1.getPointerId(0), SWIPE_RIGHT); result = true; } else if (velocityY > OFGestureListener.swipe_Min_Velocity && yDistance > OFGestureListener.swipe_Min_Distance) { if (e1.getY() > e2.getY()) // bottom to up OFAndroid.onSwipe(e1.getPointerId(0), SWIPE_UP); else OFAndroid.onSwipe(e1.getPointerId(0), SWIPE_DOWN); result = true; } return result; }
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) { if (OFAndroid.onBackPressed()) return true; else return super.onKeyUp(keyCode, event); } OFAndroid.onKeyUp(keyCode); return super.onKeyUp(keyCode, event); }
public static void disableTouchEvents() { OFGLSurfaceView glView = OFAndroid.getGLView(); if (glView != null) { glView.setOnClickListener(null); glView.setOnTouchListener(null); } }
public static void enableTouchEvents() { OFGLSurfaceView glView = OFAndroid.getGLView(); if (glView != null) { if (gestureListener == null) gestureListener = new OFGestureListener(ofActivity); glView.setOnClickListener(gestureListener); glView.setOnTouchListener(gestureListener.touchListener); } }
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (OFAndroid.keyUp(keyCode, event)) { return true; } else { return super.onKeyUp(keyCode, event); } }
public void onSurfaceChanged(GL10 gl, int w, int h) { if (!setup) { Log.i("OF", "initializing app"); OFAndroid.init(); OFAndroid.setup(w, h); initialized = true; setup = true; android.os.Process.setThreadPriority(8); OFGestureListener.swipe_Min_Distance = (int) (Math.max(w, h) * .1); OFGestureListener.swipe_Max_Distance = (int) (Math.max(w, h) * .6); /*if(ETC1Util.isETC1Supported()) Log.i("OF","ETC supported"); else Log.i("OF","ETC not supported");*/ } OFAndroid.resize(w, h); this.w = w; this.h = h; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // This passes the menu option string to OF // you can add additional behavior from java modifying this method // but keep the call to OFAndroid so OF is notified of menu events if (OFAndroid.menuItemSelected(item.getItemId())) { return true; } return super.onOptionsItemSelected(item); }
public void resume() { Log.i("OF", "onResume"); enableTouchEvents(); mGLView.onResume(); for (OFAndroidObject object : OFAndroidObject.ofObjects) { object.onResume(); } onResume(); if (OFAndroid.orientation != -1) OFAndroid.setScreenOrientation(OFAndroid.orientation); if (wl != null) lockScreenSleep(); }
@Override public boolean onDoubleTap(MotionEvent event) { final int action = event.getAction(); final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; final int pointerId = event.getPointerId(pointerIndex); OFAndroid.onTouchDoubleTap( pointerId, event.getX(pointerIndex), event.getY(pointerIndex), event.getPressure(pointerIndex)); return true; // return super.onDoubleTap(e); }
public void resume() { if (mGLView == null || resumed) return; resumed = true; Log.i("OF", "onResume"); enableTouchEvents(); mGLView.onResume(); synchronized (OFAndroidObject.ofObjects) { for (OFAndroidObject object : OFAndroidObject.ofObjects) { object.onResume(); } } if (mGLView.isSetup()) { Log.i("OF", "resume view and native"); onResume(); } if (OFAndroid.orientation != -1) OFAndroid.setScreenOrientation(OFAndroid.orientation); if (networkStateReceiver != null) { monitorNetworkState(); } }
public OFAndroid(String packageName, Activity ofActivity) { ofActivity.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Log.i("OF","external files dir: "+ // ofActivity.getApplicationContext().getExternalFilesDir(null)); OFAndroid.packageName = packageName; OFAndroidObject.setActivity(ofActivity); try { // try to find if R.raw class exists will throw // an exception if not Class<?> raw = Class.forName(packageName + ".R$raw"); // if it exists copy all the raw resources // to a folder in the sdcard Field[] files = raw.getDeclaredFields(); boolean copydata = false; SharedPreferences preferences = ofActivity.getPreferences(Context.MODE_PRIVATE); long lastInstalled = preferences.getLong("installed", 0); PackageManager pm = ofActivity.getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0); String appFile = appInfo.sourceDir; long installed = new File(appFile).lastModified(); if (installed > lastInstalled) { Editor editor = preferences.edit(); editor.putLong("installed", installed); editor.commit(); copydata = true; } dataPath = ""; try { // dataPath = Environment.getExternalStorageDirectory().getAbsolutePath(); dataPath = getRealExternalStorageDirectory(); dataPath += "/" + packageName; Log.i("OF", "creating app directory: " + dataPath); try { File dir = new File(dataPath); if (!dir.exists() && dir.mkdir() != true) throw new Exception(); } catch (Exception e) { Log.e("OF", "error creating dir " + dataPath, e); } if (copydata) { for (int i = 0; i < files.length; i++) { int fileId; String fileName = ""; InputStream from = null; File toFile = null; FileOutputStream to = null; try { fileId = files[i].getInt(null); String resName = ofActivity.getResources().getText(fileId).toString(); fileName = resName.substring(resName.lastIndexOf("/")); from = ofActivity.getResources().openRawResource(fileId); // toFile = new File(Environment.getExternalStorageDirectory() + "/" + appName + "/" // +fileName); Log.i("OF", "copying file " + fileName + " to " + dataPath); toFile = new File(dataPath + "/" + fileName); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write } catch (Exception e) { Log.e("OF", "error copying file", e); } finally { if (from != null) try { from.close(); } catch (IOException e) { } if (to != null) try { to.close(); } catch (IOException e) { } } } } } catch (Exception e) { Log.e("OF", "couldn't move app resources to data directory " + dataPath); e.printStackTrace(); } String app_name = ""; try { int app_name_id = Class.forName(packageName + ".R$string").getField("app_name").getInt(null); app_name = ofActivity.getResources().getText(app_name_id).toString(); Log.i("OF", "app name: " + app_name); } catch (Exception e) { // TODO Auto-generated catch block Log.e("OF", "error retrieving app name", e); } OFAndroid.setAppDataDir(dataPath, app_name); } catch (ClassNotFoundException e1) { } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } OFAndroid.ofActivity = ofActivity; gestureListener = new OFGestureListener(ofActivity); try { Log.v("OF", "trying to find class: " + packageName + ".R$layout"); Class<?> layout = Class.forName(packageName + ".R$layout"); View view = ofActivity.getLayoutInflater().inflate(layout.getField("main_layout").getInt(null), null); ofActivity.setContentView(view); Class<?> id = Class.forName(packageName + ".R$id"); mGLView = (OFGLSurfaceView) ofActivity.findViewById(id.getField("of_gl_surface").getInt(null)); enableTouchEvents(); } catch (Exception e) { Log.e("OF", "couldn't create view from layout falling back to GL only", e); mGLView = new OFGLSurfaceView(ofActivity); ofActivity.setContentView(mGLView); enableTouchEvents(); } // android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { OFAndroid.onKeyDown(keyCode); return super.onKeyDown(keyCode, event); }
public static void registerNetworkStateReceiver() { if (OFAndroid.networkStateReceiver != null) { OFAndroid.monitorNetworkState(); } }
public void onDrawFrame(GL10 gl) { if (setup) OFAndroid.render(); }
public static boolean isApplicationSetup() { OFGLSurfaceView glView = OFAndroid.getGLView(); return glView != null && glView.isSetup(); }
public void onSurfaceCreated(GL10 gl, EGLConfig config) { if (initialized) { OFAndroid.onSurfaceCreated(); return; } }
@Override public void surfaceDestroyed(SurfaceHolder holder) { super.surfaceDestroyed(holder); OFAndroid.onSurfaceDestroyed(); }