1 /* 2 * Copyright 2021 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 17 package com.android.libraries.testing.deviceshadower.internal.bluetooth.connection; 18 19 import java.io.FileDescriptor; 20 import java.util.Map; 21 import java.util.concurrent.ConcurrentHashMap; 22 23 /** 24 * Factory which creates {@link FileDescriptor} given an MAC address. Each MAC address can have many 25 * FileDescriptor but each FileDescriptor only maps to one MAC address. 26 */ 27 public class FileDescriptorFactory { 28 29 private static FileDescriptorFactory sInstance = null; 30 getInstance()31 public static synchronized FileDescriptorFactory getInstance() { 32 if (sInstance == null) { 33 sInstance = new FileDescriptorFactory(); 34 } 35 return sInstance; 36 } 37 reset()38 public static synchronized void reset() { 39 sInstance = null; 40 } 41 42 private final Map<FileDescriptor, String> mAddressMap; 43 FileDescriptorFactory()44 private FileDescriptorFactory() { 45 mAddressMap = new ConcurrentHashMap<>(); 46 } 47 createFileDescriptor(String address)48 public FileDescriptor createFileDescriptor(String address) { 49 FileDescriptor fd = new FileDescriptor(); 50 mAddressMap.put(fd, address); 51 return fd; 52 } 53 getAddress(FileDescriptor fd)54 public String getAddress(FileDescriptor fd) { 55 return mAddressMap.get(fd); 56 } 57 } 58