diff --git a/app/src/main/java/com/asha/md360player4android/BitmapPlayerActivity.java b/app/src/main/java/com/asha/md360player4android/BitmapPlayerActivity.java index b7608ab8..0dd34b9d 100644 --- a/app/src/main/java/com/asha/md360player4android/BitmapPlayerActivity.java +++ b/app/src/main/java/com/asha/md360player4android/BitmapPlayerActivity.java @@ -107,7 +107,7 @@ public void onHotspotHit(IMDHotspot hitHotspot, MDRay ray) { }) .pinchEnabled(true) .projectionFactory(new CustomProjectionFactory()) - .build(R.id.gl_view); + .build(findViewById(R.id.gl_view)); } private Uri getDrawableUri(@DrawableRes int resId){ diff --git a/app/src/main/java/com/asha/md360player4android/MD360PlayerActivity.java b/app/src/main/java/com/asha/md360player4android/MD360PlayerActivity.java index 937c6b36..d708f8d0 100644 --- a/app/src/main/java/com/asha/md360player4android/MD360PlayerActivity.java +++ b/app/src/main/java/com/asha/md360player4android/MD360PlayerActivity.java @@ -61,6 +61,7 @@ public abstract class MD360PlayerActivity extends Activity { private static final SparseArray sProjectionMode = new SparseArray<>(); private static final SparseArray sAntiDistortion = new SparseArray<>(); private static final SparseArray sPitchFilter = new SparseArray<>(); + private static final SparseArray sFlingEnabled = new SparseArray<>(); static { sDisplayMode.put(MDVRLibrary.DISPLAY_MODE_NORMAL,"NORMAL"); @@ -91,6 +92,9 @@ public abstract class MD360PlayerActivity extends Activity { sPitchFilter.put(1,"FILTER PITCH"); sPitchFilter.put(0,"FILTER NOP"); + + sFlingEnabled.put(1, "FLING ENABLED"); + sFlingEnabled.put(0, "FLING DISABLED"); } public static void startVideo(Context context, Uri uri){ @@ -425,6 +429,17 @@ public float onFilterPitch(float input) { } }) .init(R.id.spinner_pitch_filter); + + SpinnerHelper.with(this) + .setData(sFlingEnabled) + .setDefault(getVRLibrary().isFlingEnabled() ? 1 : 0) + .setClickHandler(new SpinnerHelper.ClickHandler() { + @Override + public void onSpinnerClicked(int index, int key, String value) { + getVRLibrary().setFlingEnabled(key == 1); + } + }) + .init(R.id.spinner_fling_enable); } diff --git a/app/src/main/java/com/asha/md360player4android/VideoPlayerActivity.java b/app/src/main/java/com/asha/md360player4android/VideoPlayerActivity.java index 58eaba5a..ba3ded61 100644 --- a/app/src/main/java/com/asha/md360player4android/VideoPlayerActivity.java +++ b/app/src/main/java/com/asha/md360player4android/VideoPlayerActivity.java @@ -102,7 +102,7 @@ public MD360Director createDirector(int index) { }) .projectionFactory(new CustomProjectionFactory()) .barrelDistortionConfig(new BarrelDistortionConfig().setDefaultEnabled(false).setScale(0.95f)) - .build(R.id.gl_view); + .build(findViewById(R.id.gl_view)); } @Override diff --git a/app/src/main/res/layout/activity_md_using_surface_view.xml b/app/src/main/res/layout/activity_md_using_surface_view.xml index a055f3c4..ebd369ff 100644 --- a/app/src/main/res/layout/activity_md_using_surface_view.xml +++ b/app/src/main/res/layout/activity_md_using_surface_view.xml @@ -273,6 +273,15 @@ android:layout_width="100dp" android:minWidth="100dp" android:layout_height="wrap_content"/> + + diff --git a/vrlib/src/main/java/com/asha/vrlib/MD360Director.java b/vrlib/src/main/java/com/asha/vrlib/MD360Director.java index ae37c1ec..7ea7bf50 100644 --- a/vrlib/src/main/java/com/asha/vrlib/MD360Director.java +++ b/vrlib/src/main/java/com/asha/vrlib/MD360Director.java @@ -118,19 +118,27 @@ private void updateViewMatrixIfNeed(){ if (camera || world){ Matrix.multiplyMM(mViewMatrix, 0, mCameraMatrix, 0, mWorldRotationMatrix, 0); - mViewQuaternion.fromMatrix(mViewMatrix); - float pitch = mViewQuaternion.getPitch(); - float yaw = mViewQuaternion.getYaw(); - float roll = mViewQuaternion.getRoll(); - - float filterPitch = mDirectorFilter.onFilterPitch(pitch); - float filterYaw = mDirectorFilter.onFilterYaw(yaw); - float filterRoll = mDirectorFilter.onFilterRoll(roll); - - if (pitch != filterPitch || yaw != filterYaw || roll != filterRoll){ - mViewQuaternion.setEulerAngles(filterPitch, filterYaw, filterRoll); - mViewQuaternion.toMatrix(mViewMatrix); - } + filterViewMatrix(); + } + } + + private void filterViewMatrix() { + if (mDirectorFilter == null) { + return; + } + + mViewQuaternion.fromMatrix(mViewMatrix); + float pitch = mViewQuaternion.getPitch(); + float yaw = mViewQuaternion.getYaw(); + float roll = mViewQuaternion.getRoll(); + + float filterPitch = mDirectorFilter.onFilterPitch(pitch); + float filterYaw = mDirectorFilter.onFilterYaw(yaw); + float filterRoll = mDirectorFilter.onFilterRoll(roll); + + if (pitch != filterPitch || yaw != filterYaw || roll != filterRoll){ + mViewQuaternion.setEulerAngles(filterPitch, filterYaw, filterRoll); + mViewQuaternion.toMatrix(mViewMatrix); } } diff --git a/vrlib/src/main/java/com/asha/vrlib/MDTouchHelper.java b/vrlib/src/main/java/com/asha/vrlib/MDTouchHelper.java index 29d80907..82af566b 100644 --- a/vrlib/src/main/java/com/asha/vrlib/MDTouchHelper.java +++ b/vrlib/src/main/java/com/asha/vrlib/MDTouchHelper.java @@ -1,9 +1,12 @@ package com.asha.vrlib; +import android.animation.PropertyValuesHolder; +import android.animation.ValueAnimator; import android.content.Context; import android.view.GestureDetector; import android.view.MotionEvent; +import com.asha.vrlib.model.MDFlingConfig; import com.asha.vrlib.model.MDPinchConfig; import java.util.LinkedList; @@ -29,6 +32,10 @@ public class MDTouchHelper { private float mSensitivity; private float defaultScale; private float mGlobalScale; + private ValueAnimator valueAnimator; + + private boolean mFlingEnabled; + private MDFlingConfig mFlingConfig; private static final int MODE_INIT = 0; private static final int MODE_PINCH = 1; @@ -49,13 +56,55 @@ public boolean onSingleTapConfirmed(MotionEvent e) { public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (mCurrentMode == MODE_PINCH) return false; - if (mAdvanceGestureListener != null) + if (mAdvanceGestureListener != null){ mAdvanceGestureListener.onDrag(distanceX / mGlobalScale, distanceY / mGlobalScale); + } + return true; + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + if (mCurrentMode == MODE_PINCH) return false; + if (!mFlingEnabled) return false; + + animStart(velocityX, velocityY); return true; } }); } + private void animCancel(){ + if (valueAnimator != null){ + valueAnimator.cancel(); + } + } + + private void animStart(float velocityX, float velocityY) { + animCancel(); + + PropertyValuesHolder hvx = PropertyValuesHolder.ofFloat("vx", velocityX, 0); + PropertyValuesHolder hvy = PropertyValuesHolder.ofFloat("vy", velocityY, 0); + valueAnimator = ValueAnimator.ofPropertyValuesHolder(hvx, hvy).setDuration(mFlingConfig.getDuring()); + valueAnimator.setInterpolator(mFlingConfig.getInterpolator()); + valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + private long lastTime = 0; + + @Override + public void onAnimationUpdate(ValueAnimator animation) { + long now = animation.getCurrentPlayTime(); + long dur = (now - lastTime); + float sx = (float) animation.getAnimatedValue("vx") * dur / -1000 * mFlingConfig.getSensitivity(); + float sy = (float) animation.getAnimatedValue("vy") * dur / -1000 * mFlingConfig.getSensitivity(); + lastTime = now; + + if (mAdvanceGestureListener != null){ + mAdvanceGestureListener.onDrag(sx / mGlobalScale, sy / mGlobalScale); + } + } + }); + valueAnimator.start(); + } + public boolean handleTouchEvent(MotionEvent event) { int action = event.getAction() & MotionEvent.ACTION_MASK; if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { @@ -89,6 +138,8 @@ public boolean handleTouchEvent(MotionEvent event) { // mLastMovePoint.set(lineCenter[0], lineCenter[1]); handlePinch(distance); } + } else if (action == MotionEvent.ACTION_DOWN){ + animCancel(); } mGestureDetector.onTouchEvent(event); @@ -132,6 +183,10 @@ public void setAdvanceGestureListener(MDVRLibrary.IAdvanceGestureListener listen this.mAdvanceGestureListener = listener; } + public boolean isPinchEnabled() { + return mPinchEnabled; + } + public void setPinchEnabled(boolean mPinchEnabled) { this.mPinchEnabled = mPinchEnabled; } @@ -146,6 +201,18 @@ public void setPinchConfig(MDPinchConfig pinchConfig) { setScaleInner(this.defaultScale); } + public boolean isFlingEnabled() { + return mFlingEnabled; + } + + public void setFlingEnabled(boolean flingEnabled) { + this.mFlingEnabled = flingEnabled; + } + + public void setFlingConfig(MDFlingConfig flingConfig) { + this.mFlingConfig = flingConfig; + } + private class PinchInfo{ private float x1; private float y1; diff --git a/vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java b/vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java index 62eff228..37f09107 100644 --- a/vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java +++ b/vrlib/src/main/java/com/asha/vrlib/MDVRLibrary.java @@ -1,6 +1,5 @@ package com.asha.vrlib; -import android.app.Activity; import android.content.Context; import android.graphics.RectF; import android.hardware.SensorEventListener; @@ -20,6 +19,7 @@ import com.asha.vrlib.compact.CompactTouchPickAdapter; import com.asha.vrlib.model.BarrelDistortionConfig; import com.asha.vrlib.model.MDDirectorBrief; +import com.asha.vrlib.model.MDFlingConfig; import com.asha.vrlib.model.MDHitEvent; import com.asha.vrlib.model.MDMainPluginBuilder; import com.asha.vrlib.model.MDPinchConfig; @@ -109,12 +109,25 @@ private MDVRLibrary(Builder builder) { initPluginManager(builder); // init glSurfaceViews - initOpenGL(builder.activity, builder.screenWrapper); + initOpenGL(builder.context, builder.screenWrapper); mTexture = builder.texture; - mTouchHelper = new MDTouchHelper(builder.activity); + + mTouchHelper = new MDTouchHelper(builder.context); + + // init touch helper + initTouchHelper(builder); + + // init picker manager + initPickerManager(builder); + + // add plugin + initPlugin(); + } + + private void initTouchHelper(Builder builder) { + mTouchHelper = new MDTouchHelper(builder.context); mTouchHelper.addClickListener(builder.gestureListener); - mTouchHelper.setPinchEnabled(builder.pinchEnabled); final UpdatePinchRunnable updatePinchRunnable = new UpdatePinchRunnable(); mTouchHelper.setAdvanceGestureListener(new IAdvanceGestureListener() { @Override @@ -128,8 +141,12 @@ public void onPinch(final float scale) { mGLHandler.post(updatePinchRunnable); } }); + mTouchHelper.setPinchEnabled(builder.pinchEnabled); mTouchHelper.setPinchConfig(builder.pinchConfig); + mTouchHelper.setFlingEnabled(builder.flingEnabled); + mTouchHelper.setFlingConfig(builder.flingConfig); + mScreenWrapper.getView().setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { @@ -137,11 +154,6 @@ public boolean onTouch(View v, MotionEvent event) { } }); - // init picker manager - initPickerManager(builder); - - // add plugin - initPlugin(); } private class UpdatePinchRunnable implements Runnable{ @@ -180,13 +192,13 @@ private void initModeManager(Builder builder) { .setTexture(builder.texture); mProjectionModeManager = new ProjectionModeManager(builder.projectionMode, mGLHandler, projectionManagerParams); - mProjectionModeManager.prepare(builder.activity, builder.notSupportCallback); + mProjectionModeManager.prepare(builder.context, builder.notSupportCallback); // init DisplayModeManager mDisplayModeManager = new DisplayModeManager(builder.displayMode, mGLHandler); mDisplayModeManager.setBarrelDistortionConfig(builder.barrelDistortionConfig); mDisplayModeManager.setAntiDistortionEnabled(builder.barrelDistortionConfig.isDefaultEnabled()); - mDisplayModeManager.prepare(builder.activity, builder.notSupportCallback); + mDisplayModeManager.prepare(builder.context, builder.notSupportCallback); // init InteractiveModeManager InteractiveModeManager.Params interactiveManagerParams = new InteractiveModeManager.Params(); @@ -194,7 +206,7 @@ private void initModeManager(Builder builder) { interactiveManagerParams.mMotionDelay = builder.motionDelay; interactiveManagerParams.mSensorListener = builder.sensorListener; mInteractiveModeManager = new InteractiveModeManager(builder.interactiveMode, mGLHandler, interactiveManagerParams); - mInteractiveModeManager.prepare(builder.activity, builder.notSupportCallback); + mInteractiveModeManager.prepare(builder.context, builder.notSupportCallback); } private void initPluginManager(Builder builder) { @@ -249,26 +261,26 @@ public MDDirectorBrief getDirectorBrief(){ return mProjectionModeManager.getDirectorBrief(); } - public void switchInteractiveMode(final Activity activity) { - mInteractiveModeManager.switchMode(activity); + public void switchInteractiveMode(final Context context) { + mInteractiveModeManager.switchMode(context); } /** * Switch Interactive Mode * - * @param activity activity + * @param context context * @param mode mode * * {@link #INTERACTIVE_MODE_MOTION} * {@link #INTERACTIVE_MODE_TOUCH} * {@link #INTERACTIVE_MODE_MOTION_WITH_TOUCH} */ - public void switchInteractiveMode(final Activity activity, final int mode){ - mInteractiveModeManager.switchMode(activity, mode); + public void switchInteractiveMode(final Context context, final int mode){ + mInteractiveModeManager.switchMode(context, mode); } - public void switchDisplayMode(final Activity activity){ - mDisplayModeManager.switchMode(activity); + public void switchDisplayMode(final Context context){ + mDisplayModeManager.switchMode(context); } /** @@ -280,22 +292,22 @@ public void switchDisplayMode(final Activity activity){ * {@link #DISPLAY_MODE_GLASS} * {@link #DISPLAY_MODE_NORMAL} */ - public void switchDisplayMode(final Activity activity, final int mode){ - mDisplayModeManager.switchMode(activity, mode); + public void switchDisplayMode(final Context context, final int mode){ + mDisplayModeManager.switchMode(context, mode); } /** * Switch Projection Mode * - * @param activity activity + * @param context context * @param mode mode * * {@link #PROJECTION_MODE_SPHERE} * {@link #PROJECTION_MODE_DOME180} * and so on. */ - public void switchProjectionMode(final Activity activity, final int mode) { - mProjectionModeManager.switchMode(activity, mode); + public void switchProjectionMode(final Context context, final int mode) { + mProjectionModeManager.switchMode(context, mode); } public void resetTouch(){ @@ -356,6 +368,30 @@ public void setPinchScale(float scale){ mTouchHelper.scaleTo(scale); } + public boolean isPinchEnabled(){ + return mTouchHelper.isPinchEnabled(); + } + + public void setPinchEnabled(boolean enabled) { + mTouchHelper.setPinchEnabled(enabled); + } + + public void setPinchConfig(MDPinchConfig pinchConfig){ + mTouchHelper.setPinchConfig(pinchConfig); + } + + public boolean isFlingEnabled(){ + return mTouchHelper.isFlingEnabled(); + } + + public void setFlingEnabled(boolean enabled) { + mTouchHelper.setFlingEnabled(enabled); + } + + public void setFlingConfig(MDFlingConfig flingConfig){ + mTouchHelper.setFlingConfig(flingConfig); + } + public void setDirectorFilter(IDirectorFilter filter){ mDirectorFilter.setDelegate(filter); } @@ -384,9 +420,8 @@ public void onTextureResize(float width, float height){ mTextureSize.set(0,0,width,height); } - - public void onOrientationChanged(Activity activity) { - mInteractiveModeManager.onOrientationChanged(activity); + public void onOrientationChanged(Context context) { + mInteractiveModeManager.onOrientationChanged(context); } public void onResume(Context context){ @@ -440,7 +475,7 @@ private void fireDestroy(){ * @return true if handled. */ public boolean handleTouchEvent(MotionEvent event) { - Log.e(TAG,"please remove the handleTouchEvent in activity!"); + Log.e(TAG,"please remove the handleTouchEvent in context!"); return false; } @@ -539,8 +574,8 @@ public interface ITouchPickListener2 { } - public static Builder with(Activity activity){ - return new Builder(activity); + public static Builder with(Context context){ + return new Builder(context); } /** @@ -550,7 +585,7 @@ public static class Builder { private int displayMode = DISPLAY_MODE_NORMAL; private int interactiveMode = INTERACTIVE_MODE_MOTION; private int projectionMode = PROJECTION_MODE_SPHERE; - private Activity activity; + private Context context; private int contentType = ContentType.DEFAULT; private MD360Texture texture; private INotSupportCallback notSupportCallback; @@ -567,9 +602,11 @@ public static class Builder { private IMDProjectionFactory projectionFactory; private MDPinchConfig pinchConfig; private IDirectorFilter directorFilter; + private boolean flingEnabled = true; // default true + private MDFlingConfig flingConfig; - private Builder(Activity activity) { - this.activity = activity; + private Builder(Context context) { + this.context = context; } public Builder displayMode(int displayMode){ @@ -723,19 +760,27 @@ public Builder directorFilter(IDirectorFilter filter){ return this; } + public Builder flingEnabled(boolean enabled){ + this.flingEnabled = enabled; + return this; + } + + public Builder flingConfig(MDFlingConfig config){ + this.flingConfig = config; + return this; + } + /** * build it! * - * @param glViewId will find the GLSurfaceView by glViewId in the giving {@link #activity} - * or find the GLTextureView by glViewId + * @param glView GLSurfaceView or GLTextureView * @return vr lib */ - public MDVRLibrary build(int glViewId){ - View view = activity.findViewById(glViewId); - if (view instanceof GLSurfaceView){ - return build((GLSurfaceView) view); - } else if(view instanceof GLTextureView){ - return build((GLTextureView) view); + public MDVRLibrary build(View glView){ + if (glView instanceof GLSurfaceView){ + return build((GLSurfaceView) glView); + } else if(glView instanceof GLTextureView){ + return build((GLTextureView) glView); } else { throw new RuntimeException("Please ensure the glViewId is instance of GLSurfaceView or GLTextureView"); } @@ -751,9 +796,10 @@ public MDVRLibrary build(GLTextureView glTextureView){ private MDVRLibrary build(MDGLScreenWrapper screenWrapper){ notNull(texture,"You must call video/bitmap function before build"); - if (this.directorFactory == null) directorFactory = new MD360DirectorFactory.DefaultImpl(); - if (this.barrelDistortionConfig == null) barrelDistortionConfig = new BarrelDistortionConfig(); - if (this.pinchConfig == null) pinchConfig = new MDPinchConfig(); + if (this.directorFactory == null) this.directorFactory = new MD360DirectorFactory.DefaultImpl(); + if (this.barrelDistortionConfig == null) this.barrelDistortionConfig = new BarrelDistortionConfig(); + if (this.pinchConfig == null) this.pinchConfig = new MDPinchConfig(); + if (this.flingConfig == null) this.flingConfig = new MDFlingConfig(); this.screenWrapper = screenWrapper; return new MDVRLibrary(this); } diff --git a/vrlib/src/main/java/com/asha/vrlib/model/MDFlingConfig.java b/vrlib/src/main/java/com/asha/vrlib/model/MDFlingConfig.java new file mode 100644 index 00000000..51ba02ba --- /dev/null +++ b/vrlib/src/main/java/com/asha/vrlib/model/MDFlingConfig.java @@ -0,0 +1,52 @@ +package com.asha.vrlib.model; + +import android.animation.TimeInterpolator; +import android.view.animation.DecelerateInterpolator; + +/** + * Created by hzqiujiadi on 2017/5/15. + * hzqiujiadi ashqalcn@gmail.com + */ + +public class MDFlingConfig { + + private TimeInterpolator mInterpolator = new DecelerateInterpolator(); + + private long mDuring = 400; + + private float mSensitivity = 1.0f; + + + public MDFlingConfig setInterpolator(TimeInterpolator i) { + this.mInterpolator = i; + return this; + } + + /*** + * @param during in ms + * */ + public MDFlingConfig setDuring(long during) { + this.mDuring = during; + return this; + } + + /*** + * @param sensitivity default is 1.0f, 10.0 is faster than 0.1f + * */ + public MDFlingConfig setSensitivity(float sensitivity) { + this.mSensitivity = sensitivity; + return this; + } + + public TimeInterpolator getInterpolator() { + return mInterpolator; + } + + public long getDuring() { + return mDuring; + } + + public float getSensitivity() { + return mSensitivity; + } +} diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/IModeStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/IModeStrategy.java index 50e9e1b1..cc822115 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/IModeStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/IModeStrategy.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy; -import android.app.Activity; import android.content.Context; /** @@ -9,11 +8,11 @@ */ public interface IModeStrategy { - void turnOnInGL(Activity activity); + void turnOnInGL(Context context); - void turnOffInGL(Activity activity); + void turnOffInGL(Context context); - boolean isSupport(Activity activity); + boolean isSupport(Context context); void onResume(Context context); diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/ModeManager.java b/vrlib/src/main/java/com/asha/vrlib/strategy/ModeManager.java index e45a4f8a..7be73b78 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/ModeManager.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/ModeManager.java @@ -1,6 +1,7 @@ package com.asha.vrlib.strategy; -import android.app.Activity; + +import android.content.Context; import com.asha.vrlib.MDVRLibrary; import com.asha.vrlib.common.MDGLHandler; @@ -27,23 +28,23 @@ public ModeManager(int mode, MDGLHandler handler) { /** * must call after new instance - * @param activity activity + * @param context context */ - public void prepare(Activity activity, MDVRLibrary.INotSupportCallback callback){ + public void prepare(Context context, MDVRLibrary.INotSupportCallback callback){ mCallback = callback; - initMode(activity,mMode); + initMode(context,mMode); } abstract protected T createStrategy(int mode); abstract protected int[] getModes(); - private void initMode(Activity activity, final int mode){ + private void initMode(Context context, final int mode){ if (mStrategy != null){ - off(activity); + off(context); } mStrategy = createStrategy(mode); - if (!mStrategy.isSupport(activity)){ + if (!mStrategy.isSupport(context)){ MDMainHandler.sharedHandler().post(new Runnable() { @Override public void run() { @@ -56,49 +57,49 @@ public void run() { } // t - on(activity); + on(context); } - public void switchMode(final Activity activity){ + public void switchMode(final Context context){ int[] modes = getModes(); int mode = getMode(); int index = Arrays.binarySearch(modes, mode); int nextIndex = (index + 1) % modes.length; int nextMode = modes[nextIndex]; - switchMode(activity, nextMode); + switchMode(context, nextMode); } - public void switchMode(final Activity activity, final int mode){ + public void switchMode(final Context context, final int mode){ if (mode == getMode()) return; mMode = mode; - initMode(activity, mMode); + initMode(context, mMode); } - public void on(final Activity activity) { + public void on(final Context context) { checkMainThread("strategy on must call from main thread!"); final T tmpStrategy = mStrategy; - if (tmpStrategy.isSupport(activity)){ + if (tmpStrategy.isSupport(context)){ getGLHandler().post(new Runnable() { @Override public void run() { - tmpStrategy.turnOnInGL(activity); + tmpStrategy.turnOnInGL(context); } }); } } - public void off(final Activity activity) { + public void off(final Context context) { checkMainThread("strategy off must call from main thread!"); final T tmpStrategy = mStrategy; - if (tmpStrategy.isSupport(activity)){ + if (tmpStrategy.isSupport(context)){ getGLHandler().post(new Runnable() { @Override public void run() { - tmpStrategy.turnOffInGL(activity); + tmpStrategy.turnOffInGL(context); } }); } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/display/GlassStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/display/GlassStrategy.java index b7bb8811..d9872a56 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/display/GlassStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/display/GlassStrategy.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.display; -import android.app.Activity; import android.content.Context; /** @@ -12,13 +11,13 @@ public class GlassStrategy extends AbsDisplayStrategy { public GlassStrategy() {} @Override - public void turnOnInGL(Activity activity) {} + public void turnOnInGL(Context context) {} @Override - public void turnOffInGL(Activity activity) {} + public void turnOffInGL(Context context) {} @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/display/NormalStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/display/NormalStrategy.java index bb84bbc9..2b539101 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/display/NormalStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/display/NormalStrategy.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.display; -import android.app.Activity; import android.content.Context; /** @@ -10,13 +9,13 @@ public class NormalStrategy extends AbsDisplayStrategy { @Override - public void turnOnInGL(Activity activity) {} + public void turnOnInGL(Context context) {} @Override - public void turnOffInGL(Activity activity) {} + public void turnOffInGL(Context context) {} @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/AbsInteractiveStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/AbsInteractiveStrategy.java index e6307597..030e019b 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/AbsInteractiveStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/AbsInteractiveStrategy.java @@ -1,5 +1,8 @@ package com.asha.vrlib.strategy.interactive; +import android.os.Handler; +import android.os.Looper; + import com.asha.vrlib.MD360Director; import com.asha.vrlib.strategy.IModeStrategy; @@ -13,6 +16,23 @@ public abstract class AbsInteractiveStrategy implements IModeStrategy, IInteract private InteractiveModeManager.Params params; + private Handler mMainHandler = null; + + protected Handler getMainHandler() { + if (null == mMainHandler) { + synchronized (this) { + if (null == mMainHandler) { + mMainHandler = new android.os.Handler(Looper.getMainLooper()); + } + } + } + return mMainHandler; + } + + protected void runOnUiThread(Runnable runnable) { + getMainHandler().post(runnable); + } + public AbsInteractiveStrategy(InteractiveModeManager.Params params) { this.params = params; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/CardboardMotionStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/CardboardMotionStrategy.java index 192d3f0f..00826c9d 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/CardboardMotionStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/CardboardMotionStrategy.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.interactive; -import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; @@ -58,11 +57,11 @@ public boolean handleDrag(int distanceX, int distanceY) { } @Override - public void onOrientationChanged(Activity activity) { + public void onOrientationChanged(Context context) { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { isOn = true; for (MD360Director director : getDirectorList()){ director.reset(); @@ -70,20 +69,20 @@ public void turnOnInGL(Activity activity) { } @Override - public void turnOffInGL(final Activity activity) { + public void turnOffInGL(final Context context) { isOn = false; - activity.runOnUiThread(new Runnable() { + runOnUiThread(new Runnable() { @Override public void run() { - unregisterSensor(activity); + unregisterSensor(context); } }); } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { if (mIsSupport == null){ - SensorManager mSensorManager = (SensorManager) activity + SensorManager mSensorManager = (SensorManager) context .getSystemService(Context.SENSOR_SERVICE); Sensor sensor1 = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); Sensor sensor2 = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/IInteractiveMode.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/IInteractiveMode.java index 47601e83..84107236 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/IInteractiveMode.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/IInteractiveMode.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.interactive; -import android.app.Activity; import android.content.Context; /** @@ -15,5 +14,5 @@ public interface IInteractiveMode { boolean handleDrag(int distanceX, int distanceY); - void onOrientationChanged(Activity activity); + void onOrientationChanged(Context context); } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/InteractiveModeManager.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/InteractiveModeManager.java index e473442a..d74ff78c 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/InteractiveModeManager.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/InteractiveModeManager.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.interactive; -import android.app.Activity; import android.content.Context; import android.hardware.SensorEventListener; @@ -77,11 +76,11 @@ public boolean handleDrag(final int distanceX, final int distanceY) { } @Override - public void onOrientationChanged(final Activity activity) { + public void onOrientationChanged(final Context context) { getGLHandler().post(new Runnable() { @Override public void run() { - getStrategy().onOrientationChanged(activity); + getStrategy().onOrientationChanged(context); } }); } @@ -103,23 +102,23 @@ public void run() { public void onResume(Context context) { mIsResumed = true; - if (getStrategy().isSupport((Activity)context)){ + if (getStrategy().isSupport(context)){ getStrategy().onResume(context); } } @Override - public void on(Activity activity) { - super.on(activity); + public void on(Context context) { + super.on(context); if (mIsResumed){ - onResume(activity); + onResume(context); } } public void onPause(Context context) { mIsResumed = false; - if (getStrategy().isSupport((Activity)context)){ + if (getStrategy().isSupport(context)){ getStrategy().onPause(context); } } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/MotionStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/MotionStrategy.java index ffc5812d..039badeb 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/MotionStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/MotionStrategy.java @@ -1,12 +1,12 @@ package com.asha.vrlib.strategy.interactive; -import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.util.Log; +import android.view.WindowManager; import com.asha.vrlib.MD360Director; import com.asha.vrlib.common.MDMainHandler; @@ -54,34 +54,36 @@ public boolean handleDrag(int distanceX, int distanceY) { } @Override - public void onOrientationChanged(Activity activity) { - mDeviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); + public void onOrientationChanged(Context context) { + WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); + mDeviceRotation = windowManager.getDefaultDisplay().getRotation(); } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { isOn = true; - mDeviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); + WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); + mDeviceRotation = windowManager.getDefaultDisplay().getRotation(); for (MD360Director director : getDirectorList()){ director.reset(); } } @Override - public void turnOffInGL(final Activity activity) { + public void turnOffInGL(final Context context) { isOn = false; - activity.runOnUiThread(new Runnable() { + runOnUiThread(new Runnable() { @Override public void run() { - unregisterSensor(activity); + unregisterSensor(context); } }); } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { if (mIsSupport == null){ - SensorManager mSensorManager = (SensorManager) activity + SensorManager mSensorManager = (SensorManager) context .getSystemService(Context.SENSOR_SERVICE); Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); mIsSupport = (sensor != null); diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/TouchStrategy.java b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/TouchStrategy.java index 43169813..329e5b07 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/TouchStrategy.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/interactive/TouchStrategy.java @@ -1,6 +1,5 @@ package com.asha.vrlib.strategy.interactive; -import android.app.Activity; import android.content.Context; import android.content.res.Resources; @@ -38,23 +37,23 @@ public boolean handleDrag(final int distanceX, final int distanceY) { } @Override - public void onOrientationChanged(Activity activity) { + public void onOrientationChanged(Context context) { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { for (MD360Director director : getDirectorList()){ director.reset(); } } @Override - public void turnOffInGL(Activity activity) { + public void turnOffInGL(Context context) { } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/DomeProjection.java b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/DomeProjection.java index 887ed87b..6cb754f6 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/DomeProjection.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/DomeProjection.java @@ -1,6 +1,6 @@ package com.asha.vrlib.strategy.projection; -import android.app.Activity; +import android.content.Context; import android.graphics.RectF; import com.asha.vrlib.model.MDMainPluginBuilder; @@ -32,18 +32,18 @@ public DomeProjection(RectF textureSize, float degree, boolean isUpper) { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { object3D = new MDDome3D(mTextureSize, mDegree, mIsUpper); - MDObject3DHelper.loadObj(activity, object3D); + MDObject3DHelper.loadObj(context, object3D); } @Override - public void turnOffInGL(Activity activity) { + public void turnOffInGL(Context context) { } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/PlaneProjection.java b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/PlaneProjection.java index 1eedbd2d..e04abd30 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/PlaneProjection.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/PlaneProjection.java @@ -1,6 +1,6 @@ package com.asha.vrlib.strategy.projection; -import android.app.Activity; +import android.content.Context; import android.graphics.RectF; import android.opengl.Matrix; @@ -32,18 +32,18 @@ private PlaneProjection(PlaneScaleCalculator calculator) { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { object3D = new MDPlane(planeScaleCalculator); - MDObject3DHelper.loadObj(activity, object3D); + MDObject3DHelper.loadObj(context, object3D); } @Override - public void turnOffInGL(Activity activity) { + public void turnOffInGL(Context context) { } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/ProjectionModeManager.java b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/ProjectionModeManager.java index 1fd52d71..69b6f470 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/ProjectionModeManager.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/ProjectionModeManager.java @@ -1,6 +1,6 @@ package com.asha.vrlib.strategy.projection; -import android.app.Activity; +import android.content.Context; import android.graphics.RectF; import com.asha.vrlib.MD360Director; @@ -79,13 +79,13 @@ public MDAbsPlugin getMainPlugin() { } @Override - public void switchMode(Activity activity, int mode) { - super.switchMode(activity, mode); + public void switchMode(Context context, int mode) { + super.switchMode(context, mode); } @Override - public void on(Activity activity) { - super.on(activity); + public void on(Context context) { + super.on(context); // destroy prev main plugin if( mMainPlugin != null){ diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/SphereProjection.java b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/SphereProjection.java index 295838b9..4dfc28a6 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/SphereProjection.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/SphereProjection.java @@ -1,6 +1,6 @@ package com.asha.vrlib.strategy.projection; -import android.app.Activity; +import android.content.Context; import com.asha.vrlib.model.MDMainPluginBuilder; import com.asha.vrlib.model.MDPosition; @@ -33,17 +33,17 @@ public MDPosition getModelPosition() { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { object3D = new MDSphere3D(); - MDObject3DHelper.loadObj(activity, object3D); + MDObject3DHelper.loadObj(context, object3D); } @Override - public void turnOffInGL(Activity activity) { + public void turnOffInGL(Context context) { } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; } diff --git a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/StereoSphereProjection.java b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/StereoSphereProjection.java index c02bf95c..4891e66a 100644 --- a/vrlib/src/main/java/com/asha/vrlib/strategy/projection/StereoSphereProjection.java +++ b/vrlib/src/main/java/com/asha/vrlib/strategy/projection/StereoSphereProjection.java @@ -1,6 +1,6 @@ package com.asha.vrlib.strategy.projection; -import android.app.Activity; +import android.content.Context; import com.asha.vrlib.MD360Director; import com.asha.vrlib.MD360DirectorFactory; @@ -35,18 +35,18 @@ public StereoSphereProjection(MDDirection direction) { } @Override - public void turnOnInGL(Activity activity) { + public void turnOnInGL(Context context) { object3D = new MDStereoSphere3D(direction); - MDObject3DHelper.loadObj(activity, object3D); + MDObject3DHelper.loadObj(context, object3D); } @Override - public void turnOffInGL(Activity activity) { + public void turnOffInGL(Context context) { } @Override - public boolean isSupport(Activity activity) { + public boolean isSupport(Context context) { return true; }