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