1 /* 2 * Copyright (C) 2020 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.google.android.car.kitchensink.audio; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.car.Car; 22 import android.car.media.CarAudioManager; 23 import android.content.Context; 24 import android.media.AudioManager; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.util.Log; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 33 import androidx.fragment.app.Fragment; 34 import androidx.viewpager.widget.ViewPager; 35 36 import com.google.android.car.kitchensink.R; 37 import com.google.android.material.tabs.TabLayout; 38 39 import java.util.List; 40 41 public class CarAudioInputTestFragment extends Fragment { 42 private static final String TAG = "CAR.AUDIO.INPUT.KS"; 43 private static final boolean DEBUG = true; 44 45 46 private Handler mHandler; 47 private Context mContext; 48 49 private Car mCar; 50 private AudioManager mAudioManager; 51 private CarAudioManager mCarAudioManager; 52 private TabLayout mZonesTabLayout; 53 private CarAudioZoneInputTabAdapter mInputAudioZoneAdapter; 54 private ViewPager mViewPager; 55 connectCar()56 private void connectCar() { 57 mContext = getContext(); 58 mHandler = new Handler(Looper.getMainLooper()); 59 mCar = Car.createCar(mContext, /* handler= */ null, 60 Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER, mCarServiceLifecycleListener); 61 } 62 63 private Car.CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> { 64 if (!ready) { 65 if (DEBUG) { 66 Log.d(TAG, "Disconnect from Car Service"); 67 } 68 return; 69 } 70 if (DEBUG) { 71 Log.d(TAG, "Connected to Car Service"); 72 } 73 mCarAudioManager = (CarAudioManager) car.getCarManager(Car.AUDIO_SERVICE); 74 75 mAudioManager = mContext.getSystemService(AudioManager.class); 76 }; 77 78 79 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)80 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { 81 Log.i(TAG, "onCreateView"); 82 View view = inflater.inflate(R.layout.audio_input, container, false); 83 connectCar(); 84 return view; 85 } 86 87 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)88 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 89 Log.i(TAG, "onViewCreated "); 90 mZonesTabLayout = view.findViewById(R.id.zones_input_tab); 91 mViewPager = (ViewPager) view.findViewById(R.id.zones_input_view_pager); 92 93 mInputAudioZoneAdapter = new CarAudioZoneInputTabAdapter(getChildFragmentManager()); 94 mViewPager.setAdapter(mInputAudioZoneAdapter); 95 initInputInfo(); 96 mZonesTabLayout.setupWithViewPager(mViewPager); 97 } 98 99 @Override onDestroyView()100 public void onDestroyView() { 101 Log.i(TAG, "onDestroyView"); 102 103 if (mCar != null && mCar.isConnected()) { 104 mCar.disconnect(); 105 mCar = null; 106 } 107 super.onDestroyView(); 108 } 109 initInputInfo()110 private void initInputInfo() { 111 if (!mCarAudioManager.isDynamicRoutingEnabled()) { 112 return; 113 } 114 List<Integer> audioZoneList = mCarAudioManager.getAudioZoneIds(); 115 for (int audioZoneId : audioZoneList) { 116 if (mCarAudioManager.getInputDevicesForZoneId(audioZoneId).isEmpty()) { 117 if (DEBUG) { 118 Log.d(TAG, "Audio Zone " + audioZoneId + " has no input devices"); 119 } 120 continue; 121 } 122 addAudioZoneInputDevices(audioZoneId); 123 } 124 mInputAudioZoneAdapter.notifyDataSetChanged(); 125 } 126 addAudioZoneInputDevices(int audioZoneId)127 private void addAudioZoneInputDevices(int audioZoneId) { 128 String title = "Audio Zone " + audioZoneId; 129 if (DEBUG) { 130 Log.d(TAG, title + " adding devices"); 131 } 132 CarAudioZoneInputFragment fragment = new CarAudioZoneInputFragment(audioZoneId, 133 mCarAudioManager, mAudioManager); 134 135 mZonesTabLayout.addTab(mZonesTabLayout.newTab().setText(title)); 136 mInputAudioZoneAdapter.addFragment(fragment, title); 137 } 138 } 139