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