Add fastpair manager to nearby to handle fast pair

Test: unit test
Bug: 202335820
Change-Id: I14fc4a73f36e46125f74d37cf14fa7909b7a6eab
diff --git a/nearby/service/java/com/android/server/nearby/NearbyService.java b/nearby/service/java/com/android/server/nearby/NearbyService.java
index 0cb3c75..f471a58 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.fastpair.FastPairManager;
 
 /**
  * Service implementing nearby functionality. The actual implementation is delegated to
@@ -29,12 +30,16 @@
 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;
 
     public NearbyService(Context contextBase) {
         super(contextBase);
+        mContext = contextBase;
         mImpl = new NearbyServiceImpl(contextBase);
+        mFastPairManager = new FastPairManager(contextBase);
     }
 
     @Override
@@ -42,9 +47,12 @@
         if (DBG) {
             Log.d(TAG, "Publishing NearbyService");
         }
+        mFastPairManager.initiate();
     }
 
     @Override
     public void onBootPhase(int phase) {
     }
-}
+
+
+}
\ No newline at end of file
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
new file mode 100644
index 0000000..d46b488
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairManager.java
@@ -0,0 +1,63 @@
+/*
+ * 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.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.util.Log;
+
+/**
+ * FastPairManager is the class initiated in nearby service to handle Fast Pair related
+ * work.
+ */
+public class FastPairManager {
+    Context mContext;
+    IntentFilter mIntentFilter;
+    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");
+            } else {
+                Log.d("FastPairService", " screen off");
+            }
+        }
+    };
+
+    public FastPairManager(Context context) {
+        mContext = context;
+        mIntentFilter = new IntentFilter();
+    }
+
+    /**
+     * Function called when nearby service start.
+     */
+    public void initiate() {
+        mIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
+        mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
+        mContext.registerReceiver(mScreenBroadcastReceiver, mIntentFilter);
+    }
+
+    /**
+     * Function to free up fast pair resource.
+     */
+    public void cleanUp() {
+        mContext.unregisterReceiver(mScreenBroadcastReceiver);
+    }
+}
diff --git a/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java b/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java
new file mode 100644
index 0000000..429fc89
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/fastpair/FastPairManagerTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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 static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class FastPairManagerTest {
+    private FastPairManager mFastPairManager;
+    @Mock private Context mContext;
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        mFastPairManager = new FastPairManager(mContext);
+    }
+
+    @Test
+    public void testFastPairInit() {
+        mFastPairManager.initiate();
+
+        verify(mContext, times(1)).registerReceiver(any(), any());
+    }
+
+    @Test
+    public void testFastPairCleanUp() {
+        mFastPairManager.cleanUp();
+
+        verify(mContext, times(1)).unregisterReceiver(any());
+    }
+}