Start NearbyService from connectivity initializer

Remove NearbyService as it is no longer needed as a wrapper, replace it
with what was NearbyServiceImpl, and include all Nearby service
initialization inside the service so it exposes a simple interface for
the connectivity initializer.

Bug: 189355156
Test: m; booted, see service started
Change-Id: Id52ae70ea3f535712eb264618d1255db9368fcca
diff --git a/nearby/service/java/com/android/server/nearby/NearbyService.java b/nearby/service/java/com/android/server/nearby/NearbyService.java
deleted file mode 100644
index b851c72..0000000
--- a/nearby/service/java/com/android/server/nearby/NearbyService.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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;
-
-import android.content.Context;
-import android.util.Log;
-
-import com.android.server.SystemService;
-import com.android.server.nearby.common.locator.LocatorContextWrapper;
-import com.android.server.nearby.fastpair.FastPairManager;
-import com.android.server.nearby.provider.FastPairDataProvider;
-
-/**
- * Service implementing nearby functionality. The actual implementation is delegated to
- * {@link NearbyServiceImpl}.
- */
-public class NearbyService extends SystemService {
-
-    public static final String TAG = "NearbyService";
-    private static final boolean DBG = true;
-
-    private final 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);
-        mLocatorContextWrapper = new LocatorContextWrapper(contextBase, null);
-        mFastPairManager = new FastPairManager(mLocatorContextWrapper);
-    }
-
-    @Override
-    public void onStart() {
-        if (DBG) {
-            Log.d(TAG, "Publishing NearbyService");
-        }
-        publishBinderService(Context.NEARBY_SERVICE, mImpl);
-    }
-
-    @Override
-    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();
-            mFastPairManager.initiate();
-        }
-    }
-
-    private void onSystemThirdPartyAppsCanStart() {
-        // Ensures that a fast pair data provider exists which will work in direct boot.
-        FastPairDataProvider.init(mContext);
-    }
-}
diff --git a/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java b/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
index 04e73a5..17261b0 100644
--- a/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
+++ b/nearby/service/java/com/android/server/nearby/NearbyServiceImpl.java
@@ -16,7 +16,8 @@
 
 package com.android.server.nearby;
 
-import static com.android.server.nearby.NearbyService.TAG;
+import static com.android.server.SystemService.PHASE_BOOT_COMPLETED;
+import static com.android.server.SystemService.PHASE_THIRD_PARTY_APPS_CAN_START;
 
 import android.annotation.Nullable;
 import android.bluetooth.BluetoothAdapter;
@@ -30,16 +31,21 @@
 import android.nearby.ScanRequest;
 import android.util.Log;
 
+import com.android.server.nearby.common.locator.LocatorContextWrapper;
+import com.android.server.nearby.fastpair.FastPairManager;
 import com.android.server.nearby.injector.Injector;
 import com.android.server.nearby.provider.DiscoveryProviderManager;
+import com.android.server.nearby.provider.FastPairDataProvider;
 
 /**
- * Implementation of {@link com.android.server.nearby.NearbyService}.
+ * Service implementing nearby functionality.
  */
 public class NearbyServiceImpl extends INearbyManager.Stub {
+    public static final String TAG = "NearbyService";
 
     private final Context mContext;
     private final SystemInjector mSystemInjector;
+    private final FastPairManager mFastPairManager;
     private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -61,6 +67,8 @@
         mContext = context;
         mSystemInjector = new SystemInjector(context);
         mProviderManager = new DiscoveryProviderManager(context, mSystemInjector);
+        final LocatorContextWrapper lcw = new LocatorContextWrapper(context, null);
+        mFastPairManager = new FastPairManager(lcw);
     }
 
     @Override
@@ -73,10 +81,25 @@
         mProviderManager.unregisterScanListener(listener);
     }
 
-    void onSystemReady() {
-        mSystemInjector.initializeBluetoothAdapter();
-        mContext.registerReceiver(mBluetoothReceiver,
-                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
+    /**
+     * Called by the service initializer.
+     *
+     * {@see com.android.server.SystemService#onBootPhase}.
+     */
+    public void onBootPhase(int phase) {
+        switch (phase) {
+            case PHASE_THIRD_PARTY_APPS_CAN_START:
+                // Ensures that a fast pair data provider exists which will work in direct boot.
+                FastPairDataProvider.init(mContext);
+                break;
+            case PHASE_BOOT_COMPLETED:
+                // The nearby service must be functioning after this boot phase.
+                mSystemInjector.initializeBluetoothAdapter();
+                mContext.registerReceiver(mBluetoothReceiver,
+                        new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
+                mFastPairManager.initiate();
+                break;
+        }
     }
 
     private static final class SystemInjector implements Injector {