blob: 6cfd7155e1e64aab078f6242fd710f4445374f19 [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;
Jason Monk8457ad82016-01-24 10:15:55 -050021import android.graphics.drawable.Icon;
22import android.net.Uri;
Jason Monka2081822016-01-18 14:41:03 -050023import android.util.AttributeSet;
Jason Monk8457ad82016-01-24 10:15:55 -050024import android.util.Log;
Jason Monka2081822016-01-18 14:41:03 -050025import android.util.SparseArray;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.FrameLayout;
30import android.widget.ImageView;
Jason Monk4f878ef2016-01-23 14:37:38 -050031import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050032import android.widget.Space;
33import com.android.systemui.R;
Jason Monk8457ad82016-01-24 10:15:55 -050034import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050035import com.android.systemui.tuner.TunerService;
36
Jason Monk8457ad82016-01-24 10:15:55 -050037import java.io.File;
Jason Monka2081822016-01-18 14:41:03 -050038import java.util.Objects;
39
40public class NavigationBarInflaterView extends FrameLayout implements TunerService.Tunable {
41
42 private static final String TAG = "NavBarInflater";
43
44 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
45
Jason Monk3ebd2392016-01-22 10:01:44 -050046 public static final String MENU_IME = "menu_ime";
47 public static final String BACK = "back";
48 public static final String HOME = "home";
49 public static final String RECENT = "recent";
50 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050051 public static final String CLIPBOARD = "clipboard";
Jason Monk8457ad82016-01-24 10:15:55 -050052 public static final String KEY = "key";
Jason Monka2081822016-01-18 14:41:03 -050053
54 public static final String GRAVITY_SEPARATOR = ";";
55 public static final String BUTTON_SEPARATOR = ",";
56
Jason Monk46a196e2016-01-23 15:28:10 -050057 public static final String SIZE_MOD_START = "[";
58 public static final String SIZE_MOD_END = "]";
59
Jason Monk8457ad82016-01-24 10:15:55 -050060 public static final String KEY_CODE_START = "(";
61 public static final String KEY_IMAGE_DELIM = ":";
62 public static final String KEY_CODE_END = ")";
63
64 protected final LayoutInflater mLayoutInflater;
65 protected final LayoutInflater mLandscapeInflater;
66
Xiaohui Chen54816002016-01-25 11:11:11 -080067 protected FrameLayout mRot0;
68 protected FrameLayout mRot90;
Jason Monk8457ad82016-01-24 10:15:55 -050069
Jason Monka2081822016-01-18 14:41:03 -050070 private SparseArray<ButtonDispatcher> mButtonDispatchers;
71 private String mCurrentLayout;
72
73 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
74 super(context, attrs);
75 mLayoutInflater = LayoutInflater.from(context);
76 Configuration landscape = new Configuration();
77 landscape.setTo(context.getResources().getConfiguration());
78 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
79 mLandscapeInflater = LayoutInflater.from(context.createConfigurationContext(landscape));
80 }
81
82 @Override
83 protected void onFinishInflate() {
84 super.onFinishInflate();
85 mRot0 = (FrameLayout) findViewById(R.id.rot0);
86 mRot90 = (FrameLayout) findViewById(R.id.rot90);
87 clearViews();
88 inflateLayout(getDefaultLayout());
89 }
90
Xiaohui Chen54816002016-01-25 11:11:11 -080091 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -050092 return mContext.getString(R.string.config_navBarLayout);
93 }
94
95 @Override
96 protected void onAttachedToWindow() {
97 super.onAttachedToWindow();
98 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
99 }
100
101 @Override
102 protected void onDetachedFromWindow() {
103 TunerService.get(getContext()).removeTunable(this);
104 super.onDetachedFromWindow();
105 }
106
107 @Override
108 public void onTuningChanged(String key, String newValue) {
109 if (NAV_BAR_VIEWS.equals(key)) {
110 if (newValue == null) {
111 newValue = getDefaultLayout();
112 }
113 if (!Objects.equals(mCurrentLayout, newValue)) {
114 clearViews();
115 inflateLayout(newValue);
116 }
117 }
118 }
119
120 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
121 mButtonDispatchers = buttonDisatchers;
122 for (int i = 0; i < buttonDisatchers.size(); i++) {
123 initiallyFill(buttonDisatchers.valueAt(i));
124 }
125 }
126
127 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500128 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500129 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500130 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500131 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500132 }
133
134 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
135 for (int i = 0; i < parent.getChildCount(); i++) {
136 // Need to manually search for each id, just in case each group has more than one
137 // of a single id. It probably mostly a waste of time, but shouldn't take long
138 // and will only happen once.
139 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
140 buttonDispatcher.addView(parent.getChildAt(i));
141 } else if (parent.getChildAt(i) instanceof ViewGroup) {
142 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
143 }
144 }
145 }
146
Xiaohui Chen54816002016-01-25 11:11:11 -0800147 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500148 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800149 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500150 String[] start = sets[0].split(BUTTON_SEPARATOR);
151 String[] center = sets[1].split(BUTTON_SEPARATOR);
152 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monk4f878ef2016-01-23 14:37:38 -0500153 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group),
154 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
155 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group),
156 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
157
Xiaohui Chen06917032016-01-26 11:20:39 -0800158 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group),
159 (ViewGroup) mRot0.findViewById(R.id.center_group_lightsout), false);
160 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group),
161 (ViewGroup) mRot90.findViewById(R.id.center_group_lightsout), true);
162
163 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
164 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500165
166 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group),
167 (ViewGroup) mRot0.findViewById(R.id.ends_group_lightsout), false);
168 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group),
169 (ViewGroup) mRot90.findViewById(R.id.ends_group_lightsout), true);
170 }
171
Jason Monk4f878ef2016-01-23 14:37:38 -0500172 private void addGravitySpacer(LinearLayout layout) {
173 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500174 }
175
176 private void inflateButtons(String[] buttons, ViewGroup parent, ViewGroup lightsOutParent,
177 boolean landscape) {
178 for (int i = 0; i < buttons.length; i++) {
179 copyToLightsout(inflateButton(buttons[i], parent, landscape), lightsOutParent);
180 }
181 }
182
183 private void copyToLightsout(View view, ViewGroup lightsOutParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800184 if (view == null) return;
185
Jason Monka2081822016-01-18 14:41:03 -0500186 if (view instanceof FrameLayout) {
187 // The only ViewGroup we support in here is a FrameLayout, so copy those manually.
188 FrameLayout original = (FrameLayout) view;
189 FrameLayout layout = new FrameLayout(view.getContext());
190 for (int i = 0; i < original.getChildCount(); i++) {
191 copyToLightsout(original.getChildAt(i), layout);
192 }
193 lightsOutParent.addView(layout, copy(view.getLayoutParams()));
194 } else if (view instanceof Space) {
195 lightsOutParent.addView(new Space(view.getContext()), copy(view.getLayoutParams()));
196 } else {
197 lightsOutParent.addView(generateLightsOutView(view), copy(view.getLayoutParams()));
198 }
199 }
200
201 private View generateLightsOutView(View view) {
202 ImageView imageView = new ImageView(view.getContext());
203 // Copy everything we can about the original view.
204 imageView.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(),
205 view.getPaddingBottom());
206 imageView.setContentDescription(view.getContentDescription());
207 imageView.setId(view.getId());
208 // Only home gets a big dot, everything else will be little.
209 imageView.setImageResource(view.getId() == R.id.home
210 ? R.drawable.ic_sysbar_lights_out_dot_large
211 : R.drawable.ic_sysbar_lights_out_dot_small);
212 return imageView;
213 }
214
215 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500216 if (layoutParams instanceof LinearLayout.LayoutParams) {
217 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
218 ((LinearLayout.LayoutParams) layoutParams).weight);
219 }
Jason Monka2081822016-01-18 14:41:03 -0500220 return new LayoutParams(layoutParams.width, layoutParams.height);
221 }
222
Xiaohui Chen54816002016-01-25 11:11:11 -0800223 @Nullable
Jason Monk46a196e2016-01-23 15:28:10 -0500224 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800225 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500226 float size = extractSize(buttonSpec);
227 String button = extractButton(buttonSpec);
228 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500229 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800230 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500231 if (landscape && isSw600Dp()) {
232 setupLandButton(v);
233 }
234 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800235 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500236 if (landscape && isSw600Dp()) {
237 setupLandButton(v);
238 }
239 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800240 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500241 if (landscape && isSw600Dp()) {
242 setupLandButton(v);
243 }
244 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800245 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500246 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800247 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500248 } else if (CLIPBOARD.equals(button)) {
249 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500250 } else if (button.startsWith(KEY)) {
251 String uri = extractImage(button);
252 int code = extractKeycode(button);
253 v = inflater.inflate(R.layout.custom_key, parent, false);
254 ((KeyButtonView) v).setCode(code);
255 if (uri != null) {
256 ((KeyButtonView) v).loadAsync(uri);
257 }
Jason Monka2081822016-01-18 14:41:03 -0500258 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800259 return null;
Jason Monka2081822016-01-18 14:41:03 -0500260 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800261
Jason Monk46a196e2016-01-23 15:28:10 -0500262 if (size != 0) {
263 ViewGroup.LayoutParams params = v.getLayoutParams();
264 params.width = (int) (params.width * size);
265 }
Jason Monka2081822016-01-18 14:41:03 -0500266 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500267 addToDispatchers(v);
268 return v;
269 }
270
Jason Monk8457ad82016-01-24 10:15:55 -0500271 public static String extractImage(String buttonSpec) {
272 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
273 return null;
274 }
275 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
276 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
277 return subStr;
278 }
279
280 public static int extractKeycode(String buttonSpec) {
281 if (!buttonSpec.contains(KEY_CODE_START)) {
282 return 1;
283 }
284 final int start = buttonSpec.indexOf(KEY_CODE_START);
285 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
286 return Integer.parseInt(subStr);
287 }
288
Jason Monk46a196e2016-01-23 15:28:10 -0500289 public static float extractSize(String buttonSpec) {
290 if (!buttonSpec.contains(SIZE_MOD_START)) {
291 return 1;
292 }
293 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
294 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
295 return Float.parseFloat(sizeStr);
296 }
297
298 public static String extractButton(String buttonSpec) {
299 if (!buttonSpec.contains(SIZE_MOD_START)) {
300 return buttonSpec;
301 }
302 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
303 }
304
Jason Monk4f878ef2016-01-23 14:37:38 -0500305 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500306 if (mButtonDispatchers != null) {
307 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
308 if (indexOfKey >= 0) {
309 mButtonDispatchers.valueAt(indexOfKey).addView(v);
310 }
311 }
Jason Monka2081822016-01-18 14:41:03 -0500312 }
313
314 private boolean isSw600Dp() {
315 Configuration configuration = mContext.getResources().getConfiguration();
316 return (configuration.smallestScreenWidthDp >= 600);
317 }
318
319 /**
320 * This manually sets the width of sw600dp landscape buttons because despite
321 * overriding the configuration from the overridden resources aren't loaded currently.
322 */
323 private void setupLandButton(View v) {
324 Resources res = mContext.getResources();
325 v.getLayoutParams().width = res.getDimensionPixelOffset(
326 R.dimen.navigation_key_width_sw600dp_land);
327 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
328 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
329 }
330
331 private void clearViews() {
332 if (mButtonDispatchers != null) {
333 for (int i = 0; i < mButtonDispatchers.size(); i++) {
334 mButtonDispatchers.valueAt(i).clear();
335 }
336 }
337 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
338 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.lights_out));
339 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
340 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.lights_out));
341 }
342
343 private void clearAllChildren(ViewGroup group) {
344 for (int i = 0; i < group.getChildCount(); i++) {
345 ((ViewGroup) group.getChildAt(i)).removeAllViews();
346 }
347 }
348}