Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*   Copyright 2013-2014 Miguel Vicente Linares
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import android.app.Activity;

import android.content.res.Configuration;

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

public class Main {
    /**
     * Calculates the default orientation of the device.<br>
     * The default orientation of most phones is portrait, and the default orientation of most tablets is landscape.
     * 
     * @param activity Activity.
     * @return {@link Configuration#ORIENTATION_PORTRAIT} or {@link Configuration#ORIENTATION_LANDSCAPE}
     */
    public static int getDefaultOrientationOfDevice(Activity activity) {
        Configuration configuration = activity.getResources().getConfiguration();
        boolean orientationLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);

        Display display = activity.getWindowManager().getDefaultDisplay();
        int rotation = display.getRotation();
        boolean parallelToDefaultVerticalAxis = (rotation == Surface.ROTATION_0)
                || (rotation == Surface.ROTATION_180);

        boolean defaultOrientationLandscape = (parallelToDefaultVerticalAxis && orientationLandscape)
                || (!parallelToDefaultVerticalAxis && !orientationLandscape);

        if (defaultOrientationLandscape) {
            return Configuration.ORIENTATION_LANDSCAPE;
        }
        return Configuration.ORIENTATION_PORTRAIT;
    }
}