Home | History | Annotate | Download | only in audioroute
      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.DialogInterface;
     22 import android.content.res.ColorStateList;
     23 import android.graphics.PorterDuff.Mode;
     24 import android.os.Bundle;
     25 import android.support.annotation.Nullable;
     26 import android.support.design.widget.BottomSheetDialogFragment;
     27 import android.telecom.CallAudioState;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     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   public static final String TAG = "AudioRouteSelectorDialogFragment";
     40   private static final String ARG_AUDIO_STATE = "audio_state";
     41 
     42   /** Called when an audio route is picked */
     43   public interface AudioRouteSelectorPresenter {
     44     void onAudioRouteSelected(int audioRoute);
     45 
     46     void onAudioRouteSelectorDismiss();
     47   }
     48 
     49   public static AudioRouteSelectorDialogFragment newInstance(CallAudioState audioState) {
     50     AudioRouteSelectorDialogFragment fragment = new AudioRouteSelectorDialogFragment();
     51     Bundle args = new Bundle();
     52     args.putParcelable(ARG_AUDIO_STATE, audioState);
     53     fragment.setArguments(args);
     54     return fragment;
     55   }
     56 
     57   @Override
     58   public void onAttach(Context context) {
     59     super.onAttach(context);
     60     FragmentUtils.checkParent(this, AudioRouteSelectorPresenter.class);
     61   }
     62 
     63   @Override
     64   public Dialog onCreateDialog(final Bundle savedInstanceState) {
     65     LogUtil.i("AudioRouteSelectorDialogFragment.onCreateDialog", null);
     66     Dialog dialog = super.onCreateDialog(savedInstanceState);
     67     dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
     68     return dialog;
     69   }
     70 
     71   @Nullable
     72   @Override
     73   public View onCreateView(
     74       LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
     75     View view = layoutInflater.inflate(R.layout.audioroute_selector, viewGroup, false);
     76     CallAudioState audioState = getArguments().getParcelable(ARG_AUDIO_STATE);
     77 
     78     initItem(
     79         (TextView) view.findViewById(R.id.audioroute_bluetooth),
     80         CallAudioState.ROUTE_BLUETOOTH,
     81         audioState);
     82     initItem(
     83         (TextView) view.findViewById(R.id.audioroute_speaker),
     84         CallAudioState.ROUTE_SPEAKER,
     85         audioState);
     86     initItem(
     87         (TextView) view.findViewById(R.id.audioroute_headset),
     88         CallAudioState.ROUTE_WIRED_HEADSET,
     89         audioState);
     90     initItem(
     91         (TextView) view.findViewById(R.id.audioroute_earpiece),
     92         CallAudioState.ROUTE_EARPIECE,
     93         audioState);
     94 
     95     // TODO(a bug): set peak height correctly to fully expand it in landscape mode.
     96     return view;
     97   }
     98 
     99   @Override
    100   public void onCancel(DialogInterface dialogInterface) {
    101     super.onCancel(dialogInterface);
    102     FragmentUtils.getParentUnsafe(
    103             AudioRouteSelectorDialogFragment.this, AudioRouteSelectorPresenter.class)
    104         .onAudioRouteSelectorDismiss();
    105   }
    106 
    107   private void initItem(TextView item, final int itemRoute, CallAudioState audioState) {
    108     int selectedColor = getResources().getColor(R.color.dialer_theme_color);
    109     if ((audioState.getSupportedRouteMask() & itemRoute) == 0) {
    110       item.setVisibility(View.GONE);
    111     } else if (audioState.getRoute() == itemRoute) {
    112       item.setTextColor(selectedColor);
    113       item.setCompoundDrawableTintList(ColorStateList.valueOf(selectedColor));
    114       item.setCompoundDrawableTintMode(Mode.SRC_ATOP);
    115     }
    116     item.setOnClickListener(
    117         (v) -> {
    118           dismiss();
    119           FragmentUtils.getParentUnsafe(
    120                   AudioRouteSelectorDialogFragment.this, AudioRouteSelectorPresenter.class)
    121               .onAudioRouteSelected(itemRoute);
    122         });
    123   }
    124 }
    125