blob: 7395a33c8be45cf4d8d900ecebd8ed13be86917b [file] [log] [blame]
Jason Monka2081822016-01-18 14:41:03 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.phone;
16
17import android.content.Context;
18import android.content.res.Configuration;
19import android.content.res.Resources;
20import android.util.AttributeSet;
21import android.util.SparseArray;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.FrameLayout;
26import android.widget.ImageView;
Jason Monk4f878ef2016-01-23 14:37:38 -050027import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050028import android.widget.Space;
29import com.android.systemui.R;
30import com.android.systemui.tuner.TunerService;
31
32import java.util.Objects;
33
34public class NavigationBarInflaterView extends FrameLayout implements TunerService.Tunable {
35
36 private static final String TAG = "NavBarInflater";
37
38 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
39
40 private static final String MENU_IME = "menu_ime";
41 private static final String BACK = "back";
42 private static final String HOME = "home";
43 private static final String RECENT = "recent";
44 private static final String NAVSPACE = "space";
45
46 public static final String GRAVITY_SEPARATOR = ";";
47 public static final String BUTTON_SEPARATOR = ",";
48
49 private final LayoutInflater mLayoutInflater;
50 private final LayoutInflater mLandscapeInflater;
51
52 private FrameLayout mRot0;
53 private FrameLayout mRot90;
54 private SparseArray<ButtonDispatcher> mButtonDispatchers;
55 private String mCurrentLayout;
56
57 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 mLayoutInflater = LayoutInflater.from(context);
60 Configuration landscape = new Configuration();
61 landscape.setTo(context.getResources().getConfiguration());
62 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
63 mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape));
64 }
65
66 @Override
67 protected void onFinishInflate() {
68 super.onFinishInflate();
69 mRot0 = (FrameLayout) findViewById(R.id.rot0);
70 mRot90 = (FrameLayout) findViewById(R.id.rot90);
71 clearViews();
72 inflateLayout(getDefaultLayout());
73 }
74
75 private String getDefaultLayout() {
76 return mContext.getString(R.string.config_navBarLayout);
77 }
78
79 @Override
80 protected void onAttachedToWindow() {
81 super.onAttachedToWindow();
82 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
83 }
84
85 @Override
86 protected void onDetachedFromWindow() {
87 TunerService.get(getContext()).removeTunable(this);
88 super.onDetachedFromWindow();
89 }
90
91 @Override
92 public void onTuningChanged(String key, String newValue) {
93 if (NAV_BAR_VIEWS.equals(key)) {
94 if (newValue == null) {
95 newValue = getDefaultLayout();
96 }
97 if (!Objects.equals(mCurrentLayout, newValue)) {
98 clearViews();
99 inflateLayout(newValue);
100 }
101 }
102 }
103
104 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
105 mButtonDispatchers = buttonDisatchers;
106 for (int i = 0; i < buttonDisatchers.size(); i++) {
107 initiallyFill(buttonDisatchers.valueAt(i));
108 }
109 }
110
111 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500112 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500113 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500114 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500115 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500116 }
117
118 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
119 for (int i = 0; i < parent.getChildCount(); i++) {
120 // Need to manually search for each id, just in case each group has more than one
121 // of a single id. It probably mostly a waste of time, but shouldn't take long
122 // and will only happen once.
123 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
124 buttonDispatcher.addView(parent.getChildAt(i));
125 } else if (parent.getChildAt(i) instanceof ViewGroup) {
126 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
127 }
128 }
129 }
130
131 private void inflateLayout(String newLayout) {
132 mCurrentLayout = newLayout;
133 String[] sets = newLayout.split(GRAVITY_SEPARATOR);
134 String[] start = sets[0].split(BUTTON_SEPARATOR);
135 String[] center = sets[1].split(BUTTON_SEPARATOR);
136 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500137 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
138 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
139 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
140 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
141
Xiaohui Chen06917032016-01-26 11:20:39 -0800142 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
143 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
144 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
145 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
146
147 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
148 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500149
150 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
151 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
152 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
153 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
154 }
155
Jason Monk4f878ef2016-01-23 14:37:38 -0500156 private void addGravitySpacer(LinearLayout layout) {
157 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500158 }
159
160 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
161 boolean landscape) {
162 for (int i = 0; i < buttons.length; i++) {
163 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
164 }
165 }
166
167 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
168 if (view instanceof FrameLayout) {
169 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
170 FrameLayout original = (FrameLayout) view;
171 FrameLayout layout = new FrameLayout(view.getContext());
172 for (int i = 0; i < original.getChildCount(); i++) {
173 copyToLightsout(original.getChildAt(i), layout);
174 }
175 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
176 } else if (view instanceof Space) {
177 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
178 } else {
179 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
180 }
181 }
182
183 private View generateLightsOutView(View view) {
184 ImageView imageView = new ImageView(view.getContext());
185 // Copy everything we can about the original view.
186 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
187 view.getPaddingBottom());
188 imageView.setContentDescription(view.getContentDescription());
189 imageView.setId(view.getId());
190 // Only home gets a big dot, everything else will be little.
191 imageView.setImageResource(view.getId() == R.id.home
192 ? R.drawable.ic_sysbar_lights_out_dot_large
193 : R.drawable.ic_sysbar_lights_out_dot_small);
194 return imageView;
195 }
196
197 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500198 if (layoutParams instanceof LinearLayout.LayoutParams) {
199 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
200 ((LinearLayout.LayoutParams) layoutParams).weight);
201 }
Jason Monka2081822016-01-18 14:41:03 -0500202 return new LayoutParams(layoutParams.width, layoutParams.height);
203 }
204
205 private View inflateButton(String button, ViewGroup parent, boolean landscape) {
206 View v = null;
207 if (HOME.equals(button)) {
208 v = (landscape ? mLandscapeInflater : mLayoutInflater)
209 .inflate(R.layout.home, parent, false);
210 if (landscape && isSw600Dp()) {
211 setupLandButton(v);
212 }
213 } else if (BACK.equals(button)) {
214 v = (landscape ? mLandscapeInflater : mLayoutInflater)
215 .inflate(R.layout.back, parent, false);
216 if (landscape && isSw600Dp()) {
217 setupLandButton(v);
218 }
219 } else if (RECENT.equals(button)) {
220 v = (landscape ? mLandscapeInflater : mLayoutInflater)
221 .inflate(R.layout.recent_apps, parent, false);
222 if (landscape && isSw600Dp()) {
223 setupLandButton(v);
224 }
225 } else if (MENU_IME.equals(button)) {
226 v = (landscape ? mLandscapeInflater : mLayoutInflater)
227 .inflate(R.layout.menu_ime, parent, false);
228 } else if (NAVSPACE.equals(button)) {
229 v = (landscape ? mLandscapeInflater : mLayoutInflater)
230 .inflate(R.layout.nav_key_space, parent, false);
231 } else {
232 throw new IllegalArgumentException("Unknown button " + button);
233 }
234 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500235 addToDispatchers(v);
236 return v;
237 }
238
239 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500240 if (mButtonDispatchers != null) {
241 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
242 if (indexOfKey >= 0) {
243 mButtonDispatchers.valueAt(indexOfKey).addView(v);
244 }
245 }
Jason Monka2081822016-01-18 14:41:03 -0500246 }
247
248 private boolean isSw600Dp() {
249 Configuration configuration = mContext.getResources().getConfiguration();
250 return (configuration.smallestScreenWidthDp >= 600);
251 }
252
253 /**
254 * This manually sets the width of sw600dp landscape buttons because despite
255 * overriding the configuration from the overridden resources aren't loaded currently.
256 */
257 private void setupLandButton(View v) {
258 Resources res = mContext.getResources();
259 v.getLayoutParams().width = res.getDimensionPixelOffset(
260 R.dimen.navigation_key_width_sw600dp_land);
261 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
262 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
263 }
264
265 private void clearViews() {
266 if (mButtonDispatchers != null) {
267 for (int i = 0; i < mButtonDispatchers.size(); i++) {
268 mButtonDispatchers.valueAt(i).clear();
269 }
270 }
271 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
272 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
273 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
274 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
275 }
276
277 private void clearAllChildren(ViewGroup group) {
278 for (int i = 0; i < group.getChildCount(); i++) {
279 ((ViewGroup) group.getChildAt(i)).removeAllViews();
280 }
281 }
282}