1ed15d1a140986473bbe7fffd72ec9618c41c5979Angus Kongpackage com.android.camera.app;
2ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
324be7cc6d138129b4087ef28f114701de54aba3cSenpo Huimport android.content.res.Configuration;
424be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu
59f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong/**
69f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong * An interface which defines the orientation manager.
79f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong */
89f1db5210361802a30a7866825c3b29ef5fe0024Angus Kongpublic interface OrientationManager {
924be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    public static enum DeviceNaturalOrientation {
1024be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu        PORTRAIT(Configuration.ORIENTATION_PORTRAIT),
1124be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu        LANDSCAPE(Configuration.ORIENTATION_LANDSCAPE);
1224be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu
1324be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu        private final int mOrientation;
1424be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu        private DeviceNaturalOrientation(int orientation) {
1524be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu            mOrientation = orientation;
1624be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu        }
1724be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    }
1824be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu
190bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public static enum DeviceOrientation {
200bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        CLOCKWISE_0(0),
210bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        CLOCKWISE_90(90),
220bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        CLOCKWISE_180(180),
230bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        CLOCKWISE_270(270);
240bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
250bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        private final int mDegrees;
260bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
270bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        private DeviceOrientation(int degrees) {
280bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu            mDegrees = degrees;
290bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        }
300bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
310bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        /**
320bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu         * Returns the degree in clockwise.
330bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu         */
340bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        public int getDegrees() {
350bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu            return mDegrees;
360bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        }
373830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling
383830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling        /**
393830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling         * Turns a degree value (0, 90, 180, 270) into one of CLOCKWISE_0,
403830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling         * CLOCKWISE_90, CLOCKWISE_180 or CLOCKWISE_270. If any other degree
41ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin         * value is given, the closest orientation of CLOCKWISE_0, CLOCKWISE_90,
42ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin         * CLOCKWISE_180, and CLOCKWISE_270 to the angular value is returned.
433830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling         */
443830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling        public static DeviceOrientation from(int degrees) {
453830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling            switch (degrees) {
46ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                case (-1):  // UNKNOWN Orientation
47ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    // Explicitly default to CLOCKWISE_0, when Orientation is UNKNOWN
48ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    return CLOCKWISE_0;
493830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                case 0:
503830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                    return CLOCKWISE_0;
513830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                case 90:
523830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                    return CLOCKWISE_90;
533830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                case 180:
543830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                    return CLOCKWISE_180;
553830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                case 270:
563830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                    return CLOCKWISE_270;
573830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling                default:
58ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    int normalizedDegrees = (Math.abs(degrees / 360) * 360 + 360 + degrees) % 360;
59ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    if (normalizedDegrees > 315 || normalizedDegrees <= 45) {
60ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                        return CLOCKWISE_0;
61ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    } else if (normalizedDegrees > 45 && normalizedDegrees <= 135) {
62ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                        return CLOCKWISE_90;
63ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    } else if (normalizedDegrees > 135 && normalizedDegrees <= 225) {
64ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                        return CLOCKWISE_180;
65ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    } else {
66ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                        return CLOCKWISE_270;
67ec604214008248f7858b3a8b66d70919947399a9I-Jong Lin                    }
683830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling            }
693830d419691ef865f01b362fee9618bac2aa8888Sascha Haeberling        }
700bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    }
71ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
729f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    public interface OnOrientationChangeListener {
739f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong        /**
749f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong         * Called when the orientation changes.
759f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong         *
760bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu         * @param orientationManager The orientation manager detects the change.
770bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu         * @param orientation The new rounded orientation.
789f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong         */
790bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu        public void onOrientationChanged(OrientationManager orientationManager,
800bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu                                         DeviceOrientation orientation);
81ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang    }
82ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
839f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    /**
849f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     * Adds the
859f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     * {@link com.android.camera.app.OrientationManager.OnOrientationChangeListener}.
869f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     */
870bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public void addOnOrientationChangeListener(OnOrientationChangeListener listener);
88ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
899f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    /**
909f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     * Removes the listener.
919f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     */
920bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public void removeOnOrientationChangeListener(OnOrientationChangeListener listener);
930bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
940bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    /**
9524be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu     * Returns the device natural orientation.
9624be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu     */
9724be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    public DeviceNaturalOrientation getDeviceNaturalOrientation();
9824be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu
9924be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    /**
1000bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * Returns the current rounded device orientation.
1010bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     */
1020bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public DeviceOrientation getDeviceOrientation();
1030bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
1040bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    /**
10524be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu     * Returns the current display rotation.
10624be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu     */
10724be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    public DeviceOrientation getDisplayRotation();
10824be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu
10924be7cc6d138129b4087ef28f114701de54aba3cSenpo Hu    /**
1100bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * Returns whether the device is in landscape based on the natural orientation
1110bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * and rotation from natural orientation.
1120bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     */
1130bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public boolean isInLandscape();
1140bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu
1150bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    /**
1160bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * Returns whether the device is in portrait based on the natural orientation
1170bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * and rotation from natural orientation.
1180bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     */
1190bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    public boolean isInPortrait();
120ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
121ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong    /**
122ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     * Lock the framework orientation to the current device orientation
123ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     * rotates. No effect if the system setting of auto-rotation is off.
124ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     */
1259f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    void lockOrientation();
126a314235e093032c2d6cdd1f1d3f6b5c10c3e75a7Your Name
127ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong    /**
128ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     * Unlock the framework orientation, so it can change when the device
129ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     * rotates. No effect if the system setting of auto-rotation is off.
130ce2b94917098f211cacaaebaa0f6b40021d3e3faAngus Kong     */
1319f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    void unlockOrientation();
132ad08811a71e246d45ecdf97402f08cf7bd68e83bChih-Chung Chang
1339f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong    /**
1340bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu     * Return whether the orientation is locked by the app or the system.
1359f1db5210361802a30a7866825c3b29ef5fe0024Angus Kong     */
1360bdc4b54a18c18d7094b2e4cea1e238005c5c4a2Senpo Hu    boolean isOrientationLocked();
137ed15d1a140986473bbe7fffd72ec9618c41c5979Angus Kong}
138