Add logic to NearbyService

Test: Locally tested and read Ble logs. Unit test will be added.
Bug: 189954300
Change-Id: I84509bc69ad010d3f5615760c9ddc9b460b37152
diff --git a/nearby/service/java/com/android/server/nearby/NearbyService.java b/nearby/service/java/com/android/server/nearby/NearbyService.java
index cc19199..12eea8b 100644
--- a/nearby/service/java/com/android/server/nearby/NearbyService.java
+++ b/nearby/service/java/com/android/server/nearby/NearbyService.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2021 The Android Open Source Project
+ * 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.
@@ -28,21 +28,20 @@
  * Service implementing nearby functionality. The actual implementation is delegated to
  * {@link NearbyServiceImpl}.
  */
-// TODO(189954300): Implement nearby service.
 public class NearbyService extends SystemService {
 
     public static final String TAG = "NearbyService";
     private static final boolean DBG = true;
-    private final NearbyServiceImpl mImpl;
-    private final Context mContext;
-    private final FastPairManager mFastPairManager;
 
+    private final Context mContext;
+    private final NearbyServiceImpl mImpl;
+    private final FastPairManager mFastPairManager;
     private LocatorContextWrapper mLocatorContextWrapper;
 
     public NearbyService(Context contextBase) {
         super(contextBase);
-        mImpl = new NearbyServiceImpl(contextBase);
         mContext = contextBase;
+        mImpl = new NearbyServiceImpl(contextBase);
         mLocatorContextWrapper = new LocatorContextWrapper(contextBase, null);
         mFastPairManager = new FastPairManager(mLocatorContextWrapper);
     }
@@ -60,6 +59,9 @@
     public void onBootPhase(int phase) {
         if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
             onSystemThirdPartyAppsCanStart();
+        } else if (phase == PHASE_BOOT_COMPLETED) {
+            // the nearby service must be functioning after this boot phase.
+            mImpl.onSystemReady();
         }
     }
 
diff --git a/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java b/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
index c84169e..59ecae1 100644
--- a/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
+++ b/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2021 The Android Open Source Project
+ * 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.
@@ -16,24 +16,92 @@
 
 package com.android.server.nearby;
 
+import static com.android.server.nearby.NearbyService.TAG;
+
+import android.annotation.Nullable;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothManager;
+import android.content.BroadcastReceiver;
 import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.nearby.INearbyManager;
 import android.nearby.IScanListener;
 import android.nearby.ScanRequest;
+import android.util.Log;
+
+import com.android.server.nearby.injector.Injector;
+import com.android.server.nearby.provider.DiscoveryProviderManager;
 
 /**
- * Implementation of {@link NearbyService}.
+ * Implementation of {@link com.android.server.nearby.NearbyService}.
  */
 public class NearbyServiceImpl extends INearbyManager.Stub {
 
+    private final Context mContext;
+    private final SystemInjector mSystemInjector;
+    private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int state = intent
+                    .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
+            if (state == BluetoothAdapter.STATE_ON) {
+                if (mSystemInjector != null) {
+                    // Have to do this logic in listener. Even during PHASE_BOOT_COMPLETED
+                    // phase, BluetoothAdapter is not null, the BleScanner is null.
+                    Log.v(TAG, "Initiating BluetoothAdapter when Bluetooth is turned on.");
+                    mSystemInjector.onBluetoothReady();
+                }
+            }
+        }
+    };
+    private DiscoveryProviderManager mProviderManager;
+
     public NearbyServiceImpl(Context context) {
+        mContext = context;
+        mSystemInjector = new SystemInjector(context);
+        mProviderManager = new DiscoveryProviderManager(context, mSystemInjector);
     }
 
     @Override
     public void registerScanListener(ScanRequest scanRequest, IScanListener listener) {
+        mProviderManager.registerScanListener(scanRequest, listener);
     }
 
     @Override
     public void unregisterScanListener(IScanListener listener) {
+        mProviderManager.unregisterScanListener(listener);
+    }
+
+    void onSystemReady() {
+        mContext.registerReceiver(mBluetoothReceiver,
+                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
+    }
+
+    private static final class SystemInjector implements Injector {
+        private final Context mContext;
+        @Nullable
+        private BluetoothAdapter mBluetoothAdapter;
+
+        SystemInjector(Context context) {
+            mContext = context;
+        }
+
+        @Override
+        @Nullable
+        public BluetoothAdapter getBluetoothAdapter() {
+            return mBluetoothAdapter;
+        }
+
+        synchronized void onBluetoothReady() {
+            if (mBluetoothAdapter != null) {
+                return;
+            }
+            BluetoothManager manager = mContext.getSystemService(BluetoothManager.class);
+            if (manager == null) {
+                return;
+            }
+            mBluetoothAdapter = manager.getAdapter();
+        }
     }
 }
diff --git a/nearby/service/java/com/android/server/nearby/injector/Injector.java b/nearby/service/java/com/android/server/nearby/injector/Injector.java
index 68bec77..926df8c 100644
--- a/nearby/service/java/com/android/server/nearby/injector/Injector.java
+++ b/nearby/service/java/com/android/server/nearby/injector/Injector.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package java.com.android.server.nearby.injector;
+package com.android.server.nearby.injector;
 
 import android.bluetooth.BluetoothAdapter;