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.incall.impl; 18 19 import android.os.Bundle; 20 import android.support.annotation.ColorInt; 21 import android.support.annotation.Nullable; 22 import android.support.v4.app.Fragment; 23 import android.util.ArraySet; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import com.android.dialer.common.Assert; 28 import com.android.dialer.common.FragmentUtils; 29 import com.android.incallui.incall.protocol.InCallButtonIds; 30 import java.util.List; 31 import java.util.Set; 32 33 /** Fragment for the in call buttons (mute, speaker, ect.). */ 34 public class InCallButtonGridFragment extends Fragment { 35 36 private static final int BUTTON_COUNT = 6; 37 private static final int BUTTONS_PER_ROW = 3; 38 39 private CheckableLabeledButton[] buttons = new CheckableLabeledButton[BUTTON_COUNT]; 40 private OnButtonGridCreatedListener buttonGridListener; 41 newInstance()42 public static Fragment newInstance() { 43 return new InCallButtonGridFragment(); 44 } 45 46 @Override onCreate(@ullable Bundle bundle)47 public void onCreate(@Nullable Bundle bundle) { 48 super.onCreate(bundle); 49 buttonGridListener = FragmentUtils.getParent(this, OnButtonGridCreatedListener.class); 50 Assert.isNotNull(buttonGridListener); 51 } 52 53 @Nullable 54 @Override onCreateView( LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle bundle)55 public View onCreateView( 56 LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle bundle) { 57 View view = inflater.inflate(R.layout.incall_button_grid, parent, false); 58 59 buttons[0] = ((CheckableLabeledButton) view.findViewById(R.id.incall_first_button)); 60 buttons[1] = ((CheckableLabeledButton) view.findViewById(R.id.incall_second_button)); 61 buttons[2] = ((CheckableLabeledButton) view.findViewById(R.id.incall_third_button)); 62 buttons[3] = ((CheckableLabeledButton) view.findViewById(R.id.incall_fourth_button)); 63 buttons[4] = ((CheckableLabeledButton) view.findViewById(R.id.incall_fifth_button)); 64 buttons[5] = ((CheckableLabeledButton) view.findViewById(R.id.incall_sixth_button)); 65 66 return view; 67 } 68 69 @Override onViewCreated(View view, @Nullable Bundle bundle)70 public void onViewCreated(View view, @Nullable Bundle bundle) { 71 super.onViewCreated(view, bundle); 72 buttonGridListener.onButtonGridCreated(this); 73 } 74 75 @Override onDestroyView()76 public void onDestroyView() { 77 super.onDestroyView(); 78 buttonGridListener.onButtonGridDestroyed(); 79 } 80 onInCallScreenDialpadVisibilityChange(boolean isShowing)81 public void onInCallScreenDialpadVisibilityChange(boolean isShowing) { 82 for (CheckableLabeledButton button : buttons) { 83 button.setImportantForAccessibility( 84 isShowing 85 ? View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS 86 : View.IMPORTANT_FOR_ACCESSIBILITY_AUTO); 87 } 88 } 89 updateButtonStates( List<ButtonController> buttonControllers, @Nullable ButtonChooser buttonChooser, int voiceNetworkType, int phoneType)90 public int updateButtonStates( 91 List<ButtonController> buttonControllers, 92 @Nullable ButtonChooser buttonChooser, 93 int voiceNetworkType, 94 int phoneType) { 95 Set<Integer> allowedButtons = new ArraySet<>(); 96 Set<Integer> disabledButtons = new ArraySet<>(); 97 for (ButtonController controller : buttonControllers) { 98 if (controller.isAllowed()) { 99 allowedButtons.add(controller.getInCallButtonId()); 100 if (!controller.isEnabled()) { 101 disabledButtons.add(controller.getInCallButtonId()); 102 } 103 } 104 } 105 106 for (ButtonController controller : buttonControllers) { 107 controller.setButton(null); 108 } 109 110 if (buttonChooser == null) { 111 buttonChooser = 112 ButtonChooserFactory.newButtonChooser(voiceNetworkType, false /* isWiFi */, phoneType); 113 } 114 115 int numVisibleButtons = getResources().getInteger(R.integer.incall_num_rows) * BUTTONS_PER_ROW; 116 List<Integer> buttonsToPlace = 117 buttonChooser.getButtonPlacement(numVisibleButtons, allowedButtons, disabledButtons); 118 119 for (int i = 0; i < BUTTON_COUNT; ++i) { 120 if (i >= buttonsToPlace.size()) { 121 buttons[i].setVisibility(View.INVISIBLE); 122 continue; 123 } 124 @InCallButtonIds int button = buttonsToPlace.get(i); 125 buttonGridListener.getButtonController(button).setButton(buttons[i]); 126 } 127 128 return numVisibleButtons; 129 } 130 updateButtonColor(@olorInt int color)131 public void updateButtonColor(@ColorInt int color) { 132 for (CheckableLabeledButton button : buttons) { 133 button.setCheckedColor(color); 134 } 135 } 136 137 /** Interface to let the listener know the status of the button grid. */ 138 public interface OnButtonGridCreatedListener { onButtonGridCreated(InCallButtonGridFragment inCallButtonGridFragment)139 void onButtonGridCreated(InCallButtonGridFragment inCallButtonGridFragment); onButtonGridDestroyed()140 void onButtonGridDestroyed(); 141 getButtonController(@nCallButtonIds int id)142 ButtonController getButtonController(@InCallButtonIds int id); 143 } 144 } 145