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.Nullable;
21 import android.support.v4.app.Fragment;
22 import android.util.ArraySet;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import com.android.dialer.common.Assert;
27 import com.android.dialer.common.FragmentUtils;
28 import com.android.incallui.incall.protocol.InCallButtonIds;
29 import java.util.List;
30 import java.util.Set;
31 
32 /** Fragment for the in call buttons (mute, speaker, ect.). */
33 public 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 
newInstance()41   public static Fragment newInstance() {
42     return new InCallButtonGridFragment();
43   }
44 
45   @Override
onCreate(@ullable Bundle bundle)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
onCreateView( LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle bundle)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
onViewCreated(View view, @Nullable Bundle bundle)69   public void onViewCreated(View view, @Nullable Bundle bundle) {
70     super.onViewCreated(view, bundle);
71     buttonGridListener.onButtonGridCreated(this);
72   }
73 
74   @Override
onDestroyView()75   public void onDestroyView() {
76     super.onDestroyView();
77     buttonGridListener.onButtonGridDestroyed();
78   }
79 
onInCallScreenDialpadVisibilityChange(boolean isShowing)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 
updateButtonStates( List<ButtonController> buttonControllers, @Nullable ButtonChooser buttonChooser, int voiceNetworkType, int phoneType)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 
130   /** Interface to let the listener know the status of the button grid. */
131   public interface OnButtonGridCreatedListener {
onButtonGridCreated(InCallButtonGridFragment inCallButtonGridFragment)132     void onButtonGridCreated(InCallButtonGridFragment inCallButtonGridFragment);
onButtonGridDestroyed()133     void onButtonGridDestroyed();
134 
getButtonController(@nCallButtonIds int id)135     ButtonController getButtonController(@InCallButtonIds int id);
136   }
137 }
138