diff options
author | dtrainor@chromium.org <dtrainor@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-21 20:25:30 +0000 |
---|---|---|
committer | dtrainor@chromium.org <dtrainor@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-21 20:25:30 +0000 |
commit | c8fcfcf6986774d91b391ffa1b9ef8df49a2e815 (patch) | |
tree | 4f196b37815dc20c810b34b0042e91f3a770b97c | |
parent | 804b16bc24aaaa854ef297d97734ceec28e36f3b (diff) | |
download | chromium_src-c8fcfcf6986774d91b391ffa1b9ef8df49a2e815.zip chromium_src-c8fcfcf6986774d91b391ffa1b9ef8df49a2e815.tar.gz chromium_src-c8fcfcf6986774d91b391ffa1b9ef8df49a2e815.tar.bz2 |
Move DeviceMotionAndOrientation off Activity Context
- Get rid of WeakContext since it ties to the Activity and is only used in one place.
- Move DeviceMotionAndOrientation onto the Application context
BUG=341231
Review URL: https://codereview.chromium.org/167803002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252629 0039d316-1c4b-4281-b951-d872f2087c98
5 files changed, 27 insertions, 60 deletions
diff --git a/base/android/java/src/org/chromium/base/WeakContext.java b/base/android/java/src/org/chromium/base/WeakContext.java deleted file mode 100644 index c4fdb01..0000000 --- a/base/android/java/src/org/chromium/base/WeakContext.java +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.base; - -import android.content.Context; - -import java.lang.ref.WeakReference; -import java.util.concurrent.Callable; - -/** - * Holds a WeakReference to Context to allow it to be GC'd. - * Also provides utility functions to getSystemService from the UI or any - * other thread (may return null, if the Context has been nullified). - */ -public class WeakContext { - private static WeakReference<Context> sWeakContext; - - public static void initializeWeakContext(final Context context) { - sWeakContext = new WeakReference<Context>(context); - } - - public static Context getContext() { - return sWeakContext.get(); - } - - // Returns a system service. May be called from any thread. - // If necessary, it will send a message to the main thread to acquire the - // service, and block waiting for it to complete. - // May return null if context is no longer available. - public static Object getSystemService(final String name) { - final Context context = sWeakContext.get(); - if (context == null) { - return null; - } - if (ThreadUtils.runningOnUiThread()) { - return context.getSystemService(name); - } - return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Object>() { - @Override - public Object call() { - return context.getSystemService(name); - } - }); - } -} diff --git a/content/browser/device_orientation/sensor_manager_android.cc b/content/browser/device_orientation/sensor_manager_android.cc index ef516d1..cec2c5e 100644 --- a/content/browser/device_orientation/sensor_manager_android.cc +++ b/content/browser/device_orientation/sensor_manager_android.cc @@ -32,7 +32,9 @@ SensorManagerAndroid::SensorManagerAndroid() is_orientation_buffer_ready_(false) { memset(received_motion_data_, 0, sizeof(received_motion_data_)); device_orientation_.Reset( - Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); + Java_DeviceMotionAndOrientation_getInstance( + AttachCurrentThread(), + base::android::GetApplicationContext())); } SensorManagerAndroid::~SensorManagerAndroid() { diff --git a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java index 71cb9cd..04b3175 100644 --- a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java +++ b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java @@ -56,7 +56,6 @@ import org.chromium.base.JNINamespace; import org.chromium.base.ObserverList; import org.chromium.base.ObserverList.RewindableIterator; import org.chromium.base.TraceEvent; -import org.chromium.base.WeakContext; import org.chromium.content.R; import org.chromium.content.browser.ContentViewGestureHandler.MotionEventDelegate; import org.chromium.content.browser.accessibility.AccessibilityInjector; @@ -451,7 +450,6 @@ public class ContentViewCore public ContentViewCore(Context context) { mContext = context; - WeakContext.initializeWeakContext(context); HeapStatsLogger.init(mContext.getApplicationContext()); mAdapterInputConnectionFactory = new AdapterInputConnectionFactory(); diff --git a/content/public/android/java/src/org/chromium/content/browser/DeviceMotionAndOrientation.java b/content/public/android/java/src/org/chromium/content/browser/DeviceMotionAndOrientation.java index 0551361..be73f8c 100644 --- a/content/public/android/java/src/org/chromium/content/browser/DeviceMotionAndOrientation.java +++ b/content/public/android/java/src/org/chromium/content/browser/DeviceMotionAndOrientation.java @@ -18,11 +18,12 @@ import com.google.common.annotations.VisibleForTesting; import org.chromium.base.CalledByNative; import org.chromium.base.CollectionUtil; import org.chromium.base.JNINamespace; -import org.chromium.base.WeakContext; +import org.chromium.base.ThreadUtils; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.Callable; /** * Android implementation of the device motion and orientation APIs. @@ -36,6 +37,9 @@ class DeviceMotionAndOrientation implements SensorEventListener { private Thread mThread; private Handler mHandler; + // A reference to the application context in order to acquire the SensorService. + private final Context mAppContext; + // The lock to access the mHandler. private final Object mHandlerLock = new Object(); @@ -76,7 +80,8 @@ class DeviceMotionAndOrientation implements SensorEventListener { boolean mDeviceMotionIsActive = false; boolean mDeviceOrientationIsActive = false; - protected DeviceMotionAndOrientation() { + protected DeviceMotionAndOrientation(Context context) { + mAppContext = context.getApplicationContext(); } /** @@ -311,8 +316,15 @@ class DeviceMotionAndOrientation implements SensorEventListener { if (mSensorManagerProxy != null) { return mSensorManagerProxy; } - SensorManager sensorManager = (SensorManager) WeakContext.getSystemService( - Context.SENSOR_SERVICE); + + SensorManager sensorManager = ThreadUtils.runOnUiThreadBlockingNoException( + new Callable<SensorManager>() { + @Override + public SensorManager call() { + return (SensorManager) mAppContext.getSystemService(Context.SENSOR_SERVICE); + } + }); + if (sensorManager != null) { mSensorManagerProxy = new SensorManagerProxyImpl(sensorManager); } @@ -428,10 +440,10 @@ class DeviceMotionAndOrientation implements SensorEventListener { } @CalledByNative - static DeviceMotionAndOrientation getInstance() { + static DeviceMotionAndOrientation getInstance(Context appContext) { synchronized (sSingletonLock) { if (sSingleton == null) { - sSingleton = new DeviceMotionAndOrientation(); + sSingleton = new DeviceMotionAndOrientation(appContext); } return sSingleton; } diff --git a/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java b/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java index 5d3d9e3..95a28ab 100644 --- a/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java +++ b/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java @@ -4,6 +4,7 @@ package org.chromium.content.browser; +import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEventListener; import android.hardware.SensorManager; @@ -26,7 +27,7 @@ public class DeviceMotionAndOrientationTest extends AndroidTestCase { public void setUp() throws Exception { super.setUp(); mMockSensorManager = new MockSensorManager(); - mDeviceMotionAndOrientation = DeviceMotionAndOrientationForTests.getInstance(); + mDeviceMotionAndOrientation = DeviceMotionAndOrientationForTests.getInstance(getContext()); mDeviceMotionAndOrientation.setSensorManagerProxy(mMockSensorManager); } @@ -320,11 +321,12 @@ public class DeviceMotionAndOrientationTest extends AndroidTestCase { private double value3 = 0; private String mCalls = ""; - private DeviceMotionAndOrientationForTests(){ + private DeviceMotionAndOrientationForTests(Context context) { + super(context); } - static DeviceMotionAndOrientationForTests getInstance() { - return new DeviceMotionAndOrientationForTests(); + static DeviceMotionAndOrientationForTests getInstance(Context context) { + return new DeviceMotionAndOrientationForTests(context); } private void verifyValues(double v1, double v2, double v3) { |