Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.Activity;
import android.content.Context;

import android.content.res.Configuration;
import android.os.Build;

import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

public class Main {
    static final int SCREEN_ORIENTATION_UNKNOWN = 0;
    static final int SCREEN_ORIENTATION_PORTRAIT = 1;
    static final int SCREEN_ORIENTATION_PORTRAITUPSIDEDOWN = 2;
    static final int SCREEN_ORIENTATION_LANDSCAPELEFT = 3;
    static final int SCREEN_ORIENTATION_LANDSCAPERIGHT = 4;

    public static int getSurfaceOrientation(Activity activity) {

        // Sanity check:
        if (activity == null) {
            return -1; // invalid value
        }

        Configuration config = activity.getResources().getConfiguration();
        Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        int displayRotation;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            displayRotation = display.getRotation(); // only available from Froyo
        } else {
            displayRotation = display.getOrientation();
        }

        int activityOrientation = SCREEN_ORIENTATION_UNKNOWN;

        switch (config.orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
        case Configuration.ORIENTATION_SQUARE:
            activityOrientation = ((displayRotation == Surface.ROTATION_0
                    || displayRotation == Surface.ROTATION_270) ? SCREEN_ORIENTATION_PORTRAIT
                            : SCREEN_ORIENTATION_PORTRAITUPSIDEDOWN);
            break;

        case Configuration.ORIENTATION_LANDSCAPE:
            activityOrientation = ((displayRotation == Surface.ROTATION_0 || displayRotation == Surface.ROTATION_90)
                    ? SCREEN_ORIENTATION_LANDSCAPELEFT
                    : SCREEN_ORIENTATION_LANDSCAPERIGHT);
            break;

        case Configuration.ORIENTATION_UNDEFINED:
        default:
            break;
        }

        return activityOrientation;
    }
}