Home | History | Annotate | Download | only in webkit
      1 /*
      2  * Copyright (C) 2010 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 android.webkit;
     18 
     19 /**
     20  * This class is simply a container for the methods used to implement DeviceMotion and
     21  * DeviceOrientation, including the mock DeviceOrientationClient for use in LayoutTests.
     22  *
     23  * This could be part of WebViewCore, but have moved it to its own class to
     24  * avoid bloat there.
     25  */
     26 final class DeviceMotionAndOrientationManager {
     27     private WebViewCore mWebViewCore;
     28 
     29     public DeviceMotionAndOrientationManager(WebViewCore webViewCore) {
     30         mWebViewCore = webViewCore;
     31     }
     32 
     33     /**
     34      * Sets that the Page for this WebViewCore should use a mock DeviceOrientation
     35      * client.
     36      */
     37     public void setUseMock() {
     38         assert WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName());
     39         nativeSetUseMock(mWebViewCore);
     40     }
     41 
     42     /**
     43      * Set the position for the mock DeviceOrientation service for this WebViewCore.
     44      */
     45     public void setMockOrientation(boolean canProvideAlpha, double alpha, boolean canProvideBeta,
     46             double beta, boolean canProvideGamma, double gamma) {
     47         assert WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName());
     48         nativeSetMockOrientation(mWebViewCore, canProvideAlpha, alpha, canProvideBeta, beta,
     49                 canProvideGamma, gamma);
     50     }
     51 
     52     // We only provide accelerationIncludingGravity.
     53     public void onMotionChange(Double x, Double y, Double z, double interval) {
     54         nativeOnMotionChange(mWebViewCore,
     55                 x != null, x != null ? x.doubleValue() : 0.0,
     56                 y != null, y != null ? y.doubleValue() : 0.0,
     57                 z != null, z != null ? z.doubleValue() : 0.0,
     58                 interval);
     59     }
     60     public void onOrientationChange(Double alpha, Double beta, Double gamma) {
     61         nativeOnOrientationChange(mWebViewCore,
     62                 alpha != null, alpha != null ? alpha.doubleValue() : 0.0,
     63                 beta != null, beta != null ? beta.doubleValue() : 0.0,
     64                 gamma != null, gamma != null ? gamma.doubleValue() : 0.0);
     65     }
     66 
     67     // Native functions
     68     private static native void nativeSetUseMock(WebViewCore webViewCore);
     69     private static native void nativeSetMockOrientation(WebViewCore webViewCore,
     70             boolean canProvideAlpha, double alpha, boolean canProvideBeta, double beta,
     71             boolean canProvideGamma, double gamma);
     72     private static native void nativeOnMotionChange(WebViewCore webViewCore,
     73             boolean canProvideX, double x, boolean canProvideY, double y,
     74             boolean canProvideZ, double z, double interval);
     75     private static native void nativeOnOrientationChange(WebViewCore webViewCore,
     76             boolean canProvideAlpha, double alpha, boolean canProvideBeta, double beta,
     77             boolean canProvideGamma, double gamma);
     78 }
     79