blob: d625fc2bc7386d0756d8aa42284ba397c73d8c44 [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;
Jason Monk8457ad82016-01-24 10:15:55 -050031import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050032import com.android.systemui.tuner.TunerService;
33
34import java.util.Objects;
35
36public class NavigationBarInflaterView extends FrameLayout implements TunerService.Tunable {
37
38 private static final String TAG = "NavBarInflater";
39
40 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
41
Jason Monk3ebd2392016-01-22 10:01:44 -050042 public static final String MENU_IME = "menu_ime";
43 public static final String BACK = "back";
44 public static final String HOME = "home";
45 public static final String RECENT = "recent";
46 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050047 public static final String CLIPBOARD = "clipboard";
Jason Monk8457ad82016-01-24 10:15:55 -050048 public static final String KEY = "key";
Jason Monka2081822016-01-18 14:41:03 -050049
50 public static final String GRAVITY_SEPARATOR = ";";
51 public static final String BUTTON_SEPARATOR = ",";
52
Jason Monk46a196e2016-01-23 15:28:10 -050053 public static final String SIZE_MOD_START = "[";
54 public static final String SIZE_MOD_END = "]";
55
Jason Monk8457ad82016-01-24 10:15:55 -050056 public static final String KEY_CODE_START = "(";
57 public static final String KEY_IMAGE_DELIM = ":";
58 public static final String KEY_CODE_END = ")";
59
Jason Monk67a1cce2016-02-05 13:31:03 -050060 protected LayoutInflater mLayoutInflater;
61 protected LayoutInflater mLandscapeInflater;
62 private int mDensity;
Jason Monk8457ad82016-01-24 10:15:55 -050063
Xiaohui Chen54816002016-01-25 11:11:11 -080064 protected FrameLayout mRot0;
65 protected FrameLayout mRot90;
Jason Monk8457ad82016-01-24 10:15:55 -050066
Jason Monka2081822016-01-18 14:41:03 -050067 private SparseArray<ButtonDispatcher> mButtonDispatchers;
68 private String mCurrentLayout;
69
70 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
71 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -050072 mDensity = context.getResources().getConfiguration().densityDpi;
73 createInflaters();
74 }
75
76 private void createInflaters() {
77 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -050078 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -050079 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -050080 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -050081 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
82 }
83
84 @Override
85 protected void onConfigurationChanged(Configuration newConfig) {
86 super.onConfigurationChanged(newConfig);
87 if (mDensity != newConfig.densityDpi) {
88 mDensity = newConfig.densityDpi;
89 createInflaters();
90 clearViews();
91 inflateLayout(mCurrentLayout);
92 }
Jason Monka2081822016-01-18 14:41:03 -050093 }
94
95 @Override
96 protected void onFinishInflate() {
97 super.onFinishInflate();
98 mRot0 = (FrameLayout) findViewById(R.id.rot0);
99 mRot90 = (FrameLayout) findViewById(R.id.rot90);
100 clearViews();
101 inflateLayout(getDefaultLayout());
102 }
103
Xiaohui Chen54816002016-01-25 11:11:11 -0800104 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500105 return mContext.getString(R.string.config_navBarLayout);
106 }
107
108 @Override
109 protected void onAttachedToWindow() {
110 super.onAttachedToWindow();
111 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
112 }
113
114 @Override
115 protected void onDetachedFromWindow() {
116 TunerService.get(getContext()).removeTunable(this);
117 super.onDetachedFromWindow();
118 }
119
120 @Override
121 public void onTuningChanged(String key, String newValue) {
122 if (NAV_BAR_VIEWS.equals(key)) {
123 if (newValue == null) {
124 newValue = getDefaultLayout();
125 }
126 if (!Objects.equals(mCurrentLayout, newValue)) {
127 clearViews();
128 inflateLayout(newValue);
129 }
130 }
131 }
132
133 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
134 mButtonDispatchers = buttonDisatchers;
135 for (int i = 0; i < buttonDisatchers.size(); i++) {
136 initiallyFill(buttonDisatchers.valueAt(i));
137 }
138 }
139
140 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500141 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500142 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500143 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500144 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500145 }
146
147 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
148 for (int i = 0; i < parent.getChildCount(); i++) {
149 // Need to manually search for each id, just in case each group has more than one
150 // of a single id. It probably mostly a waste of time, but shouldn't take long
151 // and will only happen once.
152 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
153 buttonDispatcher.addView(parent.getChildAt(i));
154 } else if (parent.getChildAt(i) instanceof ViewGroup) {
155 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
156 }
157 }
158 }
159
Xiaohui Chen54816002016-01-25 11:11:11 -0800160 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500161 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800162 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500163 String[] start = sets[0].split(BUTTON_SEPARATOR);
164 String[] center = sets[1].split(BUTTON_SEPARATOR);
165 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500166 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
167 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
168 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
169 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
170
Xiaohui Chen06917032016-01-26 11:20:39 -0800171 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
172 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
173 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
174 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
175
176 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
177 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500178
179 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
180 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
181 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
182 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
183 }
184
Jason Monk4f878ef2016-01-23 14:37:38 -0500185 private void addGravitySpacer(LinearLayout layout) {
186 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500187 }
188
189 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
190 boolean landscape) {
191 for (int i = 0; i < buttons.length; i++) {
192 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
193 }
194 }
195
196 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800197 if (view == null) return;
198
Jason Monka2081822016-01-18 14:41:03 -0500199 if (view instanceof FrameLayout) {
200 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
201 FrameLayout original = (FrameLayout) view;
202 FrameLayout layout = new FrameLayout(view.getContext());
203 for (int i = 0; i < original.getChildCount(); i++) {
204 copyToLightsout(original.getChildAt(i), layout);
205 }
206 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
207 } else if (view instanceof Space) {
208 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
209 } else {
210 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
211 }
212 }
213
214 private View generateLightsOutView(View view) {
215 ImageView imageView = new ImageView(view.getContext());
216 // Copy everything we can about the original view.
217 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
218 view.getPaddingBottom());
219 imageView.setContentDescription(view.getContentDescription());
220 imageView.setId(view.getId());
221 // Only home gets a big dot, everything else will be little.
222 imageView.setImageResource(view.getId() == R.id.home
223 ? R.drawable.ic_sysbar_lights_out_dot_large
224 : R.drawable.ic_sysbar_lights_out_dot_small);
225 return imageView;
226 }
227
228 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500229 if (layoutParams instanceof LinearLayout.LayoutParams) {
230 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
231 ((LinearLayout.LayoutParams) layoutParams).weight);
232 }
Jason Monka2081822016-01-18 14:41:03 -0500233 return new LayoutParams(layoutParams.width, layoutParams.height);
234 }
235
Xiaohui Chen54816002016-01-25 11:11:11 -0800236 @Nullable
Jason Monk46a196e2016-01-23 15:28:10 -0500237 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800238 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500239 float size = extractSize(buttonSpec);
240 String button = extractButton(buttonSpec);
241 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500242 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800243 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500244 if (landscape && isSw600Dp()) {
245 setupLandButton(v);
246 }
247 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800248 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500249 if (landscape && isSw600Dp()) {
250 setupLandButton(v);
251 }
252 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800253 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500254 if (landscape && isSw600Dp()) {
255 setupLandButton(v);
256 }
257 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800258 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500259 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800260 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500261 } else if (CLIPBOARD.equals(button)) {
262 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500263 } else if (button.startsWith(KEY)) {
264 String uri = extractImage(button);
265 int code = extractKeycode(button);
266 v = inflater.inflate(R.layout.custom_key, parent, false);
267 ((KeyButtonView) v).setCode(code);
268 if (uri != null) {
269 ((KeyButtonView) v).loadAsync(uri);
270 }
Jason Monka2081822016-01-18 14:41:03 -0500271 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800272 return null;
Jason Monka2081822016-01-18 14:41:03 -0500273 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800274
Jason Monk46a196e2016-01-23 15:28:10 -0500275 if (size != 0) {
276 ViewGroup.LayoutParams params = v.getLayoutParams();
277 params.width = (int) (params.width * size);
278 }
Jason Monka2081822016-01-18 14:41:03 -0500279 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500280 addToDispatchers(v);
281 return v;
282 }
283
Jason Monk8457ad82016-01-24 10:15:55 -0500284 public static String extractImage(String buttonSpec) {
285 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
286 return null;
287 }
288 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
289 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
290 return subStr;
291 }
292
293 public static int extractKeycode(String buttonSpec) {
294 if (!buttonSpec.contains(KEY_CODE_START)) {
295 return 1;
296 }
297 final int start = buttonSpec.indexOf(KEY_CODE_START);
298 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
299 return Integer.parseInt(subStr);
300 }
301
Jason Monk46a196e2016-01-23 15:28:10 -0500302 public static float extractSize(String buttonSpec) {
303 if (!buttonSpec.contains(SIZE_MOD_START)) {
304 return 1;
305 }
306 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
307 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
308 return Float.parseFloat(sizeStr);
309 }
310
311 public static String extractButton(String buttonSpec) {
312 if (!buttonSpec.contains(SIZE_MOD_START)) {
313 return buttonSpec;
314 }
315 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
316 }
317
Jason Monk4f878ef2016-01-23 14:37:38 -0500318 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500319 if (mButtonDispatchers != null) {
320 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
321 if (indexOfKey >= 0) {
322 mButtonDispatchers.valueAt(indexOfKey).addView(v);
323 }
324 }
Jason Monka2081822016-01-18 14:41:03 -0500325 }
326
327 private boolean isSw600Dp() {
328 Configuration configuration = mContext.getResources().getConfiguration();
329 return (configuration.smallestScreenWidthDp >= 600);
330 }
331
332 /**
333 * This manually sets the width of sw600dp landscape buttons because despite
334 * overriding the configuration from the overridden resources aren't loaded currently.
335 */
336 private void setupLandButton(View v) {
337 Resources res = mContext.getResources();
338 v.getLayoutParams().width = res.getDimensionPixelOffset(
339 R.dimen.navigation_key_width_sw600dp_land);
340 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
341 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
342 }
343
344 private void clearViews() {
345 if (mButtonDispatchers != null) {
346 for (int i = 0; i < mButtonDispatchers.size(); i++) {
347 mButtonDispatchers.valueAt(i).clear();
348 }
349 }
350 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
351 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
352 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
353 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
354 }
355
356 private void clearAllChildren(ViewGroup group) {
357 for (int i = 0; i < group.getChildCount(); i++) {
358 ((ViewGroup) group.getChildAt(i)).removeAllViews();
359 }
360 }
361}