blob: 864e16edbc3148c85f1b21ce3c9f0e7169fbe66c [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 Monk3b587142016-01-23 16:47:59 -050046 public static final String CLIPBOARD = "clipboard";
Jason Monka2081822016-01-18 14:41:03 -050047
48 public static final String GRAVITY_SEPARATOR = ";";
49 public static final String BUTTON_SEPARATOR = ",";
50
Xiaohui Chen54816002016-01-25 11:11:11 -080051 protected final LayoutInflater mLayoutInflater;
52 protected final LayoutInflater mLandscapeInflater;
Jason Monka2081822016-01-18 14:41:03 -050053
Jason Monk46a196e2016-01-23 15:28:10 -050054 public static final String SIZE_MOD_START = "[";
55 public static final String SIZE_MOD_END = "]";
56
Xiaohui Chen54816002016-01-25 11:11:11 -080057 protected FrameLayout mRot0;
58 protected FrameLayout mRot90;
Jason Monka2081822016-01-18 14:41:03 -050059 private SparseArray<ButtonDispatcher> mButtonDispatchers;
60 private String mCurrentLayout;
61
62 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 mLayoutInflater = LayoutInflater.from(context);
65 Configuration landscape = new Configuration();
66 landscape.setTo(context.getResources().getConfiguration());
67 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
68 mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape));
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74 mRot0 = (FrameLayout) findViewById(R.id.rot0);
75 mRot90 = (FrameLayout) findViewById(R.id.rot90);
76 clearViews();
77 inflateLayout(getDefaultLayout());
78 }
79
Xiaohui Chen54816002016-01-25 11:11:11 -080080 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -050081 return mContext.getString(R.string.config_navBarLayout);
82 }
83
84 @Override
85 protected void onAttachedToWindow() {
86 super.onAttachedToWindow();
87 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
88 }
89
90 @Override
91 protected void onDetachedFromWindow() {
92 TunerService.get(getContext()).removeTunable(this);
93 super.onDetachedFromWindow();
94 }
95
96 @Override
97 public void onTuningChanged(String key, String newValue) {
98 if (NAV_BAR_VIEWS.equals(key)) {
99 if (newValue == null) {
100 newValue = getDefaultLayout();
101 }
102 if (!Objects.equals(mCurrentLayout, newValue)) {
103 clearViews();
104 inflateLayout(newValue);
105 }
106 }
107 }
108
109 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
110 mButtonDispatchers = buttonDisatchers;
111 for (int i = 0; i < buttonDisatchers.size(); i++) {
112 initiallyFill(buttonDisatchers.valueAt(i));
113 }
114 }
115
116 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500117 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500118 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500119 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500120 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500121 }
122
123 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
124 for (int i = 0; i < parent.getChildCount(); i++) {
125 // Need to manually search for each id, just in case each group has more than one
126 // of a single id. It probably mostly a waste of time, but shouldn't take long
127 // and will only happen once.
128 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
129 buttonDispatcher.addView(parent.getChildAt(i));
130 } else if (parent.getChildAt(i) instanceof ViewGroup) {
131 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
132 }
133 }
134 }
135
Xiaohui Chen54816002016-01-25 11:11:11 -0800136 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500137 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800138 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500139 String[] start = sets[0].split(BUTTON_SEPARATOR);
140 String[] center = sets[1].split(BUTTON_SEPARATOR);
141 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500142 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
143 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
144 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
145 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
146
Xiaohui Chen06917032016-01-26 11:20:39 -0800147 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
148 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
149 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
150 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
151
152 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
153 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500154
155 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
156 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
157 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
158 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
159 }
160
Jason Monk4f878ef2016-01-23 14:37:38 -0500161 private void addGravitySpacer(LinearLayout layout) {
162 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500163 }
164
165 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
166 boolean landscape) {
167 for (int i = 0; i < buttons.length; i++) {
168 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
169 }
170 }
171
172 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800173 if (view == null) return;
174
Jason Monka2081822016-01-18 14:41:03 -0500175 if (view instanceof FrameLayout) {
176 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
177 FrameLayout original = (FrameLayout) view;
178 FrameLayout layout = new FrameLayout(view.getContext());
179 for (int i = 0; i < original.getChildCount(); i++) {
180 copyToLightsout(original.getChildAt(i), layout);
181 }
182 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
183 } else if (view instanceof Space) {
184 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
185 } else {
186 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
187 }
188 }
189
190 private View generateLightsOutView(View view) {
191 ImageView imageView = new ImageView(view.getContext());
192 // Copy everything we can about the original view.
193 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
194 view.getPaddingBottom());
195 imageView.setContentDescription(view.getContentDescription());
196 imageView.setId(view.getId());
197 // Only home gets a big dot, everything else will be little.
198 imageView.setImageResource(view.getId() == R.id.home
199 ? R.drawable.ic_sysbar_lights_out_dot_large
200 : R.drawable.ic_sysbar_lights_out_dot_small);
201 return imageView;
202 }
203
204 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500205 if (layoutParams instanceof LinearLayout.LayoutParams) {
206 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
207 ((LinearLayout.LayoutParams) layoutParams).weight);
208 }
Jason Monka2081822016-01-18 14:41:03 -0500209 return new LayoutParams(layoutParams.width, layoutParams.height);
210 }
211
Xiaohui Chen54816002016-01-25 11:11:11 -0800212 @Nullable
Jason Monk46a196e2016-01-23 15:28:10 -0500213 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800214 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500215 float size = extractSize(buttonSpec);
216 String button = extractButton(buttonSpec);
217 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500218 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800219 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500220 if (landscape && isSw600Dp()) {
221 setupLandButton(v);
222 }
223 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800224 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500225 if (landscape && isSw600Dp()) {
226 setupLandButton(v);
227 }
228 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800229 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500230 if (landscape && isSw600Dp()) {
231 setupLandButton(v);
232 }
233 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800234 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500235 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800236 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500237 } else if (CLIPBOARD.equals(button)) {
238 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500239 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800240 return null;
Jason Monka2081822016-01-18 14:41:03 -0500241 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800242
Jason Monk46a196e2016-01-23 15:28:10 -0500243 if (size != 0) {
244 ViewGroup.LayoutParams params = v.getLayoutParams();
245 params.width = (int) (params.width * size);
246 }
Jason Monka2081822016-01-18 14:41:03 -0500247 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500248 addToDispatchers(v);
249 return v;
250 }
251
Jason Monk46a196e2016-01-23 15:28:10 -0500252 public static float extractSize(String buttonSpec) {
253 if (!buttonSpec.contains(SIZE_MOD_START)) {
254 return 1;
255 }
256 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
257 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
258 return Float.parseFloat(sizeStr);
259 }
260
261 public static String extractButton(String buttonSpec) {
262 if (!buttonSpec.contains(SIZE_MOD_START)) {
263 return buttonSpec;
264 }
265 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
266 }
267
Jason Monk4f878ef2016-01-23 14:37:38 -0500268 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500269 if (mButtonDispatchers != null) {
270 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
271 if (indexOfKey >= 0) {
272 mButtonDispatchers.valueAt(indexOfKey).addView(v);
273 }
274 }
Jason Monka2081822016-01-18 14:41:03 -0500275 }
276
277 private boolean isSw600Dp() {
278 Configuration configuration = mContext.getResources().getConfiguration();
279 return (configuration.smallestScreenWidthDp >= 600);
280 }
281
282 /**
283 * This manually sets the width of sw600dp landscape buttons because despite
284 * overriding the configuration from the overridden resources aren't loaded currently.
285 */
286 private void setupLandButton(View v) {
287 Resources res = mContext.getResources();
288 v.getLayoutParams().width = res.getDimensionPixelOffset(
289 R.dimen.navigation_key_width_sw600dp_land);
290 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
291 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
292 }
293
294 private void clearViews() {
295 if (mButtonDispatchers != null) {
296 for (int i = 0; i < mButtonDispatchers.size(); i++) {
297 mButtonDispatchers.valueAt(i).clear();
298 }
299 }
300 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
301 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
302 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
303 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
304 }
305
306 private void clearAllChildren(ViewGroup group) {
307 for (int i = 0; i < group.getChildCount(); i++) {
308 ((ViewGroup) group.getChildAt(i)).removeAllViews();
309 }
310 }
311}