提交 8f01383b 编写于 作者: ErnestX's avatar ErnestX

refactor: safe check getActivityContext to avoid NPE

上级 e4bd4734
......@@ -628,10 +628,10 @@ public abstract class GSYBaseVideoPlayer extends GSYVideoControlView {
final GSYBaseVideoPlayer gsyVideoPlayer;
if (!hadNewConstructor) {
constructor = (Constructor<GSYBaseVideoPlayer>) GSYBaseVideoPlayer.this.getClass().getConstructor(Context.class);
gsyVideoPlayer = constructor.newInstance(getActivityContext());
gsyVideoPlayer = constructor.newInstance(mContext);
} else {
constructor = (Constructor<GSYBaseVideoPlayer>) GSYBaseVideoPlayer.this.getClass().getConstructor(Context.class, Boolean.class);
gsyVideoPlayer = constructor.newInstance(getActivityContext(), true);
gsyVideoPlayer = constructor.newInstance(mContext, true);
}
gsyVideoPlayer.setId(getFullId());
......
......@@ -273,8 +273,9 @@ public abstract class GSYVideoControlView extends GSYVideoView implements View.O
});
}
mSeekEndOffset = CommonUtil.dip2px(getActivityContext(), 50);
if (getActivityContext()!=null) {
mSeekEndOffset = CommonUtil.dip2px(getActivityContext(), 50);
}
}
@Override
......@@ -668,10 +669,12 @@ public abstract class GSYVideoControlView extends GSYVideoView implements View.O
}
protected void touchSurfaceMove(float deltaX, float deltaY, float y) {
int curWidth = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenHeight : mScreenWidth;
int curHeight = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenWidth : mScreenHeight;
int curWidth = 0;
int curHeight = 0;
if (getActivityContext()!=null) {
curWidth = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenHeight : mScreenWidth;
curHeight = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenWidth : mScreenHeight;
}
if (mChangePosition) {
int totalTimeDuration = getDuration();
mSeekTimePosition = (int) (mDownPosition + (deltaX * totalTimeDuration / curWidth) / mSeekRatio);
......@@ -698,10 +701,10 @@ public abstract class GSYVideoControlView extends GSYVideoView implements View.O
}
protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
int curWidth = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenHeight : mScreenWidth;
int curWidth = 0;
if (getActivityContext()!=null) {
curWidth = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenHeight : mScreenWidth;
}
if (absDeltaX > mThreshold || absDeltaY > mThreshold) {
cancelProgressTimer();
if (absDeltaX >= mThreshold) {
......
......@@ -270,9 +270,9 @@ public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMe
mTextureViewContainer = (ViewGroup) findViewById(R.id.surface_container);
if (isInEditMode())
return;
mScreenWidth = getActivityContext().getResources().getDisplayMetrics().widthPixels;
mScreenHeight = getActivityContext().getResources().getDisplayMetrics().heightPixels;
mAudioManager = (AudioManager) getActivityContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
mScreenHeight = mContext.getResources().getDisplayMetrics().heightPixels;
mAudioManager = (AudioManager) mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
......@@ -329,7 +329,9 @@ public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMe
getGSYVideoManager().setPlayTag(mPlayTag);
getGSYVideoManager().setPlayPosition(mPlayPosition);
mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
((Activity) getActivityContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (mContext instanceof Activity) {
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
mBackUpPlayingBufferState = -1;
getGSYVideoManager().prepare(mUrl, (mMapHeadData == null) ? new HashMap<String, String>() : mMapHeadData, mLooping, mSpeed, mCache, mCachePath, mOverrideExtension);
setStateAndUi(CURRENT_STATE_PREPAREING);
......@@ -603,8 +605,9 @@ public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMe
if (!mIfCurrentIsFullscreen)
getGSYVideoManager().setLastListener(null);
mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
((Activity) getActivityContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (mContext instanceof Activity) {
((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
releaseNetWorkState();
if (mVideoAllCallBack != null && isCurrentMediaListener()) {
......@@ -633,7 +636,9 @@ public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMe
getGSYVideoManager().setCurrentVideoWidth(0);
mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
((Activity) getActivityContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (mContext instanceof Activity) {
((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
releaseNetWorkState();
......@@ -821,7 +826,7 @@ public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMe
*/
protected void createNetWorkState() {
if (mNetInfoModule == null) {
mNetInfoModule = new NetInfoModule(getActivityContext().getApplicationContext(), new NetInfoModule.NetChangeListener() {
mNetInfoModule = new NetInfoModule(mContext.getApplicationContext(), new NetInfoModule.NetChangeListener() {
@Override
public void changed(String state) {
if (!mNetSate.equals(state)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册