blob: ee4a102937603604d8165ec221d0fa53b0b16255 [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 Monk5332e592016-02-15 15:16:57 -0500166 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), false);
167 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500168
Jason Monk5332e592016-02-15 15:16:57 -0500169 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), false);
170 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), true);
Xiaohui Chen06917032016-01-26 11:20:39 -0800171
172 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
173 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500174
Jason Monk5332e592016-02-15 15:16:57 -0500175 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), false);
176 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), true);
Jason Monk4f878ef2016-01-23 14:37:38 -0500177 }
178
Jason Monk4f878ef2016-01-23 14:37:38 -0500179 private void addGravitySpacer(LinearLayout layout) {
180 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500181 }
182
Jason Monk5332e592016-02-15 15:16:57 -0500183 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape) {
Jason Monka2081822016-01-18 14:41:03 -0500184 for (int i = 0; i < buttons.length; i++) {
Yorke Lee0681c092016-04-06 15:15:19 -0700185 inflateButton(buttons[i], parent, landscape, i);
Jason Monka2081822016-01-18 14:41:03 -0500186 }
187 }
188
Jason Monka2081822016-01-18 14:41:03 -0500189 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500190 if (layoutParams instanceof LinearLayout.LayoutParams) {
191 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
192 ((LinearLayout.LayoutParams) layoutParams).weight);
193 }
Jason Monka2081822016-01-18 14:41:03 -0500194 return new LayoutParams(layoutParams.width, layoutParams.height);
195 }
196
Xiaohui Chen54816002016-01-25 11:11:11 -0800197 @Nullable
Yorke Lee0681c092016-04-06 15:15:19 -0700198 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
199 int indexInParent) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800200 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500201 float size = extractSize(buttonSpec);
202 String button = extractButton(buttonSpec);
203 View v = null;
Jason Monka2081822016-01-18 14:41:03 -0500204 if (HOME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800205 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500206 if (landscape && isSw600Dp()) {
207 setupLandButton(v);
208 }
209 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800210 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500211 if (landscape && isSw600Dp()) {
212 setupLandButton(v);
213 }
214 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800215 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500216 if (landscape && isSw600Dp()) {
217 setupLandButton(v);
218 }
219 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800220 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500221 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800222 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500223 } else if (CLIPBOARD.equals(button)) {
224 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500225 } else if (button.startsWith(KEY)) {
226 String uri = extractImage(button);
227 int code = extractKeycode(button);
228 v = inflater.inflate(R.layout.custom_key, parent, false);
229 ((KeyButtonView) v).setCode(code);
230 if (uri != null) {
231 ((KeyButtonView) v).loadAsync(uri);
232 }
Jason Monka2081822016-01-18 14:41:03 -0500233 } else {
Xiaohui Chen54816002016-01-25 11:11:11 -0800234 return null;
Jason Monka2081822016-01-18 14:41:03 -0500235 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800236
Jason Monk46a196e2016-01-23 15:28:10 -0500237 if (size != 0) {
238 ViewGroup.LayoutParams params = v.getLayoutParams();
239 params.width = (int) (params.width * size);
240 }
Jason Monka2081822016-01-18 14:41:03 -0500241 parent.addView(v);
Jason Monk4f878ef2016-01-23 14:37:38 -0500242 addToDispatchers(v);
243 return v;
244 }
245
Jason Monk8457ad82016-01-24 10:15:55 -0500246 public static String extractImage(String buttonSpec) {
247 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
248 return null;
249 }
250 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
251 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
252 return subStr;
253 }
254
255 public static int extractKeycode(String buttonSpec) {
256 if (!buttonSpec.contains(KEY_CODE_START)) {
257 return 1;
258 }
259 final int start = buttonSpec.indexOf(KEY_CODE_START);
260 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
261 return Integer.parseInt(subStr);
262 }
263
Jason Monk46a196e2016-01-23 15:28:10 -0500264 public static float extractSize(String buttonSpec) {
265 if (!buttonSpec.contains(SIZE_MOD_START)) {
266 return 1;
267 }
268 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
269 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
270 return Float.parseFloat(sizeStr);
271 }
272
273 public static String extractButton(String buttonSpec) {
274 if (!buttonSpec.contains(SIZE_MOD_START)) {
275 return buttonSpec;
276 }
277 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
278 }
279
Jason Monk4f878ef2016-01-23 14:37:38 -0500280 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500281 if (mButtonDispatchers != null) {
282 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
283 if (indexOfKey >= 0) {
284 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700285 } else if (v instanceof ViewGroup) {
286 final ViewGroup viewGroup = (ViewGroup)v;
287 final int N = viewGroup.getChildCount();
288 for (int i = 0; i < N; i++) {
289 addToDispatchers(viewGroup.getChildAt(i));
290 }
Jason Monka2081822016-01-18 14:41:03 -0500291 }
292 }
Jason Monka2081822016-01-18 14:41:03 -0500293 }
294
295 private boolean isSw600Dp() {
296 Configuration configuration = mContext.getResources().getConfiguration();
297 return (configuration.smallestScreenWidthDp >= 600);
298 }
299
300 /**
301 * This manually sets the width of sw600dp landscape buttons because despite
302 * overriding the configuration from the overridden resources aren't loaded currently.
303 */
304 private void setupLandButton(View v) {
305 Resources res = mContext.getResources();
306 v.getLayoutParams().width = res.getDimensionPixelOffset(
307 R.dimen.navigation_key_width_sw600dp_land);
308 int padding = res.getDimensionPixelOffset(R.dimen.navigation_key_padding_sw600dp_land);
309 v.setPadding(padding, v.getPaddingTop(), padding, v.getPaddingBottom());
310 }
311
312 private void clearViews() {
313 if (mButtonDispatchers != null) {
314 for (int i = 0; i < mButtonDispatchers.size(); i++) {
315 mButtonDispatchers.valueAt(i).clear();
316 }
317 }
318 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500319 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500320 }
321
322 private void clearAllChildren(ViewGroup group) {
323 for (int i = 0; i < group.getChildCount(); i++) {
324 ((ViewGroup) group.getChildAt(i)).removeAllViews();
325 }
326 }
327}