Java Code Examples for android.app.Activity#setRequestedOrientation()

The following examples show how to use android.app.Activity#setRequestedOrientation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URotateLayout.java    From UCDMediaPlayer_Android with MIT License 6 votes vote down vote up
public void toggleOrientation() {
    if (getContext() instanceof Activity && orientation != ORIENTATION_LOCKED) {
        Activity mActivity = (Activity) getContext();
        if (isLandscape()) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }
        else {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }
        if (orientation == ORIENTATION_SENSOR) {
            getHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    setOrientation(orientation);
                }
            }, 2000);
        }
    }
}
 
Example 2
Source File: Utils.java    From whiteboard with Apache License 2.0 6 votes vote down vote up
/**
 * ��ת��Ļ
 */
public void rotateScreen(Activity activity, PainterSettings settings)
{
	if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
	{
		settings.setOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		Utils.getInstance().saveSettings(activity, settings);

		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	} else
	{
		settings.setOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Utils.getInstance().saveSettings(activity, settings);
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
}
 
Example 3
Source File: OrientationModule.java    From react-native-orientation-locker with MIT License 6 votes vote down vote up
@ReactMethod
public void lockToLandscapeRight() {
    final Activity activity = getCurrentActivity();
    if (activity == null) return;
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    isLocked = true;

    // force send an UI orientation event
    lastOrientationValue = "LANDSCAPE-RIGHT";
    WritableMap params = Arguments.createMap();
    params.putString("orientation", lastOrientationValue);
    if (ctx.hasActiveCatalystInstance()) {
        ctx
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        .emit("orientationDidChange", params);
    }

    // send a locked event
    WritableMap lockParams = Arguments.createMap();
    lockParams.putString("orientation", lastOrientationValue);
    if (ctx.hasActiveCatalystInstance()) {
        ctx
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        .emit("lockDidChange", lockParams);
    }
}
 
Example 4
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@SuppressLint("WrongConstant")
public static void unlockOrientation(Activity activity) {
    if (activity == null) {
        return;
    }
    try {
        if (prevOrientation != -10) {
            activity.setRequestedOrientation(prevOrientation);
            prevOrientation = -10;
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
Example 5
Source File: EspressoTestUtils.java    From Kore with Apache License 2.0 5 votes vote down vote up
public static void rotateDevice(Activity activity) {
    int orientation
            = activity.getResources().getConfiguration().orientation;
    activity.setRequestedOrientation(
            (orientation == Configuration.ORIENTATION_PORTRAIT) ?
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 
Example 6
Source File: ActivityUtils.java    From AndroidRipper with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Sets the Orientation (Landscape/Portrait) for the current activity.
 * 
 * @param orientation An orientation constant such as {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE} or {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT}
 */

public void setActivityOrientation(int orientation)
{
	Activity activity = getCurrentActivity();
	if(activity != null){
		activity.setRequestedOrientation(orientation);	
	}
}
 
Example 7
Source File: VAInstrumentation.java    From VirtualAPK with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 8
Source File: ImageRotationExtension.java    From FTCVision with MIT License 5 votes vote down vote up
private void setActivityOrientation(int state) {
    try {
        Activity activity = Util.getActivity();
        activity.setRequestedOrientation(state);
    } catch (IllegalArgumentException e) {
        Log.e("ScreenOrientation", "Looks like screen orientation changed and the app crashed!\r\n" +
                "It's likely you are using an incompatible Activity or a TestableVisionOpMode.\r\n" +
                "Refrain from setting screen orientation settings to fix this issue.");
    }
}
 
Example 9
Source File: BasicUiUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Set activity to be portrait
 * @param activity
 * @param isPortrait
 */
public static void setActivityPortraitOrientation(Activity activity,
                                                  boolean isPortrait) {
    if (isPortrait) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}
 
Example 10
Source File: YoikScreenOrientation.java    From showCaseCordova with Apache License 2.0 5 votes vote down vote up
private boolean routeScreenOrientation(JSONArray args, CallbackContext callbackContext) {

        String action = args.optString(0);

        if (action.equals("set")) {

            String orientation = args.optString(1);

            Log.d(TAG, "Requested ScreenOrientation: " + orientation);

            Activity activity = cordova.getActivity();

            if (orientation.equals(UNLOCKED)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            } else if (orientation.equals(LANDSCAPE_PRIMARY)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else if (orientation.equals(PORTRAIT_PRIMARY)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (orientation.equals(LANDSCAPE)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            } else if (orientation.equals(PORTRAIT)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            } else if (orientation.equals(LANDSCAPE_SECONDARY)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            } else if (orientation.equals(PORTRAIT_SECONDARY)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }

            callbackContext.success();
            return true;

        } else {
            callbackContext.error("ScreenOrientation not recognised");
            return false;
        }
    }
 
Example 11
Source File: BaseVideoController.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 反向横屏
 */
protected void onOrientationReverseLandscape(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    if (mControlWrapper.isFullScreen()) {
        handlePlayerStateChanged(VideoView.PLAYER_FULL_SCREEN);
    } else {
        mControlWrapper.startFullScreen();
    }
}
 
Example 12
Source File: ActivityControlImpl.java    From FastLib with Apache License 2.0 5 votes vote down vote up
/**
 * 设置屏幕方向--注意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());
        }
    }
}
 
Example 13
Source File: Util.java    From letv with Apache License 2.0 4 votes vote down vote up
public static void screenLandscape(Activity activity) {
    activity.setRequestedOrientation(0);
}
 
Example 14
Source File: Utils.java    From AndroidBlueprints with Apache License 2.0 4 votes vote down vote up
/**
 * Locks the device window in portrait mode
 */
public static void lockOrientationPortrait(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 
Example 15
Source File: UIsUtils.java    From letv with Apache License 2.0 4 votes vote down vote up
public static void setScreenPortrait(Activity activity) {
    activity.setRequestedOrientation(1);
}
 
Example 16
Source File: UIsUtils.java    From letv with Apache License 2.0 4 votes vote down vote up
public static void setScreenLandscape(Activity activity) {
    activity.setRequestedOrientation(0);
}
 
Example 17
Source File: PluginInjector.java    From Android-Plugin-Framework with MIT License 4 votes vote down vote up
static void resetWindowConfig(final Context pluginContext, final PluginDescriptor pd,
							  final Activity activity,
							  final ActivityInfo activityInfo,
							  final PluginActivityInfo pluginActivityInfo) {

	if (pluginActivityInfo != null) {

		//如果PluginContextTheme的getPackageName返回了插件包名,需要在这里对attribute修正
		activity.getWindow().getAttributes().packageName = FairyGlobal.getHostApplication().getPackageName();

		if (null != pluginActivityInfo.getWindowSoftInputMode()) {
			activity.getWindow().setSoftInputMode((int)Long.parseLong(pluginActivityInfo.getWindowSoftInputMode().replace("0x", ""), 16));
		}
		if (Build.VERSION.SDK_INT >= 14) {
			if (null != pluginActivityInfo.getUiOptions()) {
				activity.getWindow().setUiOptions((int)Long.parseLong(pluginActivityInfo.getUiOptions().replace("0x", ""), 16));
			}
		}
		if (null != pluginActivityInfo.getScreenOrientation()) {
			int orientation = (int)Long.parseLong(pluginActivityInfo.getScreenOrientation());
			//noinspection ResourceType
			if (orientation != activityInfo.screenOrientation && !activity.isChild()) {
				//noinspection ResourceType
                   //框架中只内置了unspec和landscape两种screenOrientation
                   //如果是其他类型,这里通过代码实现切换
                   LogUtil.v("修改screenOrientation");
				activity.setRequestedOrientation(orientation);
			}
		}
		if (Build.VERSION.SDK_INT >= 18 && !activity.isChild()) {
			Boolean isImmersive = ResourceUtil.getBoolean(pluginActivityInfo.getImmersive(), pluginContext);
			if (isImmersive != null) {
				activity.setImmersive(isImmersive);
			}
		}

		String activityClassName = activity.getClass().getName();
		LogUtil.v(activityClassName, "immersive", pluginActivityInfo.getImmersive());
		LogUtil.v(activityClassName, "screenOrientation", pluginActivityInfo.getScreenOrientation());
		LogUtil.v(activityClassName, "launchMode", pluginActivityInfo.getLaunchMode());
		LogUtil.v(activityClassName, "windowSoftInputMode", pluginActivityInfo.getWindowSoftInputMode());
		LogUtil.v(activityClassName, "uiOptions", pluginActivityInfo.getUiOptions());
	}

	//如果是独立插件,由于没有合并资源,这里还需要替换掉 mActivityInfo,
	//避免activity试图通过ActivityInfo中的资源id来读取资源时失败
	activityInfo.icon = pd.getApplicationIcon();
	activityInfo.logo = pd.getApplicationLogo();
	if (Build.VERSION.SDK_INT >= 19) {
		activity.getWindow().setIcon(activityInfo.icon);
		activity.getWindow().setLogo(activityInfo.logo);
	}
}
 
Example 18
Source File: AndroidUtilities.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public static void lockOrientation(Activity activity)
{
    if (activity == null || prevOrientation != -10)
    {
        return;
    }
    try
    {
        prevOrientation = activity.getRequestedOrientation();
        WindowManager manager = (WindowManager) activity.getSystemService(Activity.WINDOW_SERVICE);
        if (manager != null && manager.getDefaultDisplay() != null)
        {
            int rotation = manager.getDefaultDisplay().getRotation();
            int orientation = activity.getResources().getConfiguration().orientation;

            if (rotation == Surface.ROTATION_270)
            {
                if (orientation == Configuration.ORIENTATION_PORTRAIT)
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            }
            else if (rotation == Surface.ROTATION_90)
            {
                if (orientation == Configuration.ORIENTATION_PORTRAIT)
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
                else
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
            else if (rotation == Surface.ROTATION_0)
            {
                if (orientation == Configuration.ORIENTATION_LANDSCAPE)
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                else
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            }
            else
            {
                if (orientation == Configuration.ORIENTATION_LANDSCAPE)
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
                else
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
            }
        }
    }
    catch (Exception e)
    {
        FileLog.e(e);
    }
}
 
Example 19
Source File: AppTools.java    From RxEasyHttp with Apache License 2.0 2 votes vote down vote up
/**
 * </br><b>title : </b>		设置Activity的显示方向为垂直方向
 * </br><b>description :</b>强制设置Actiity的显示方向为垂直方向。
 *
 * @param activity Activity对象
 */
public static void setScreenVertical(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 
Example 20
Source File: ScreenUtils.java    From Common with Apache License 2.0 2 votes vote down vote up
/**
 * Set the screen to portrait.
 *
 * @param activity The activity.
 */
public static void setPortrait(@NonNull final Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}