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 17 package com.android.bluetooth.le_audio; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import com.android.bluetooth.Utils; 23 import com.android.internal.annotations.VisibleForTesting; 24 25 /** Factory class for object initialization to help with unit testing */ 26 public class LeAudioObjectsFactory { 27 private static final String TAG = LeAudioObjectsFactory.class.getSimpleName(); 28 29 private static LeAudioObjectsFactory sInstance; 30 private static final Object INSTANCE_LOCK = new Object(); 31 LeAudioObjectsFactory()32 private LeAudioObjectsFactory() {} 33 34 /** 35 * Get the singleton instance of object factory 36 * 37 * @return the singleton instance, guaranteed not null 38 */ getInstance()39 public static LeAudioObjectsFactory getInstance() { 40 synchronized (INSTANCE_LOCK) { 41 if (sInstance == null) { 42 sInstance = new LeAudioObjectsFactory(); 43 } 44 } 45 return sInstance; 46 } 47 48 /** 49 * Allow unit tests to substitute {@link LeAudioObjectsFactory} with a test instance 50 * 51 * @param objectsFactory a test instance of the {@link LeAudioObjectsFactory} 52 */ 53 @VisibleForTesting setInstanceForTesting(LeAudioObjectsFactory objectsFactory)54 public static void setInstanceForTesting(LeAudioObjectsFactory objectsFactory) { 55 Utils.enforceInstrumentationTestMode(); 56 synchronized (INSTANCE_LOCK) { 57 Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory); 58 sInstance = objectsFactory; 59 } 60 } 61 62 /** 63 * Get a {@link LeAudioTmapGattServer} object 64 * 65 * @param context local context 66 */ getTmapGattServer(Context context)67 public LeAudioTmapGattServer getTmapGattServer(Context context) { 68 return new LeAudioTmapGattServer( 69 new LeAudioTmapGattServer.BluetoothGattServerProxy(context)); 70 } 71 } 72