1 /* 2 * Copyright (C) 2019 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.car.dialer.livedata; 18 19 import android.bluetooth.BluetoothHeadsetClient; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 25 import androidx.lifecycle.LiveData; 26 27 import com.android.car.dialer.log.L; 28 import com.android.car.dialer.telecom.UiCallManager; 29 30 /** 31 * Provides the current connecting audio route. 32 */ 33 public class AudioRouteLiveData extends LiveData<Integer> { 34 private static final String TAG = "CD.AudioRouteLiveData"; 35 36 private final Context mContext; 37 private final IntentFilter mAudioRouteChangeFilter; 38 39 private final BroadcastReceiver mAudioRouteChangeReceiver = new BroadcastReceiver() { 40 @Override 41 public void onReceive(Context context, Intent intent) { 42 updateAudioRoute(); 43 } 44 }; 45 AudioRouteLiveData(Context context)46 public AudioRouteLiveData(Context context) { 47 mContext = context; 48 mAudioRouteChangeFilter = 49 new IntentFilter(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED); 50 } 51 52 @Override onActive()53 protected void onActive() { 54 updateAudioRoute(); 55 mContext.registerReceiver(mAudioRouteChangeReceiver, mAudioRouteChangeFilter); 56 } 57 58 @Override onInactive()59 protected void onInactive() { 60 mContext.unregisterReceiver(mAudioRouteChangeReceiver); 61 } 62 updateAudioRoute()63 private void updateAudioRoute() { 64 int audioRoute = UiCallManager.get().getAudioRoute(); 65 if (getValue() == null || audioRoute != getValue()) { 66 L.d(TAG, "updateAudioRoute to %s", audioRoute); 67 setValue(audioRoute); 68 } 69 } 70 } 71