| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.hardware.Sensor; | 8 import android.hardware.Sensor; |
| 9 import android.hardware.SensorEvent; | 9 import android.hardware.SensorEvent; |
| 10 import android.hardware.SensorEventListener; | 10 import android.hardware.SensorEventListener; |
| 11 import android.hardware.SensorManager; | 11 import android.hardware.SensorManager; |
| 12 import android.os.Handler; | 12 import android.os.Handler; |
| 13 import android.os.HandlerThread; |
| 13 import android.os.Looper; | 14 import android.os.Looper; |
| 14 import android.util.Log; | 15 import android.util.Log; |
| 15 | 16 |
| 16 import com.google.common.annotations.VisibleForTesting; | 17 import com.google.common.annotations.VisibleForTesting; |
| 17 import com.google.common.collect.ImmutableSet; | 18 import com.google.common.collect.ImmutableSet; |
| 18 import com.google.common.collect.Sets; | 19 import com.google.common.collect.Sets; |
| 19 | 20 |
| 20 import org.chromium.base.CalledByNative; | 21 import org.chromium.base.CalledByNative; |
| 21 import org.chromium.base.JNINamespace; | 22 import org.chromium.base.JNINamespace; |
| 22 import org.chromium.base.WeakContext; | 23 import org.chromium.base.WeakContext; |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 352 |
| 352 protected void gotRotationRate(double alpha, double beta, double gamma) { | 353 protected void gotRotationRate(double alpha, double beta, double gamma) { |
| 353 synchronized (mNativePtrLock) { | 354 synchronized (mNativePtrLock) { |
| 354 if (mNativePtr != 0) { | 355 if (mNativePtr != 0) { |
| 355 nativeGotRotationRate(mNativePtr, alpha, beta, gamma); | 356 nativeGotRotationRate(mNativePtr, alpha, beta, gamma); |
| 356 } | 357 } |
| 357 } | 358 } |
| 358 } | 359 } |
| 359 | 360 |
| 360 private Handler getHandler() { | 361 private Handler getHandler() { |
| 362 // TODO(timvolodine): Remove the mHandlerLock when sure that getHandler
is not called |
| 363 // from multiple threads. This will be the case when device motion and d
evice orientation |
| 364 // use the same polling thread (also see crbug/234282). |
| 361 synchronized (mHandlerLock) { | 365 synchronized (mHandlerLock) { |
| 362 // If we don't have a background thread, start it now. | 366 if (mHandler == null) { |
| 363 if (mThread == null) { | 367 HandlerThread thread = new HandlerThread("DeviceMotionAndOrienta
tion"); |
| 364 mThread = new Thread(new Runnable() { | 368 thread.start(); |
| 365 @Override | 369 mHandler = new Handler(thread.getLooper()); // blocks on thread
start |
| 366 public void run() { | |
| 367 Looper.prepare(); | |
| 368 // Our Handler doesn't actually have to do anything, bec
ause | |
| 369 // SensorManager posts directly to the underlying Looper
. | |
| 370 setHandler(new Handler()); | |
| 371 Looper.loop(); | |
| 372 } | |
| 373 }); | |
| 374 mThread.start(); | |
| 375 } | |
| 376 // Wait for the background thread to spin up. | |
| 377 while (mHandler == null) { | |
| 378 try { | |
| 379 mHandlerLock.wait(); | |
| 380 } catch (InterruptedException e) { | |
| 381 // Somebody doesn't want us to wait! That's okay, SensorMana
ger accepts null. | |
| 382 return null; | |
| 383 } | |
| 384 } | 370 } |
| 385 return mHandler; | 371 return mHandler; |
| 386 } | 372 } |
| 387 } | 373 } |
| 388 | 374 |
| 389 private void setHandler(Handler handler) { | |
| 390 synchronized (mHandlerLock) { | |
| 391 mHandler = handler; | |
| 392 mHandlerLock.notify(); | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 @CalledByNative | 375 @CalledByNative |
| 397 static DeviceMotionAndOrientation getInstance() { | 376 static DeviceMotionAndOrientation getInstance() { |
| 398 synchronized (sSingletonLock) { | 377 synchronized (sSingletonLock) { |
| 399 if (sSingleton == null) { | 378 if (sSingleton == null) { |
| 400 sSingleton = new DeviceMotionAndOrientation(); | 379 sSingleton = new DeviceMotionAndOrientation(); |
| 401 } | 380 } |
| 402 return sSingleton; | 381 return sSingleton; |
| 403 } | 382 } |
| 404 } | 383 } |
| 405 | 384 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 442 |
| 464 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { | 443 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { |
| 465 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | 444 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); |
| 466 if (!sensors.isEmpty()) { | 445 if (!sensors.isEmpty()) { |
| 467 mSensorManager.unregisterListener(listener, sensors.get(0)); | 446 mSensorManager.unregisterListener(listener, sensors.get(0)); |
| 468 } | 447 } |
| 469 } | 448 } |
| 470 } | 449 } |
| 471 | 450 |
| 472 } | 451 } |
| OLD | NEW |