blob: 4ec36f60811133933d0879a86f3e3d5509f16318 [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
Jason Monkc62cf802016-05-10 11:02:24 -040070 private View mLastRot0;
71 private View mLastRot90;
72
Jason Monka2081822016-01-18 14:41:03 -050073 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
74 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -050075 mDensity = context.getResources().getConfiguration().densityDpi;
76 createInflaters();
77 }
78
79 private void createInflaters() {
80 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -050081 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -050082 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -050083 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -050084 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
85 }
86
87 @Override
88 protected void onConfigurationChanged(Configuration newConfig) {
89 super.onConfigurationChanged(newConfig);
90 if (mDensity != newConfig.densityDpi) {
91 mDensity = newConfig.densityDpi;
92 createInflaters();
93 clearViews();
94 inflateLayout(mCurrentLayout);
95 }
Jason Monka2081822016-01-18 14:41:03 -050096 }
97
98 @Override
99 protected void onFinishInflate() {
100 super.onFinishInflate();
101 mRot0 = (FrameLayout) findViewById(R.id.rot0);
102 mRot90 = (FrameLayout) findViewById(R.id.rot90);
103 clearViews();
104 inflateLayout(getDefaultLayout());
105 }
106
Xiaohui Chen54816002016-01-25 11:11:11 -0800107 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500108 return mContext.getString(R.string.config_navBarLayout);
109 }
110
111 @Override
112 protected void onAttachedToWindow() {
113 super.onAttachedToWindow();
114 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
115 }
116
117 @Override
118 protected void onDetachedFromWindow() {
119 TunerService.get(getContext()).removeTunable(this);
120 super.onDetachedFromWindow();
121 }
122
123 @Override
124 public void onTuningChanged(String key, String newValue) {
125 if (NAV_BAR_VIEWS.equals(key)) {
126 if (newValue == null) {
127 newValue = getDefaultLayout();
128 }
129 if (!Objects.equals(mCurrentLayout, newValue)) {
130 clearViews();
131 inflateLayout(newValue);
132 }
133 }
134 }
135
136 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
137 mButtonDispatchers = buttonDisatchers;
138 for (int i = 0; i < buttonDisatchers.size(); i++) {
139 initiallyFill(buttonDisatchers.valueAt(i));
140 }
141 }
142
143 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500144 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500145 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500146 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500147 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500148 }
149
150 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
151 for (int i = 0; i < parent.getChildCount(); i++) {
152 // Need to manually search for each id, just in case each group has more than one
153 // of a single id. It probably mostly a waste of time, but shouldn't take long
154 // and will only happen once.
155 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
156 buttonDispatcher.addView(parent.getChildAt(i));
157 } else if (parent.getChildAt(i) instanceof ViewGroup) {
158 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
159 }
160 }
161 }
162
Xiaohui Chen54816002016-01-25 11:11:11 -0800163 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500164 mCurrentLayout = newLayout;
Xiaohui Chen54816002016-01-25 11:11:11 -0800165 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500166 String[] start = sets[0].split(BUTTON_SEPARATOR);
167 String[] center = sets[1].split(BUTTON_SEPARATOR);
168 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400169 // Inflate these in start to end order or accessibility traversal will be messed up.
Jason Monk5332e592016-02-15 15:16:57 -0500170 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), false);
171 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500172
Jason Monk5332e592016-02-15 15:16:57 -0500173 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), false);
174 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), true);
Xiaohui Chen06917032016-01-26 11:20:39 -0800175
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
Jason Monk5332e592016-02-15 15:16:57 -0500179 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), false);
180 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500181 }
182
Jason Monk4f878ef2016-01-23 14:37:38 -0500183 private void addGravitySpacer(LinearLayout layout) {
184 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500185 }
186
Jason Monk5332e592016-02-15 15:16:57 -0500187 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape) {
Jason Monka2081822016-01-18 14:41:03 -0500188 for (int i = 0; i < buttons.length; i++) {
Yorke Lee0681c092016-04-06 15:15:19 -0700189 inflateButton(buttons[i], parent, landscape, i);
Jason Monka2081822016-01-18 14:41:03 -0500190 }
191 }
192
Jason Monka2081822016-01-18 14:41:03 -0500193 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500194 if (layoutParams instanceof LinearLayout.LayoutParams) {
195 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
196 ((LinearLayout.LayoutParams) layoutParams).weight);
197 }
Jason Monka2081822016-01-18 14:41:03 -0500198 return new LayoutParams(layoutParams.width, layoutParams.height);
199 }
200
Xiaohui Chen54816002016-01-25 11:11:11 -0800201 @Nullable
Yorke Lee0681c092016-04-06 15:15:19 -0700202 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
203 int indexInParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800204 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500205 float size = extractSize(buttonSpec);
206 String button = extractButton(buttonSpec);
207 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500208 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800209 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500210 if (landscape && isSw600Dp()) {
211 setupLandButton(v);
212 }
213 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800214 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500215 if (landscape && isSw600Dp()) {
216 setupLandButton(v);
217 }
218 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800219 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500220 if (landscape && isSw600Dp()) {
221 setupLandButton(v);
222 }
223 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800224 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500225 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800226 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500227 } else if (CLIPBOARD.equals(button)) {
228 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500229 } else if (button.startsWith(KEY)) {
230 String uri = extractImage(button);
231 int code = extractKeycode(button);
232 v = inflater.inflate(R.layout.custom_key, parent, false);
233 ((KeyButtonView) v).setCode(code);
234 if (uri != null) {
235 ((KeyButtonView) v).loadAsync(uri);
236 }
Jason Monka2081822016-01-18 14:41:03 -0500237 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800238 return null;
Jason Monka2081822016-01-18 14:41:03 -0500239 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800240
Jason Monk46a196e2016-01-23 15:28:10 -0500241 if (size != 0) {
242 ViewGroup.LayoutParams params = v.getLayoutParams();
243 params.width = (int) (params.width * size);
244 }
Jason Monka2081822016-01-18 14:41:03 -0500245 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500246 addToDispatchers(v);
Jason Monkc62cf802016-05-10 11:02:24 -0400247 View lastView = landscape ? mLastRot90 : mLastRot0;
248 if (lastView != null) {
249 v.setAccessibilityTraversalAfter(lastView.getId());
250 }
251 if (landscape) {
252 mLastRot90 = v;
253 } else {
254 mLastRot0 = v;
255 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500256 return v;
257 }
258
Jason Monk8457ad82016-01-24 10:15:55 -0500259 public static String extractImage(String buttonSpec) {
260 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
261 return null;
262 }
263 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
264 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
265 return subStr;
266 }
267
268 public static int extractKeycode(String buttonSpec) {
269 if (!buttonSpec.contains(KEY_CODE_START)) {
270 return 1;
271 }
272 final int start = buttonSpec.indexOf(KEY_CODE_START);
273 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
274 return Integer.parseInt(subStr);
275 }
276
Jason Monk46a196e2016-01-23 15:28:10 -0500277 public static float extractSize(String buttonSpec) {
278 if (!buttonSpec.contains(SIZE_MOD_START)) {
279 return 1;
280 }
281 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
282 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
283 return Float.parseFloat(sizeStr);
284 }
285
286 public static String extractButton(String buttonSpec) {
287 if (!buttonSpec.contains(SIZE_MOD_START)) {
288 return buttonSpec;
289 }
290 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
291 }
292
Jason Monk4f878ef2016-01-23 14:37:38 -0500293 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500294 if (mButtonDispatchers != null) {
295 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
296 if (indexOfKey >= 0) {
297 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700298 } else if (v instanceof ViewGroup) {
299 final ViewGroup viewGroup = (ViewGroup)v;
300 final int N = viewGroup.getChildCount();
301 for (int i = 0; i < N; i++) {
302 addToDispatchers(viewGroup.getChildAt(i));
303 }
Jason Monka2081822016-01-18 14:41:03 -0500304 }
305 }
Jason Monka2081822016-01-18 14:41:03 -0500306 }
307
308 private boolean isSw600Dp() {
309 Configuration configuration = mContext.getResources().getConfiguration();
310 return (configuration.smallestScreenWidthDp >= 600);
311 }
312
313 /**
314 * This manually sets the width of sw600dp landscape buttons because despite
315 * overriding the configuration from the overridden resources aren't loaded currently.
316 */
317 private void setupLandButton(View v) {
318 Resources res = mContext.getResources();
319 v.getLayoutParams().width = res.getDimensionPixelOffset(
320 R.dimen.navigation_key_width_sw600dp_land);
321 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
322 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
323 }
324
325 private void clearViews() {
326 if (mButtonDispatchers != null) {
327 for (int i = 0; i < mButtonDispatchers.size(); i++) {
328 mButtonDispatchers.valueAt(i).clear();
329 }
330 }
331 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500332 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500333 }
334
335 private void clearAllChildren(ViewGroup group) {
336 for (int i = 0; i < group.getChildCount(); i++) {
337 ((ViewGroup) group.getChildAt(i)).removeAllViews();
338 }
339 }
340}