blob: b2b093cf0033a02e86f177d90574cc1cdecc5007 [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;
Jason Monka2081822016-01-18 14:41:03 -050020import android.util.AttributeSet;
21import android.util.SparseArray;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080022import android.view.Display;
23import android.view.Display.Mode;
Jason Monka2081822016-01-18 14:41:03 -050024import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080027import android.view.WindowManager;
Jason Monka2081822016-01-18 14:41:03 -050028import android.widget.FrameLayout;
Jason Monk4f878ef2016-01-23 14:37:38 -050029import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050030import android.widget.Space;
Annie Chin1ea49352016-05-27 15:23:35 -070031
Jason Monka2081822016-01-18 14:41:03 -050032import com.android.systemui.R;
Jason Monk197f4db2016-08-26 13:28:20 -040033import com.android.systemui.plugins.PluginListener;
34import com.android.systemui.plugins.PluginManager;
35import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider;
Jason Monk8457ad82016-01-24 10:15:55 -050036import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050037import com.android.systemui.tuner.TunerService;
Jason Monk197f4db2016-08-26 13:28:20 -040038import com.android.systemui.tuner.TunerService.Tunable;
Jason Monka2081822016-01-18 14:41:03 -050039
Jason Monk197f4db2016-08-26 13:28:20 -040040import java.util.ArrayList;
41import java.util.List;
Jason Monka2081822016-01-18 14:41:03 -050042import java.util.Objects;
43
Jason Monk197f4db2016-08-26 13:28:20 -040044public class NavigationBarInflaterView extends FrameLayout
45 implements Tunable, PluginListener<NavBarButtonProvider> {
Jason Monka2081822016-01-18 14:41:03 -050046
47 private static final String TAG = "NavBarInflater";
48
49 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
50
Jason Monk3ebd2392016-01-22 10:01:44 -050051 public static final String MENU_IME = "menu_ime";
52 public static final String BACK = "back";
53 public static final String HOME = "home";
54 public static final String RECENT = "recent";
55 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050056 public static final String CLIPBOARD = "clipboard";
Jason Monk8457ad82016-01-24 10:15:55 -050057 public static final String KEY = "key";
Jason Monka2081822016-01-18 14:41:03 -050058
59 public static final String GRAVITY_SEPARATOR = ";";
60 public static final String BUTTON_SEPARATOR = ",";
61
Jason Monk46a196e2016-01-23 15:28:10 -050062 public static final String SIZE_MOD_START = "[";
63 public static final String SIZE_MOD_END = "]";
64
Jason Monk8457ad82016-01-24 10:15:55 -050065 public static final String KEY_CODE_START = "(";
66 public static final String KEY_IMAGE_DELIM = ":";
67 public static final String KEY_CODE_END = ")";
68
Jason Monk197f4db2016-08-26 13:28:20 -040069 private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
70
Jason Monk67a1cce2016-02-05 13:31:03 -050071 protected LayoutInflater mLayoutInflater;
72 protected LayoutInflater mLandscapeInflater;
73 private int mDensity;
Jason Monk8457ad82016-01-24 10:15:55 -050074
Xiaohui Chen54816002016-01-25 11:11:11 -080075 protected FrameLayout mRot0;
76 protected FrameLayout mRot90;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080077 private boolean isRot0Landscape;
Jason Monk8457ad82016-01-24 10:15:55 -050078
Jason Monka2081822016-01-18 14:41:03 -050079 private SparseArray<ButtonDispatcher> mButtonDispatchers;
80 private String mCurrentLayout;
81
Xiaohui Chen40e978e2016-11-29 15:10:04 -080082 private View mLastPortrait;
83 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040084
Adrian Roosdb12b152016-07-12 15:38:55 -070085 private boolean mAlternativeOrder;
86
Jason Monka2081822016-01-18 14:41:03 -050087 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
88 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -050089 mDensity = context.getResources().getConfiguration().densityDpi;
90 createInflaters();
Xiaohui Chen40e978e2016-11-29 15:10:04 -080091 Display display = ((WindowManager)
92 context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
93 Mode displayMode = display.getMode();
94 isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
Jason Monk67a1cce2016-02-05 13:31:03 -050095 }
96
97 private void createInflaters() {
98 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -050099 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500100 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500101 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500102 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
103 }
104
105 @Override
106 protected void onConfigurationChanged(Configuration newConfig) {
107 super.onConfigurationChanged(newConfig);
108 if (mDensity != newConfig.densityDpi) {
109 mDensity = newConfig.densityDpi;
110 createInflaters();
Jason Monk9a6552d2016-05-20 11:21:59 -0400111 inflateChildren();
Jason Monk67a1cce2016-02-05 13:31:03 -0500112 clearViews();
113 inflateLayout(mCurrentLayout);
114 }
Jason Monka2081822016-01-18 14:41:03 -0500115 }
116
117 @Override
118 protected void onFinishInflate() {
119 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400120 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500121 clearViews();
122 inflateLayout(getDefaultLayout());
123 }
124
Jason Monk9a6552d2016-05-20 11:21:59 -0400125 private void inflateChildren() {
126 removeAllViews();
127 mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
128 mRot0.setId(R.id.rot0);
129 addView(mRot0);
130 mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
131 false);
132 mRot90.setId(R.id.rot90);
133 addView(mRot90);
Adrian Roosdb12b152016-07-12 15:38:55 -0700134 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400135 if (getParent() instanceof NavigationBarView) {
136 ((NavigationBarView) getParent()).updateRotatedViews();
137 }
138 }
139
Xiaohui Chen54816002016-01-25 11:11:11 -0800140 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500141 return mContext.getString(R.string.config_navBarLayout);
142 }
143
144 @Override
145 protected void onAttachedToWindow() {
146 super.onAttachedToWindow();
147 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
Jason Monk197f4db2016-08-26 13:28:20 -0400148 PluginManager.getInstance(getContext()).addPluginListener(NavBarButtonProvider.ACTION, this,
149 NavBarButtonProvider.VERSION, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500150 }
151
152 @Override
153 protected void onDetachedFromWindow() {
154 TunerService.get(getContext()).removeTunable(this);
155 super.onDetachedFromWindow();
156 }
157
158 @Override
159 public void onTuningChanged(String key, String newValue) {
160 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500161 if (!Objects.equals(mCurrentLayout, newValue)) {
162 clearViews();
163 inflateLayout(newValue);
164 }
165 }
166 }
167
168 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
169 mButtonDispatchers = buttonDisatchers;
170 for (int i = 0; i < buttonDisatchers.size(); i++) {
171 initiallyFill(buttonDisatchers.valueAt(i));
172 }
173 }
174
Adrian Roosdb12b152016-07-12 15:38:55 -0700175 public void setAlternativeOrder(boolean alternativeOrder) {
176 if (alternativeOrder != mAlternativeOrder) {
177 mAlternativeOrder = alternativeOrder;
178 updateAlternativeOrder();
179 }
180 }
181
182 private void updateAlternativeOrder() {
183 updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
184 updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
185 updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
186 updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
187 }
188
189 private void updateAlternativeOrder(View v) {
190 if (v instanceof ReverseLinearLayout) {
191 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
192 }
193 }
194
Jason Monka2081822016-01-18 14:41:03 -0500195 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500196 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500197 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500198 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500199 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500200 }
201
202 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
203 for (int i = 0; i < parent.getChildCount(); i++) {
204 // Need to manually search for each id, just in case each group has more than one
205 // of a single id. It probably mostly a waste of time, but shouldn't take long
206 // and will only happen once.
207 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
208 buttonDispatcher.addView(parent.getChildAt(i));
209 } else if (parent.getChildAt(i) instanceof ViewGroup) {
210 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
211 }
212 }
213 }
214
Xiaohui Chen54816002016-01-25 11:11:11 -0800215 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500216 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400217 if (newLayout == null) {
218 newLayout = getDefaultLayout();
219 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800220 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500221 String[] start = sets[0].split(BUTTON_SEPARATOR);
222 String[] center = sets[1].split(BUTTON_SEPARATOR);
223 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400224 // Inflate these in start to end order or accessibility traversal will be messed up.
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800225 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);
226 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);
Jason Monk4f878ef2016-01-23 14:37:38 -0500227
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800228 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), isRot0Landscape);
229 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), !isRot0Landscape);
Xiaohui Chen06917032016-01-26 11:20:39 -0800230
231 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
232 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500233
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800234 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);
235 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);
Jason Monk4f878ef2016-01-23 14:37:38 -0500236 }
237
Jason Monk4f878ef2016-01-23 14:37:38 -0500238 private void addGravitySpacer(LinearLayout layout) {
239 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500240 }
241
Jason Monk5332e592016-02-15 15:16:57 -0500242 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape) {
Jason Monka2081822016-01-18 14:41:03 -0500243 for (int i = 0; i < buttons.length; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800244 inflateButton(buttons[i], parent, landscape);
Jason Monka2081822016-01-18 14:41:03 -0500245 }
246 }
247
Jason Monka2081822016-01-18 14:41:03 -0500248 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500249 if (layoutParams instanceof LinearLayout.LayoutParams) {
250 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
251 ((LinearLayout.LayoutParams) layoutParams).weight);
252 }
Jason Monka2081822016-01-18 14:41:03 -0500253 return new LayoutParams(layoutParams.width, layoutParams.height);
254 }
255
Xiaohui Chen54816002016-01-25 11:11:11 -0800256 @Nullable
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800257 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800258 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500259 float size = extractSize(buttonSpec);
Jason Monk197f4db2016-08-26 13:28:20 -0400260 View v = createView(buttonSpec, parent, inflater, landscape);
261 if (v == null) return null;
262
263 if (size != 0) {
264 ViewGroup.LayoutParams params = v.getLayoutParams();
265 params.width = (int) (params.width * size);
266 }
267 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800268 addToDispatchers(v);
269 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monk197f4db2016-08-26 13:28:20 -0400270 if (lastView != null) {
271 v.setAccessibilityTraversalAfter(lastView.getId());
272 }
273 if (landscape) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800274 mLastLandscape = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400275 } else {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800276 mLastPortrait = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400277 }
278 return v;
279 }
280
281 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater,
282 boolean landscape) {
Jason Monk46a196e2016-01-23 15:28:10 -0500283 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400284 String button = extractButton(buttonSpec);
285 // Let plugins go first so they can override a standard view if they want.
286 for (NavBarButtonProvider provider : mPlugins) {
287 v = provider.createView(buttonSpec, parent);
288 if (v != null) return v;
289 }
Jason Monka2081822016-01-18 14:41:03 -0500290 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700291 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500292 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800293 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500294 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800295 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500296 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800297 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500298 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800299 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500300 } else if (CLIPBOARD.equals(button)) {
301 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500302 } else if (button.startsWith(KEY)) {
303 String uri = extractImage(button);
304 int code = extractKeycode(button);
305 v = inflater.inflate(R.layout.custom_key, parent, false);
306 ((KeyButtonView) v).setCode(code);
307 if (uri != null) {
308 ((KeyButtonView) v).loadAsync(uri);
309 }
Jason Monkc62cf802016-05-10 11:02:24 -0400310 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500311 return v;
312 }
313
Jason Monk8457ad82016-01-24 10:15:55 -0500314 public static String extractImage(String buttonSpec) {
315 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
316 return null;
317 }
318 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
319 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
320 return subStr;
321 }
322
323 public static int extractKeycode(String buttonSpec) {
324 if (!buttonSpec.contains(KEY_CODE_START)) {
325 return 1;
326 }
327 final int start = buttonSpec.indexOf(KEY_CODE_START);
328 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
329 return Integer.parseInt(subStr);
330 }
331
Jason Monk46a196e2016-01-23 15:28:10 -0500332 public static float extractSize(String buttonSpec) {
333 if (!buttonSpec.contains(SIZE_MOD_START)) {
334 return 1;
335 }
336 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
337 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
338 return Float.parseFloat(sizeStr);
339 }
340
341 public static String extractButton(String buttonSpec) {
342 if (!buttonSpec.contains(SIZE_MOD_START)) {
343 return buttonSpec;
344 }
345 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
346 }
347
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800348 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500349 if (mButtonDispatchers != null) {
350 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
351 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800352 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700353 } else if (v instanceof ViewGroup) {
354 final ViewGroup viewGroup = (ViewGroup)v;
355 final int N = viewGroup.getChildCount();
356 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800357 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700358 }
Jason Monka2081822016-01-18 14:41:03 -0500359 }
360 }
Jason Monka2081822016-01-18 14:41:03 -0500361 }
362
Jason Monka2081822016-01-18 14:41:03 -0500363
Jason Monka2081822016-01-18 14:41:03 -0500364
365 private void clearViews() {
366 if (mButtonDispatchers != null) {
367 for (int i = 0; i < mButtonDispatchers.size(); i++) {
368 mButtonDispatchers.valueAt(i).clear();
369 }
370 }
371 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500372 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500373 }
374
375 private void clearAllChildren(ViewGroup group) {
376 for (int i = 0; i < group.getChildCount(); i++) {
377 ((ViewGroup) group.getChildAt(i)).removeAllViews();
378 }
379 }
Jason Monk197f4db2016-08-26 13:28:20 -0400380
381 @Override
382 public void onPluginConnected(NavBarButtonProvider plugin) {
383 mPlugins.add(plugin);
384 clearViews();
385 inflateLayout(mCurrentLayout);
386 }
387
388 @Override
389 public void onPluginDisconnected(NavBarButtonProvider plugin) {
390 mPlugins.remove(plugin);
391 clearViews();
392 inflateLayout(mCurrentLayout);
393 }
Jason Monka2081822016-01-18 14:41:03 -0500394}