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