Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Jul 13, 2020
1 parent 2394d7d commit 9df46aa
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.provider.Settings;
Expand All @@ -10,6 +11,8 @@

import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;

import java.lang.ref.WeakReference;

/**
* 处理屏幕旋转的的逻辑
* Created by shuyu on 2016/11/11.
Expand All @@ -21,7 +24,7 @@ public class OrientationUtils {
private static final int LAND_TYPE_NORMAL = 1;
private static final int LAND_TYPE_REVERSE = 2;

private Activity mActivity;
private WeakReference<Activity> mActivity;
private GSYBaseVideoPlayer mVideoPlayer;
private OrientationEventListener mOrientationEventListener;
private OrientationOption mOrientationOption;
Expand Down Expand Up @@ -49,7 +52,7 @@ public OrientationUtils(Activity activity, GSYBaseVideoPlayer gsyVideoPlayer) {
}

public OrientationUtils(Activity activity, GSYBaseVideoPlayer gsyVideoPlayer, OrientationOption orientationOption) {
this.mActivity = activity;
this.mActivity = new WeakReference(activity);
this.mVideoPlayer = gsyVideoPlayer;
if (orientationOption == null) {
this.mOrientationOption = new OrientationOption();
Expand All @@ -61,11 +64,16 @@ public OrientationUtils(Activity activity, GSYBaseVideoPlayer gsyVideoPlayer, Or
}

protected void init() {
mOrientationEventListener = new OrientationEventListener(mActivity.getApplicationContext()) {
final Activity activity = mActivity.get();
if(activity == null) {
return;
}
final Context context = activity.getApplicationContext();
mOrientationEventListener = new OrientationEventListener(context) {
@SuppressLint("SourceLockedOrientationActivity")
@Override
public void onOrientationChanged(int rotation) {
boolean autoRotateOn = (Settings.System.getInt(mActivity.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
boolean autoRotateOn = (Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
if (!autoRotateOn && mRotateWithSystem) {
if (!mIsOnlyRotateLand || getIsLand() == LAND_TYPE_NULL) {
return;
Expand Down Expand Up @@ -176,8 +184,12 @@ private void initGravity(Activity activity) {
}

private void setRequestedOrientation(int requestedOrientation) {
final Activity activity = mActivity.get();
if(activity == null) {
return;
}
try {
mActivity.setRequestedOrientation(requestedOrientation);
activity.setRequestedOrientation(requestedOrientation);
} catch (IllegalStateException exception) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O || Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
Debuger.printfError("OrientationUtils", exception);
Expand All @@ -196,8 +208,13 @@ public void resolveByClick() {
return;
}
mClick = true;

final Activity activity = mActivity.get();
if(activity == null) {
return;
}
if (mIsLand == LAND_TYPE_NULL) {
int request = mActivity.getRequestedOrientation();
int request = activity.getRequestedOrientation();
if (request == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
mScreenType = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
} else {
Expand Down

0 comments on commit 9df46aa

Please sign in to comment.