Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.app;
     18 
     19 import android.app.Activity;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.content.pm.ActivityInfo;
     23 import android.content.res.Configuration;
     24 import android.provider.Settings;
     25 import android.view.OrientationEventListener;
     26 import android.view.Surface;
     27 
     28 import com.android.gallery3d.common.ApiHelper;
     29 import com.android.gallery3d.ui.OrientationSource;
     30 
     31 public class OrientationManager implements OrientationSource {
     32     private static final String TAG = "OrientationManager";
     33 
     34     // Orientation hysteresis amount used in rounding, in degrees
     35     private static final int ORIENTATION_HYSTERESIS = 5;
     36 
     37     private Activity mActivity;
     38     private MyOrientationEventListener mOrientationListener;
     39     // If the framework orientation is locked.
     40     private boolean mOrientationLocked = false;
     41 
     42     // This is true if "Settings -> Display -> Rotation Lock" is checked. We
     43     // don't allow the orientation to be unlocked if the value is true.
     44     private boolean mRotationLockedSetting = false;
     45 
     46     public OrientationManager(Activity activity) {
     47         mActivity = activity;
     48         mOrientationListener = new MyOrientationEventListener(activity);
     49     }
     50 
     51     public void resume() {
     52         ContentResolver resolver = mActivity.getContentResolver();
     53         mRotationLockedSetting = Settings.System.getInt(
     54                 resolver, Settings.System.ACCELEROMETER_ROTATION, 0) != 1;
     55         mOrientationListener.enable();
     56     }
     57 
     58     public void pause() {
     59         mOrientationListener.disable();
     60     }
     61 
     62     ////////////////////////////////////////////////////////////////////////////
     63     //  Orientation handling
     64     //
     65     //  We can choose to lock the framework orientation or not. If we lock the
     66     //  framework orientation, we calculate a a compensation value according to
     67     //  current device orientation and send it to listeners. If we don't lock
     68     //  the framework orientation, we always set the compensation value to 0.
     69     ////////////////////////////////////////////////////////////////////////////
     70 
     71     // Lock the framework orientation to the current device orientation
     72     public void lockOrientation() {
     73         if (mOrientationLocked) return;
     74         mOrientationLocked = true;
     75         if (ApiHelper.HAS_ORIENTATION_LOCK) {
     76             mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
     77         } else {
     78             mActivity.setRequestedOrientation(calculateCurrentScreenOrientation());
     79         }
     80     }
     81 
     82     // Unlock the framework orientation, so it can change when the device
     83     // rotates.
     84     public void unlockOrientation() {
     85         if (!mOrientationLocked) return;
     86         mOrientationLocked = false;
     87         Log.d(TAG, "unlock orientation");
     88         mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
     89     }
     90 
     91     private int calculateCurrentScreenOrientation() {
     92         int displayRotation = getDisplayRotation();
     93         // Display rotation >= 180 means we need to use the REVERSE landscape/portrait
     94         boolean standard = displayRotation < 180;
     95         if (mActivity.getResources().getConfiguration().orientation
     96                 == Configuration.ORIENTATION_LANDSCAPE) {
     97             return standard
     98                     ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
     99                     : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    100         } else {
    101             if (displayRotation == 90 || displayRotation == 270) {
    102                 // If displayRotation = 90 or 270 then we are on a landscape
    103                 // device. On landscape devices, portrait is a 90 degree
    104                 // clockwise rotation from landscape, so we need
    105                 // to flip which portrait we pick as display rotation is counter clockwise
    106                 standard = !standard;
    107             }
    108             return standard
    109                     ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    110                     : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    111         }
    112     }
    113 
    114     // This listens to the device orientation, so we can update the compensation.
    115     private class MyOrientationEventListener extends OrientationEventListener {
    116         public MyOrientationEventListener(Context context) {
    117             super(context);
    118         }
    119 
    120         @Override
    121         public void onOrientationChanged(int orientation) {
    122             // We keep the last known orientation. So if the user first orient
    123             // the camera then point the camera to floor or sky, we still have
    124             // the correct orientation.
    125             if (orientation == ORIENTATION_UNKNOWN) return;
    126             orientation = roundOrientation(orientation, 0);
    127         }
    128     }
    129 
    130     @Override
    131     public int getDisplayRotation() {
    132         return getDisplayRotation(mActivity);
    133     }
    134 
    135     @Override
    136     public int getCompensation() {
    137         return 0;
    138     }
    139 
    140     private static int roundOrientation(int orientation, int orientationHistory) {
    141         boolean changeOrientation = false;
    142         if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
    143             changeOrientation = true;
    144         } else {
    145             int dist = Math.abs(orientation - orientationHistory);
    146             dist = Math.min(dist, 360 - dist);
    147             changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    148         }
    149         if (changeOrientation) {
    150             return ((orientation + 45) / 90 * 90) % 360;
    151         }
    152         return orientationHistory;
    153     }
    154 
    155     private static int getDisplayRotation(Activity activity) {
    156         int rotation = activity.getWindowManager().getDefaultDisplay()
    157                 .getRotation();
    158         switch (rotation) {
    159             case Surface.ROTATION_0: return 0;
    160             case Surface.ROTATION_90: return 90;
    161             case Surface.ROTATION_180: return 180;
    162             case Surface.ROTATION_270: return 270;
    163         }
    164         return 0;
    165     }
    166 }
    167