blob: 2a38f771e921899df76c1cb02419690838604d6a [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
Gus Prevasab336792018-11-14 13:52:20 -050017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
18
Xiaohui Chen54816002016-01-25 11:11:11 -080019import android.annotation.Nullable;
Jason Monka2081822016-01-18 14:41:03 -050020import android.content.Context;
21import android.content.res.Configuration;
Jason Monkea05f872017-01-24 19:43:36 -050022import android.graphics.drawable.Icon;
Jason Monka2081822016-01-18 14:41:03 -050023import android.util.AttributeSet;
Jason Monk7f6a2ab2017-04-21 13:48:19 -040024import android.util.Log;
Jason Monka2081822016-01-18 14:41:03 -050025import android.util.SparseArray;
Jason Monk7f6a2ab2017-04-21 13:48:19 -040026import android.view.Gravity;
Jason Monka2081822016-01-18 14:41:03 -050027import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.FrameLayout;
Jason Monk4f878ef2016-01-23 14:37:38 -050031import android.widget.LinearLayout;
Jason Monka2081822016-01-18 14:41:03 -050032import android.widget.Space;
Annie Chin1ea49352016-05-27 15:23:35 -070033
Charles Chenaa94d95b2019-02-19 14:18:19 +080034import com.android.internal.annotations.VisibleForTesting;
Jason Monkde850bb2017-02-01 19:26:30 -050035import com.android.systemui.Dependency;
Jason Monka2081822016-01-18 14:41:03 -050036import com.android.systemui.R;
Jason Monk197f4db2016-08-26 13:28:20 -040037import com.android.systemui.plugins.PluginListener;
Jason Monk197f4db2016-08-26 13:28:20 -040038import com.android.systemui.plugins.statusbar.phone.NavBarButtonProvider;
Gus Prevasab336792018-11-14 13:52:20 -050039import com.android.systemui.recents.OverviewProxyService;
40import com.android.systemui.shared.plugins.PluginManager;
Mike Digmanf77fb912018-05-21 11:21:30 -070041import com.android.systemui.statusbar.phone.ReverseLinearLayout.ReverseRelativeLayout;
Jason Monk8457ad82016-01-24 10:15:55 -050042import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monka2081822016-01-18 14:41:03 -050043import com.android.systemui.tuner.TunerService;
Jason Monk197f4db2016-08-26 13:28:20 -040044import com.android.systemui.tuner.TunerService.Tunable;
Jason Monka2081822016-01-18 14:41:03 -050045
Jason Monk197f4db2016-08-26 13:28:20 -040046import java.util.ArrayList;
47import java.util.List;
Jason Monka2081822016-01-18 14:41:03 -050048import java.util.Objects;
49
Jason Monk197f4db2016-08-26 13:28:20 -040050public class NavigationBarInflaterView extends FrameLayout
51 implements Tunable, PluginListener<NavBarButtonProvider> {
Jason Monka2081822016-01-18 14:41:03 -050052
53 private static final String TAG = "NavBarInflater";
54
55 public static final String NAV_BAR_VIEWS = "sysui_nav_bar";
Jason Monkea05f872017-01-24 19:43:36 -050056 public static final String NAV_BAR_LEFT = "sysui_nav_bar_left";
57 public static final String NAV_BAR_RIGHT = "sysui_nav_bar_right";
Jason Monka2081822016-01-18 14:41:03 -050058
Mike Digmana48cf192018-02-12 17:52:48 -080059 public static final String MENU_IME_ROTATE = "menu_ime";
Jason Monk3ebd2392016-01-22 10:01:44 -050060 public static final String BACK = "back";
61 public static final String HOME = "home";
62 public static final String RECENT = "recent";
63 public static final String NAVSPACE = "space";
Jason Monk3b587142016-01-23 16:47:59 -050064 public static final String CLIPBOARD = "clipboard";
Matthew Ngb55c02c2019-02-15 16:20:31 -080065 public static final String HOME_HANDLE = "home_handle";
Matthew Ngadd4c392019-03-01 16:02:31 -080066 public static final String ASSISTANT_HANDLE = "assistant_handle";
Jason Monk8457ad82016-01-24 10:15:55 -050067 public static final String KEY = "key";
Jason Monkea05f872017-01-24 19:43:36 -050068 public static final String LEFT = "left";
69 public static final String RIGHT = "right";
Mike Digmanf77fb912018-05-21 11:21:30 -070070 public static final String CONTEXTUAL = "contextual";
Jason Monka2081822016-01-18 14:41:03 -050071
72 public static final String GRAVITY_SEPARATOR = ";";
73 public static final String BUTTON_SEPARATOR = ",";
74
Jason Monk46a196e2016-01-23 15:28:10 -050075 public static final String SIZE_MOD_START = "[";
76 public static final String SIZE_MOD_END = "]";
77
Jason Monk8457ad82016-01-24 10:15:55 -050078 public static final String KEY_CODE_START = "(";
79 public static final String KEY_IMAGE_DELIM = ":";
80 public static final String KEY_CODE_END = ")";
Jason Monk7f6a2ab2017-04-21 13:48:19 -040081 private static final String WEIGHT_SUFFIX = "W";
82 private static final String WEIGHT_CENTERED_SUFFIX = "WC";
Jason Monk8457ad82016-01-24 10:15:55 -050083
Jason Monk197f4db2016-08-26 13:28:20 -040084 private final List<NavBarButtonProvider> mPlugins = new ArrayList<>();
85
Jason Monk67a1cce2016-02-05 13:31:03 -050086 protected LayoutInflater mLayoutInflater;
87 protected LayoutInflater mLandscapeInflater;
Jason Monk8457ad82016-01-24 10:15:55 -050088
Charles Chenaa94d95b2019-02-19 14:18:19 +080089 protected FrameLayout mHorizontal;
90 protected FrameLayout mVertical;
Jason Monk8457ad82016-01-24 10:15:55 -050091
Charles Chenaa94d95b2019-02-19 14:18:19 +080092 @VisibleForTesting
93 SparseArray<ButtonDispatcher> mButtonDispatchers;
Jason Monka2081822016-01-18 14:41:03 -050094 private String mCurrentLayout;
95
Xiaohui Chen40e978e2016-11-29 15:10:04 -080096 private View mLastPortrait;
97 private View mLastLandscape;
Jason Monkc62cf802016-05-10 11:02:24 -040098
Charles Chenaa94d95b2019-02-19 14:18:19 +080099 private boolean mIsVertical;
Adrian Roosdb12b152016-07-12 15:38:55 -0700100 private boolean mAlternativeOrder;
Mike Digmanf77fb912018-05-21 11:21:30 -0700101 private boolean mUsingCustomLayout;
102
103 private OverviewProxyService mOverviewProxyService;
Adrian Roosdb12b152016-07-12 15:38:55 -0700104
Jason Monka2081822016-01-18 14:41:03 -0500105 public NavigationBarInflaterView(Context context, AttributeSet attrs) {
106 super(context, attrs);
Jason Monk67a1cce2016-02-05 13:31:03 -0500107 createInflaters();
Mike Digmanf77fb912018-05-21 11:21:30 -0700108 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk67a1cce2016-02-05 13:31:03 -0500109 }
110
Charles Chenaa94d95b2019-02-19 14:18:19 +0800111 @VisibleForTesting
112 void createInflaters() {
Jason Monk67a1cce2016-02-05 13:31:03 -0500113 mLayoutInflater = LayoutInflater.from(mContext);
Jason Monka2081822016-01-18 14:41:03 -0500114 Configuration landscape = new Configuration();
Jason Monk67a1cce2016-02-05 13:31:03 -0500115 landscape.setTo(mContext.getResources().getConfiguration());
Jason Monka2081822016-01-18 14:41:03 -0500116 landscape.orientation = Configuration.ORIENTATION_LANDSCAPE;
Jason Monk67a1cce2016-02-05 13:31:03 -0500117 mLandscapeInflater = LayoutInflater.from(mContext.createConfigurationContext(landscape));
118 }
119
120 @Override
Jason Monka2081822016-01-18 14:41:03 -0500121 protected void onFinishInflate() {
122 super.onFinishInflate();
Jason Monk9a6552d2016-05-20 11:21:59 -0400123 inflateChildren();
Jason Monka2081822016-01-18 14:41:03 -0500124 clearViews();
125 inflateLayout(getDefaultLayout());
126 }
127
Jason Monk9a6552d2016-05-20 11:21:59 -0400128 private void inflateChildren() {
129 removeAllViews();
Charles Chenaa94d95b2019-02-19 14:18:19 +0800130 mHorizontal = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout,
131 this /* root */, false /* attachToRoot */);
132 addView(mHorizontal);
133 mVertical = (FrameLayout) mLayoutInflater.inflate(R.layout.navigation_layout_vertical,
134 this /* root */, false /* attachToRoot */);
135 addView(mVertical);
Adrian Roosdb12b152016-07-12 15:38:55 -0700136 updateAlternativeOrder();
Jason Monk9a6552d2016-05-20 11:21:59 -0400137 }
138
Xiaohui Chen54816002016-01-25 11:11:11 -0800139 protected String getDefaultLayout() {
Mike Digmanf77fb912018-05-21 11:21:30 -0700140 final int defaultResource = mOverviewProxyService.shouldShowSwipeUpUI()
141 ? R.string.config_navBarLayoutQuickstep
142 : R.string.config_navBarLayout;
143 return mContext.getString(defaultResource);
Jason Monka2081822016-01-18 14:41:03 -0500144 }
145
146 @Override
147 protected void onAttachedToWindow() {
148 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500149 Dependency.get(TunerService.class).addTunable(this, NAV_BAR_VIEWS, NAV_BAR_LEFT,
Jason Monkea05f872017-01-24 19:43:36 -0500150 NAV_BAR_RIGHT);
Jason Monk5bec68f2017-02-08 20:45:10 -0800151 Dependency.get(PluginManager.class).addPluginListener(this,
152 NavBarButtonProvider.class, true /* Allow multiple */);
Jason Monka2081822016-01-18 14:41:03 -0500153 }
154
155 @Override
156 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500157 Dependency.get(TunerService.class).removeTunable(this);
158 Dependency.get(PluginManager.class).removePluginListener(this);
Jason Monka2081822016-01-18 14:41:03 -0500159 super.onDetachedFromWindow();
160 }
161
162 @Override
163 public void onTuningChanged(String key, String newValue) {
164 if (NAV_BAR_VIEWS.equals(key)) {
Jason Monka2081822016-01-18 14:41:03 -0500165 if (!Objects.equals(mCurrentLayout, newValue)) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700166 mUsingCustomLayout = newValue != null;
Jason Monka2081822016-01-18 14:41:03 -0500167 clearViews();
168 inflateLayout(newValue);
169 }
Jason Monkea05f872017-01-24 19:43:36 -0500170 } else if (NAV_BAR_LEFT.equals(key) || NAV_BAR_RIGHT.equals(key)) {
171 clearViews();
172 inflateLayout(mCurrentLayout);
Jason Monka2081822016-01-18 14:41:03 -0500173 }
174 }
175
Mike Digmanf77fb912018-05-21 11:21:30 -0700176 public void onLikelyDefaultLayoutChange() {
177 // Don't override custom layouts
178 if (mUsingCustomLayout) return;
179
180 // Reevaluate new layout
181 final String newValue = getDefaultLayout();
182 if (!Objects.equals(mCurrentLayout, newValue)) {
183 clearViews();
184 inflateLayout(newValue);
185 }
186 }
187
Matthew Ng6607c3d2018-04-26 15:23:27 -0700188 public void setButtonDispatchers(SparseArray<ButtonDispatcher> buttonDispatchers) {
189 mButtonDispatchers = buttonDispatchers;
190 for (int i = 0; i < buttonDispatchers.size(); i++) {
191 initiallyFill(buttonDispatchers.valueAt(i));
Jason Monka2081822016-01-18 14:41:03 -0500192 }
193 }
194
Charles Chenaa94d95b2019-02-19 14:18:19 +0800195 void updateButtonDispatchersCurrentView() {
Matthew Ng1efc9c32018-04-11 18:13:56 -0700196 if (mButtonDispatchers != null) {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800197 View view = mIsVertical ? mVertical : mHorizontal;
Matthew Ng1efc9c32018-04-11 18:13:56 -0700198 for (int i = 0; i < mButtonDispatchers.size(); i++) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700199 final ButtonDispatcher dispatcher = mButtonDispatchers.valueAt(i);
200 dispatcher.setCurrentView(view);
Matthew Ng1efc9c32018-04-11 18:13:56 -0700201 }
202 }
203 }
204
Charles Chenaa94d95b2019-02-19 14:18:19 +0800205 void setVertical(boolean vertical) {
206 if (vertical != mIsVertical) {
207 mIsVertical = vertical;
208 }
209 }
210
211 void setAlternativeOrder(boolean alternativeOrder) {
Adrian Roosdb12b152016-07-12 15:38:55 -0700212 if (alternativeOrder != mAlternativeOrder) {
213 mAlternativeOrder = alternativeOrder;
214 updateAlternativeOrder();
215 }
216 }
217
218 private void updateAlternativeOrder() {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800219 updateAlternativeOrder(mHorizontal.findViewById(R.id.ends_group));
220 updateAlternativeOrder(mHorizontal.findViewById(R.id.center_group));
221 updateAlternativeOrder(mVertical.findViewById(R.id.ends_group));
222 updateAlternativeOrder(mVertical.findViewById(R.id.center_group));
Adrian Roosdb12b152016-07-12 15:38:55 -0700223 }
224
225 private void updateAlternativeOrder(View v) {
226 if (v instanceof ReverseLinearLayout) {
227 ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
228 }
229 }
230
Jason Monka2081822016-01-18 14:41:03 -0500231 private void initiallyFill(ButtonDispatcher buttonDispatcher) {
Charles Chenaa94d95b2019-02-19 14:18:19 +0800232 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.ends_group));
233 addAll(buttonDispatcher, mHorizontal.findViewById(R.id.center_group));
234 addAll(buttonDispatcher, mVertical.findViewById(R.id.ends_group));
235 addAll(buttonDispatcher, mVertical.findViewById(R.id.center_group));
Jason Monka2081822016-01-18 14:41:03 -0500236 }
237
238 private void addAll(ButtonDispatcher buttonDispatcher, ViewGroup parent) {
239 for (int i = 0; i < parent.getChildCount(); i++) {
240 // Need to manually search for each id, just in case each group has more than one
241 // of a single id. It probably mostly a waste of time, but shouldn't take long
242 // and will only happen once.
243 if (parent.getChildAt(i).getId() == buttonDispatcher.getId()) {
244 buttonDispatcher.addView(parent.getChildAt(i));
Matthew Ng6607c3d2018-04-26 15:23:27 -0700245 }
246 if (parent.getChildAt(i) instanceof ViewGroup) {
Jason Monka2081822016-01-18 14:41:03 -0500247 addAll(buttonDispatcher, (ViewGroup) parent.getChildAt(i));
248 }
249 }
250 }
251
Xiaohui Chen54816002016-01-25 11:11:11 -0800252 protected void inflateLayout(String newLayout) {
Jason Monka2081822016-01-18 14:41:03 -0500253 mCurrentLayout = newLayout;
Jason Monk9a6552d2016-05-20 11:21:59 -0400254 if (newLayout == null) {
255 newLayout = getDefaultLayout();
256 }
Xiaohui Chen54816002016-01-25 11:11:11 -0800257 String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
Mikael Magnussonc71ab602018-01-29 17:56:10 +0100258 if (sets.length != 3) {
259 Log.d(TAG, "Invalid layout.");
260 newLayout = getDefaultLayout();
261 sets = newLayout.split(GRAVITY_SEPARATOR, 3);
262 }
Jason Monka2081822016-01-18 14:41:03 -0500263 String[] start = sets[0].split(BUTTON_SEPARATOR);
264 String[] center = sets[1].split(BUTTON_SEPARATOR);
265 String[] end = sets[2].split(BUTTON_SEPARATOR);
Jason Monkc62cf802016-05-10 11:02:24 -0400266 // Inflate these in start to end order or accessibility traversal will be messed up.
Charles Chenaa94d95b2019-02-19 14:18:19 +0800267 inflateButtons(start, mHorizontal.findViewById(R.id.ends_group),
268 false /* landscape */, true /* start */);
269 inflateButtons(start, mVertical.findViewById(R.id.ends_group),
270 true /* landscape */, true /* start */);
Jason Monk4f878ef2016-01-23 14:37:38 -0500271
Charles Chenaa94d95b2019-02-19 14:18:19 +0800272 inflateButtons(center, mHorizontal.findViewById(R.id.center_group),
273 false /* landscape */, false /* start */);
274 inflateButtons(center, mVertical.findViewById(R.id.center_group),
275 true /* landscape */, false /* start */);
Xiaohui Chen06917032016-01-26 11:20:39 -0800276
Charles Chenaa94d95b2019-02-19 14:18:19 +0800277 addGravitySpacer(mHorizontal.findViewById(R.id.ends_group));
278 addGravitySpacer(mVertical.findViewById(R.id.ends_group));
Jason Monk4f878ef2016-01-23 14:37:38 -0500279
Charles Chenaa94d95b2019-02-19 14:18:19 +0800280 inflateButtons(end, mHorizontal.findViewById(R.id.ends_group),
281 false /* landscape */, false /* start */);
282 inflateButtons(end, mVertical.findViewById(R.id.ends_group),
283 true /* landscape */, false /* start */);
Matthew Ng1efc9c32018-04-11 18:13:56 -0700284
285 updateButtonDispatchersCurrentView();
Jason Monk4f878ef2016-01-23 14:37:38 -0500286 }
287
Jason Monk4f878ef2016-01-23 14:37:38 -0500288 private void addGravitySpacer(LinearLayout layout) {
289 layout.addView(new Space(mContext), new LinearLayout.LayoutParams(0, 0, 1));
Jason Monka2081822016-01-18 14:41:03 -0500290 }
291
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400292 private void inflateButtons(String[] buttons, ViewGroup parent, boolean landscape,
293 boolean start) {
Jason Monka2081822016-01-18 14:41:03 -0500294 for (int i = 0; i < buttons.length; i++) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400295 inflateButton(buttons[i], parent, landscape, start);
Jason Monka2081822016-01-18 14:41:03 -0500296 }
297 }
298
Jason Monka2081822016-01-18 14:41:03 -0500299 private ViewGroup.LayoutParams copy(ViewGroup.LayoutParams layoutParams) {
Jason Monk4f878ef2016-01-23 14:37:38 -0500300 if (layoutParams instanceof LinearLayout.LayoutParams) {
301 return new LinearLayout.LayoutParams(layoutParams.width, layoutParams.height,
302 ((LinearLayout.LayoutParams) layoutParams).weight);
303 }
Jason Monka2081822016-01-18 14:41:03 -0500304 return new LayoutParams(layoutParams.width, layoutParams.height);
305 }
306
Xiaohui Chen54816002016-01-25 11:11:11 -0800307 @Nullable
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400308 protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
309 boolean start) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800310 LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400311 View v = createView(buttonSpec, parent, inflater);
Jason Monk197f4db2016-08-26 13:28:20 -0400312 if (v == null) return null;
313
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400314 v = applySize(v, buttonSpec, landscape, start);
Jason Monk197f4db2016-08-26 13:28:20 -0400315 parent.addView(v);
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800316 addToDispatchers(v);
317 View lastView = landscape ? mLastLandscape : mLastPortrait;
Jason Monkc7ba5432017-07-06 15:12:21 -0400318 View accessibilityView = v;
Mike Digmanf77fb912018-05-21 11:21:30 -0700319 if (v instanceof ReverseRelativeLayout) {
320 accessibilityView = ((ReverseRelativeLayout) v).getChildAt(0);
Jason Monkc7ba5432017-07-06 15:12:21 -0400321 }
Jason Monk197f4db2016-08-26 13:28:20 -0400322 if (lastView != null) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400323 accessibilityView.setAccessibilityTraversalAfter(lastView.getId());
Jason Monk197f4db2016-08-26 13:28:20 -0400324 }
325 if (landscape) {
Jason Monkc7ba5432017-07-06 15:12:21 -0400326 mLastLandscape = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400327 } else {
Jason Monkc7ba5432017-07-06 15:12:21 -0400328 mLastPortrait = accessibilityView;
Jason Monk197f4db2016-08-26 13:28:20 -0400329 }
330 return v;
331 }
332
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400333 private View applySize(View v, String buttonSpec, boolean landscape, boolean start) {
334 String sizeStr = extractSize(buttonSpec);
335 if (sizeStr == null) return v;
336
337 if (sizeStr.contains(WEIGHT_SUFFIX)) {
Mike Digmanf77fb912018-05-21 11:21:30 -0700338 // To support gravity, wrap in RelativeLayout and apply gravity to it.
339 // Children wanting to use gravity must be smaller then the frame.
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400340 float weight = Float.parseFloat(sizeStr.substring(0, sizeStr.indexOf(WEIGHT_SUFFIX)));
Mike Digmanf77fb912018-05-21 11:21:30 -0700341 ReverseRelativeLayout frame = new ReverseRelativeLayout(mContext);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400342 LayoutParams childParams = new LayoutParams(v.getLayoutParams());
Mike Digmanf77fb912018-05-21 11:21:30 -0700343
344 // Compute gravity to apply
345 int gravity = (landscape) ? (start ? Gravity.TOP : Gravity.BOTTOM)
346 : (start ? Gravity.START : Gravity.END);
347 if (sizeStr.endsWith(WEIGHT_CENTERED_SUFFIX)) gravity = Gravity.CENTER;
348
349 // Set default gravity, flipped if needed in reversed layouts (270 RTL and 90 LTR)
350 frame.setDefaultGravity(gravity);
351 frame.setGravity(gravity); // Apply gravity to root
352
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400353 frame.addView(v, childParams);
Mike Digmanf77fb912018-05-21 11:21:30 -0700354
355 // Use weighting to set the width of the frame
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400356 frame.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, weight));
Mike Digmanf77fb912018-05-21 11:21:30 -0700357
358 // Ensure ripples can be drawn outside bounds
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400359 frame.setClipChildren(false);
360 frame.setClipToPadding(false);
Mike Digmanf77fb912018-05-21 11:21:30 -0700361
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400362 return frame;
363 }
Mike Digmanf77fb912018-05-21 11:21:30 -0700364
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400365 float size = Float.parseFloat(sizeStr);
366 ViewGroup.LayoutParams params = v.getLayoutParams();
367 params.width = (int) (params.width * size);
368 return v;
369 }
370
371 private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
Jason Monk46a196e2016-01-23 15:28:10 -0500372 View v = null;
Jason Monk197f4db2016-08-26 13:28:20 -0400373 String button = extractButton(buttonSpec);
Jason Monkea05f872017-01-24 19:43:36 -0500374 if (LEFT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800375 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_LEFT, NAVSPACE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400376 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500377 } else if (RIGHT.equals(button)) {
Mike Digmana48cf192018-02-12 17:52:48 -0800378 String s = Dependency.get(TunerService.class).getValue(NAV_BAR_RIGHT, MENU_IME_ROTATE);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400379 button = extractButton(s);
Jason Monkea05f872017-01-24 19:43:36 -0500380 }
Jason Monk197f4db2016-08-26 13:28:20 -0400381 // Let plugins go first so they can override a standard view if they want.
382 for (NavBarButtonProvider provider : mPlugins) {
383 v = provider.createView(buttonSpec, parent);
384 if (v != null) return v;
385 }
Jason Monka2081822016-01-18 14:41:03 -0500386 if (HOME.equals(button)) {
Annie Chinb6ab24f2016-06-01 17:46:27 -0700387 v = inflater.inflate(R.layout.home, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500388 } else if (BACK.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800389 v = inflater.inflate(R.layout.back, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500390 } else if (RECENT.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800391 v = inflater.inflate(R.layout.recent_apps, parent, false);
Mike Digmana48cf192018-02-12 17:52:48 -0800392 } else if (MENU_IME_ROTATE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800393 v = inflater.inflate(R.layout.menu_ime, parent, false);
Jason Monka2081822016-01-18 14:41:03 -0500394 } else if (NAVSPACE.equals(button)) {
Xiaohui Chen54816002016-01-25 11:11:11 -0800395 v = inflater.inflate(R.layout.nav_key_space, parent, false);
Jason Monk3b587142016-01-23 16:47:59 -0500396 } else if (CLIPBOARD.equals(button)) {
397 v = inflater.inflate(R.layout.clipboard, parent, false);
Mike Digmanf77fb912018-05-21 11:21:30 -0700398 } else if (CONTEXTUAL.equals(button)) {
399 v = inflater.inflate(R.layout.contextual, parent, false);
Matthew Ngb55c02c2019-02-15 16:20:31 -0800400 } else if (HOME_HANDLE.equals(button)) {
401 v = inflater.inflate(R.layout.home_handle, parent, false);
Matthew Ngadd4c392019-03-01 16:02:31 -0800402 } else if (ASSISTANT_HANDLE.equals(button)) {
403 v = inflater.inflate(R.layout.assistant_handle, parent, false);
Jason Monk8457ad82016-01-24 10:15:55 -0500404 } else if (button.startsWith(KEY)) {
405 String uri = extractImage(button);
406 int code = extractKeycode(button);
407 v = inflater.inflate(R.layout.custom_key, parent, false);
408 ((KeyButtonView) v).setCode(code);
409 if (uri != null) {
Jason Monkea05f872017-01-24 19:43:36 -0500410 if (uri.contains(":")) {
411 ((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));
412 } else if (uri.contains("/")) {
413 int index = uri.indexOf('/');
414 String pkg = uri.substring(0, index);
415 int id = Integer.parseInt(uri.substring(index + 1));
416 ((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));
417 }
Jason Monk8457ad82016-01-24 10:15:55 -0500418 }
Jason Monkc62cf802016-05-10 11:02:24 -0400419 }
Jason Monk4f878ef2016-01-23 14:37:38 -0500420 return v;
421 }
422
Jason Monk8457ad82016-01-24 10:15:55 -0500423 public static String extractImage(String buttonSpec) {
424 if (!buttonSpec.contains(KEY_IMAGE_DELIM)) {
425 return null;
426 }
427 final int start = buttonSpec.indexOf(KEY_IMAGE_DELIM);
428 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_CODE_END));
429 return subStr;
430 }
431
432 public static int extractKeycode(String buttonSpec) {
433 if (!buttonSpec.contains(KEY_CODE_START)) {
434 return 1;
435 }
436 final int start = buttonSpec.indexOf(KEY_CODE_START);
437 String subStr = buttonSpec.substring(start + 1, buttonSpec.indexOf(KEY_IMAGE_DELIM));
438 return Integer.parseInt(subStr);
439 }
440
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400441 public static String extractSize(String buttonSpec) {
Jason Monk46a196e2016-01-23 15:28:10 -0500442 if (!buttonSpec.contains(SIZE_MOD_START)) {
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400443 return null;
Jason Monk46a196e2016-01-23 15:28:10 -0500444 }
445 final int sizeStart = buttonSpec.indexOf(SIZE_MOD_START);
Jason Monk7f6a2ab2017-04-21 13:48:19 -0400446 return buttonSpec.substring(sizeStart + 1, buttonSpec.indexOf(SIZE_MOD_END));
Jason Monk46a196e2016-01-23 15:28:10 -0500447 }
448
449 public static String extractButton(String buttonSpec) {
450 if (!buttonSpec.contains(SIZE_MOD_START)) {
451 return buttonSpec;
452 }
453 return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
454 }
455
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800456 private void addToDispatchers(View v) {
Jason Monka2081822016-01-18 14:41:03 -0500457 if (mButtonDispatchers != null) {
458 final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
459 if (indexOfKey >= 0) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800460 mButtonDispatchers.valueAt(indexOfKey).addView(v);
Matthew Ng6607c3d2018-04-26 15:23:27 -0700461 }
462 if (v instanceof ViewGroup) {
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700463 final ViewGroup viewGroup = (ViewGroup)v;
464 final int N = viewGroup.getChildCount();
465 for (int i = 0; i < N; i++) {
Xiaohui Chen40e978e2016-11-29 15:10:04 -0800466 addToDispatchers(viewGroup.getChildAt(i));
Keisuke Kuroyanagi47bd7332016-05-06 10:49:54 -0700467 }
Jason Monka2081822016-01-18 14:41:03 -0500468 }
469 }
Jason Monka2081822016-01-18 14:41:03 -0500470 }
471
Jason Monka2081822016-01-18 14:41:03 -0500472
Jason Monka2081822016-01-18 14:41:03 -0500473
474 private void clearViews() {
475 if (mButtonDispatchers != null) {
476 for (int i = 0; i < mButtonDispatchers.size(); i++) {
477 mButtonDispatchers.valueAt(i).clear();
478 }
479 }
Charles Chenaa94d95b2019-02-19 14:18:19 +0800480 clearAllChildren(mHorizontal.findViewById(R.id.nav_buttons));
481 clearAllChildren(mVertical.findViewById(R.id.nav_buttons));
Jason Monka2081822016-01-18 14:41:03 -0500482 }
483
484 private void clearAllChildren(ViewGroup group) {
485 for (int i = 0; i < group.getChildCount(); i++) {
486 ((ViewGroup) group.getChildAt(i)).removeAllViews();
487 }
488 }
Jason Monk197f4db2016-08-26 13:28:20 -0400489
490 @Override
Jason Monk20ff3f92017-01-09 15:13:23 -0500491 public void onPluginConnected(NavBarButtonProvider plugin, Context context) {
Jason Monk197f4db2016-08-26 13:28:20 -0400492 mPlugins.add(plugin);
493 clearViews();
494 inflateLayout(mCurrentLayout);
495 }
496
497 @Override
498 public void onPluginDisconnected(NavBarButtonProvider plugin) {
499 mPlugins.remove(plugin);
500 clearViews();
501 inflateLayout(mCurrentLayout);
502 }
Jason Monka2081822016-01-18 14:41:03 -0500503}