blob: f04a9ee7140416174cddaefc00e971c0f88daabb [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;
Jason Monk8457ad82016-01-24 10:15:55 -050073
Xiaohui Chen54816002016-01-25 11:11:11 -080074 protected FrameLayout mRot0;
75 protected FrameLayout mRot90;
Xiaohui Chen40e978e2016-11-29 15:10:04 -080076 private boolean isRot0Landscape;
Jason Monk8457ad82016-01-24 10:15:55 -050077
Jason Monka2081822016-01-18 14:41:03 -050078 private SparseArray<ButtonDispatcher> mButtonDispatchers;
79 private String mCurrentLayout;
80
Xiaohui Chen40e978e2016-11-29 15:10:04 -080081 private View mLastPortrait;
82 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040083
Adrian Roosdb12b152016-07-12 15:38:55 -070084 private boolean mAlternativeOrder;
85
Jason Monka2081822016-01-18 14:41:03 -050086 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
87 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -050088 createInflaters();
Xiaohui Chen40e978e2016-11-29 15:10:04 -080089 Display display = ((WindowManager)
90 context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
91 Mode displayMode = display.getMode();
92 isRot0Landscape = displayMode.getPhysicalWidth() > displayMode.getPhysicalHeight();
Jason Monk67a1cce2016-02-05 13:31:03 -050093 }
94
95 private void createInflaters() {
96 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -050097 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -050098 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -050099 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500100 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
101 }
102
103 @Override
Jason Monka2081822016-01-18 14:41:03 -0500104 protected void onFinishInflate() {
105 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400106 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500107 clearViews();
108 inflateLayout(getDefaultLayout());
109 }
110
Jason Monk9a6552d2016-05-20 11:21:59 -0400111 private void inflateChildren() {
112 removeAllViews();
113 mRot0 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout, this, false);
114 mRot0.setId(R.id.rot0);
115 addView(mRot0);
116 mRot90 = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_rot90, this,
117 false);
118 mRot90.setId(R.id.rot90);
119 addView(mRot90);
Adrian Roosdb12b152016-07-12 15:38:55 -0700120 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400121 if (getParent() instanceof NavigationBarView) {
122 ((NavigationBarView) getParent()).updateRotatedViews();
123 }
124 }
125
Xiaohui Chen54816002016-01-25 11:11:11 -0800126 protected String getDefaultLayout() {
Jason Monka2081822016-01-18 14:41:03 -0500127 return mContext.getString(R.string.config_navBarLayout);
128 }
129
130 @Override
131 protected void onAttachedToWindow() {
132 super.onAttachedToWindow();
133 TunerService.get(getContext()).addTunable(this, NAV_BAR_VIEWS);
Jason Monk197f4db2016-08-26 13:28:20 -0400134 PluginManager.getInstance(getContext()).addPluginListener(NavBarButtonProvider.ACTION, this,
135 NavBarButtonProvider.VERSION, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500136 }
137
138 @Override
139 protected void onDetachedFromWindow() {
140 TunerService.get(getContext()).removeTunable(this);
141 super.onDetachedFromWindow();
142 }
143
144 @Override
145 public void onTuningChanged(String key, String newValue) {
146 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500147 if (!Objects.equals(mCurrentLayout, newValue)) {
148 clearViews();
149 inflateLayout(newValue);
150 }
151 }
152 }
153
154 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDisatchers) {
155 mButtonDispatchers = buttonDisatchers;
156 for (int i = 0; i < buttonDisatchers.size(); i++) {
157 initiallyFill(buttonDisatchers.valueAt(i));
158 }
159 }
160
Adrian Roosdb12b152016-07-12 15:38:55 -0700161 public void setAlternativeOrder(boolean alternativeOrder) {
162 if (alternativeOrder != mAlternativeOrder) {
163 mAlternativeOrder = alternativeOrder;
164 updateAlternativeOrder();
165 }
166 }
167
168 private void updateAlternativeOrder() {
169 updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
170 updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
171 updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
172 updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
173 }
174
175 private void updateAlternativeOrder(View v) {
176 if (v instanceof ReverseLinearLayout) {
177 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
178 }
179 }
180
Jason Monka2081822016-01-18 14:41:03 -0500181 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500182 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500183 addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500184 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.ends_group));
Jason Monka2081822016-01-18 14:41:03 -0500185 addAll(buttonDispatcher, (ViewGroup) mRot90.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500186 }
187
188 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
189 for (int i = 0; i < parent.getChildCount(); i++) {
190 // Need to manually search for each id, just in case each group has more than one
191 // of a single id. It probably mostly a waste of time, but shouldn't take long
192 // and will only happen once.
193 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
194 buttonDispatcher.addView(parent.getChildAt(i));
195 } else if (parent.getChildAt(i) instanceof ViewGroup) {
196 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
197 }
198 }
199 }
200
Xiaohui Chen54816002016-01-25 11:11:11 -0800201 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500202 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400203 if (newLayout == null) {
204 newLayout = getDefaultLayout();
205 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800206 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Jason Monka2081822016-01-18 14:41:03 -0500207 String[] start = sets[0].split(BUTTON_SEPARATOR);
208 String[] center = sets[1].split(BUTTON_SEPARATOR);
209 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400210 // Inflate these in start to end order or accessibility traversal will be messed up.
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800211 inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);
212 inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);
Jason Monk4f878ef2016-01-23 14:37:38 -0500213
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800214 inflateButtons(center, (ViewGroup) mRot0.findViewById(R.id.center_group), isRot0Landscape);
215 inflateButtons(center, (ViewGroup) mRot90.findViewById(R.id.center_group), !isRot0Landscape);
Xiaohui Chen06917032016-01-26 11:20:39 -0800216
217 addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));
218 addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500219
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800220 inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);
221 inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);
Jason Monk4f878ef2016-01-23 14:37:38 -0500222 }
223
Jason Monk4f878ef2016-01-23 14:37:38 -0500224 private void addGravitySpacer(LinearLayout layout) {
225 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500226 }
227
Jason Monk5332e592016-02-15 15:16:57 -0500228 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape) {
Jason Monka2081822016-01-18 14:41:03 -0500229 for (int i = 0; i < buttons.length; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800230 inflateButton(buttons[i], parent, landscape);
Jason Monka2081822016-01-18 14:41:03 -0500231 }
232 }
233
Jason Monka2081822016-01-18 14:41:03 -0500234 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500235 if (layoutParams instanceof LinearLayout.LayoutParams) {
236 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
237 ((LinearLayout.LayoutParams) layoutParams).weight);
238 }
Jason Monka2081822016-01-18 14:41:03 -0500239 return new LayoutParams(layoutParams.width, layoutParams.height);
240 }
241
Xiaohui Chen54816002016-01-25 11:11:11 -0800242 @Nullable
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800243 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800244 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk46a196e2016-01-23 15:28:10 -0500245 float size = extractSize(buttonSpec);
Jason Monk197f4db2016-08-26 13:28:20 -0400246 View v = createView(buttonSpec, parent, inflater, landscape);
247 if (v == null) return null;
248
249 if (size != 0) {
250 ViewGroup.LayoutParams params = v.getLayoutParams();
251 params.width = (int) (params.width * size);
252 }
253 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800254 addToDispatchers(v);
255 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monk197f4db2016-08-26 13:28:20 -0400256 if (lastView != null) {
257 v.setAccessibilityTraversalAfter(lastView.getId());
258 }
259 if (landscape) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800260 mLastLandscape = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400261 } else {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800262 mLastPortrait = v;
Jason Monk197f4db2016-08-26 13:28:20 -0400263 }
264 return v;
265 }
266
267 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater,
268 boolean landscape) {
Jason Monk46a196e2016-01-23 15:28:10 -0500269 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400270 String button = extractButton(buttonSpec);
271 // Let plugins go first so they can override a standard view if they want.
272 for (NavBarButtonProvider provider : mPlugins) {
273 v = provider.createView(buttonSpec, parent);
274 if (v != null) return v;
275 }
Jason Monka2081822016-01-18 14:41:03 -0500276 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700277 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500278 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800279 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500280 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800281 v = inflater.inflate(R.layout.recent_apps, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500282 } else if (MENU_IME.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800283 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500284 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800285 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500286 } else if (CLIPBOARD.equals(button)) {
287 v = inflater.inflate(R.layout.clipboard, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500288 } else if (button.startsWith(KEY)) {
289 String uri = extractImage(button);
290 int code = extractKeycode(button);
291 v = inflater.inflate(R.layout.custom_key, parent, false);
292 ((KeyButtonView) v).setCode(code);
293 if (uri != null) {
294 ((KeyButtonView) v).loadAsync(uri);
295 }
Jason Monkc62cf802016-05-10 11:02:24 -0400296 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500297 return v;
298 }
299
Jason Monk8457ad82016-01-24 10:15:55 -0500300 public static String extractImage(String buttonSpec) {
301 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
302 return null;
303 }
304 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
305 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
306 return subStr;
307 }
308
309 public static int extractKeycode(String buttonSpec) {
310 if (!buttonSpec.contains(KEY_CODE_START)) {
311 return 1;
312 }
313 final int start = buttonSpec.indexOf(KEY_CODE_START);
314 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
315 return Integer.parseInt(subStr);
316 }
317
Jason Monk46a196e2016-01-23 15:28:10 -0500318 public static float extractSize(String buttonSpec) {
319 if (!buttonSpec.contains(SIZE_MOD_START)) {
320 return 1;
321 }
322 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
323 String sizeStr = buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
324 return Float.parseFloat(sizeStr);
325 }
326
327 public static String extractButton(String buttonSpec) {
328 if (!buttonSpec.contains(SIZE_MOD_START)) {
329 return buttonSpec;
330 }
331 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
332 }
333
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800334 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500335 if (mButtonDispatchers != null) {
336 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
337 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800338 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700339 } else if (v instanceof ViewGroup) {
340 final ViewGroup viewGroup = (ViewGroup)v;
341 final int N = viewGroup.getChildCount();
342 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800343 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700344 }
Jason Monka2081822016-01-18 14:41:03 -0500345 }
346 }
Jason Monka2081822016-01-18 14:41:03 -0500347 }
348
Jason Monka2081822016-01-18 14:41:03 -0500349
Jason Monka2081822016-01-18 14:41:03 -0500350
351 private void clearViews() {
352 if (mButtonDispatchers != null) {
353 for (int i = 0; i < mButtonDispatchers.size(); i++) {
354 mButtonDispatchers.valueAt(i).clear();
355 }
356 }
357 clearAllChildren((ViewGroup) mRot0.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500358 clearAllChildren((ViewGroup) mRot90.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500359 }
360
361 private void clearAllChildren(ViewGroup group) {
362 for (int i = 0; i < group.getChildCount(); i++) {
363 ((ViewGroup) group.getChildAt(i)).removeAllViews();
364 }
365 }
Jason Monk197f4db2016-08-26 13:28:20 -0400366
367 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500368 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400369 mPlugins.add(plugin);
370 clearViews();
371 inflateLayout(mCurrentLayout);
372 }
373
374 @Override
375 public void onPluginDisconnected(NavBarButtonProvider plugin) {
376 mPlugins.remove(plugin);
377 clearViews();
378 inflateLayout(mCurrentLayout);
379 }
Jason Monka2081822016-01-18 14:41:03 -0500380}