1 /* 2 * Copyright (C) 2016 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.incallui.audioroute; 18 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.content.res.ColorStateList; 22 import android.graphics.PorterDuff.Mode; 23 import android.os.Bundle; 24 import android.support.annotation.Nullable; 25 import android.support.design.widget.BottomSheetDialogFragment; 26 import android.telecom.CallAudioState; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.view.ViewGroup; 31 import android.view.WindowManager; 32 import android.widget.TextView; 33 import com.android.dialer.common.FragmentUtils; 34 import com.android.dialer.common.LogUtil; 35 36 /** Shows picker for audio routes */ 37 public class AudioRouteSelectorDialogFragment extends BottomSheetDialogFragment { 38 39 private static final String ARG_AUDIO_STATE = "audio_state"; 40 41 /** Called when an audio route is picked */ 42 public interface AudioRouteSelectorPresenter { onAudioRouteSelected(int audioRoute)43 void onAudioRouteSelected(int audioRoute); 44 } 45 newInstance(CallAudioState audioState)46 public static AudioRouteSelectorDialogFragment newInstance(CallAudioState audioState) { 47 AudioRouteSelectorDialogFragment fragment = new AudioRouteSelectorDialogFragment(); 48 Bundle args = new Bundle(); 49 args.putParcelable(ARG_AUDIO_STATE, audioState); 50 fragment.setArguments(args); 51 return fragment; 52 } 53 54 @Override onAttach(Context context)55 public void onAttach(Context context) { 56 super.onAttach(context); 57 FragmentUtils.checkParent(this, AudioRouteSelectorPresenter.class); 58 } 59 60 @Override onCreateDialog(final Bundle savedInstanceState)61 public Dialog onCreateDialog(final Bundle savedInstanceState) { 62 LogUtil.i("AudioRouteSelectorDialogFragment.onCreateDialog", null); 63 Dialog dialog = super.onCreateDialog(savedInstanceState); 64 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 65 return dialog; 66 } 67 68 @Nullable 69 @Override onCreateView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle)70 public View onCreateView( 71 LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { 72 View view = layoutInflater.inflate(R.layout.audioroute_selector, viewGroup, false); 73 CallAudioState audioState = getArguments().getParcelable(ARG_AUDIO_STATE); 74 75 initItem( 76 (TextView) view.findViewById(R.id.audioroute_bluetooth), 77 CallAudioState.ROUTE_BLUETOOTH, 78 audioState); 79 initItem( 80 (TextView) view.findViewById(R.id.audioroute_speaker), 81 CallAudioState.ROUTE_SPEAKER, 82 audioState); 83 initItem( 84 (TextView) view.findViewById(R.id.audioroute_headset), 85 CallAudioState.ROUTE_WIRED_HEADSET, 86 audioState); 87 initItem( 88 (TextView) view.findViewById(R.id.audioroute_earpiece), 89 CallAudioState.ROUTE_EARPIECE, 90 audioState); 91 return view; 92 } 93 initItem(TextView item, final int itemRoute, CallAudioState audioState)94 private void initItem(TextView item, final int itemRoute, CallAudioState audioState) { 95 int selectedColor = getResources().getColor(R.color.dialer_theme_color); 96 if ((audioState.getSupportedRouteMask() & itemRoute) == 0) { 97 item.setVisibility(View.GONE); 98 } else if (audioState.getRoute() == itemRoute) { 99 item.setTextColor(selectedColor); 100 item.setCompoundDrawableTintList(ColorStateList.valueOf(selectedColor)); 101 item.setCompoundDrawableTintMode(Mode.SRC_ATOP); 102 } 103 item.setOnClickListener( 104 new OnClickListener() { 105 @Override 106 public void onClick(View v) { 107 dismiss(); 108 FragmentUtils.getParentUnsafe( 109 AudioRouteSelectorDialogFragment.this, AudioRouteSelectorPresenter.class) 110 .onAudioRouteSelected(itemRoute); 111 } 112 }); 113 } 114 } 115