blob: d49e295a6323a33ab1cc819fdec36af920c958f5 [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
Xiaohui Chen54816002016-01-25 11:11:11 -080017import android.annotation.Nullable;
Jason Monka2081822016-01-18 14:41:03 -050018import android.content.Context;
19import android.content.res.Configuration;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.util.SparseArray;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.FrameLayout;
27import android.widget.ImageView;
Jason Monk4f878ef2016-01-23 14:37:38 -050028import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050029import android.widget.Space;
30import com.android.systemui.R;
31import com.android.systemui.tuner.TunerService;
32
33import java.util.Objects;
34
35public class NavigationBarInflaterView extends FrameLayout implements TunerService.Tunable {
36
37 private static final String TAG = "NavBarInflater";
38
39 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
40
Jason Monk3ebd2392016-01-22 10:01:44 -050041 public static final String MENU_IME = "menu_ime";
42 public static final String BACK = "back";
43 public static final String HOME = "home";
44 public static final String RECENT = "recent";
45 public static final String NAVSPACE = "space";
Jason Monka2081822016-01-18 14:41:03 -050046
47 public static final String GRAVITY_SEPARATOR = ";";
48 public static final String BUTTON_SEPARATOR = ",";
49
Xiaohui Chen54816002016-01-25 11:11:11 -080050 protected final LayoutInflater mLayoutInflater;
51 protected final LayoutInflater mLandscapeInflater;
Jason Monka2081822016-01-18 14:41:03 -050052
Jason Monk46a196e2016-01-23 15:28:10 -050053 public static final String SIZE_MOD_START = "[";
54 public static final String SIZE_MOD_END = "]";
55
Xiaohui Chen54816002016-01-25 11:11:11 -080056 protected FrameLayout mRot0;
57 protected FrameLayout mRot90;
Jason Monka2081822016-01-18 14:41:03 -050058 private SparseArray<ButtonDispatcher> mButtonDispatchers;
59 private String mCurrentLayout;
60
61 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 mLayoutInflater = LayoutInflater.from(context);
64 Configuration landscape = new Configuration();
65 landscape.setTo(context.getResources().getConfiguration());
66 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
67 mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape));
68 }
69
70 @Override
71 protected void onFinishInflate() {
72 super.onFinishInflate();
73 mRot0 = (FrameLayout) findViewById(R.id.rot0);
74 mRot90 = (FrameLayout) findViewById(R.id.rot90);
75 clearViews();
76 inflateLayout(getDefaultLayout());
77 }
78
Xiaohui Chen54816002016-01-25 11:11:11 -080079 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -050080 return mContext.getString(R.string.config_navBarLayout);
81 }
82
83 @Override
84 protected void onAttachedToWindow() {
85 super.onAttachedToWindow();
86 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
87 }
88
89 @Override
90 protected void onDetachedFromWindow() {
91 TunerService.get(getContext()).removeTunable(this);
92 super.onDetachedFromWindow();
93 }
94
95 @Override
96 public void onTuningChanged(String key, String newValue) {
97 if (NAV_BAR_VIEWS.equals(key)) {
98 if (newValue == null) {
99 newValue = getDefaultLayout();
100 }
101 if (!Objects.equals(mCurrentLayout, newValue)) {
102 clearViews();
103 inflateLayout(newValue);
104 }
105 }
106 }
107
108 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
109 mButtonDispatchers = buttonDisatchers;
110 for (int i = 0; i < buttonDisatchers.size(); i++) {
111 initiallyFill(buttonDisatchers.valueAt(i));
112 }
113 }
114
115 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500116 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500117 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500118 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500119 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500120 }
121
122 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
123 for (int i = 0; i < parent.getChildCount(); i++) {
124 // Need to manually search for each id, just in case each group has more than one
125 // of a single id. It probably mostly a waste of time, but shouldn't take long
126 // and will only happen once.
127 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
128 buttonDispatcher.addView(parent.getChildAt(i));
129 } else if (parent.getChildAt(i) instanceof ViewGroup) {
130 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
131 }
132 }
133 }
134
Xiaohui Chen54816002016-01-25 11:11:11 -0800135 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500136 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800137 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500138 String[] start = sets[0].split(BUTTON_SEPARATOR);
139 String[] center = sets[1].split(BUTTON_SEPARATOR);
140 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500141 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
142 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
143 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
144 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
145
Xiaohui Chen06917032016-01-26 11:20:39 -0800146 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
147 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
148 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
149 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
150
151 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
152 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500153
154 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
155 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
156 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
157 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
158 }
159
Jason Monk4f878ef2016-01-23 14:37:38 -0500160 private void addGravitySpacer(LinearLayout layout) {
161 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500162 }
163
164 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
165 boolean landscape) {
166 for (int i = 0; i < buttons.length; i++) {
167 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
168 }
169 }
170
171 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800172 if (view == null) return;
173
Jason Monka2081822016-01-18 14:41:03 -0500174 if (view instanceof FrameLayout) {
175 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
176 FrameLayout original = (FrameLayout) view;
177 FrameLayout layout = new FrameLayout(view.getContext());
178 for (int i = 0; i < original.getChildCount(); i++) {
179 copyToLightsout(original.getChildAt(i), layout);
180 }
181 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
182 } else if (view instanceof Space) {
183 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
184 } else {
185 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
186 }
187 }
188
189 private View generateLightsOutView(View view) {
190 ImageView imageView = new ImageView(view.getContext());
191 // Copy everything we can about the original view.
192 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
193 view.getPaddingBottom());
194 imageView.setContentDescription(view.getContentDescription());
195 imageView.setId(view.getId());
196 // Only home gets a big dot, everything else will be little.
197 imageView.setImageResource(view.getId() == R.id.home
198 ? R.drawable.ic_sysbar_lights_out_dot_large
199 : R.drawable.ic_sysbar_lights_out_dot_small);
200 return imageView;
201 }
202
203 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500204 if (layoutParams instanceof LinearLayout.LayoutParams) {
205 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
206 ((LinearLayout.LayoutParams) layoutParams).weight);
207 }
Jason Monka2081822016-01-18 14:41:03 -0500208 return new LayoutParams(layoutParams.width, layoutParams.height);
209 }
210
Xiaohui Chen54816002016-01-25 11:11:11 -0800211 @Nullable
Jason Monk46a196e2016-01-23 15:28:10 -0500212 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800213 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500214 float size = extractSize(buttonSpec);
215 String button = extractButton(buttonSpec);
216 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500217 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800218 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500219 if (landscape && isSw600Dp()) {
220 setupLandButton(v);
221 }
222 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800223 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500224 if (landscape && isSw600Dp()) {
225 setupLandButton(v);
226 }
227 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800228 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500229 if (landscape && isSw600Dp()) {
230 setupLandButton(v);
231 }
232 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800233 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500234 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800235 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500236 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800237 return null;
Jason Monka2081822016-01-18 14:41:03 -0500238 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800239
Jason Monk46a196e2016-01-23 15:28:10 -0500240 if (size != 0) {
241 ViewGroup.LayoutParams params = v.getLayoutParams();
242 params.width = (int) (params.width * size);
243 }
Jason Monka2081822016-01-18 14:41:03 -0500244 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500245 addToDispatchers(v);
246 return v;
247 }
248
Jason Monk46a196e2016-01-23 15:28:10 -0500249 public static float extractSize(String buttonSpec) {
250 if (!buttonSpec.contains(SIZE_MOD_START)) {
251 return 1;
252 }
253 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
254 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
255 return Float.parseFloat(sizeStr);
256 }
257
258 public static String extractButton(String buttonSpec) {
259 if (!buttonSpec.contains(SIZE_MOD_START)) {
260 return buttonSpec;
261 }
262 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
263 }
264
Jason Monk4f878ef2016-01-23 14:37:38 -0500265 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500266 if (mButtonDispatchers != null) {
267 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
268 if (indexOfKey >= 0) {
269 mButtonDispatchers.valueAt(indexOfKey).addView(v);
270 }
271 }
Jason Monka2081822016-01-18 14:41:03 -0500272 }
273
274 private boolean isSw600Dp() {
275 Configuration configuration = mContext.getResources().getConfiguration();
276 return (configuration.smallestScreenWidthDp >= 600);
277 }
278
279 /**
280 * This manually sets the width of sw600dp landscape buttons because despite
281 * overriding the configuration from the overridden resources aren't loaded currently.
282 */
283 private void setupLandButton(View v) {
284 Resources res = mContext.getResources();
285 v.getLayoutParams().width = res.getDimensionPixelOffset(
286 R.dimen.navigation_key_width_sw600dp_land);
287 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
288 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
289 }
290
291 private void clearViews() {
292 if (mButtonDispatchers != null) {
293 for (int i = 0; i < mButtonDispatchers.size(); i++) {
294 mButtonDispatchers.valueAt(i).clear();
295 }
296 }
297 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
298 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
299 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
300 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
301 }
302
303 private void clearAllChildren(ViewGroup group) {
304 for (int i = 0; i < group.getChildCount(); i++) {
305 ((ViewGroup) group.getChildAt(i)).removeAllViews();
306 }
307 }
308}