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() { |
361 synchronized (mHandlerLock) { | 362 synchronized (mHandlerLock) { |
joth
2013/04/18 17:26:26
unless getHandler() is called from multiple thread
timvolodine
2013/04/22 10:00:52
In the transitional implementation (i.e. using sha
| |
362 // If we don't have a background thread, start it now. | 363 if (mHandler == null) { |
363 if (mThread == null) { | 364 HandlerThread thread = new HandlerThread("DeviceMotionAndOrienta tion"); |
364 mThread = new Thread(new Runnable() { | 365 thread.start(); |
365 @Override | 366 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 } | 367 } |
385 return mHandler; | 368 return mHandler; |
386 } | 369 } |
387 } | 370 } |
388 | 371 |
389 private void setHandler(Handler handler) { | |
390 synchronized (mHandlerLock) { | |
391 mHandler = handler; | |
392 mHandlerLock.notify(); | |
393 } | |
394 } | |
395 | |
396 @CalledByNative | 372 @CalledByNative |
397 static DeviceMotionAndOrientation getInstance() { | 373 static DeviceMotionAndOrientation getInstance() { |
398 synchronized (sSingletonLock) { | 374 synchronized (sSingletonLock) { |
399 if (sSingleton == null) { | 375 if (sSingleton == null) { |
400 sSingleton = new DeviceMotionAndOrientation(); | 376 sSingleton = new DeviceMotionAndOrientation(); |
401 } | 377 } |
402 return sSingleton; | 378 return sSingleton; |
403 } | 379 } |
404 } | 380 } |
405 | 381 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 | 439 |
464 public void unregisterListener(SensorEventListener listener, int sensorT ype) { | 440 public void unregisterListener(SensorEventListener listener, int sensorT ype) { |
465 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | 441 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); |
466 if (!sensors.isEmpty()) { | 442 if (!sensors.isEmpty()) { |
467 mSensorManager.unregisterListener(listener, sensors.get(0)); | 443 mSensorManager.unregisterListener(listener, sensors.get(0)); |
468 } | 444 } |
469 } | 445 } |
470 } | 446 } |
471 | 447 |
472 } | 448 } |
OLD | NEW |