1 package com.android.camera.app;
2 
3 import android.content.res.Configuration;
4 
5 /**
6  * An interface which defines the orientation manager.
7  */
8 public interface OrientationManager {
9     public static enum DeviceNaturalOrientation {
10         PORTRAIT(Configuration.ORIENTATION_PORTRAIT),
11         LANDSCAPE(Configuration.ORIENTATION_LANDSCAPE);
12 
13         private final int mOrientation;
DeviceNaturalOrientation(int orientation)14         private DeviceNaturalOrientation(int orientation) {
15             mOrientation = orientation;
16         }
17     }
18 
19     public static enum DeviceOrientation {
20         CLOCKWISE_0(0),
21         CLOCKWISE_90(90),
22         CLOCKWISE_180(180),
23         CLOCKWISE_270(270);
24 
25         private final int mDegrees;
26 
DeviceOrientation(int degrees)27         private DeviceOrientation(int degrees) {
28             mDegrees = degrees;
29         }
30 
31         /**
32          * Returns the degree in clockwise.
33          */
getDegrees()34         public int getDegrees() {
35             return mDegrees;
36         }
37 
38         /**
39          * Turns a degree value (0, 90, 180, 270) into one of CLOCKWISE_0,
40          * CLOCKWISE_90, CLOCKWISE_180 or CLOCKWISE_270. If any other degree
41          * value is given, the closest orientation of CLOCKWISE_0, CLOCKWISE_90,
42          * CLOCKWISE_180, and CLOCKWISE_270 to the angular value is returned.
43          */
from(int degrees)44         public static DeviceOrientation from(int degrees) {
45             switch (degrees) {
46                 case (-1):  // UNKNOWN Orientation
47                     // Explicitly default to CLOCKWISE_0, when Orientation is UNKNOWN
48                     return CLOCKWISE_0;
49                 case 0:
50                     return CLOCKWISE_0;
51                 case 90:
52                     return CLOCKWISE_90;
53                 case 180:
54                     return CLOCKWISE_180;
55                 case 270:
56                     return CLOCKWISE_270;
57                 default:
58                     int normalizedDegrees = (Math.abs(degrees / 360) * 360 + 360 + degrees) % 360;
59                     if (normalizedDegrees > 315 || normalizedDegrees <= 45) {
60                         return CLOCKWISE_0;
61                     } else if (normalizedDegrees > 45 && normalizedDegrees <= 135) {
62                         return CLOCKWISE_90;
63                     } else if (normalizedDegrees > 135 && normalizedDegrees <= 225) {
64                         return CLOCKWISE_180;
65                     } else {
66                         return CLOCKWISE_270;
67                     }
68             }
69         }
70     }
71 
72     public interface OnOrientationChangeListener {
73         /**
74          * Called when the orientation changes.
75          *
76          * @param orientationManager The orientation manager detects the change.
77          * @param orientation The new rounded orientation.
78          */
onOrientationChanged(OrientationManager orientationManager, DeviceOrientation orientation)79         public void onOrientationChanged(OrientationManager orientationManager,
80                                          DeviceOrientation orientation);
81     }
82 
83     /**
84      * Adds the
85      * {@link com.android.camera.app.OrientationManager.OnOrientationChangeListener}.
86      */
addOnOrientationChangeListener(OnOrientationChangeListener listener)87     public void addOnOrientationChangeListener(OnOrientationChangeListener listener);
88 
89     /**
90      * Removes the listener.
91      */
removeOnOrientationChangeListener(OnOrientationChangeListener listener)92     public void removeOnOrientationChangeListener(OnOrientationChangeListener listener);
93 
94     /**
95      * Returns the device natural orientation.
96      */
getDeviceNaturalOrientation()97     public DeviceNaturalOrientation getDeviceNaturalOrientation();
98 
99     /**
100      * Returns the current rounded device orientation.
101      */
getDeviceOrientation()102     public DeviceOrientation getDeviceOrientation();
103 
104     /**
105      * Returns the current display rotation.
106      */
getDisplayRotation()107     public DeviceOrientation getDisplayRotation();
108 
109     /**
110      * Returns whether the device is in landscape based on the natural orientation
111      * and rotation from natural orientation.
112      */
isInLandscape()113     public boolean isInLandscape();
114 
115     /**
116      * Returns whether the device is in portrait based on the natural orientation
117      * and rotation from natural orientation.
118      */
isInPortrait()119     public boolean isInPortrait();
120 
121     /**
122      * Lock the framework orientation to the current device orientation
123      * rotates. No effect if the system setting of auto-rotation is off.
124      */
lockOrientation()125     void lockOrientation();
126 
127     /**
128      * Unlock the framework orientation, so it can change when the device
129      * rotates. No effect if the system setting of auto-rotation is off.
130      */
unlockOrientation()131     void unlockOrientation();
132 
133     /**
134      * Return whether the orientation is locked by the app or the system.
135      */
isOrientationLocked()136     boolean isOrientationLocked();
137 }
138