blob: 6de4d5cd78157463dd7301c2f6077a1da2bc3548 [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
Xiaohui Chen54816002016-01-25 11:11:11 -080053 protected FrameLayout mRot0;
54 protected FrameLayout mRot90;
Jason Monka2081822016-01-18 14:41:03 -050055 private SparseArray<ButtonDispatcher> mButtonDispatchers;
56 private String mCurrentLayout;
57
58 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 mLayoutInflater = LayoutInflater.from(context);
61 Configuration landscape = new Configuration();
62 landscape.setTo(context.getResources().getConfiguration());
63 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
64 mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape));
65 }
66
67 @Override
68 protected void onFinishInflate() {
69 super.onFinishInflate();
70 mRot0 = (FrameLayout) findViewById(R.id.rot0);
71 mRot90 = (FrameLayout) findViewById(R.id.rot90);
72 clearViews();
73 inflateLayout(getDefaultLayout());
74 }
75
Xiaohui Chen54816002016-01-25 11:11:11 -080076 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -050077 return mContext.getString(R.string.config_navBarLayout);
78 }
79
80 @Override
81 protected void onAttachedToWindow() {
82 super.onAttachedToWindow();
83 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
84 }
85
86 @Override
87 protected void onDetachedFromWindow() {
88 TunerService.get(getContext()).removeTunable(this);
89 super.onDetachedFromWindow();
90 }
91
92 @Override
93 public void onTuningChanged(String key, String newValue) {
94 if (NAV_BAR_VIEWS.equals(key)) {
95 if (newValue == null) {
96 newValue = getDefaultLayout();
97 }
98 if (!Objects.equals(mCurrentLayout, newValue)) {
99 clearViews();
100 inflateLayout(newValue);
101 }
102 }
103 }
104
105 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
106 mButtonDispatchers = buttonDisatchers;
107 for (int i = 0; i < buttonDisatchers.size(); i++) {
108 initiallyFill(buttonDisatchers.valueAt(i));
109 }
110 }
111
112 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500113 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500114 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500115 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500116 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500117 }
118
119 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
120 for (int i = 0; i < parent.getChildCount(); i++) {
121 // Need to manually search for each id, just in case each group has more than one
122 // of a single id. It probably mostly a waste of time, but shouldn't take long
123 // and will only happen once.
124 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
125 buttonDispatcher.addView(parent.getChildAt(i));
126 } else if (parent.getChildAt(i) instanceof ViewGroup) {
127 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
128 }
129 }
130 }
131
Xiaohui Chen54816002016-01-25 11:11:11 -0800132 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500133 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800134 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500135 String[] start = sets[0].split(BUTTON_SEPARATOR);
136 String[] center = sets[1].split(BUTTON_SEPARATOR);
137 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500138 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
139 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
140 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
141 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
142
Xiaohui Chen06917032016-01-26 11:20:39 -0800143 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
144 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
145 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
146 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
147
148 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
149 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500150
151 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
152 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
153 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
154 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
155 }
156
Jason Monk4f878ef2016-01-23 14:37:38 -0500157 private void addGravitySpacer(LinearLayout layout) {
158 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500159 }
160
161 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
162 boolean landscape) {
163 for (int i = 0; i < buttons.length; i++) {
164 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
165 }
166 }
167
168 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800169 if (view == null) return;
170
Jason Monka2081822016-01-18 14:41:03 -0500171 if (view instanceof FrameLayout) {
172 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
173 FrameLayout original = (FrameLayout) view;
174 FrameLayout layout = new FrameLayout(view.getContext());
175 for (int i = 0; i < original.getChildCount(); i++) {
176 copyToLightsout(original.getChildAt(i), layout);
177 }
178 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
179 } else if (view instanceof Space) {
180 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
181 } else {
182 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
183 }
184 }
185
186 private View generateLightsOutView(View view) {
187 ImageView imageView = new ImageView(view.getContext());
188 // Copy everything we can about the original view.
189 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
190 view.getPaddingBottom());
191 imageView.setContentDescription(view.getContentDescription());
192 imageView.setId(view.getId());
193 // Only home gets a big dot, everything else will be little.
194 imageView.setImageResource(view.getId() == R.id.home
195 ? R.drawable.ic_sysbar_lights_out_dot_large
196 : R.drawable.ic_sysbar_lights_out_dot_small);
197 return imageView;
198 }
199
200 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500201 if (layoutParams instanceof LinearLayout.LayoutParams) {
202 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
203 ((LinearLayout.LayoutParams) layoutParams).weight);
204 }
Jason Monka2081822016-01-18 14:41:03 -0500205 return new LayoutParams(layoutParams.width, layoutParams.height);
206 }
207
Xiaohui Chen54816002016-01-25 11:11:11 -0800208 @Nullable
209 protected View inflateButton(String button, ViewGroup parent, boolean landscape) {
210 View v;
211 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monka2081822016-01-18 14:41:03 -0500212 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800213 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500214 if (landscape && isSw600Dp()) {
215 setupLandButton(v);
216 }
217 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800218 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500219 if (landscape && isSw600Dp()) {
220 setupLandButton(v);
221 }
222 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800223 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500224 if (landscape && isSw600Dp()) {
225 setupLandButton(v);
226 }
227 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800228 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500229 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800230 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500231 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800232 return null;
Jason Monka2081822016-01-18 14:41:03 -0500233 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800234
Jason Monka2081822016-01-18 14:41:03 -0500235 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500236 addToDispatchers(v);
237 return v;
238 }
239
240 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500241 if (mButtonDispatchers != null) {
242 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
243 if (indexOfKey >= 0) {
244 mButtonDispatchers.valueAt(indexOfKey).addView(v);
245 }
246 }
Jason Monka2081822016-01-18 14:41:03 -0500247 }
248
249 private boolean isSw600Dp() {
250 Configuration configuration = mContext.getResources().getConfiguration();
251 return (configuration.smallestScreenWidthDp >= 600);
252 }
253
254 /**
255 * This manually sets the width of sw600dp landscape buttons because despite
256 * overriding the configuration from the overridden resources aren't loaded currently.
257 */
258 private void setupLandButton(View v) {
259 Resources res = mContext.getResources();
260 v.getLayoutParams().width = res.getDimensionPixelOffset(
261 R.dimen.navigation_key_width_sw600dp_land);
262 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
263 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
264 }
265
266 private void clearViews() {
267 if (mButtonDispatchers != null) {
268 for (int i = 0; i < mButtonDispatchers.size(); i++) {
269 mButtonDispatchers.valueAt(i).clear();
270 }
271 }
272 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
273 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
274 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
275 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
276 }
277
278 private void clearAllChildren(ViewGroup group) {
279 for (int i = 0; i < group.getChildCount(); i++) {
280 ((ViewGroup) group.getChildAt(i)).removeAllViews();
281 }
282 }
283}