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
17package com.android.incallui.incall.impl;
18
19import android.os.Bundle;
20import android.support.annotation.ColorInt;
21import android.support.annotation.Nullable;
22import android.support.v4.app.Fragment;
23import android.util.ArraySet;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import com.android.dialer.common.Assert;
28import com.android.dialer.common.FragmentUtils;
29import com.android.incallui.incall.protocol.InCallButtonIds;
30import java.util.List;
31import java.util.Set;
32
33/** Fragment for the in call buttons (mute, speaker, ect.). */
34public 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
42  public static Fragment newInstance() {
43    return new InCallButtonGridFragment();
44  }
45
46  @Override
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
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
70  public void onViewCreated(View view, @Nullable Bundle bundle) {
71    super.onViewCreated(view, bundle);
72    buttonGridListener.onButtonGridCreated(this);
73  }
74
75  @Override
76  public void onDestroyView() {
77    super.onDestroyView();
78    buttonGridListener.onButtonGridDestroyed();
79  }
80
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
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
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 {
139    void onButtonGridCreated(InCallButtonGridFragment inCallButtonGridFragment);
140    void onButtonGridDestroyed();
141
142    ButtonController getButtonController(@InCallButtonIds int id);
143  }
144}
145