下面列出了android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Called by the {@link android.app.Activity#onPostCreate(Bundle)} to update
* the state of the container screen.
*/
public void syncScreenState() {
int screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
int navigationBarColor = -1;
if (currentFragment instanceof FlowrFragment) {
screenOrientation = ((FlowrFragment) currentFragment).getScreenOrientation();
navigationBarColor = ((FlowrFragment) currentFragment).getNavigationBarColor();
}
if (screen != null) {
screen.onCurrentFragmentChanged(getCurrentFragment());
screen.setScreenOrientation(screenOrientation);
screen.setNavigationBarColor(navigationBarColor);
}
syncToolbarState();
syncDrawerState();
}
@Override
protected void onStart() {
super.onStart();
// set the video player's orientation as what the user wants
String str = SkyTubeApp.getPreferenceManager().getString(getString(R.string.pref_key_screen_orientation), getString(R.string.pref_screen_auto_value));
int orientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
if (str.equals(getString(R.string.pref_screen_landscape_value)))
orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
if (str.equals(getString(R.string.pref_screen_portrait_value)))
orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
if (str.equals(getString(R.string.pref_screen_sensor_value)))
orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
setRequestedOrientation(orientation);
}
@Override
public void callActivityOnCreate(Activity activity, Bundle icicle) {
VirtualCore.get().getComponentDelegate().beforeActivityCreate(activity);
IBinder token = mirror.android.app.Activity.mToken.get(activity);
ActivityClientRecord r = VActivityManager.get().getActivityRecord(token);
if (r != null) {
r.activity = activity;
}
ContextFixer.fixContext(activity);
ActivityFixer.fixActivity(activity);
ActivityInfo info = null;
if (r != null) {
info = r.info;
}
if (info != null) {
if (info.theme != 0) {
activity.setTheme(info.theme);
}
if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
&& info.screenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
activity.setRequestedOrientation(info.screenOrientation);
}
}
super.callActivityOnCreate(activity, icicle);
VirtualCore.get().getComponentDelegate().afterActivityCreate(activity);
}
@CalledByNative
public static void lockOrientation(@Nullable WindowAndroid window, byte webScreenOrientation) {
// WindowAndroid may be null if the tab is being reparented.
if (window == null) return;
Activity activity = window.getActivity().get();
// Locking orientation is only supported for web contents that have an associated activity.
// Note that we can't just use the focused activity, as that would lead to bugs where
// unlockOrientation unlocks a different activity to the one that was locked.
if (activity == null) return;
int orientation = getOrientationFromWebScreenOrientations(webScreenOrientation, window,
activity);
if (orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
return;
}
activity.setRequestedOrientation(orientation);
}
private void onActivityCreated(Activity activity) throws RemoteException {
try {
Intent targetIntent = activity.getIntent();
if (targetIntent != null) {
ActivityInfo targetInfo = targetIntent.getParcelableExtra(Env.EXTRA_TARGET_INFO);
ActivityInfo stubInfo = targetIntent.getParcelableExtra(Env.EXTRA_STUB_INFO);
if (targetInfo != null && stubInfo != null) {
RunningActivities.onActivtyCreate(activity, targetInfo, stubInfo);
if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
&& targetInfo.screenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
activity.setRequestedOrientation(targetInfo.screenOrientation);
}
PluginManager.getInstance().onActivityCreated(stubInfo, targetInfo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fixTaskDescription(activity, targetInfo);
}
}
}
} catch (Exception e) {
Log.i(TAG, "onActivityCreated fail", e);
}
}
private int getCameraPictureOrientation() {
if (getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
outputOrientation = getCameraPictureRotation(getActivity().getWindowManager()
.getDefaultDisplay()
.getOrientation());
} else if (getCameraInfo().facing == CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (360 - displayOrientation) % 360;
} else {
outputOrientation = displayOrientation;
}
return outputOrientation;
}
public static String getOrientationString(int orientation) {
switch (orientation) {
case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
return "Unspecified";
case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
return "Behind";
case ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:
return "Full sensor";
case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
return "Full user";
case ActivityInfo.SCREEN_ORIENTATION_LOCKED:
return "Locked";
case ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
return "No sensor";
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
return "Landscape";
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
return "Portrait";
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
return "Reverse portrait";
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
return "Reverse landscape";
case ActivityInfo.SCREEN_ORIENTATION_USER:
return "User";
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
return "Sensor landscape";
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
return "Sensor portrait";
case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
return "Sensor";
case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
return "User landscape";
case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
return "User portrait";
default:
return "null";
}
}
public static int getOrientation(Context context) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
int value = 0;
try {
value = Integer.valueOf(pref.getString(Settings.ORIENTATION, "0"));
} catch (NumberFormatException e) {
}
if (value == 0) {
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
} else if (value == 1) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
public static String screenOrientationDesc(int curScreenOr) {
String oriDesc = curScreenOr + "";
switch (curScreenOr) {
case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
oriDesc = "在后面BEHIND";
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
oriDesc = "竖屏";
break;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
oriDesc = "横屏";
break;
case ActivityInfo.SCREEN_ORIENTATION_USER:
oriDesc = "跟随用户";
break;
case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
oriDesc = "跟随传感器";
break;
case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
oriDesc = "未指明的";
break;
case ActivityInfo.SCREEN_ORIENTATION_LOCKED:
oriDesc = "locked";
break;
}
return oriDesc;
}
protected void injectActivity(Activity activity) {
final Intent intent = activity.getIntent();
if (PluginUtil.isIntentFromPlugin(intent)) {
Context base = activity.getBaseContext();
try {
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(intent);
Reflector.with(base).field("mResources").set(plugin.getResources());
Reflector reflector = Reflector.with(activity);
reflector.field("mBase").set(plugin.createPluginContext(activity.getBaseContext()));
reflector.field("mApplication").set(plugin.getApplication());
// set screenOrientation
ActivityInfo activityInfo = plugin.getActivityInfo(PluginUtil.getComponent(intent));
if (activityInfo.screenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
activity.setRequestedOrientation(activityInfo.screenOrientation);
}
// for native activity
ComponentName component = PluginUtil.getComponent(intent);
Intent wrapperIntent = new Intent(intent);
wrapperIntent.setClassName(component.getPackageName(), component.getClassName());
activity.setIntent(wrapperIntent);
} catch (Exception e) {
Log.w(TAG, e);
}
}
}
@SuppressWarnings("WrongConstant")
@Override
public void setScreenOrientation(int orientation) {
// if the orientation is unspecified we set it to activity default
if (orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
setRequestedOrientation(getDefaultOrientation());
} else {
setRequestedOrientation(orientation);
}
}
private static int getManifestOrientation(Activity activity){
try {
ActivityInfo app = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
return app.screenOrientation;
} catch (PackageManager.NameNotFoundException e) {
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
}
/**
* The requested screen orientation. Commonly used values are
unspecified (-1), landscape (0), portrait (1), sensor (4), and user (2). " +
"See the Android developer documentation for ActivityInfo.Screen_Orientation for the " +
"complete list of possible settings.
*
* ScreenOrientation property getter method.
*
* @return screen orientation
*/
@SimpleProperty(category = PropertyCategory.APPEARANCE,
description = "The requested screen orientation, specified as a text value. " +
"Commonly used values are " +
"landscape, portrait, sensor, user and unspecified. " +
"See the Android developer documentation for ActivityInfo.Screen_Orientation for the " +
"complete list of possible settings.")
public String ScreenOrientation() {
switch (getRequestedOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
return "behind";
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
return "landscape";
case ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
return "nosensor";
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
return "portrait";
case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
return "sensor";
case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
return "unspecified";
case ActivityInfo.SCREEN_ORIENTATION_USER:
return "user";
case 10: // ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
return "fullSensor";
case 8: // ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
return "reverseLandscape";
case 9: // ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
return "reversePortrait";
case 6: // ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
return "sensorLandscape";
case 7: // ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
return "sensorPortrait";
}
return "unspecified";
}
private int getCameraPictureOrientation() {
if (getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
outputOrientation = getCameraPictureRotation(getActivity().getWindowManager()
.getDefaultDisplay()
.getOrientation());
} else if (getCameraInfo().facing == CameraInfo.CAMERA_FACING_FRONT) {
outputOrientation = (360 - displayOrientation) % 360;
} else {
outputOrientation = displayOrientation;
}
return outputOrientation;
}
/**
* 设置屏幕方向--注意targetSDK设置27以上不能设置windowIsTranslucent=true属性不然应用直接崩溃-强烈建议手机应用锁定竖屏
* 错误为 Only fullscreen activities can request orientation
* 默认自动 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
* 竖屏 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
* 横屏 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
* {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}
*
* @param activity
*/
public void setActivityOrientation(Activity activity) {
//全局控制屏幕横竖屏
//先判断xml没有设置屏幕模式避免将开发者本身想设置的覆盖掉
if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
try {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} catch (Exception e) {
e.printStackTrace();
LoggerManager.e(TAG, "setRequestedOrientation:" + e.getMessage());
}
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mGalleryView == null) {
return;
}
int screenRotation = mScreenRotation.getSelectedItemPosition();
int layoutMode = GalleryView.sanitizeLayoutMode(mReadingDirection.getSelectedItemPosition());
int scaleMode = GalleryView.sanitizeScaleMode(mScaleMode.getSelectedItemPosition());
int startPosition = GalleryView.sanitizeStartPosition(mStartPosition.getSelectedItemPosition());
boolean keepScreenOn = mKeepScreenOn.isChecked();
boolean showClock = mShowClock.isChecked();
boolean showProgress = mShowProgress.isChecked();
boolean showBattery = mShowBattery.isChecked();
boolean showPageInterval = mShowPageInterval.isChecked();
boolean volumePage = mVolumePage.isChecked();
boolean readingFullscreen = mReadingFullscreen.isChecked();
boolean customScreenLightness = mCustomScreenLightness.isChecked();
int screenLightness = mScreenLightness.getProgress();
boolean oldReadingFullscreen = Settings.getReadingFullscreen();
Settings.putScreenRotation(screenRotation);
Settings.putReadingDirection(layoutMode);
Settings.putPageScaling(scaleMode);
Settings.putStartPosition(startPosition);
Settings.putKeepScreenOn(keepScreenOn);
Settings.putShowClock(showClock);
Settings.putShowProgress(showProgress);
Settings.putShowBattery(showBattery);
Settings.putShowPageInterval(showPageInterval);
Settings.putVolumePage(volumePage);
Settings.putReadingFullscreen(readingFullscreen);
Settings.putCustomScreenLightness(customScreenLightness);
Settings.putScreenLightness(screenLightness);
int orientation;
switch (screenRotation) {
default:
case 0:
orientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
break;
case 1:
orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
break;
case 2:
orientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
break;
}
setRequestedOrientation(orientation);
mGalleryView.setLayoutMode(layoutMode);
mGalleryView.setScaleMode(scaleMode);
mGalleryView.setStartPosition(startPosition);
if (keepScreenOn) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
if (mClock != null) {
mClock.setVisibility(showClock ? View.VISIBLE : View.GONE);
}
if (mProgress != null) {
mProgress.setVisibility(showProgress ? View.VISIBLE : View.GONE);
}
if (mBattery != null) {
mBattery.setVisibility(showBattery ? View.VISIBLE : View.GONE);
}
mGalleryView.setPagerInterval(showPageInterval ? getResources().getDimensionPixelOffset(R.dimen.gallery_pager_interval) : 0);
mGalleryView.setScrollInterval(showPageInterval ? getResources().getDimensionPixelOffset(R.dimen.gallery_scroll_interval) : 0);
setScreenLightness(customScreenLightness, screenLightness);
// Update slider
mLayoutMode = layoutMode;
updateSlider();
if (oldReadingFullscreen != readingFullscreen) {
recreate();
}
}
/**
* 为插件中的Activity分配代理
*
* @param mLoadedApk 插件的实例
* @param actInfo 插件Activity对应的ActivityInfo
* @return 返回代理Activity的类名
*/
public static String findActivityProxy(PluginLoadedApk mLoadedApk, ActivityInfo actInfo) {
boolean isTranslucent = false;
boolean isHandleConfigChange = false;
boolean isLandscape = false;
boolean hasTaskAffinity = false;
boolean supportPip = false;
//通过主题判断是否是透明的
Resources.Theme mTheme = mLoadedApk.getPluginTheme();
isTranslucent = ActivityInfoUtils.isTranslucentTheme(mTheme, actInfo);
if (!isTranslucent) {
//兼容遗留逻辑
if (actInfo.metaData != null) {
String special_cfg = actInfo.metaData.getString(IntentConstant.META_KEY_ACTIVITY_SPECIAL);
if (!TextUtils.isEmpty(special_cfg)) {
if (special_cfg.contains(IntentConstant.PLUGIN_ACTIVITY_TRANSLUCENT)) {
PluginDebugLog.runtimeLog(TAG,
"findActivityProxy meta data contains translucent flag");
isTranslucent = true;
}
if (special_cfg.contains(IntentConstant.PLUGIN_ACTIVTIY_HANDLE_CONFIG_CHAGNE)) {
PluginDebugLog.runtimeLog(TAG,
"findActivityProxy meta data contains handleConfigChange flag");
isHandleConfigChange = true;
}
}
}
}
if (supportPictureInPicture(actInfo)) {
PluginDebugLog.runtimeLog(TAG, "findActivityProxy activity taskAffinity: "
+ actInfo.taskAffinity + " hasTaskAffinity = true" + ", supportPictureInPicture = true");
supportPip = true;
}
if (actInfo.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
String pkgName = mLoadedApk.getPluginPackageName();
if (TextUtils.equals(actInfo.taskAffinity, pkgName + IntentConstant.TASK_AFFINITY_CONTAINER1)) {
PluginDebugLog.runtimeLog(TAG, "findActivityProxy activity taskAffinity: "
+ actInfo.taskAffinity + " hasTaskAffinity = true");
hasTaskAffinity = true;
}
}
if (actInfo.screenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
PluginDebugLog.runtimeLog(TAG, "findActivityProxy activity screenOrientation: "
+ actInfo.screenOrientation + " isHandleConfigChange = false");
isHandleConfigChange = false;
}
if (actInfo.screenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
PluginDebugLog.runtimeLog(TAG, "findActivityProxy isLandscape = true");
isLandscape = true;
}
return matchActivityProxyByFeature(supportPip, hasTaskAffinity, isTranslucent, isLandscape,
isHandleConfigChange, mLoadedApk.getProcessName());
}
/**
* 初始化
* 包括设置theme,android2.3需要在super.oncreate之前调用settheme
*
* @param bundle Bundle
*/
public boolean initTargetActivity(Bundle bundle) {
Intent curIntent = super.getIntent();
if (curIntent == null) {
finish();
return false;
}
GPTComponentInfo info = GPTComponentInfo.parseFromIntent(curIntent);
if (info == null) {
finish();
return false;
}
String targetClassName = info.className;
String targetPackageName = info.packageName;
mTargetPackageName = targetPackageName;
mTargetClassName = targetClassName;
if (!ProxyEnvironment.isEnterProxy(targetPackageName)
|| ProxyEnvironment.getInstance(targetPackageName).getRemapedActivityClass(targetClassName) != this
.getClass()) {
if (targetClassName == null) {
targetClassName = "";
}
if (!TextUtils.isEmpty(targetPackageName)) {
if (!info.reschedule) {
Intent intent = new Intent(super.getIntent());
intent.setComponent(new ComponentName(targetPackageName, targetClassName));
ProxyEnvironment.enterProxy(super.getApplicationContext(), intent, true, true);
}
}
finish();
return false;
}
// 设置屏幕方向
int orientation = ProxyEnvironment.getInstance(mTargetPackageName).getTargetActivityOrientation(
mTargetClassName);
if (orientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
setRequestedOrientation(orientation);
}
// 设置主题
setTheme(ProxyEnvironment.getInstance(targetPackageName).getTargetActivityThemeResource(targetClassName));
return true;
}
/**
* @inheritDoc
*/
@Override
public int getScreenOrientation() {
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
private static int getOrientationFromWebScreenOrientations(byte orientation,
@Nullable WindowAndroid window, Context context) {
switch (orientation) {
case ScreenOrientationValues.DEFAULT:
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
case ScreenOrientationValues.PORTRAIT_PRIMARY:
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
case ScreenOrientationValues.PORTRAIT_SECONDARY:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
case ScreenOrientationValues.LANDSCAPE_PRIMARY:
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
case ScreenOrientationValues.LANDSCAPE_SECONDARY:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
case ScreenOrientationValues.PORTRAIT:
return ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
case ScreenOrientationValues.LANDSCAPE:
return ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
case ScreenOrientationValues.ANY:
return ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR;
case ScreenOrientationValues.NATURAL:
// If the tab is being reparented, we don't have a display strongly associated with
// it, so we get the default display.
DisplayAndroid displayAndroid = (window != null) ? window.getDisplay()
: DisplayAndroid.getNonMultiDisplay(context);
int rotation = displayAndroid.getRotation();
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
if (displayAndroid.getDisplayHeight() >= displayAndroid.getDisplayWidth()) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
if (displayAndroid.getDisplayHeight() < displayAndroid.getDisplayWidth()) {
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
default:
Log.w(TAG, "Trying to lock to unsupported orientation!");
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
}