Only fullscreen activities can r

2019-04-24  本文已影响0人  不正就是歪573

Only fullscreen activities can request orientation终极解决方法

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Aspect
public class FixOrientationCrash {
    private static final String TAG = FixOrientationCrash.class.getSimpleName();

    @Around("execution(* android.app.Activity+.setRequestedOrientation(..))")
    public Object fixThridCrash(ProceedingJoinPoint joinPoint) {
        Object object = null;
        try {
            if (Build.VERSION.SDK_INT == 26 && joinPoint.getTarget() instanceof Activity && this.isTranslucentOrFloating((Activity) joinPoint.getTarget())) {
                boolean result = this.fixOrientation((Activity) joinPoint.getTarget());
                LogUtils.i(TAG, "onCreate fixOrientation when Oreo, result = " + result, new Object[0]);
            } else {
                object = joinPoint.proceed();
            }
        } catch (Throwable var4) {
            var4.printStackTrace();
        }

        return object;
    }

    @Around("execution(* android.app.Activity+.onCreate(..))")
    public Object fixSourceCrash(ProceedingJoinPoint joinPoint) {
        Object object = null;
        try {
            if (Build.VERSION.SDK_INT == 26 && joinPoint.getTarget() instanceof Activity && this.isTranslucentOrFloating((Activity) joinPoint.getTarget())) {
                boolean result = this.fixOrientation((Activity) joinPoint.getTarget());
                LogUtils.i(TAG, "onCreate fixOrientation when Oreo, result = " + result, new Object[0]);
            }
        } catch (Throwable var4) {
            var4.printStackTrace();
        }

        try {
            object = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return object;
    }

    private boolean isTranslucentOrFloating(Activity activity) {
        boolean isTranslucentOrFloating = false;

        try {
            int[] styleableRes = (int[]) ((int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get((Object) null));
            TypedArray ta = activity.obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (Boolean) m.invoke((Object) null, ta);
            m.setAccessible(false);
        } catch (Exception var5) {
            LogUtils.e(TAG, "isTranslucentOrFloating occur error: " + var5.getMessage(), new Object[0]);
        }

        return isTranslucentOrFloating;
    }

    private boolean fixOrientation(Activity activity) {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(activity);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception var3) {
            LogUtils.e(TAG, "fixOrientation occur error: " + var3.getMessage(), new Object[0]);
            return false;
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读