1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.server.uwb.discovery;
17 
18 import android.content.AttributionSource;
19 import android.content.Context;
20 
21 import androidx.annotation.WorkerThread;
22 
23 import com.android.server.uwb.discovery.DiscoveryAdvertiseProvider.DiscoveryAdvertiseCallback;
24 import com.android.server.uwb.discovery.DiscoveryScanProvider.DiscoveryScanCallback;
25 import com.android.server.uwb.discovery.ble.BleDiscoveryAdvertiseProvider;
26 import com.android.server.uwb.discovery.ble.BleDiscoveryScanProvider;
27 import com.android.server.uwb.discovery.info.DiscoveryInfo;
28 
29 import java.util.concurrent.Executor;
30 
31 /** Factory for creating DiscoveryProvider. */
32 @WorkerThread
33 public class DiscoveryProviderFactory {
DiscoveryProviderFactory()34     private DiscoveryProviderFactory() {}
35 
36     /**
37      * Create a DiscoveryScanProvider.
38      *
39      * @param attributionSource Attribution Source.
40      * @param context Context.
41      * @param executor Executor.
42      * @param discoveryInfo Info of the discovery request.
43      * @param discoveryScanCallback callback for discovery scan events.
44      */
createScanner( AttributionSource attributionSource, Context context, Executor executor, DiscoveryInfo discoveryInfo, DiscoveryScanCallback discoveryScanCallback)45     public static DiscoveryScanProvider createScanner(
46             AttributionSource attributionSource,
47             Context context,
48             Executor executor,
49             DiscoveryInfo discoveryInfo,
50             DiscoveryScanCallback discoveryScanCallback)
51             throws AssertionError {
52 
53         switch (discoveryInfo.transportType) {
54             case BLE:
55                 return new BleDiscoveryScanProvider(
56                         attributionSource,
57                         context,
58                         executor,
59                         discoveryInfo.scanInfo.get(),
60                         discoveryScanCallback);
61         }
62         return null;
63     }
64 
65     /**
66      * Create a DiscoveryAdvertiseProvider.
67      *
68      * @param attributionSource Attribution Source.
69      * @param context Context.
70      * @param executor Executor.
71      * @param discoveryInfo Info of the discovery request.
72      * @param discoveryAdvertiseCallback callback for discovery advertise events.
73      */
createAdvertiser( AttributionSource attributionSource, Context context, Executor executor, DiscoveryInfo discoveryInfo, DiscoveryAdvertiseCallback discoveryAdvertiseCallback)74     public static DiscoveryAdvertiseProvider createAdvertiser(
75             AttributionSource attributionSource,
76             Context context,
77             Executor executor,
78             DiscoveryInfo discoveryInfo,
79             DiscoveryAdvertiseCallback discoveryAdvertiseCallback)
80             throws AssertionError {
81 
82         switch (discoveryInfo.transportType) {
83             case BLE:
84                 return new BleDiscoveryAdvertiseProvider(
85                         attributionSource,
86                         context,
87                         executor,
88                         discoveryInfo.advertiseInfo.get(),
89                         discoveryAdvertiseCallback);
90         }
91         return null;
92     }
93 }
94