Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: device/sensors/android/java/src/org/chromium/device/sensors/DeviceSensors.java

Issue 2847523002: Android: Remove GetApplicationContext part 4 (Closed)
Patch Set: Rebase and fix build Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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.device.sensors; 5 package org.chromium.device.sensors;
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.HandlerThread;
14 14
15 import org.chromium.base.CollectionUtil; 15 import org.chromium.base.CollectionUtil;
16 import org.chromium.base.ContextUtils;
16 import org.chromium.base.Log; 17 import org.chromium.base.Log;
17 import org.chromium.base.ThreadUtils; 18 import org.chromium.base.ThreadUtils;
18 import org.chromium.base.VisibleForTesting; 19 import org.chromium.base.VisibleForTesting;
19 import org.chromium.base.annotations.CalledByNative; 20 import org.chromium.base.annotations.CalledByNative;
20 import org.chromium.base.annotations.JNINamespace; 21 import org.chromium.base.annotations.JNINamespace;
21 22
22 import java.util.HashSet; 23 import java.util.HashSet;
23 import java.util.List; 24 import java.util.List;
24 import java.util.Set; 25 import java.util.Set;
25 26
26 /** 27 /**
27 * Android implementation of the device {motion|orientation} APIs. 28 * Android implementation of the device {motion|orientation} APIs.
28 */ 29 */
29 @JNINamespace("device") 30 @JNINamespace("device")
30 class DeviceSensors implements SensorEventListener { 31 class DeviceSensors implements SensorEventListener {
31 private static final String TAG = "DeviceSensors"; 32 private static final String TAG = "DeviceSensors";
32 33
33 // Matches kEnableExperimentalWebPlatformFeatures. 34 // Matches kEnableExperimentalWebPlatformFeatures.
34 private static final String EXPERIMENTAL_WEB_PLAFTORM_FEATURES = 35 private static final String EXPERIMENTAL_WEB_PLAFTORM_FEATURES =
35 "enable-experimental-web-platform-features"; 36 "enable-experimental-web-platform-features";
36 37
37 // These fields are lazily initialized by getHandler(). 38 // These fields are lazily initialized by getHandler().
38 private Thread mThread; 39 private Thread mThread;
39 private Handler mHandler; 40 private Handler mHandler;
40 41
41 // A reference to the application context in order to acquire the SensorServ ice.
42 private final Context mAppContext;
43
44 // The lock to access the mHandler. 42 // The lock to access the mHandler.
45 private final Object mHandlerLock = new Object(); 43 private final Object mHandlerLock = new Object();
46 44
47 // Non-zero if and only if we're listening for events. 45 // Non-zero if and only if we're listening for events.
48 // To avoid race conditions on the C++ side, access must be synchronized. 46 // To avoid race conditions on the C++ side, access must be synchronized.
49 private long mNativePtr; 47 private long mNativePtr;
50 48
51 // The lock to access the mNativePtr. 49 // The lock to access the mNativePtr.
52 private final Object mNativePtrLock = new Object(); 50 private final Object mNativePtrLock = new Object();
53 51
(...skipping 27 matching lines...) Expand all
81 @VisibleForTesting 79 @VisibleForTesting
82 final Set<Integer> mActiveSensors = new HashSet<Integer>(); 80 final Set<Integer> mActiveSensors = new HashSet<Integer>();
83 final List<Set<Integer>> mOrientationSensorSets; 81 final List<Set<Integer>> mOrientationSensorSets;
84 Set<Integer> mDeviceOrientationSensors; 82 Set<Integer> mDeviceOrientationSensors;
85 boolean mDeviceMotionIsActive; 83 boolean mDeviceMotionIsActive;
86 boolean mDeviceOrientationIsActive; 84 boolean mDeviceOrientationIsActive;
87 boolean mDeviceOrientationIsActiveWithBackupSensors; 85 boolean mDeviceOrientationIsActiveWithBackupSensors;
88 boolean mDeviceOrientationAbsoluteIsActive; 86 boolean mDeviceOrientationAbsoluteIsActive;
89 boolean mOrientationNotAvailable; 87 boolean mOrientationNotAvailable;
90 88
91 protected DeviceSensors(Context context) { 89 protected DeviceSensors() {
92 mAppContext = context.getApplicationContext();
93
94 mOrientationSensorSets = CollectionUtil.newArrayList(DEVICE_ORIENTATION_ SENSORS_A, 90 mOrientationSensorSets = CollectionUtil.newArrayList(DEVICE_ORIENTATION_ SENSORS_A,
95 DEVICE_ORIENTATION_SENSORS_B, DEVICE_ORIENTATION_SENSORS_C); 91 DEVICE_ORIENTATION_SENSORS_B, DEVICE_ORIENTATION_SENSORS_C);
96 } 92 }
97 93
98 // For orientation we use a 3-way fallback approach where up to 3 different sets of sensors 94 // For orientation we use a 3-way fallback approach where up to 3 different sets of sensors
99 // are attempted if necessary. The sensors to be used for orientation are de termined in the 95 // are attempted if necessary. The sensors to be used for orientation are de termined in the
100 // following order: 96 // following order:
101 // A: GAME_ROTATION_VECTOR (relative) 97 // A: GAME_ROTATION_VECTOR (relative)
102 // B: ROTATION_VECTOR (absolute) 98 // B: ROTATION_VECTOR (absolute)
103 // C: combination of ACCELEROMETER and MAGNETIC_FIELD (absolute) 99 // C: combination of ACCELEROMETER and MAGNETIC_FIELD (absolute)
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 Math.toDegrees(mRotationAngles[2])); 412 Math.toDegrees(mRotationAngles[2]));
417 } 413 }
418 414
419 private SensorManagerProxy getSensorManagerProxy() { 415 private SensorManagerProxy getSensorManagerProxy() {
420 if (mSensorManagerProxy != null) { 416 if (mSensorManagerProxy != null) {
421 return mSensorManagerProxy; 417 return mSensorManagerProxy;
422 } 418 }
423 419
424 ThreadUtils.assertOnUiThread(); 420 ThreadUtils.assertOnUiThread();
425 SensorManager sensorManager = 421 SensorManager sensorManager =
426 (SensorManager) mAppContext.getSystemService(Context.SENSOR_SERV ICE); 422 (SensorManager) ContextUtils.getApplicationContext().getSystemSe rvice(
423 Context.SENSOR_SERVICE);
427 424
428 if (sensorManager != null) { 425 if (sensorManager != null) {
429 mSensorManagerProxy = new SensorManagerProxyImpl(sensorManager); 426 mSensorManagerProxy = new SensorManagerProxyImpl(sensorManager);
430 } 427 }
431 return mSensorManagerProxy; 428 return mSensorManagerProxy;
432 } 429 }
433 430
434 @VisibleForTesting 431 @VisibleForTesting
435 void setSensorManagerProxy(SensorManagerProxy sensorManagerProxy) { 432 void setSensorManagerProxy(SensorManagerProxy sensorManagerProxy) {
436 mSensorManagerProxy = sensorManagerProxy; 433 mSensorManagerProxy = sensorManagerProxy;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 if (mHandler == null) { 555 if (mHandler == null) {
559 HandlerThread thread = new HandlerThread("DeviceMotionAndOrienta tion"); 556 HandlerThread thread = new HandlerThread("DeviceMotionAndOrienta tion");
560 thread.start(); 557 thread.start();
561 mHandler = new Handler(thread.getLooper()); // blocks on thread start 558 mHandler = new Handler(thread.getLooper()); // blocks on thread start
562 } 559 }
563 return mHandler; 560 return mHandler;
564 } 561 }
565 } 562 }
566 563
567 @CalledByNative 564 @CalledByNative
568 static DeviceSensors create(Context appContext) { 565 static DeviceSensors create() {
569 return new DeviceSensors(appContext); 566 return new DeviceSensors();
570 } 567 }
571 568
572 /** 569 /**
573 * Native JNI calls, 570 * Native JNI calls,
574 * see device/sensors/sensor_manager_android.cc 571 * see device/sensors/sensor_manager_android.cc
575 */ 572 */
576 573
577 /** 574 /**
578 * Orientation of the device with respect to its reference frame. 575 * Orientation of the device with respect to its reference frame.
579 */ 576 */
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 mSensorManager.unregisterListener(listener, sensors.get(0)); 637 mSensorManager.unregisterListener(listener, sensors.get(0));
641 } catch (IllegalArgumentException e) { 638 } catch (IllegalArgumentException e) {
642 // Suppress occasional exception on Digma iDxD* devices: 639 // Suppress occasional exception on Digma iDxD* devices:
643 // Receiver not registered: android.hardware.SystemSensorManager $1 640 // Receiver not registered: android.hardware.SystemSensorManager $1
644 // See crbug.com/596453. 641 // See crbug.com/596453.
645 Log.w(TAG, "Failed to unregister device sensor " + sensors.get(0 ).getName()); 642 Log.w(TAG, "Failed to unregister device sensor " + sensors.get(0 ).getName());
646 } 643 }
647 } 644 }
648 } 645 }
649 } 646 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698