Merge "Remove autovalue from Event."
diff --git a/nearby/service/java/com/android/server/nearby/NearbyService.java b/nearby/service/java/com/android/server/nearby/NearbyService.java
index f471a58..dcd802e 100644
--- a/nearby/service/java/com/android/server/nearby/NearbyService.java
+++ b/nearby/service/java/com/android/server/nearby/NearbyService.java
@@ -20,6 +20,7 @@
import android.util.Log;
import com.android.server.SystemService;
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
import com.android.server.nearby.fastpair.FastPairManager;
/**
@@ -30,16 +31,19 @@
public class NearbyService extends SystemService {
private static final String TAG = "NearbyService";
private static final boolean DBG = true;
- private Context mContext;
-
private final NearbyServiceImpl mImpl;
private final FastPairManager mFastPairManager;
+ private LocatorContextWrapper mLocatorContextWrapper;
+
+
+
public NearbyService(Context contextBase) {
super(contextBase);
- mContext = contextBase;
mImpl = new NearbyServiceImpl(contextBase);
- mFastPairManager = new FastPairManager(contextBase);
+ mLocatorContextWrapper = new LocatorContextWrapper(contextBase, null);
+ mFastPairManager = new FastPairManager(mLocatorContextWrapper);
+
}
@Override
diff --git a/nearby/service/java/com/android/server/nearby/common/locator/Locator.java b/nearby/service/java/com/android/server/nearby/common/locator/Locator.java
index 40d77df..f8b43a6 100644
--- a/nearby/service/java/com/android/server/nearby/common/locator/Locator.java
+++ b/nearby/service/java/com/android/server/nearby/common/locator/Locator.java
@@ -135,7 +135,6 @@
public <T> T getOptional(Class<T> type) {
Locator locator = this;
do {
-
T instance = locator.getInstance(type);
if (instance != null) {
return instance;
@@ -227,6 +226,17 @@
}
/**
+ * Find the first locator from the context wrapper.
+ */
+ public static <T> T getFromContextWrapper(LocatorContextWrapper wrapper, Class<T> type) {
+ Locator locator = wrapper.getLocator();
+ if (locator == null) {
+ throw new IllegalStateException("No locator found in context wrapper");
+ }
+ return locator.get(type);
+ }
+
+ /**
* Finds the first locator, then searches the chain of locators for a binding for the given
* type.
* Returns null if no binding was found.
diff --git a/nearby/service/java/com/android/server/nearby/common/locator/LocatorContextWrapper.java b/nearby/service/java/com/android/server/nearby/common/locator/LocatorContextWrapper.java
index 9dddcdb..03df33f 100644
--- a/nearby/service/java/com/android/server/nearby/common/locator/LocatorContextWrapper.java
+++ b/nearby/service/java/com/android/server/nearby/common/locator/LocatorContextWrapper.java
@@ -25,9 +25,11 @@
*/
public class LocatorContextWrapper extends ContextWrapper implements LocatorContext {
private final Locator mLocator;
+ private final Context mContext;
/** Constructs a context wrapper with a Locator linked to the passed locator. */
public LocatorContextWrapper(Context context, @Nullable Locator parentLocator) {
super(context);
+ mContext = context;
// Assigning under initialization object, but it's safe, since locator is used lazily.
this.mLocator = new Locator(this, parentLocator);
}
@@ -41,6 +43,13 @@
this(context, Locator.findLocator(context));
}
+ /**
+ * Get the context of the context wrapper.
+ */
+ public Context getContext() {
+ return mContext;
+ }
+
@Override
public Locator getLocator() {
return mLocator;
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
index d46b488..1487d4b 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
@@ -22,27 +22,38 @@
import android.content.IntentFilter;
import android.util.Log;
+import com.android.server.nearby.common.locator.Locator;
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
+import com.android.server.nearby.fastpair.cache.FastPairCacheManager;
+
/**
* FastPairManager is the class initiated in nearby service to handle Fast Pair related
* work.
*/
public class FastPairManager {
- Context mContext;
+ LocatorContextWrapper mLocatorContextWrapper;
IntentFilter mIntentFilter;
+ Locator mLocator;
private BroadcastReceiver mScreenBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("FastPairService", " screen on");
+ // STOPSHIP(b/202335820): Remove this logic in prod.
+ Locator.getFromContextWrapper(mLocatorContextWrapper, FastPairCacheManager.class)
+ .printLog();
} else {
Log.d("FastPairService", " screen off");
}
}
};
- public FastPairManager(Context context) {
- mContext = context;
+
+ public FastPairManager(LocatorContextWrapper contextWrapper) {
+ mLocatorContextWrapper = contextWrapper;
mIntentFilter = new IntentFilter();
+ mLocator = mLocatorContextWrapper.getLocator();
+ mLocator.bind(new FastPairModule());
}
/**
@@ -51,13 +62,16 @@
public void initiate() {
mIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
- mContext.registerReceiver(mScreenBroadcastReceiver, mIntentFilter);
+ mLocatorContextWrapper.getContext()
+ .registerReceiver(mScreenBroadcastReceiver, mIntentFilter);
+
+ Locator.getFromContextWrapper(mLocatorContextWrapper, FastPairCacheManager.class);
}
/**
* Function to free up fast pair resource.
*/
public void cleanUp() {
- mContext.unregisterReceiver(mScreenBroadcastReceiver);
+ mLocatorContextWrapper.getContext().unregisterReceiver(mScreenBroadcastReceiver);
}
}
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairModule.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairModule.java
new file mode 100644
index 0000000..59edac0
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairModule.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.fastpair;
+
+import android.content.Context;
+
+import com.android.server.nearby.common.locator.Locator;
+import com.android.server.nearby.common.locator.Module;
+import com.android.server.nearby.fastpair.cache.FastPairCacheManager;
+
+/**
+ * Module that associates all of the fast pair related singleton class
+ */
+public class FastPairModule extends Module {
+ /**
+ * Initiate the class that needs to be singleton.
+ */
+ @Override
+ public void configure(Context context, Class<?> type, Locator locator) {
+ if (type.equals(FastPairCacheManager.class)) {
+ locator.bind(FastPairCacheManager.class, new FastPairCacheManager(context));
+ }
+
+ }
+
+ /**
+ * Clean up the singleton classes.
+ */
+ @Override
+ public void destroy(Context context, Class<?> type, Object instance) {
+ super.destroy(context, type, instance);
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java b/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java
new file mode 100644
index 0000000..30871eb
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/fastpair/cache/FastPairCacheManager.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.fastpair.cache;
+
+import android.content.Context;
+import android.util.Log;
+
+/**
+ * Save FastPair device info to database to avoid multiple requesting.
+ */
+public class FastPairCacheManager {
+ public FastPairCacheManager(Context context) {
+ }
+
+ /**
+ * Test function to verify FastPairCacheManager setup.
+ */
+ public void printLog() {
+ Log.d("FastPairCacheManager", "print log");
+ }
+}
diff --git a/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java b/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java
index 429fc89..bbf8cdf 100644
--- a/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java
+++ b/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java
@@ -22,6 +22,8 @@
import android.content.Context;
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
+
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@@ -31,11 +33,13 @@
public class FastPairManagerTest {
private FastPairManager mFastPairManager;
@Mock private Context mContext;
+ private LocatorContextWrapper mLocatorContextWrapper;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
- mFastPairManager = new FastPairManager(mContext);
+ mLocatorContextWrapper = new LocatorContextWrapper(mContext);
+ mFastPairManager = new FastPairManager(mLocatorContextWrapper);
}
@Test