Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;
import android.content.Context;

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

public class Main {
    /** Returns the result of Display.getRotation, or Surface.ROTATION_0 if the getRotation method isn't available in
     * the current Android API.
     */
    public static int getDeviceRotation(Context context) {
        try {
            Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
            Method rotationMethod = null;
            // look for getRotation or the deprecated getOrientation
            for (String methodName : new String[] { "getRotation", "getOrientation" }) {
                try {
                    rotationMethod = Display.class.getMethod(methodName);
                    break;
                } catch (Exception ignored) {
                }
            }
            if (rotationMethod != null) {
                return (Integer) rotationMethod.invoke(display);
            }
        } catch (Exception ignored) {
        }
        return Surface.ROTATION_0;
    }
}