Example usage for android.view Display getRotation

List of usage examples for android.view Display getRotation

Introduction

In this page you can find the example usage for android.view Display getRotation.

Prototype

@Surface.Rotation
public int getRotation() 

Source Link

Document

Returns the rotation of the screen from its "natural" orientation.

Usage

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

@TargetApi(Build.VERSION_CODES.FROYO)
private boolean handleVolumeButtonEvent(KeyEvent event) {

    // Disable volume button handling during TTS
    if (!config.isVolumeKeyNavEnabled() || ttsIsRunning()) {
        return false;
    }/* w  w w  . j av  a 2s  .c  o m*/

    Activity activity = getActivity();

    if (activity == null) {
        return false;
    }

    boolean invert = false;

    int rotation = Surface.ROTATION_0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        rotation = display.getRotation();
    }

    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_90:
        invert = false;
        break;
    case Surface.ROTATION_180:
    case Surface.ROTATION_270:
        invert = true;
        break;
    }

    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return true;
    }

    if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
        if (invert) {
            pageDown(Orientation.HORIZONTAL);
        } else {
            pageUp(Orientation.HORIZONTAL);
        }
    } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if (invert) {
            pageUp(Orientation.HORIZONTAL);
        } else {
            pageDown(Orientation.HORIZONTAL);
        }
    }

    return true;
}

From source file:com.android.launcher2.Launcher.java

private int mapConfigurationOriActivityInfoOri(int configOri) {
    final Display d = getWindowManager().getDefaultDisplay();
    int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
    switch (d.getRotation()) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180:
        // We are currently in the same basic orientation as the natural orientation
        naturalOri = configOri;/*from   ww  w  . java  2  s  .  c  o m*/
        break;
    case Surface.ROTATION_90:
    case Surface.ROTATION_270:
        // We are currently in the other basic orientation to the natural orientation
        naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ? Configuration.ORIENTATION_PORTRAIT
                : Configuration.ORIENTATION_LANDSCAPE;
        break;
    }

    int[] oriMap = { ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
            ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
            ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE };
    // Since the map starts at portrait, we need to offset if this device's natural orientation
    // is landscape.
    int indexOffset = 0;
    if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
        indexOffset = 1;
    }
    return oriMap[(d.getRotation() + indexOffset) % 4];
}

From source file:org.telegram.ui.ChatActivity.java

private void fixLayout() {
    if (chatListView != null) {
        ViewTreeObserver obs = chatListView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override/*from w  w  w  . ja v a 2s  . c o m*/
            public boolean onPreDraw() {
                if (parentActivity == null) {
                    chatListView.getViewTreeObserver().removeOnPreDrawListener(this);
                    return false;
                }
                WindowManager manager = (WindowManager) ApplicationLoader.applicationContext
                        .getSystemService(Activity.WINDOW_SERVICE);
                Display display = manager.getDefaultDisplay();
                int rotation = display.getRotation();
                int height;
                int currentActionBarHeight = parentActivity.getSupportActionBar().getHeight();

                if (currentActionBarHeight != Utilities.dp(48) && currentActionBarHeight != Utilities.dp(40)) {
                    height = currentActionBarHeight;
                } else {
                    height = Utilities.dp(48);
                    if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
                        height = Utilities.dp(40);
                    }
                }

                if (avatarImageView != null) {
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) avatarImageView
                            .getLayoutParams();
                    params.width = height;
                    params.height = height;
                    avatarImageView.setLayoutParams(params);
                }

                chatListView.getViewTreeObserver().removeOnPreDrawListener(this);

                if (currentEncryptedChat != null) {
                    TextView title = (TextView) parentActivity.findViewById(R.id.action_bar_title);
                    if (title == null) {
                        final int subtitleId = parentActivity.getResources().getIdentifier("action_bar_title",
                                "id", "android");
                        title = (TextView) parentActivity.findViewById(subtitleId);
                    }
                    if (title != null) {
                        title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_white, 0, 0, 0);
                        title.setCompoundDrawablePadding(Utilities.dp(4));
                    }
                }

                return false;
            }
        });
    }
}